Advertisement
Khadija_Assem

Untitled

Dec 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.game.sample;
  2.  
  3. import eg.edu.alexu.csd.oop.game.GameObject;
  4. import eg.edu.alexu.csd.oop.game.World;
  5. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Clown;
  6. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Gate;
  7. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.GateDirector;
  8. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.ShelfDirector;
  9. import eg.edu.alexu.csd.oop.game.sample.GameObjects.MyMouseListener;
  10. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Plates;
  11. import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Shelf;
  12. import eg.edu.alexu.csd.oop.game.sample.GameObjects.UndoButton;
  13. import eg.edu.alexu.csd.oop.game.sample.Memento.Caretaker;
  14. import eg.edu.alexu.csd.oop.game.sample.Memento.Originator;
  15. import eg.edu.alexu.csd.oop.game.sample.ShapeFactory.ShapeFactory;
  16. import eg.edu.alexu.csd.oop.game.sample.State.FallingState;
  17. import eg.edu.alexu.csd.oop.game.sample.State.ShelfState;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseListener;
  22. import java.sql.SQLClientInfoException;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25.  
  26. public class Game implements World {
  27. private static int MAX_TIME = 1 * 60 * 1000; // 1 minute
  28. private int score = 0;
  29. private long startTime = System.currentTimeMillis();
  30. private final int width;
  31. private final int height;
  32. private final List<GameObject> constant = new LinkedList<GameObject>();
  33. private final List<GameObject> moving = new LinkedList<GameObject>();
  34. private final List<GameObject> control = new LinkedList<GameObject>();
  35. ShapeFactory sF ;
  36.  
  37. public Game(int screenWidth, int screenHeight) {
  38. //moving.add(new ImageObject((int)(Math.random() * screenWidth), -1 * (int)(Math.random() * screenHeight), "/star.png"));
  39. sF = new ShapeFactory();
  40. sF.setWorld(this);
  41. width = screenWidth;
  42. height = screenHeight;
  43. control.add(Clown.getInstance());
  44. constant.add(new Shelf(10,70,500,true, Color.RED));
  45. constant.add(new Shelf(750,70,500,true, Color.RED));
  46. constant.add(new Shelf(10,70+100,300,true, Color.BLUE));
  47. constant.add(new Shelf(950,70+100,300,true, Color.BLUE));
  48. for(int i = 0;i<constant.size();i++)
  49. ShelfDirector.getInstance().addShelf((Shelf)constant.get(i));
  50. GateDirector.getInstance().addGate(new Gate(10, 70));
  51. sF.setGate(0);
  52. moving.add(sF.getShape());
  53. // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX(), -250));
  54. // sF.setGate(1);
  55. // moving.add(sF.getShape());
  56. // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(0).getX()-ShelfDirector.getInstance().showShelfeDetails(0).getWidth(), -250+100));
  57. // sF.setGate(2);
  58. // moving.add(sF.getShape());
  59. // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX(), -250+100));
  60. // sF.setGate(3);
  61. // moving.add(sF.getShape());
  62. }
  63. private boolean intersect(GameObject o1, GameObject o2){
  64. return (Math.abs((o1.getX()+o1.getWidth()/2) - (o2.getX()+o2.getWidth()/2)) <= o1.getWidth()) && (Math.abs((o1.getY()+o1.getHeight()/2) - (o2.getY()+o2.getHeight()/2)) <= o1.getHeight());
  65. }
  66. boolean flag = true;
  67. @Override
  68. public boolean refresh() {
  69. boolean timeout = System.currentTimeMillis() - startTime > MAX_TIME; // time end and game over
  70. if(flag) {
  71. Frame.getFrames()[Frame.getFrames().length-1].add(UndoButton.getInstance());
  72. constant.add(UndoButton.getInstance());
  73. flag = false;
  74. }
  75. // Originator originator = new Originator();
  76. // Caretaker careTaker = new Caretaker();
  77. // originator.setState(this);
  78. // careTaker.addMemento(originator.save());
  79. for(int i=0;i<moving.size();i++) {
  80. // if (!moving.get(i).isVisible())
  81. // moving.remove(i);
  82. if (((Plates) moving.get(i)).getState() instanceof ShelfState || ((Plates) moving.get(i)).getState() instanceof FallingState) {
  83. ((Plates) moving.get(i)).getState().doAction(((Plates) moving.get(i)));
  84. // System.out.println(moving.get(i).getX());
  85. // if (moving.get(i).getX() + 489 < 1100 && !((Plates) moving.get(i)).isUsed() && (((Plates) moving.get(i)).getGate() == 1 || ((Plates) moving.get(i)).getGate() == 3)) {
  86. // sF.setGate(((Plates) moving.get(i)).getGate());
  87. // moving.add(sF.getShape());
  88. // ((Plates) moving.get(i)).setUsed(true);
  89. // } else if (moving.get(i).getX() * -1 < 360 && !((Plates) moving.get(i)).isUsed() && (((Plates) moving.get(i)).getGate() == 0 || ((Plates) moving.get(i)).getGate() == 2)) {
  90. // sF.setGate(((Plates) moving.get(i)).getGate());
  91. // moving.add(sF.getShape());
  92. // ((Plates) moving.get(i)).setUsed(true);
  93. // }
  94. }
  95. }
  96. // for(GameObject m : moving){ // change ball position
  97. // BubleObject ball = (BubleObject)m;
  98. // ball.setX(ball.getX() + (ball.isLeft() ? 1 : -1));
  99. // ball.setY(ball.getY() + (ball.isTop() ? 1 : -1));
  100. // boolean hitBar = false;
  101. // if(!timeout)
  102. // for(GameObject bar : control)
  103. // if(intersect(bar, ball)){ // hit the ball
  104. // score++; // acquire score
  105. // hitBar = true;
  106. // if(!ball.isTop())
  107. // ball.setY(Math.min(getHeight() - 2 * ball.getHeight(), ball.getY() + ball.getHeight()/2));
  108. // else
  109. // ball.setY(Math.max(0, ball.getY() - ball.getHeight()/2));
  110. // }
  111. // if(hitBar || ball.getY() < 0 || ball.getY()+ball.getHeight()+20 > getHeight())
  112. // ball.setTop(!ball.isTop()); // reverse vertical direction
  113. // if(ball.getX() < 0 || ball.getX()+ball.getWidth() > getWidth())
  114. // ball.setLeft(!ball.isLeft()); // reverse horizontal direction
  115. // }
  116. // for(GameObject bar : control)
  117. // for(GameObject c : constant)
  118. // if(intersect(bar, c)) // hit a cross
  119. // score = Math.max(0, score-1); // lose score
  120. return !timeout;
  121. }
  122. @Override
  123. public int getSpeed() {
  124. return 10;
  125. }
  126. @Override
  127. public int getControlSpeed() {
  128. return 10;
  129. }
  130. @Override
  131. public List<GameObject> getConstantObjects() {
  132. return constant;
  133. }
  134. @Override
  135. public List<GameObject> getMovableObjects() {
  136. return moving;
  137. }
  138. @Override
  139. public List<GameObject> getControlableObjects() {
  140. return control;
  141. }
  142. @Override
  143. public int getWidth() {
  144. return width;
  145. }
  146. @Override
  147. public int getHeight() {
  148. return height;
  149. }
  150. @Override
  151. public String getStatus() {
  152. return "";
  153. // return "Please Use Arrows To Move | Location = " + control.get(0).getX() + "," + control.get(0).getY() + " | Score=" + score + " | Time=" + Math.max(0, (MAX_TIME - (System.currentTimeMillis()-startTime))/1000); // update status
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement