Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.game.sample.world;
  2.  
  3. import java.awt.Color;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Stack;
  7.  
  8. import eg.edu.alexu.csd.oop.game.GameObject;
  9. import eg.edu.alexu.csd.oop.game.World;
  10. import eg.edu.alexu.csd.oop.game.sample.State.State_2;
  11. import eg.edu.alexu.csd.oop.game.sample.levels.Strategy;
  12. import eg.edu.alexu.csd.oop.game.sample.object.Clown;
  13. import eg.edu.alexu.csd.oop.game.sample.object.ConnectionPool;
  14. import eg.edu.alexu.csd.oop.game.sample.object.Plate;
  15. import eg.edu.alexu.csd.oop.game.sample.object.Shape;
  16. import eg.edu.alexu.csd.oop.game.sample.object.StickLeft;
  17. import eg.edu.alexu.csd.oop.game.sample.object.StickRight;
  18.  
  19. public class Game implements World{
  20. private static int MAX_TIME;
  21. private int score=0;
  22. private long startTime = System.currentTimeMillis();
  23. private final int width;
  24. private final int height;
  25. private List<GameObject> constant = new LinkedList<GameObject>();
  26. private List<GameObject> moving = new LinkedList<GameObject>();
  27. private List<GameObject> control = new LinkedList<GameObject>();
  28. private Stack<Plate> RStack = new Stack<Plate>();
  29. private Stack<Plate> LStack = new Stack<Plate>();
  30. private List<Plate> R = new LinkedList<Plate>();
  31. private List<Plate> L = new LinkedList<Plate>();
  32. private Strategy level;
  33. private int limitheight;
  34.  
  35. ConnectionPool son = new ConnectionPool();
  36.  
  37. public Game(int screenWidth, int screenHeight,Strategy level) {
  38. this.level = level;
  39. Game.MAX_TIME = this.level.getMaxTime();
  40. this.limitheight = this.level.maxHeightOfPlate();
  41. score = 0;
  42. width = screenWidth;
  43. height = screenHeight;
  44. control = State_2.getControl();
  45. moving = State_2.getMoving();
  46. constant = State_2.getConstant();
  47. /*
  48. * control.add(new StickRight(440, 570, "/right.png", true));
  49. * control.add(new StickLeft(330, 535, "/left.png", true));
  50. * control.add(new Clown(270, 530,"/Clown.png", true));
  51. */
  52. for (int i = 3; i < 7; i++) {
  53. moving.add((GameObject) son.checkOut());
  54. moving.add((GameObject) son.checkOut());
  55. }
  56.  
  57. System.out.println();
  58.  
  59. }
  60.  
  61. public void moveToLimit() {
  62.  
  63. if (this.control.get(0).getX() == 855) {
  64.  
  65. this.control.get(2).setX(700);
  66. this.control.get(0).setX(855);
  67. this.control.get(1).setX(730);
  68. // objectsLimit();
  69.  
  70. } else if (this.control.get(0).getX() > 855) {
  71.  
  72. this.control.get(2).setX(700);
  73. this.control.get(0).setX(855);
  74. this.control.get(1).setX(730);
  75. // objectsLimit();
  76.  
  77. }
  78.  
  79. if (this.control.get(1).getX() == 35) {
  80.  
  81. this.control.get(2).setX(5);
  82. this.control.get(0).setX(160);
  83. this.control.get(1).setX(35);
  84. // objectsLimit();
  85.  
  86. }
  87.  
  88. else if (this.control.get(1).getX() < 35) {
  89.  
  90. this.control.get(2).setX(5);
  91. this.control.get(0).setX(160);
  92. this.control.get(1).setX(35);
  93. // objectsLimit();
  94.  
  95. }
  96.  
  97. }
  98.  
  99. private boolean intersect(GameObject o1, GameObject o2) {
  100. //return (o1.getY()+o1.getHeight()==o2.getY()&&o1.getX()==o2.getX());
  101. return (Math.abs((o1.getX() + o1.getWidth() / 2) - (o2.getX() + o2.getWidth() / 2)) <= o1.getWidth())
  102. && (Math.abs((o1.getY() + o1.getHeight() / 2) - (o2.getY() + o2.getHeight() / 2)) <= o1.getHeight());
  103. //return (( o1.getX()>=o2.getX() )&& (o1.getX()<=o2.getX()+o2.getWidth())
  104. // && (o2.getY()==o1.getY() ) );
  105.  
  106. }
  107.  
  108. @Override
  109. public boolean refresh() {
  110. moveToLimit();
  111. boolean timeout = score == constant.size() || System.currentTimeMillis() - startTime > MAX_TIME;
  112. // randomly hide constant objects
  113. //for (GameObject n : constant)
  114. //if (n.isVisible() && Math.random() < 0.0002)
  115. // ((Plate) n).setVisible(false);
  116. // change position of moving objects
  117. boolean direction = false;
  118. for (GameObject m : moving) {
  119. m.setY((m.getY() + 1));
  120. if (m.getY() == getHeight()) {
  121. // reuse the star in another position
  122. m.setY(-1 * (int) (Math.random() * getHeight()));
  123. m.setX((int) (Math.random() * getWidth()));
  124. }
  125. m.setX(m.getX());
  126. }
  127. // for(GameObject c : control)
  128. // c.setY(c.getHeight());
  129. // check intersection with constant
  130.  
  131. // check intersection with moving objects
  132. if (false &&!RStack.isEmpty()) {
  133. Stack<Plate>Rtemp = RStack;
  134. Plate[] R=new Plate[RStack.size()];
  135. int i=0;
  136.  
  137. while(Rtemp.peek()!=null)
  138. R[i++]=Rtemp.pop();
  139. R[R.length-1].setX(control.get(0).getX());
  140. for ( i=R.length-2;i<=0;i--)
  141. R[i].setX(R[i+1].getX());
  142.  
  143.  
  144.  
  145.  
  146.  
  147. } if (false &&!LStack.isEmpty()) {
  148. Stack<Plate>Ltemp = LStack;
  149.  
  150. Plate[] L=new Plate[LStack.size()];
  151.  
  152. int i=0;
  153. System.out.println(Ltemp.size());
  154. while(Ltemp.peek()!=null)
  155. L[i++]=Ltemp.pop();
  156. L[L.length-1].setX(control.get(0).getX());
  157. for ( i=L.length-2;i<=0;i--)
  158. L[i].setX(L[i+1].getX());
  159. }
  160. if (!L.isEmpty()) {
  161. L.get(0).setX(control.get(1).getX());
  162. for (int i=1;i<L.size();i++)
  163. L.get(i).setX(L.get(i-1).getX());
  164. }
  165. if (!R.isEmpty()) {
  166. R.get(0).setX(control.get(0).getX());
  167. for (int i=1;i<R.size();i++)
  168. R.get(i).setX(R.get(i-1).getX());
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. for(Plate m :moving.toArray(new Plate[moving.size()])){
  180. boolean flag=false;
  181. // System.out.println("done");
  182.  
  183. if (RStack.isEmpty()) {
  184. if (intersect(control.get(0), m) ) {
  185. m.setX(control.get(0).getX());
  186.  
  187. m.setY(m.getY());
  188. RStack.push(m); R.add(m);
  189. if (m.getShape()==Shape.Crown)
  190. m.setY(m.getY()+10);
  191.  
  192.  
  193. else m.setY(m.getY());
  194. control.add(m);
  195. moving.remove(m); flag=true;
  196. }
  197. }
  198. if (!RStack.isEmpty()&&intersect(R.get(R.size()-1), m) ) {
  199. m.setX(RStack.peek().getX());
  200. m.setY(m.getY());
  201.  
  202. if (( R.get(R.size()-1)).getShape()==Shape.Lantern&&(m.getShape()==Shape.Crown))
  203. m.setY(m.getY()+15);
  204. if (( R.get(R.size()-1)).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
  205. m.setY(m.getY()-15);
  206. RStack.push(m); R.add(m);
  207.  
  208. control.add(m);
  209.  
  210. moving.remove(m); flag=true;
  211.  
  212. }
  213. if (LStack.isEmpty()) {
  214. if (intersect(control.get(1), m) ) {
  215. m.setX(control.get(1).getX()-15);
  216.  
  217. m.setY(m.getY());
  218. LStack.push(m); L.add(m);
  219.  
  220. if (m.getShape()==Shape.Crown)
  221. m.setY(m.getY()+10);
  222. else m.setY(m.getY());
  223.  
  224. control.add(m);
  225. moving.remove(m); flag=true;
  226. }
  227. }
  228. if (!LStack.isEmpty()&&intersect(L.get(L.size()-1), m) ) {
  229. m.setX(LStack.peek().getX()-15);
  230. m.setY(m.getY());
  231. if (( L.get(L.size()-1)).getShape()==Shape.Lantern&&(m.getShape()==Shape.Crown))
  232. m.setY(m.getY()+15);
  233. if (( L.get(L.size()-1)).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
  234. m.setY(m.getY()-15);
  235. LStack.push(m); L.add(m);
  236.  
  237. control.add(m);
  238.  
  239. moving.remove(m); flag=true;
  240.  
  241. }
  242. //if (flag==true) m.setX(m.getX()-15);
  243. /*
  244. if (intersect(LStack.peek(), m) ) {
  245. m.setX(LStack.peek().getX());
  246. if (( RStack.peek()).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
  247. m.setY(m.getY()-10);
  248. LStack.push(m);
  249. m.setY(m.getY());
  250.  
  251.  
  252. }
  253. if (hR>400&&intersect(RStack.peek(), m)) {
  254. if (((Plate)m).getShape()==Shape.Crown)
  255. m.setY(m.getY()+30);
  256. if (((Plate)m).getShape()==Shape.Lantern)
  257. m.setY(m.getY()+15);
  258. hR-=100;
  259.  
  260.  
  261. }
  262. if (hL>400&&intersect(LStack.peek(), m)) {
  263. if (((Plate)m).getShape()==Shape.Crown)
  264. m.setY(m.getY()+30);
  265. if (((Plate)m).getShape()==Shape.Lantern)
  266. m.setY(m.getY()+15);
  267. hL-=100;
  268.  
  269.  
  270. }
  271. */
  272.  
  273. // m.setX(m.getX() + (Math.random() > 0.5 ? 1 : -1));
  274.  
  275. }
  276.  
  277. //return true; // game over (lose)
  278.  
  279. // check if any constant object still visible
  280. boolean foundVisible = false;
  281. for (GameObject n : constant)
  282. foundVisible |= n.isVisible();
  283. /*
  284. * if(!foundVisible) return false; // game ends (win)
  285. */
  286. return true;
  287. }
  288.  
  289. @Override
  290. public int getSpeed() {
  291. return level.speed();
  292. }
  293.  
  294. @Override
  295. public int getControlSpeed() {
  296. return 20;
  297. }
  298.  
  299. @Override
  300. public List<GameObject> getConstantObjects() {
  301. return constant;
  302. }
  303.  
  304. @Override
  305. public List<GameObject> getMovableObjects() {
  306. return moving;
  307. }
  308.  
  309. @Override
  310. public List<GameObject> getControlableObjects() {
  311. return control;
  312. }
  313.  
  314. @Override
  315. public int getWidth() {
  316. return width;
  317. }
  318.  
  319. @Override
  320. public int getHeight() {
  321. return height;
  322. }
  323.  
  324. @Override
  325. public String getStatus() {
  326. return "Please Use Arrows To Move | " + "Score=" + score + " | Time="
  327. + Math.max(0, (MAX_TIME - (System.currentTimeMillis() - startTime)) / 1000);
  328. }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement