Enemy.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package model.drawObjects;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics2D;
  5. import java.awt.geom.Ellipse2D;
  6. import java.awt.geom.Line2D;
  7. import java.awt.geom.Point2D;
  8. public class Enemy extends Person{
  9. private Color color;
  10. private Ellipse2D.Double circle;
  11. private Point2D beginPoint;
  12. private Dimension size;
  13. private double xSpeed,ySpeed;
  14. /**
  15. *
  16. * @param path, the path the player is walking on.
  17. * @param c, the color of the enemy
  18. * @param size, the size of the enemy
  19. * @param stepsToFinishTheMiddle, the speed to finish the middle octagon
  20. */
  21. public Enemy(Line2D path,Color c,int size,double stepsToFinishTheMiddle) {
  22. super(path.getX1()-size/2,path.getY1()-size/2);
  23. beginPoint = new Point2D.Double(middlePoint.getX(),middlePoint.getY());
  24. this.size = new Dimension(size,size);
  25. color = c;
  26. circle = new Ellipse2D.Double(middlePoint.getX(),middlePoint.getY(),this.size.width,this.size.height);
  27. index = 0;
  28. xSpeed = (path.getX2() - circle.getCenterX())/stepsToFinishTheMiddle;
  29. ySpeed = (path.getY2() - circle.getCenterY())/stepsToFinishTheMiddle;
  30. }
  31. @Override
  32. public void draw(Graphics2D g2) {
  33. g2.setPaint(color);
  34. g2.fill(circle);
  35. }
  36. private void moveTowardsPlayer()
  37. {
  38. double newX, newY;
  39. newX = index*xSpeed+beginPoint.getX();
  40. newY = index*ySpeed+beginPoint.getY();
  41. index++;
  42. middlePoint.setLocation(newX, newY);
  43. circle.setFrame(middlePoint, size);
  44. }
  45. @Override
  46. public void update() {
  47. moveTowardsPlayer();
  48. }
  49. public Color getColor() {
  50. return color;
  51. }
  52. public void setColor(Color color) {
  53. this.color = color;
  54. }
  55. public Ellipse2D.Double getCircle() {
  56. return circle;
  57. }
  58. public void setCircle(Ellipse2D.Double circle) {
  59. this.circle = circle;
  60. }
  61. public boolean hit(Bullet bullet){
  62. if(color.equals(bullet.getColor()) && circle.getBounds2D().intersectsLine(bullet.bullet)){
  63. return true;
  64. }
  65. return false;
  66. }
  67. }