Advertisement
xatzisktv

Untitled

Feb 20th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package game2;
  2.  
  3. import utilities.Action;
  4. import utilities.Vector2D;
  5.  
  6. import java.awt.*;
  7. import java.awt.geom.AffineTransform;
  8.  
  9. import static utilities.Constants.*;
  10.  
  11. public class Ship extends GameObject{
  12. public static final int RADIUS = 8;
  13. // rotation velocity in radians per second
  14. public static final double STEER_RATE = 2* Math.PI;
  15. // acceleration when thrust is applied
  16. public static final double MAG_ACC = 200;
  17. // constant speed loss factor
  18. public static final double DRAG = 0.99;
  19. public static final Color COLOR = Color.cyan;
  20. public Vector2D position; // on frame
  21. public Vector2D velocity; // per second
  22. // direction in which the nose of the ship is pointing
  23. // this will be the direction in which thrust is applied
  24. // it is a unit vector representing the angle by which the ship has rotated
  25. public Vector2D direction;
  26. //coordinates for ship
  27. static final int[] XP = {1,1,0,-1,-1,-3,-3,-1,-2,2,1,3,3};
  28.  
  29. static final int[] YP = {0,-2,-4,-2,0,1,2,2,3,3,2,2,1};
  30. //coordinates for thruster
  31. int[] XPTHRUST2 = {-1,0,1};
  32. int[] YPTHRUST2 = {3,5,3};
  33. int[] XPTHRUST = {-2,0,2};
  34. int[] YPTHRUST = {3,6,3};
  35.  
  36.  
  37. static boolean thrusting = false;
  38.  
  39. public Bullet bullet = null;
  40. long time;
  41. static int lives;
  42. int shootdelay, bulletlife;
  43.  
  44.  
  45.  
  46. // controller which provides an Action object in each frame
  47. private Controller ctrl;
  48.  
  49. public Ship(Controller ctrl) {
  50. lives = 4;
  51. shootdelay = 400;
  52. bulletlife = 30;
  53. this.ctrl = ctrl;
  54. position = new Vector2D(FRAME_WIDTH/2, FRAME_HEIGHT/2);
  55. velocity = new Vector2D();
  56. direction = new Vector2D(0, -1);
  57. }
  58.  
  59. public void update() {
  60. Action action = ctrl.action();
  61. direction.rotate(action.turn*STEER_RATE*DT);
  62. velocity.addScaled(direction, MAG_ACC * DT * action.thrust);
  63. velocity.mult(DRAG);
  64. position.addScaled(velocity, DT);
  65. position.wrap(FRAME_WIDTH, FRAME_HEIGHT);
  66.  
  67. if (action.thrust != 0) thrusting = true;
  68. else thrusting = false;
  69.  
  70. if(action.shoot){
  71. if(System.currentTimeMillis()-time > shootdelay){
  72. time = System.currentTimeMillis();
  73. mkBullet();
  74. action.shoot = false;
  75. }
  76.  
  77. }
  78.  
  79.  
  80. }
  81.  
  82. private void mkBullet(){
  83. Bullet bullet = new Bullet(this, bulletlife);
  84. this.bullet = bullet;
  85. }
  86.  
  87. public void draw(Graphics2D g){
  88. AffineTransform at = g.getTransform();
  89. g.translate(position.x, position.y);
  90. double rot = direction.angle() + Math.PI / 2;
  91. g.rotate(rot);
  92. g.scale(5, 5);
  93. g.setColor(COLOR);
  94. g.fillPolygon(XP, YP, XP.length);
  95. if (thrusting) {
  96. g.setColor(Color.yellow);
  97. g.fillPolygon(XPTHRUST, YPTHRUST, XPTHRUST.length);
  98. g.setColor(Color.red);
  99. g.fillPolygon(XPTHRUST2, YPTHRUST2, XPTHRUST2.length);
  100. }
  101.  
  102.  
  103.  
  104. g.setTransform(at);
  105.  
  106. }
  107.  
  108. @Override
  109. int radius() {
  110. return RADIUS;
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement