-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductionUnit.java
129 lines (113 loc) · 3.37 KB
/
ProductionUnit.java
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
/*******************************************************************************
* Copyright (C) 2019-2020 ROMAINPC
* Copyright (C) 2019-2020 Picachoc
*
* This file is part of PICROM-Wars
*
* PICROM-Wars is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PICROM-Wars is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package picrom.entity.castle;
import picrom.owner.Neutral;
import picrom.utils.Settings;
/**
* ProductionUnits are capable to produce Producible objects.
*
* This class also compute time remaining to produce.
*
* @see picrom.entity.castle.Producible
* @see picrom.entity.castle.Castle
* @see picrom.entity.unit.Unit
*/
public class ProductionUnit {
private Castle castle;
private Producible currentProduction;
private boolean produced;
private int timeLeft;
/**
* Constructor.
*
* @param castle Castle which contain the ProductionUnit.
*/
public ProductionUnit(Castle castle) {
this.castle = castle;
produced = true;
timeLeft = 0;
}
/**
* Decrease by 1 the number of turns needed to produce, also produce the
* Producible if money and time are enough. Current production become null when
* the production is finish.
*/
public void update() {
if (!produced) {
timeLeft--;
if (timeLeft <= 0 && castle.getTreasure() >= currentProduction.getProductionCost()) {
castle.setTreasure(castle.getTreasure() - currentProduction.getProductionCost()); // Apply cost
currentProduction.produce(castle);
produced = true;
}
}
}
/**
* Define the Producible to produce.
*
* @param production Producible already instantiated
*/
public void setProduction(Producible production) {
currentProduction = production;
produced = false;
if (production != null) {
int multiplier = castle.getOwner() instanceof Neutral ? Settings.NEUTRAL_PRODUCTION_MULTIPLIER : 1;
timeLeft = production.getProductionTime() * multiplier;
}
}
/**
* @return Producible currently in production.
*/
public Producible getProduction() {
return currentProduction;
}
/**
* @return true if the production is done.
*/
public boolean isProduced() {
return produced;
}
/**
* interrupt production
*/
public void stop() {
produced = true;
timeLeft = 0;
}
/**
* Get the time remaining to finish production.
*
* @return Number of updates of this production unit needed to finish production
* (not include money cost). Could be negativ (waiting for money).
*/
public int getTimeLeft() {
return timeLeft;
}
/**
* @return Money needed to do production.
*/
public int getCost() {
return currentProduction == null ? 0 : currentProduction.getProductionCost();
}
@Override
public String toString() {
return "ProductionUnit: " + (produced ? "no production" : "Production(" + timeLeft + "): " + currentProduction);
}
}