-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.pde
272 lines (232 loc) · 8.66 KB
/
Controller.pde
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* The controller is the central logic for the game and stores its state. The Controller is responsible
/* for resolving and updating the game state, drawing the game as well as monitoring if the game is over.
*/
public class Controller {
boolean gameInPlay, endDay, buildMode, chooseWorkerServe;
PVector workerSpawn;
String displayMessage;
int winCondition = 0;
Player player;
Time time;
Inn inn;
Gold gold;
ArrayList<EnvironmentItem> items = new ArrayList<EnvironmentItem>();
ArrayList<Customer> customers = new ArrayList<Customer>();
ArrayList<Feeling> feelings = new ArrayList<Feeling>();
ArrayList<Worker> workers = new ArrayList<Worker>();
Boss nextBoss;
King king;
CollisionDetector collisionDetector = new CollisionDetector();
Cleaner cleaner = new Cleaner();
Spawner spawner = new Spawner();
Animator animator = new Animator();
Popularity popularity = new Popularity();
Build build = new Build();
public Controller () {
this.gold = new Gold();
}
// Start the game
public void start() {
this.endDay = true;
this.time = new Time();
this.inn = new Inn();
this.buildMode = true;
this.animator.setupBuildItems();
this.gold.addGold(100);
this.spawner.setDoorPos(this.inn.getDoorPos());
this.gameInPlay = true;
this.player = spawner.spawnPlayer();
}
public void addInnGold(int amount) {
this.gold.addGold(amount);
}
// End the day
public void dayEnd() {
this.endDay = true;
this.buildMode = true;
if(this.popularity.kingReady()) {
this.spawnKing();
}
}
// Go through the required routines at the start of the day, such as calculating ustomers
// And checking whether a new boss is to be dealt with.
public void startDay() {
if(this.endDay) {
this.time.newDay();
this.calculateCustomers();
for(Customer customer: this.customers) {
customer.leave();
}
this.customers = new ArrayList<Customer>();
if(this.nextBoss != null) {
this.customers.add(this.nextBoss);
for(Customer customer: this.nextBoss.entourage) {
this.customers.add(customer);
}
this.nextBoss = null;
} else if(this.king != null) {
this.customers.add(this.king);
for(Customer customer: this.king.entourage) {
this.customers.add(customer);
}
this.king = null;
} else {
this.customers.add(spawner.spawnCustomer());
}
this.endDay = false;
this.buildMode = false;
}
}
// Establish what a server should be serving
public void chooseWorkerServe(float x, float y) {
this.workerSpawn = new PVector(x, y);
this.chooseWorkerServe = true;
}
// Establish what a server should be serving
public void workerServer(ItemType item) {
if(this.gold.buyItem(500) && this.build.unlocked) {
this.workers.add(this.spawner.spawnWorker(item, this.workerSpawn.x, this.workerSpawn.y));
}
}
// Check that an item is being placed in a valid location
public boolean checkPlacementLocation(Shape shape) {
for(EnvironmentItem item : this.items) {
if(collisionDetector.checkCollision(item.getShape(), shape))
return false;
}
for(Wall wall : this.inn.getWalls()) {
if(collisionDetector.checkCollision(wall.getShape(), shape))
return false;
}
if(collisionDetector.checkCollision(this.inn.getDoor().getShape(), shape))
return false;
return true;
}
/**
* Calculates the customers that have to attend the inn that day, based on popularity of faction.
*/
private void calculateCustomers() {
this.spawner.setKnightSpawn(this.popularity.calculateSpawn(Faction.KNIGHT));
this.spawner.setWizardSpawn(this.popularity.calculateSpawn(Faction.WIZARD));
this.spawner.setElfSpawn(this.popularity.calculateSpawn(Faction.ELF));
this.spawner.setZombieSpawn(this.popularity.calculateSpawn(Faction.ZOMBIE));
println("Customers: "+this.spawner.getCustomersInDay());
this.time.setSpawnTimer(960/this.spawner.getCustomersInDay());
}
// Spawn the faction leader of a given faction
public void spawnBoss(Faction faction) {
this.nextBoss = spawner.spawnBoss(faction);
}
// Spawn the king
public void spawnKing() {
this.king = spawner.spawnKing();
}
// Handle key presses, either moving the player or moving the build swuare
public void movePlayer(float x, float y, Facing direction) {
PVector change = new PVector(x,y);
if(buildMode) {
if(!this.chooseWorkerServe) {
build.moveBuildSquare(direction);
}
} else {
if(checkMove(this.player.getPos(), change)) {
this.player.move(change);
this.player.setFacing(direction);
}
}
}
// Checka move is valid
public boolean checkMove(PVector currentPos, PVector change) {
PVector nextPos = currentPos.add(change);
if(inn.wallCollision(nextPos.copy()))
return false;
return true;
}
// End the game and establish whether or not the character has won before displaying the end of game scren.
public void endGame(int win) {
this.winCondition = win;
this.endDay = false;
this.gameInPlay = false;
if(win == -1) {
this.displayMessage = "You did not satisfy his majesty. Game over.";
} else {
this.displayMessage = "Huzzah! You have gained the glory of the crown,\nand are to become the royal innkeep";
}
this.displayMessage += "\nTotal Gold Made: " + this.gold.accumulated;
}
// Draw the gamestate
public void drawGame() {
if(this.winCondition != 0) {
this.animator.drawEndScreen();
} else if(this.endDay) {
this.animator.endDay(this);
} else if(this.gameInPlay) {
this.animator.drawActiveGame(this);
this.cleaner.cleanGame();
this.collisionDetector.checkCollisions();
}
}
// Check whether an item should be picked up
public EnvironmentItem findItem(Shape shape) {
for(EnvironmentItem item : items) {
if(this.collisionDetector.checkCollision(item.getShape(), shape)) {
return item;
}
}
return null;
}
// Use an item and apply it to all characters within the shape
public void useItem(EnvironmentItem item, Shape shape) {
for(Customer customer : this.customers) {
if(this.collisionDetector.checkCollision(customer.getShape(), shape)) {
customer.useItem(item);
}
}
}
// Handle an item key press, either building, using or establishing the serving of items.
public void itemKeyPress(int itemKey) {
if(buildMode) {
if(this.chooseWorkerServe) {
boolean selected = false;
System.out.println("Switching");
switch (itemKey) {
case 1 :
this.workerServer(ItemType.BEER);
selected = true;
break;
case 2 :
this.workerServer(ItemType.CHICKENLEG);
selected = true;
break;
case 3 :
this.workerServer(ItemType.CHALICE);
selected = true;
break;
case 4:
this.workerServer(ItemType.CHEESE);
selected = true;
break;
}
if(selected)
this.chooseWorkerServe = false;
} else {
EnvironmentItem item = build.placeItem(itemKey);
if(item!= null)
this.items.add(item);
}
} else {
player.useItem(itemKey);
}
}
// Add a feeling to the game state
public void addFeeling(Feeling feeling) {
this.feelings.add(feeling);
}
// Create a new customer
public void newCustomer() {
Customer customer = spawner.spawnCustomer();
if(customer != null)
this.customers.add(customer);
}
}