Set 3 AP computer science programming project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * super class for any enemy in the game
  3. * a enemy can spawn at a random location along the border of the map
  4. * a enemy can also calculate the distance and angle to the player
  5. * 5-23-16
  6. */
  7. package tanks;
  8. public abstract class Enemy extends Living
  9. {
  10. public Enemy()
  11. {
  12. super();
  13. }
  14. public abstract void move();
  15. /*
  16. * enemy spawns at a random location on boarder of map. Random number 1-4
  17. * to determine which edge of map enemy spawns. Random number code to
  18. * determine how far down or how far over player spawns on given edge.
  19. */
  20. public void spawn(int w, int h)
  21. {
  22. int temp=(int)(Math.random()*(4)) + 1;
  23. if(temp==1)
  24. {
  25. y=0;
  26. x=(int)(Math.random()*(w));
  27. }
  28. else if(temp==2)
  29. {
  30. y=(int)(Math.random()*(h));
  31. x=w;
  32. }
  33. else if(temp==3)
  34. {
  35. y=h;
  36. x=(int)(Math.random()*(w+1));
  37. }
  38. else
  39. {
  40. x=0;
  41. y=(int)(Math.random()*(h - 200));
  42. }
  43. }
  44. /*
  45. * Used right triangle algebra to determine distance from x,y of enemy
  46. * to x,y of player.
  47. */
  48. public double distToPlayer(RotationalElement p)
  49. {
  50. double d, a, b;
  51. a=Math.abs(x-p.x);
  52. b=Math.abs(y-p.y);
  53. d=Math.sqrt(a*a+b*b);
  54. return(d);
  55. }
  56. /*
  57. * Found lengths of imaginary triangle between player and enemy.
  58. * Then used inverse trig to determine angle.
  59. */
  60. public double angleToPlayer(RotationalElement p)
  61. {
  62. double a, b, ang;
  63. a=(x-p.x); //doesnt have to me absolute value since using atan2
  64. b=(y-p.y);
  65. ang=Math.atan2(b,a); //atan2 prevents divisible by zero errors
  66. ang = Math.toDegrees(ang);
  67. return(ang);
  68. }
  69. }