-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResources.java
More file actions
84 lines (67 loc) · 2.47 KB
/
Resources.java
File metadata and controls
84 lines (67 loc) · 2.47 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
package concurent.student.first;
import java.util.concurrent.atomic.AtomicInteger;
public class Resources {
private static final int CAPACITY_LOWER_LIMIT = UnitType.PEASANT.foodCost * 5;
private AtomicInteger gold;
private AtomicInteger wood;
private AtomicInteger capacityLimit;
private AtomicInteger capacity;
public Resources(){
this.gold = new AtomicInteger(UnitType.PEASANT.goldCost * 5);
this.wood = new AtomicInteger(0);
this.capacityLimit = new AtomicInteger(CAPACITY_LOWER_LIMIT);
this.capacity = new AtomicInteger(0);
}
public int getGold(){
return gold.get();
}
public void addGold(int amount){
this.gold.set(this.gold.get() + amount);
}
public int getWood(){
return wood.get();
}
public void addWood(int amount){
this.wood.set(this.wood.get() + amount);
}
/**
* Determines if a building can be built or not based on the current resources.
*
* @param goldCost Gold cost of the building
* @param woodCost Wood cost of the building
* @return True, if there are enough resources to build it, false otherwise
*/
public boolean canBuild(int goldCost, int woodCost){
return gold.get() >= goldCost && wood.get() >= woodCost;
}
/**
* Determines if a unit can be trained based on the current resources.
*
* @param goldCost Gold cost of the unit
* @param woodCost Wood cost of the unit
* @param foodCost Food cost of the unit, uses the capacity resource
* @return True, if there are enough resources to train it, false otherwise
*/
public boolean canTrain(int goldCost, int woodCost, int foodCost){
return gold.get() >= goldCost && wood.get() >= woodCost && (capacity.get() + foodCost <= capacityLimit.get());
}
public void removeCost(int gold, int wood){
this.gold.set(this.gold.get() - gold);
this.wood.set(this.wood.get() - wood);
}
public int getCapacityLimit(){
return this.capacityLimit.get();
}
/**
* Building a farm increases the capacity limit by 10
*/
public void farmBuilt(){
this.capacityLimit.set(this.capacityLimit.get() + 10);
}
public int getCapacity(){
return this.capacity.get();
}
public void updateCapacity(int foodCost){
this.capacity.set(this.capacity.get() + foodCost);
}
}