-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocket.java
More file actions
executable file
·104 lines (84 loc) · 3.34 KB
/
Rocket.java
File metadata and controls
executable file
·104 lines (84 loc) · 3.34 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
98
99
100
101
102
103
104
import bc.*;
import java.util.*;
public class Rocket extends UnitInstance {
private boolean isBuilt;
private boolean inFlight;
public Rocket(int id, boolean isBuilt) {
super(id);
this.isBuilt = isBuilt;
this.inFlight = false;
if (isBuilt) {
Earth.createGlobalTask(Command.LOAD_ROCKET, this.getLocation());
}
}
public boolean isInFlight() {
return inFlight;
}
@Override
public void run() {
if (isBuilt) {
if (!inFlight && this.getLocation().getPlanet() == Planet.Earth && Player.gc.unit(this.getId()).structureGarrison().size() == 8) {
// TODO: Don't forget about changing the location
MapLocation locationToLand = Player.getRandomLocationToLandOnMars();
System.out.println("Rocket: " + this.getId() + " Trying to launch");
if (Player.gc.canLaunchRocket(this.getId(), locationToLand)) {
Player.gc.launchRocket(this.getId(), locationToLand);
System.out.println("Rocket: " + this.getId() + " launched!");
Earth.structureLocations.remove(Player.locationToString(this.getLocation()));
inFlight = true;
}
} else if (this.getLocation().getPlanet() == Planet.Mars) {
unload();
}
}
}
/**
* Method will unload all the units it can when the rocket reaches mars.
*/
public void unload() {
for (int i = 0; i < 8; i++) {
Direction direction = Direction.swigToEnum(i);
if (Player.gc.canUnload(this.getId(), direction)) {
Player.gc.unload(this.getId(), direction);
MapLocation unloadLocation = this.getLocation().add(direction);
int unitId = Player.gc.senseUnitAtLocation(unloadLocation).id();
UnitType unitType = Player.gc.unit(unitId).unitType();
UnitInstance unitInstance;
switch (unitType) {
case Knight:
unitInstance = new Knight(unitId);
break;
case Ranger:
unitInstance = new Ranger(unitId);
break;
case Healer:
unitInstance = new Healer(unitId);
break;
case Mage:
unitInstance = new Mage(unitId);
break;
default:
unitInstance = new Worker(unitId);
break;
}
if (unitType == UnitType.Worker) {
Mars.marsWorkerMap.put(unitId, unitInstance);
} else {
Mars.marsAttackerMap.put(unitId, unitInstance);
}
}
}
}
/**
* Every round this method is called to try to load units that were previously unable to be loaded
*/
public boolean loadUnit(int unitId) {
if (Player.gc.canLoad(this.getId(), unitId)) {
Player.gc.load(this.getId(), unitId);
Earth.earthGarrisonedUnits.add(unitId);
System.out.println("Rocket: " + this.getId() + " loaded unit " + unitId);
return true;
}
return false;
}
}