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. /*
  109. * (non-Javadoc)
  110. *
  111. * @see burgertime.AbstractCharacter#updatePosition(int, int)
  112. */
  113. @Override
  114. public void updatePosition(double x, double y) {
  115. if (this.isAlive == false) {
  116. this.character = new Rectangle2D.Double(-100, -100, 0, 0);
  117. this.currentPosition = new Point2D.Double(-5, -5);
  118. }
  119. for (int i = 0; i < 4; i++) {
  120. for (int j = 0; j < 4; j++) {
  121. for (int k = 0; k < 5; k++) {
  122. if (this.character
  123. .intersects(this.drawComponent.burger[i].burger[j][k])
  124. && this.drawComponent.burger[i].getIsMoving(j)) {
  125. this.isAlive = false;
  126. this.xDelta = 0;
  127. this.yDelta = 1;
  128. this.addScore(100);
  129. Timer timer = new Timer();
  130. timer.schedule(new TimerTask() {
  131.  
  132. @Override
  133. public void run() {
  134. Guard.this.resetPosition();
  135. }
  136. }, 2500);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142.  
  143. Point2D newPositionTopLeft = new Point2D.Double(
  144. this.currentPosition.getX() + (x * this.VELOCITY),
  145. this.currentPosition.getY() + (y * this.VELOCITY));
  146.  
  147. Point2D newPositionBottomLeft = new Point2D.Double(
  148. this.currentPosition.getX() + (x * this.VELOCITY),
  149. this.currentPosition.getY() + (y * this.VELOCITY)
  150. + super.height);
  151. Point2D newPositionBottomRight = new Point2D.Double(
  152. this.currentPosition.getX() + (x * this.VELOCITY) + super.width,
  153. this.currentPosition.getY() + (y * this.VELOCITY)
  154. + super.height);
  155.  
  156. this.pointList[0] = newPositionBottomLeft;
  157. this.pointList[1] = newPositionBottomRight;
  158.  
  159. if (this.isOnPath(this.pointList)) {
  160. this.currentPosition = newPositionTopLeft;
  161. this.character = new Rectangle2D.Double(
  162. super.currentPosition.getX(), super.currentPosition.getY(),
  163. super.width, super.height);
  164.  
  165. }
  166. }
  167.  
  168. /**
  169. * TODO Put here a description of what this method does.
  170. *
  171. * @return
  172. *
  173. */
  174. @SuppressWarnings("javadoc")
  175. public boolean previousInIntersection() {
  176. Point2D prevPositionBottomLeft = new Point2D.Double(
  177. this.currentPosition.getX() - (this.xDelta * this.VELOCITY),
  178. this.currentPosition.getY() - (this.yDelta * this.VELOCITY)
  179. + super.height);
  180. Point2D prevPositionBottomRight = new Point2D.Double(
  181. this.currentPosition.getX() - (this.xDelta * this.VELOCITY)
  182. + super.width, this.currentPosition.getY()
  183. - (this.yDelta * this.VELOCITY) + super.height);
  184.  
  185. this.prevPointList[0] = prevPositionBottomLeft;
  186. this.prevPointList[1] = prevPositionBottomRight;
  187.  
  188. return this.isAtIntersection(this.prevPointList);
  189. }
  190.  
  191. /**
  192. * TODO Put here a description of what this method does.
  193. *
  194. * @param x
  195. * @param y
  196. * @return
  197. */
  198. public boolean isOption(int x, int y) {
  199. int xBuffer = x;
  200. int yBuffer = y;
  201. Point2D[] option = new Point2D[2];
  202.  
  203. Point2D nextPossiblePositionBottomLeft = new Point2D.Double(
  204. this.currentPosition.getX() + (this.xDelta * this.VELOCITY)
  205. + xBuffer, this.currentPosition.getY()
  206. + (this.yDelta * this.VELOCITY) + super.height
  207. + yBuffer);
  208. Point2D nextPossiblePositionBottomRight = new Point2D.Double(
  209. this.currentPosition.getX() + (this.xDelta * this.VELOCITY)
  210. + super.width + xBuffer, this.currentPosition.getY()
  211. + (this.yDelta * this.VELOCITY) + super.height
  212. + yBuffer);
  213.  
  214. option[0] = nextPossiblePositionBottomLeft;
  215. option[1] = nextPossiblePositionBottomRight;
  216.  
  217. return (this.isOnPath(option));
  218. }
  219.  
  220. /**
  221. * TODO Put here a description of what this method does.
  222. *
  223. */
  224. public void moveGuard() {
  225. Point2D[] bottomCornersCurrent = new Point2D[2];
  226. bottomCornersCurrent[0] = new Point2D.Double(
  227. this.currentPosition.getX(), this.currentPosition.getY()
  228. + super.height);
  229. bottomCornersCurrent[1] = new Point2D.Double(
  230. this.currentPosition.getX() + super.width,
  231. this.currentPosition.getY() + super.height);
  232.  
  233. int type = 0;
  234. int posOrNegX = 0;
  235. int posOrNegY = 0;
  236.  
  237. if (this.isAtIntersection(bottomCornersCurrent)
  238. && !this.previousInIntersection()) {
  239.  
  240. if (this.drawComponent.hero.currentPosition.getX() > this.currentPosition
  241. .getX()) {
  242. posOrNegX = 1;
  243. } else {
  244. posOrNegX = -1;
  245. }
  246. if (this.drawComponent.hero.currentPosition.getY() > this.currentPosition
  247. .getY()) {
  248. posOrNegY = 1;
  249. } else {
  250. posOrNegY = -1;
  251. }
  252.  
  253. double num = Math.random();
  254. if (num < 0.5) {
  255. if (!this.isOption(15 * posOrNegX, 0)) {
  256. if (this.isOption(0, 15 * posOrNegY)) {
  257. type = 1;
  258. } else {
  259. this.xDelta *= -1;
  260. this.yDelta *= -1;
  261. this.stuck = true;
  262. }
  263. }
  264. } else {
  265. type = 1;
  266. if (!this.isOption(0, 15 * posOrNegY)) {
  267. if (this.isOption(15 * posOrNegX, 0)) {
  268. type = 0;
  269. } else {
  270. this.xDelta *= -1;
  271. this.yDelta *= -1;
  272. this.stuck = true;
  273. }
  274. }
  275. }
  276. if (type == 0 && this.stuck == false) {
  277. if (posOrNegX > 0) {
  278. this.xDelta = 1;
  279. this.yDelta = 0;
  280. } else {
  281. this.xDelta = -1;
  282. this.yDelta = 0;
  283. }
  284. } else if (type == 1 && this.stuck == false) {
  285. if (posOrNegY > 0) {
  286. this.xDelta = 0;
  287. this.yDelta = 1;
  288. } else {
  289. this.xDelta = 0;
  290. this.yDelta = -1;
  291. }
  292. }
  293. }
  294. this.stuck = false;
  295. this.updatePosition(this.xDelta, this.yDelta);
  296. }
  297.  
  298. /*
  299. * (non-Javadoc)
  300. *
  301. * @see burgertime.Drawable#getColor()
  302. */
  303. @Override
  304. public Color getColor() {
  305. return this.guardColor;
  306. }
  307.  
  308. /*
  309. * (non-Javadoc)
  310. *
  311. * @see burgertime.Drawable#getShape()
  312. */
  313. @Override
  314. public Shape getShape() {
  315. return this.character;
  316. }
  317.  
  318. /*
  319. * (non-Javadoc)
  320. *
  321. * @see burgertime.AbstractCharacter#isOnPath(java.awt.geom.Point2D[])
  322. */
  323. @Override
  324. public boolean isOnPath(Point2D[] pointList) {
  325. int count = 0;
  326. for (int i = 0; i < pointList.length; i++) {
  327. for (int j = 0; j < super.pathList.size(); j++) {
  328. if (super.pathList.get(j).contains(pointList[i])) {
  329. count++;
  330. break;
  331. }
  332. }
  333. }
  334. if (count == 2) {
  335. return true;
  336. }
  337. return false;
  338. }
  339.  
  340. /**
  341. * TODO Put here a description of what this method does.
  342. *
  343. * @param pointList
  344. * @return
  345. */
  346. public boolean isAtIntersection(Point2D[] pointList) {
  347. int count = 0;
  348. Rectangle2D firstRect1 = null;
  349. Rectangle2D secondRect1 = null;
  350. Rectangle2D firstRect2 = null;
  351. Rectangle2D secondRect2 = null;
  352. for (int i = 0; i < pointList.length; i++) {
  353. count = 0;
  354. for (int j = 0; j < super.pathList.size(); j++) {
  355. if (super.pathList.get(j).contains(pointList[i])
  356. && Math.abs(super.pathList.get(j).getMinY()
  357. - pointList[i].getY()) > 4
  358. && Math.abs(super.pathList.get(j).getMinX()
  359. - pointList[i].getX()) > 4) {
  360. count++;
  361. if (count == 1 && i == 0) {
  362. firstRect1 = new Rectangle2D.Double();
  363. firstRect1 = super.pathList.get(j);
  364. } else if (i == 0) {
  365. secondRect1 = new Rectangle2D.Double();
  366. secondRect1 = super.pathList.get(j);
  367. if (firstRect1.equals(secondRect1)) {
  368. count--;
  369. secondRect1 = new Rectangle2D.Double(-10, -10, 0, 0);
  370. }
  371. } else if (count == 1 && i == 1) {
  372. firstRect2 = new Rectangle2D.Double();
  373. firstRect2 = super.pathList.get(j);
  374. } else if (i == 1) {
  375. secondRect2 = new Rectangle2D.Double();
  376. secondRect2 = super.pathList.get(j);
  377. if (firstRect2.equals(secondRect2)) {
  378. count--;
  379. secondRect2 = new Rectangle2D.Double(-1, -1, -1, -1);
  380. }
  381. }
  382. } else if (count == 2) {
  383. break;
  384. }
  385. }
  386. }
  387. if (firstRect1 == null) {
  388. firstRect1 = new Rectangle2D.Double(1000, 100, 10, 10);
  389. }
  390. if (firstRect2 == null) {
  391. firstRect2 = new Rectangle2D.Double(-19, 10, 25, 50);
  392. }
  393. if (secondRect1 == null) {
  394. secondRect1 = new Rectangle2D.Double(-35, -10, 100, 100);
  395. }
  396. if (secondRect2 == null) {
  397. secondRect2 = new Rectangle2D.Double(-100, 1000, 100, 100);
  398. }
  399. if (firstRect1.equals(firstRect2) && secondRect1.equals(secondRect2)) {
  400. return true;
  401. }
  402. return false;
  403. }
  404.  
  405. /*
  406. * (non-Javadoc)
  407. *
  408. * @see java.lang.Runnable#run()
  409. */
  410. @Override
  411. public void run() {
  412. try {
  413. while (this.drawComponent.isLevelThreading[this.levelNumber]) {
  414. this.moveGuard();
  415. Thread.sleep(10);
  416. }
  417. } catch (InterruptedException e) {
  418. // do nothing here
  419. }
  420. }
  421.  
  422. /* (non-Javadoc)
  423. * @see burgertime.AbstractCharacter#getImage()
  424. */
  425. @Override
  426. public BufferedImage getImage() {
  427. try {
  428. BufferedImage image=ImageIO.read(new File("image/Monster.png"));
  429. return image;
  430. } catch (IOException e) {
  431. e.printStackTrace();
  432. }
  433. return null;
  434. }
  435.  
  436. }

Report this snippet  

You need to login to post a comment.