Advertisement
Khadija_Assem

Untitled

Dec 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 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(ShelfDirector.getInstance().showShelfeDetails(0).getX(), ShelfDirector.getInstance().showShelfeDetails(0).getY()));
  51. sF.setGate(0);
  52. moving.add(sF.getShape());
  53. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX()+ShelfDirector.getInstance().showShelfeDetails(1).getWidth(), ShelfDirector.getInstance().showShelfeDetails(1).getY()));
  54. sF.setGate(1);
  55. moving.add(sF.getShape());
  56. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(2).getX(), ShelfDirector.getInstance().showShelfeDetails(2).getY()));
  57. sF.setGate(2);
  58. moving.add(sF.getShape());
  59. GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(3).getX()+ShelfDirector.getInstance().showShelfeDetails(3).getWidth(), ShelfDirector.getInstance().showShelfeDetails(3).getY()));
  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() < getWidth()-150 && !((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. }
  90. else if (moving.get(i).getX() > 150 && !((Plates) moving.get(i)).isUsed() && (((Plates) moving.get(i)).getGate() == 0 || ((Plates) moving.get(i)).getGate() == 2)) {
  91. sF.setGate(((Plates) moving.get(i)).getGate());
  92. moving.add(sF.getShape());
  93. ((Plates) moving.get(i)).setUsed(true);
  94. }
  95. }
  96. }
  97. // for(GameObject m : moving){ // change ball position
  98. // BubleObject ball = (BubleObject)m;
  99. // ball.setX(ball.getX() + (ball.isLeft() ? 1 : -1));
  100. // ball.setY(ball.getY() + (ball.isTop() ? 1 : -1));
  101. // boolean hitBar = false;
  102. // if(!timeout)
  103. // for(GameObject bar : control)
  104. // if(intersect(bar, ball)){ // hit the ball
  105. // score++; // acquire score
  106. // hitBar = true;
  107. // if(!ball.isTop())
  108. // ball.setY(Math.min(getHeight() - 2 * ball.getHeight(), ball.getY() + ball.getHeight()/2));
  109. // else
  110. // ball.setY(Math.max(0, ball.getY() - ball.getHeight()/2));
  111. // }
  112. // if(hitBar || ball.getY() < 0 || ball.getY()+ball.getHeight()+20 > getHeight())
  113. // ball.setTop(!ball.isTop()); // reverse vertical direction
  114. // if(ball.getX() < 0 || ball.getX()+ball.getWidth() > getWidth())
  115. // ball.setLeft(!ball.isLeft()); // reverse horizontal direction
  116. // }
  117. // for(GameObject bar : control)
  118. // for(GameObject c : constant)
  119. // if(intersect(bar, c)) // hit a cross
  120. // score = Math.max(0, score-1); // lose score
  121. return !timeout;
  122. }
  123. @Override
  124. public int getSpeed() {
  125. return 10;
  126. }
  127. @Override
  128. public int getControlSpeed() {
  129. return 10;
  130. }
  131. @Override
  132. public List<GameObject> getConstantObjects() {
  133. return constant;
  134. }
  135. @Override
  136. public List<GameObject> getMovableObjects() {
  137. return moving;
  138. }
  139. @Override
  140. public List<GameObject> getControlableObjects() {
  141. return control;
  142. }
  143. @Override
  144. public int getWidth() {
  145. return width;
  146. }
  147. @Override
  148. public int getHeight() {
  149. return height;
  150. }
  151. @Override
  152. public String getStatus() {
  153. return "";
  154. // 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
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement