-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeasant.java
More file actions
129 lines (109 loc) · 4.33 KB
/
Peasant.java
File metadata and controls
129 lines (109 loc) · 4.33 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package concurent.student.first;
import java.util.concurrent.atomic.AtomicBoolean;
public class Peasant extends Unit {
private static final int HARVEST_WAIT_TIME = 100;
private static final int HARVEST_AMOUNT = 10;
private AtomicBoolean isHarvesting = new AtomicBoolean(false);
private AtomicBoolean isBuilding = new AtomicBoolean(false);
private Peasant(Base owner) {
super(owner, UnitType.PEASANT);
}
public static Peasant createPeasant(Base owner){
return new Peasant(owner);
}
/**
* Starts gathering gold.
*/
public void startMining(){
// TODO Set isHarvesting to true
// TODO Start harvesting on a new thread
// TODO Harvesting: Sleep for HARVEST_WAIT_TIME, then add the resource - HARVEST_AMOUNT
if(!isHarvesting.get() && !isBuilding.get()) { //////////////sync?
System.out.println("Peasant starting mining");
isHarvesting.set(true);
new Thread(() -> {
sleepForMsec(HARVEST_WAIT_TIME);
getOwner().getResources().addGold(HARVEST_AMOUNT);
}).start();
stopHarvesting();
}
}
/**
* Starts gathering wood.
*/
public void startCuttingWood(){
// TODO Set isHarvesting to true
// TODO Start harvesting on a new thread
// TODO Harvesting: Sleep for HARVEST_WAIT_TIME, then add the resource - HARVEST_AMOUNT
if(!isHarvesting.get() && !isBuilding.get()) {
System.out.println("Peasant starting cutting wood");
isHarvesting.set(true);
new Thread(() -> {
sleepForMsec(HARVEST_WAIT_TIME);
getOwner().getResources().addWood(HARVEST_AMOUNT);
}).start();
stopHarvesting();
}
}
/**
* Peasant should stop all harvesting once this is invoked
*/
public void stopHarvesting(){
this.isHarvesting.set(false);
}
/**
* Tries to build a certain type of building.
* Can only build if there are enough gold and wood for the building
* to be built.
*
* @param buildingType Type of the building
* @return true, if the building process has started
* false, if there are insufficient resources
*/
public boolean tryBuilding(UnitType buildingType){
// TODO Start building on a separate thread if there are enough resources
// TODO Use the Resources class' canBuild method to determine
// TODO Use the startBuilding method if the process can be started
if(!isHarvesting.get() && !isBuilding.get()) {
if(getOwner().getResources().canBuild(buildingType.goldCost, buildingType.woodCost)) {
startBuilding(buildingType);
return true;
}
}
return false;
}
/**
* Start building a certain type of building.
* Keep in mind that a peasant can only build one building at one time.
*
* @param buildingType Type of the building
*/
private void startBuilding(UnitType buildingType){
// TODO Ensure that only one building can be built at a time - use isBuilding atomic boolean
// TODO Building steps: Remove cost, build the building, wait the wait time
// TODO Use Building's createBuilding method to create the building
if(!isBuilding.get()) {
getOwner().getResources().removeCost(buildingType.goldCost, buildingType.woodCost);
Thread building = new Thread(()->{
isBuilding.set(true);
sleepForMsec(buildingType.buildTime);
isBuilding.set(false);
});
building.start();
try {
building.join();
} catch (InterruptedException e) {
}
getOwner().getBuildings().add(Building.createBuilding(buildingType, getOwner()));
}
}
/**
* Determines if a peasant is free or not.
* This means that the peasant is neither harvesting, nor building.
*
* @return Whether he is free
*/
public boolean isFree(){
return !isHarvesting.get() && !isBuilding.get();
}
}