Burger Time - Hero


/ Published in: Java
Save to your folder(s)

OO Software Development Project


Copy this code and paste it in your HTML
  1. /**
  2.  *
  3.  */
  4. package burgertime;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Shape;
  8. import java.awt.geom.Ellipse2D;
  9. import java.awt.geom.Point2D;
  10. import java.awt.geom.Rectangle2D;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15.  
  16. import javax.imageio.ImageIO;
  17.  
  18. /**
  19.  * This is the hero class, has all of the characteristics of the user (the
  20.  * hero).
  21.  *
  22.  * @author xiaox. trowbrct. obrienm.
  23.  */
  24. public class Hero extends AbstractCharacter {
  25. public int VELOCITY;
  26. public Burger[] burgers;
  27. public int lives;
  28. public int pepperSprays;
  29. public Rectangle2D character;
  30. private Color heroColor;
  31. public Guard[] guards;
  32. public boolean heroState;
  33. public boolean pepperDeployed;
  34. public boolean cheat;
  35.  
  36. /**
  37. * Creates the character, initializes velocity.
  38. *
  39. * @param start
  40. * @param characterWidth
  41. * @param characterHeight
  42. * @param pathList
  43. * @param burger
  44. * @param guards
  45. */
  46. Hero(Point2D start, double characterWidth, double characterHeight,
  47. ArrayList<Rectangle2D> pathList, Burger[] burger, Guard[] guards, Score score) {
  48. super(start, characterWidth, characterHeight, pathList,score);
  49. this.VELOCITY = 4;
  50. this.burgers = new Burger[4];
  51. this.guards = new Guard[4];
  52. for (int i = 0; i < 4; i++) {
  53. this.burgers[i] = burger[i];
  54. this.guards[i] = guards[i];
  55. }
  56. this.heroColor = Color.GREEN;
  57. this.lives = 3;
  58. this.pepperSprays = 5;
  59. this.character = new Rectangle2D.Double(super.currentPosition.getX(),
  60. super.currentPosition.getY(), characterWidth, characterHeight);
  61.  
  62. this.heroState = false;
  63. this.pepperDeployed = false;
  64. this.cheat = false;
  65. }
  66.  
  67. /**
  68. * When the hero dies, reset it's position to the start point.
  69. *
  70. */
  71. public void resetPosition() {
  72. this.character.setRect(new Rectangle2D.Double(super.STARTPOINT.getX(),
  73. super.STARTPOINT.getY(), super.width, super.height));
  74. this.currentPosition = new Point2D.Double(super.STARTPOINT.getX(),
  75. super.STARTPOINT.getY());
  76. }
  77.  
  78. /**
  79. * Creates a circle for the pepper spray to appear on the screen. Returns
  80. * the circle.
  81. *
  82. * @return pepperSpray
  83. */
  84. public Ellipse2D.Double deployPepperSpray() {
  85. this.pepperDeployed = true;
  86. this.character.getCenterX() - 30,
  87. this.character.getCenterY() - 30, 60, 60);
  88. return pepperSpray;
  89. }
  90.  
  91. /*
  92. * (non-Javadoc)
  93. *
  94. * @see burgertime.AbstractCharacter#updatePosition(int, int)
  95. */
  96. @Override
  97. public void updatePosition(double x, double y) {
  98. // moves the position the given direction from the ActionControl class
  99. Point2D[] pointList = this.getHeroCorners(x, y);
  100.  
  101. if (isOnPath(pointList) || this.cheat == true) {
  102. if (this.contactWithGuard(pointList) == false) {
  103. this.currentPosition = pointList[0];
  104. this.character = new Rectangle2D.Double(
  105. super.currentPosition.getX(),
  106. super.currentPosition.getY(), super.width, super.height);
  107.  
  108. for (int i = 0; i < this.burgers.length; i++) {
  109. this.burgers[i].depressSection(pointList[2].getX(),
  110. pointList[2].getY());
  111. this.burgers[i].depressSection(pointList[3].getX(),
  112. pointList[3].getY());
  113. }
  114.  
  115. } else {
  116. this.die();
  117. this.addScore(-100);
  118. }
  119. }
  120. }
  121.  
  122. /**
  123. * Gets the bottom two points of the hero and returns it to check for
  124. * collision detection.
  125. *
  126. * @param x
  127. * @param y
  128. * @return heroCorners
  129. */
  130. public Point2D[] getHeroCorners(double x, double y) {
  131. Point2D[] pointList = new Point2D[4];
  132. Point2D newPositionTopLeft = new Point2D.Double(
  133. this.currentPosition.getX() + (x * this.VELOCITY),
  134. this.currentPosition.getY() + (y * this.VELOCITY));
  135. Point2D newPositionTopRight = new Point2D.Double(
  136. this.currentPosition.getX() + (x * this.VELOCITY) + super.width,
  137. this.currentPosition.getY() + (y * this.VELOCITY));
  138. Point2D newPositionBottomLeft = new Point2D.Double(
  139. this.currentPosition.getX() + (x * this.VELOCITY),
  140. this.currentPosition.getY() + (y * this.VELOCITY)
  141. + super.height);
  142. Point2D newPositionBottomRight = new Point2D.Double(
  143. this.currentPosition.getX() + (x * this.VELOCITY) + super.width,
  144. this.currentPosition.getY() + (y * this.VELOCITY)
  145. + super.height);
  146.  
  147. pointList[0] = newPositionTopLeft;
  148. pointList[1] = newPositionTopRight;
  149. pointList[2] = newPositionBottomLeft;
  150. pointList[3] = newPositionBottomRight;
  151.  
  152. return pointList;
  153. }
  154.  
  155. @Override
  156. public boolean isOnPath(Point2D[] pointList) {
  157. int count = 0;
  158. for (int i = 2; i < pointList.length; i++) {
  159. for (int j = 0; j < super.pathList.size(); j++) {
  160. if (super.pathList.get(j).contains(pointList[i])) {
  161. count++;
  162. break;
  163. }
  164. }
  165. }
  166. if (count == 2) {
  167. return true;
  168. }
  169. return false;
  170. }
  171.  
  172. /**
  173. * Checks for contact with the guard.
  174. *
  175. * @param pointList
  176. * @return isContact
  177. */
  178. public boolean contactWithGuard(Point2D[] pointList) {
  179. if (this.pepperDeployed) {
  180. return false;
  181. }
  182. for (int i = 0; i < this.guards.length; i++) {
  183. for (int j = 0; j < pointList.length; j++) {
  184. if (this.guards[i].getShape().contains(pointList[j])) {
  185. return true;
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191.  
  192. /*
  193. * (non-Javadoc)
  194. *
  195. * @see burgertime.Drawable#getColor()
  196. */
  197. @Override
  198. public Color getColor() {
  199. return this.heroColor;
  200. }
  201.  
  202. /*
  203. * (non-Javadoc)
  204. *
  205. * @see burgertime.Drawable#getShape()
  206. */
  207. @Override
  208. public Shape getShape() {
  209. return this.character;
  210. }
  211.  
  212. @Override
  213. public void die() {
  214. this.heroState = true;
  215. this.pepperDeployed = false;
  216. this.lives--;
  217. }
  218.  
  219. /**
  220. * Returns whether the hero is dead or alive.
  221. *
  222. * @return heroState
  223. */
  224. public boolean isDead() {
  225. return this.heroState;
  226. }
  227.  
  228. /* (non-Javadoc)
  229. * @see burgertime.AbstractCharacter#getImage()
  230. */
  231. @Override
  232. public BufferedImage getImage() {
  233. try {
  234. BufferedImage image=ImageIO.read(new File("image/chef.png"));
  235. return image;
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. }
  239. return null;
  240. }
  241.  
  242. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.