Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game2;
- import utilities.Action;
- import utilities.Vector2D;
- import java.awt.*;
- import java.awt.geom.AffineTransform;
- import static utilities.Constants.*;
- public class Ship extends GameObject{
- public static final int RADIUS = 8;
- // rotation velocity in radians per second
- public static final double STEER_RATE = 2* Math.PI;
- // acceleration when thrust is applied
- public static final double MAG_ACC = 200;
- // constant speed loss factor
- public static final double DRAG = 0.99;
- public static final Color COLOR = Color.cyan;
- public Vector2D position; // on frame
- public Vector2D velocity; // per second
- // direction in which the nose of the ship is pointing
- // this will be the direction in which thrust is applied
- // it is a unit vector representing the angle by which the ship has rotated
- public Vector2D direction;
- //coordinates for ship
- static final int[] XP = {1,1,0,-1,-1,-3,-3,-1,-2,2,1,3,3};
- static final int[] YP = {0,-2,-4,-2,0,1,2,2,3,3,2,2,1};
- //coordinates for thruster
- int[] XPTHRUST2 = {-1,0,1};
- int[] YPTHRUST2 = {3,5,3};
- int[] XPTHRUST = {-2,0,2};
- int[] YPTHRUST = {3,6,3};
- static boolean thrusting = false;
- public Bullet bullet = null;
- long time;
- static int lives;
- int shootdelay, bulletlife;
- // controller which provides an Action object in each frame
- private Controller ctrl;
- public Ship(Controller ctrl) {
- lives = 4;
- shootdelay = 400;
- bulletlife = 30;
- this.ctrl = ctrl;
- position = new Vector2D(FRAME_WIDTH/2, FRAME_HEIGHT/2);
- velocity = new Vector2D();
- direction = new Vector2D(0, -1);
- }
- public void update() {
- Action action = ctrl.action();
- direction.rotate(action.turn*STEER_RATE*DT);
- velocity.addScaled(direction, MAG_ACC * DT * action.thrust);
- velocity.mult(DRAG);
- position.addScaled(velocity, DT);
- position.wrap(FRAME_WIDTH, FRAME_HEIGHT);
- if (action.thrust != 0) thrusting = true;
- else thrusting = false;
- if(action.shoot){
- if(System.currentTimeMillis()-time > shootdelay){
- time = System.currentTimeMillis();
- mkBullet();
- action.shoot = false;
- }
- }
- }
- private void mkBullet(){
- Bullet bullet = new Bullet(this, bulletlife);
- this.bullet = bullet;
- }
- public void draw(Graphics2D g){
- AffineTransform at = g.getTransform();
- g.translate(position.x, position.y);
- double rot = direction.angle() + Math.PI / 2;
- g.rotate(rot);
- g.scale(5, 5);
- g.setColor(COLOR);
- g.fillPolygon(XP, YP, XP.length);
- if (thrusting) {
- g.setColor(Color.yellow);
- g.fillPolygon(XPTHRUST, YPTHRUST, XPTHRUST.length);
- g.setColor(Color.red);
- g.fillPolygon(XPTHRUST2, YPTHRUST2, XPTHRUST2.length);
- }
- g.setTransform(at);
- }
- @Override
- int radius() {
- return RADIUS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement