Posted By

obrienm on 08/29/14


Tagged


Versions (?)

Burger Time - Guard


 / Published in: Java
 

OO Software Development Project

  1. /**
  2.  *
  3.  */
  4. package burgertime;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Shape;
  8. import java.awt.geom.Point2D;
  9. import java.awt.geom.Rectangle2D;
  10. import java.awt.geom.Rectangle2D.Double;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Timer;
  16. import java.util.TimerTask;
  17.  
  18. import javax.imageio.ImageIO;
  19.  
  20. /**
  21.  * Guard class has one guard and handles all of the moving and collision
  22.  * detection with the path
  23.  *
  24.  * @author xiaox. trowbrct. obrienm.
  25.  */
  26. public class Guard extends AbstractCharacter implements Runnable {
  27. private double VELOCITY;
  28. public boolean isAlive;
  29. private Rectangle2D character;
  30. private Rectangle2D currentPathRectangle;
  31. private Color guardColor;
  32. private DrawableComponent drawComponent;
  33. private Point2D[] pointList;
  34. private Point2D[] prevPointList;
  35. private int xDelta;
  36. private int yDelta;
  37. private int levelNumber;
  38. private boolean stuck;
  39.  
  40. /**
  41. * Takes the start point, width, heighth, and path list for the guard to
  42. * follow
  43. *
  44. * @param start
  45. * @param characterWidth
  46. * @param characterHeight
  47. * @param pathList
  48. * @param drawableComponent
  49. * @param levelNumber
  50. * */
  51. Guard(Point2D start, double characterWidth, double characterHeight,
  52. ArrayList<Rectangle2D> pathList,
  53. DrawableComponent drawableComponent, int levelNumber, Score score) {
  54. super(start, characterWidth, characterHeight, pathList, score);
  55. this.VELOCITY = 0.65;
  56. this.stuck = false;
  57. this.drawComponent = drawableComponent;
  58. this.currentPathRectangle = new Rectangle2D.Double();
  59. this.guardColor = Color.RED;
  60. this.character = new Rectangle2D.Double(super.currentPosition.getX(),
  61. super.currentPosition.getY(), characterWidth, characterHeight);
  62. this.xDelta = 0;
  63. this.yDelta = 1;
  64. this.levelNumber = levelNumber;
  65. this.isAlive = true;
  66.  
  67. this.prevPointList = new Point2D[2];
  68. this.pointList = new Point2D[2];
  69.  
  70. Point2D newPositionBottomLeft = new Point2D.Double(
  71. this.currentPosition.getX(), this.currentPosition.getY()
  72. + super.height);
  73. Point2D newPositionBottomRight = new Point2D.Double(
  74. this.currentPosition.getX() + super.width,
  75. this.currentPosition.getY() + super.height);
  76.  
  77. this.pointList[0] = newPositionBottomLeft;
  78. this.pointList[1] = newPositionBottomRight;
  79.  
  80. Point2D prevPositionBottomLeft = new Point2D.Double(
  81. this.currentPosition.getX() - (this.xDelta * this.VELOCITY),
  82. this.currentPosition.getY() - (this.yDelta * this.VELOCITY)
  83. + super.height);
  84. Point2D prevPositionBottomRight = new Point2D.Double(
  85. this.currentPosition.getX() - (this.xDelta * this.VELOCITY)
  86. + super.width, this.currentPosition.getY()
  87. - (this.yDelta * this.VELOCITY) + super.height);
  88.  
  89. this.prevPointList[0] = prevPositionBottomLeft;
  90. this.prevPointList[1] = prevPositionBottomRight;
  91. }
  92.  
  93. /**
  94. * When the Hero dies, this resets the guard's position
  95. *
  96. */
  97. public void resetPosition() {
  98. this.isAlive = true;
  99. this.character.setRect(new Rectangle2D.Double(super.STARTPOINT.getX(),
  100. super.STARTPOINT.getY(), super.width, super.height));
  101. this.currentPosition = new Point2D.Double(super.STARTPOINT.getX(),
  102. super.STARTPOINT.getY());
  103. this.xDelta = 0;
  104. this.yDelta = 1;
  105. this.score.addScore(-100);
  106. }
  107.  
  108. // -----FROM DRAWABLE INTERFACE-----//
  109.  
  110. /*
  111. * (non-Javadoc)
  112. *
  113. * @see burgertime.AbstractCharacter#updatePosition(int, int)
  114. */
  115. @Override
  116. public void updatePosition(double x, double y) {
  117. if (this.isAlive == false) {
  118. this.character = new Rectangle2D.Double(-100, -100, 0, 0);
  119. this.currentPosition = new Point2D.Double(-5, -5);
  120. }
  121. for (int i = 0; i < 4; i++) {
  122. for (int j = 0; j < 4; j++) {
  123. for (int k = 0; k < 5; k++) {
  124. if (this.character
  125. .intersects(this.drawComponent.burger[i].burger[j][k])
  126. && this.drawComponent.burger[i].getIsMoving(j)) {
  127. this.isAlive = false;
  128. this.xDelta = 0;
  129. this.yDelta = 1;
  130. this.addScore(100);
  131. Timer timer = new Timer();
  132. timer.schedule(new TimerTask() {
  133.  
  134. @Override
  135. public void run() {
  136. Guard.this.resetPosition();
  137. }
  138. }, 2500);
  139. break;
  140. }
  141. }
  142. }
  143. }
  144.  
  145. Point2D newPositionTopLeft = new Point2D.Double(
  146. this.currentPosition.getX() + (x * this.VELOCITY),
  147. this.currentPosition.getY() + (y * this.VELOCITY));
  148.  
  149. Point2D newPositionBottomLeft = new Point2D.Double(
  150. this.currentPosition.getX() + (x * this.VELOCITY),
  151. this.currentPosition.getY() + (y * this.VELOCITY)
  152. + super.height);
  153. Point2D newPositionBottomRight = new Point2D.Double(
  154. this.currentPosition.getX() + (x * this.VELOCITY) + super.width,
  155. this.currentPosition.getY() + (y * this.VELOCITY)
  156. + super.height);
  157.  
  158. this.pointList[0] = newPositionBottomLeft;
  159. this.pointList[1] = newPositionBottomRight;
  160.  
  161. if (this.isOnPath(this.pointList)) {
  162. this.currentPosition = newPositionTopLeft;
  163. this.character = new Rectangle2D.Double(
  164. super.currentPosition.getX(), super.currentPosition.getY(),
  165. super.width, super.height);
  166.  
  167. }
  168. }
  169.  
  170. /**
  171. * TODO Put here a description of what this method does.
  172. *
  173. * @return
  174. *
  175. */
  176. @SuppressWarnings("javadoc")
  177. public boolean previousInIntersection() {
  178. Point2D prevPositionBottomLeft = new Point2D.Double(
  179. this.currentPosition.getX() - (this.xDelta * this.VELOCITY),
  180. this.currentPosition.getY() - (this.yDelta * this.VELOCITY)
  181. + super.height);
  182. Point2D prevPositionBottomRight = new Point2D.Double(
  183. this.currentPosition.getX() - (this.xDelta * this.VELOCITY)
  184. + super.width, this.currentPosition.getY()
  185. - (this.yDelta * this.VELOCITY) + super.height);
  186.  
  187. this.prevPointList[0] = prevPositionBottomLeft;
  188. this.prevPointList[1] = prevPositionBottomRight;
  189.  
  190. return this.isAtIntersection(this.prevPointList);
  191. }
  192.  
  193. /**
  194. * TODO Put here a description of what this method does.
  195. *
  196. * @param x
  197. * @param y
  198. * @return
  199. */
  200. public boolean isOption(int x, int y) {
  201. int xBuffer = x;
  202. int yBuffer = y;
  203. Point2D[] option = new Point2D[2];
  204.  
  205. Point2D nextPossiblePositionBottomLeft = new Point2D.Double(
  206. this.currentPosition.getX() + (this.xDelta * this.VELOCITY)
  207. + xBuffer, this.currentPosition.getY()
  208. + (this.yDelta * this.VELOCITY) + super.height
  209. + yBuffer);
  210. Point2D nextPossiblePositionBottomRight = new Point2D.Double(
  211. this.currentPosition.getX() + (this.xDelta * this.VELOCITY)
  212. + super.width + xBuffer, this.currentPosition.getY()
  213. + (this.yDelta * this.VELOCITY) + super.height
  214. + yBuffer);
  215.  
  216. option[0] = nextPossiblePositionBottomLeft;
  217. option[1] = nextPossiblePositionBottomRight;
  218.  
  219. return (this.isOnPath(option));
  220. }
  221.  
  222. /**
  223. * TODO Put here a description of what this method does.
  224. *
  225. */
  226. public void moveGuard() {
  227. Point2D[] bottomCornersCurrent = new Point2D[2];
  228. bottomCornersCurrent[0] = new Point2D.Double(
  229. this.currentPosition.getX(), this.currentPosition.getY()
  230. + super.height);
  231. bottomCornersCurrent[1] = new Point2D.Double(
  232. this.currentPosition.getX() + super.width,
  233. this.currentPosition.getY() + super.height);
  234.  
  235. int type = 0;
  236. int posOrNegX = 0;
  237. int posOrNegY = 0;
  238.  
  239. if (this.isAtIntersection(bottomCornersCurrent)
  240. && !this.previousInIntersection()) {
  241.  
  242. if (this.drawComponent.hero.currentPosition.getX() > this.currentPosition
  243. .getX()) {
  244. posOrNegX = 1;
  245. } else {
  246. posOrNegX = -1;
  247. }
  248. if (this.drawComponent.hero.currentPosition.getY() > this.currentPosition
  249. .getY()) {
  250. posOrNegY = 1;
  251. } else {
  252. posOrNegY = -1;
  253. }
  254.  
  255. double num = Math.random();
  256. if (num < 0.5) {
  257. if (!this.isOption(15 * posOrNegX, 0)) {
  258. if (this.isOption(0, 15 * posOrNegY)) {
  259. type = 1;
  260. } else {
  261. this.xDelta *= -1;
  262. this.yDelta *= -1;
  263. this.stuck = true;
  264. }
  265. }
  266. } else {
  267. type = 1;
  268. if (!this.isOption(0, 15 * posOrNegY)) {
  269. if (this.isOption(15 * posOrNegX, 0)) {
  270. type = 0;
  271. } else {
  272. this.xDelta *= -1;
  273. this.yDelta *= -1;
  274. this.stuck = true;
  275. }
  276. }
  277. }
  278. if (type == 0 && this.stuck == false) {
  279. if (posOrNegX > 0) {
  280. this.xDelta = 1;
  281. this.yDelta = 0;
  282. } else {
  283. this.xDelta = -1;
  284. this.yDelta = 0;
  285. }
  286. } else if (type == 1 && this.stuck == false) {
  287. if (posOrNegY > 0) {
  288. this.xDelta = 0;
  289. this.yDelta = 1;
  290. } else {
  291. this.xDelta = 0;
  292. this.yDelta = -1;
  293. }
  294. }
  295. }
  296. this.stuck = false;
  297. this.updatePosition(this.xDelta, this.yDelta);
  298. }
  299.  
  300. /*
  301. * (non-Javadoc)
  302. *
  303. * @see burgertime.Drawable#getColor()
  304. */
  305. @Override
  306. public Color getColor() {
  307. return this.guardColor;
  308. }
  309.  
  310. /*
  311. * (non-Javadoc)
  312. *
  313. * @see burgertime.Drawable#getShape()
  314. */
  315. @Override
  316. public Shape getShape() {
  317. return this.character;
  318. }
  319.  
  320. /*
  321. * (non-Javadoc)
  322. *
  323. * @see burgertime.AbstractCharacter#isOnPath(java.awt.geom.Point2D[])
  324. */
  325. @Override
  326. public boolean isOnPath(Point2D[] pointList) {
  327. int count = 0;
  328. for (int i = 0; i < pointList.length; i++) {
  329. for (int j = 0; j < super.pathList.size(); j++) {
  330. if (super.pathList.get(j).contains(pointList[i])) {
  331. count++;
  332. break;
  333. }
  334. }
  335. }
  336. if (count == 2) {
  337. return true;
  338. }
  339. return false;
  340. }
  341.  
  342. /**
  343. * TODO Put here a description of what this method does.
  344. *
  345. * @param pointList
  346. * @return
  347. */
  348. public boolean isAtIntersection(Point2D[] pointList) {
  349. int count = 0;
  350. Rectangle2D firstRect1 = null;
  351. Rectangle2D secondRect1 = null;
  352. Rectangle2D firstRect2 = null;
  353. Rectangle2D secondRect2 = null;
  354. for (int i = 0; i < pointList.length; i++) {
  355. count = 0;
  356. for (int j = 0; j < super.pathList.size(); j++) {
  357. if (super.pathList.get(j).contains(pointList[i])
  358. && Math.abs(super.pathList.get(j).getMinY()
  359. - pointList[i].getY()) > 4
  360. && Math.abs(super.pathList.get(j).getMinX()
  361. - pointList[i].getX()) > 4) {
  362. count++;
  363. if (count == 1 && i == 0) {
  364. firstRect1 = new Rectangle2D.Double();
  365. firstRect1 = super.pathList.get(j);
  366. } else if (i == 0) {
  367. secondRect1 = new Rectangle2D.Double();
  368. secondRect1 = super.pathList.get(j);
  369. if (firstRect1.equals(secondRect1)) {
  370. count--;
  371. secondRect1 = new Rectangle2D.Double(-10, -10, 0, 0);
  372. }
  373. } else if (count == 1 && i == 1) {
  374. firstRect2 = new Rectangle2D.Double();
  375. firstRect2 = super.pathList.get(j);
  376. } else if (i == 1) {
  377. secondRect2 = new Rectangle2D.Double();
  378. secondRect2 = super.pathList.get(j);
  379. if (firstRect2.equals(secondRect2)) {
  380. count--;
  381. secondRect2 = new Rectangle2D.Double(-1, -1, -1, -1);
  382. }
  383. }
  384. } else if (count == 2) {
  385. break;
  386. }
  387. }
  388. }
  389. if (firstRect1 == null) {
  390. firstRect1 = new Rectangle2D.Double(1000, 100, 10, 10);
  391. }
  392. if (firstRect2 == null) {
  393. firstRect2 = new Rectangle2D.Double(-19, 10, 25, 50);
  394. }
  395. if (secondRect1 == null) {
  396. secondRect1 = new Rectangle2D.Double(-35, -10, 100, 100);
  397. }
  398. if (secondRect2 == null) {
  399. secondRect2 = new Rectangle2D.Double(-100, 1000, 100, 100);
  400. }
  401. if (firstRect1.equals(firstRect2) && secondRect1.equals(secondRect2)) {
  402. return true;
  403. }
  404. return false;
  405. }
  406.  
  407. /*
  408. * (non-Javadoc)
  409. *
  410. * @see java.lang.Runnable#run()
  411. */
  412. @Override
  413. public void run() {
  414. try {
  415. while (this.drawComponent.isLevelThreading[this.levelNumber]) {
  416. this.moveGuard();
  417. Thread.sleep(10);
  418. }
  419. } catch (InterruptedException e) {
  420. // do nothing here
  421. }
  422. }
  423.  
  424. /* (non-Javadoc)
  425. * @see burgertime.AbstractCharacter#getImage()
  426. */
  427. @Override
  428. public BufferedImage getImage() {
  429. try {
  430. BufferedImage image=ImageIO.read(new File("image/Monster.png"));
  431. return image;
  432. } catch (IOException e) {
  433. e.printStackTrace();
  434. }
  435. return null;
  436. }
  437.  
  438. }

Report this snippet  

You need to login to post a comment.