-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasherShip.java
More file actions
45 lines (40 loc) · 1.6 KB
/
BasherShip.java
File metadata and controls
45 lines (40 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import oop.ex2.*;
/**
* This ship attempts to collide with other ships. It will always accelerate, and will
* constantly turn towards the closest ship. If it gets within a distance of 0.19 units (inclusive)
* from another ship, it will attempt to turn on its shields.
*/
public class BasherShip extends SpaceShip {
/**
* The distance from another ship that when it is closer than it, the ship wants to turn on it's shields.
*/
private final double distanceForShield = 0.19;
/**
* Does the actions of this ship for this round.
* This is called once per round by the SpaceWars game driver.
*
* @param game the game object to which this ship belongs.
*/
public void doAction(SpaceWars game) {
toAccel = true;
checkClosestShip(game);
super.doAction(game);
}
/**
* Checks what is the distance and angle from the closest ship, and changes the flags that stand for wish
* to turn on shield or to turn toward the closest ship according to that information.
*
* @param game the game in which the ship belongs, that in it the ship checks what it needs.
*/
private void checkClosestShip(SpaceWars game) {
SpaceShipPhysics otherShipsPhysics = game.getClosestShipTo(this).getPhysics();
double distance = physics.distanceFrom(otherShipsPhysics);
double angle = physics.angleTo(otherShipsPhysics);
if (distance <= distanceForShield)
toActivateShield = true;
if (angle < 0)
turnAngle = turnRight;
else if (angle > 0)
turnAngle = turnLeft;
}
}