Created
November 29, 2013 01:48
-
-
Save danhp/5d6e77df22463359cac3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ai; | |
import mvc.Control; | |
import spaceracer.*; | |
public class StudentAIQuestion6 implements Control { | |
private final Radar myRadar; | |
private final Spaceship myShip; | |
private float desiredSpeed; | |
private float desiredYPos; | |
public StudentAIQuestion6(final Spaceship ship) { | |
myShip = ship; | |
myRadar = myShip.getRadar(); | |
} | |
public void moveShip() { | |
desiredSpeed = Constants.PLAYER_MAX_SPEED_X; | |
desiredYPos = myShip.getY(); | |
if (myRadar.isAsteroidInFront()){ | |
if (!OnboardComputer.getClosestYRange(myShip, myRadar.getAsteroids()).isEmpty()){ | |
desiredYPos = OnboardComputer.getClosestYRange(myShip, myRadar.getAsteroids()).getCenter(); | |
desiredSpeed = (myRadar.distanceTo(myRadar.getClosestAsteroidInFront())/Constants.RADAR_RANGE) | |
*(Constants.PLAYER_MAX_SPEED_X/3); | |
} | |
else{ | |
if(!myShip.isShieldFullyCharged()) | |
desiredSpeed = (myRadar.distanceTo(myRadar.getClosestAsteroidInFront())/Constants.RADAR_RANGE) | |
*(Constants.PLAYER_MAX_SPEED_X/3); | |
} | |
} | |
if (myShip.isShieldOn()) //for speed | |
desiredSpeed = Constants.PLAYER_MAX_SPEED_X; | |
if (Math.abs(myShip.getSpeedX() - desiredSpeed) >= 0.01){ | |
if (myShip.getSpeedX() < desiredSpeed) | |
myShip.accelerate(); | |
else | |
myShip.decelerate(); | |
} | |
if (Math.abs(myShip.getY() - desiredYPos) >= 0.01){ | |
if (myShip.getY() > desiredYPos) | |
myShip.moveDown(); | |
else | |
myShip.moveUp(); | |
} | |
if (myRadar.isCollisionImminent()) | |
myShip.activateShield(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment