-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitInstance.java
More file actions
executable file
·97 lines (78 loc) · 2.69 KB
/
UnitInstance.java
File metadata and controls
executable file
·97 lines (78 loc) · 2.69 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import bc.*;
import java.util.LinkedList;
import java.util.Queue;
/**
* The top level class of all kinds of units. Each unit has an ID and a task queue. All units have a run method
* to execute their code
*/
public abstract class UnitInstance {
private int id;
private Queue<RobotTask> taskQueue;
private UnitType unitType;
private RobotTask emergencyTask;
public UnitInstance(int id) {
this.id = id;
taskQueue = new LinkedList<>();
unitType = Player.gc.unit(id).unitType();
emergencyTask = null;
}
/**
* Each structure will need to know how to run specific commands. Rockets, Factories, and Blueprints
* all run differently
*/
public abstract void run();
public int getId() {
return id;
}
public int getVisionRange() {
return (int)(Player.gc.unit(this.getId()).visionRange());
}
public boolean hasTasks() {
return (!taskQueue.isEmpty());
}
public MapLocation getLocation() {
return Player.gc.unit(this.getId()).location().mapLocation();
}
public UnitType getUnitType() {
return unitType;
}
public RobotTask getCurrentTask() {
return taskQueue.peek();
}
public void addTaskToQueue(RobotTask task) {
taskQueue.add(task);
}
public void pollCurrentTask() {
taskQueue.poll();
}
public RobotTask getEmergencyTask() {
return emergencyTask;
}
public void setEmergencyTask(RobotTask emergencyTask) {
this.emergencyTask = emergencyTask;
}
/**
* Gets all the enemy units in the range of this unit instance with the given range
* @return A vecUnit of all the enemy units in range
*/
public VecUnit getEnemyUnitsInRange() {
Team otherTeam = Player.gc.team() == Team.Blue ? Team.Red : Team.Blue;
return Player.gc.senseNearbyUnitsByTeam(this.getLocation(), this.getVisionRange(), otherTeam);
}
/**
* Helper method that will get the id of the closest enemy unit
* @param enemyUnits The list of all enemy units in vision range
* @return The id of the closest enemy unit
*/
public Unit getClosestEnemy(VecUnit enemyUnits) {
Unit closestEnemy = enemyUnits.get(0);
int closestDistance = (int)(this.getLocation().distanceSquaredTo(enemyUnits.get(0).location().mapLocation()));
for (int i = 0; i < enemyUnits.size(); i++) {
MapLocation enemyUnitLocation = enemyUnits.get(i).location().mapLocation();
if (this.getLocation().distanceSquaredTo(enemyUnitLocation) < closestDistance) {
closestEnemy = enemyUnits.get(i);
}
}
return closestEnemy;
}
}