public class Ship { float x, y, xv, yv, bodyRotation; boolean engineBurning; private int currNoMissiles; private Vector missiles; private float creationTime; private boolean isInvincible; private int shipColor = 240; private final int w=10; private final int h=10; private final int r=w/2; private final int noOfMissiles = 4; // was 8 private final float maxSpeed = 7.5; // was 10.0 private final float burnRate = 0.5; // ship acceleration private final float rotationRate = PI/16; private final float initialMissileBoost = 5; // was 3 private final float invincibleTime = 2000; public Ship(AsteroidHandler roidHandler) { x = width/2; y = height/2; creationTime = millis(); isInvincible = true; // randomize new start position if within an asteroid while(roidHandler.whichRoid(x, y, w*3) != null) { x = random(50, width - 50); y = random(50, height - 50); } // randomize start velocities xv = random(-1,-1); yv = random(-1,-1); bodyRotation = PI/2; engineBurning = false; missiles = new Vector(0); } public void move() { float dx = mouseX - x; float dy = mouseY - y; // for the two first (in neg y coord syst) quadrants: if(dy>=0) { bodyRotation = acos( dx/ (sqrt(dx*dx+dy*dy)) ); } else { bodyRotation = PI + acos( -dx/ (sqrt(dx*dx+dy*dy)) ); } if(engineBurning) { //xv += dx/inverseBurnRate; xv += burnRate * cos(bodyRotation); if(xv>=maxSpeed) xv = maxSpeed; //yv += dy/inverseBurnRate; yv += burnRate * sin(bodyRotation); if(yv>=maxSpeed) yv = maxSpeed; } // Bounce against walls if(x+r>=width-1) { x=width-r-2; xv *= -1; //reverse x-velocity } if(x-r<=0) { x=2+r; xv *= -1; } if(y+r>=height-1) { y = height-r-2; yv *= -1; //reverse y-velocity } if(y-r<=0) { y=2+r; yv *= -1; } // calculate new position x += xv; y += yv; } public void render() { // The Ship if(isInvincible) { // darker color for invincible ship shipColor = 120; // check if invincibility time is passed if(millis() - this.creationTime > invincibleTime) { isInvincible = false; shipColor = 240; } } fill(shipColor, 0, 0); noStroke(); rect((int) x, (int) y, w, h); stroke(255); // render "crosshair" point point((x+w*cos(bodyRotation)), (y+w*sin(bodyRotation))); // The Missiles Missile tmpMissile; for(int i=0; i