-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tutorial.js
341 lines (274 loc) · 8.96 KB
/
Tutorial.js
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*global MainGame*/
/*global Event*/
/*global Global*/
// singleton
// stores the Tut on-going data
var Tut = {
hiringMinisterIsOpen: false,
dossierOpen: false,
contractOpen: false,
statsBinderIsOpen: false, // Is the binder that holds global stats / money stats open?
};
var Tutorial = {
initialized: false,
tuts: [],
activeTut: null,
activeIndex: 0,
timer: null, // Unlike checkTimer, this timer is used to record how long the player spent in each tutorial sequence
// generate the tutorials
generate: function() {
// if there is an active, finish it without triggering anything;
if (this.activeTut !== null) {
this.check(this.activeTut);
return;
}
// lazy init
if (!this.initialized) {
Tut.numHouses = MainGame.board.findBuilding(null, null, 'housing', null).length;
this.tuts = MainGame.game.cache.getJSON('Tutorial');
console.assert(this.tuts.length);
//console.log("Parsed Tutorials, total: ", this.tuts.length);
this.checkTimer = MainGame.game.time.create(false);
this.checkTimer.loop(50, this.loopingCheck, this);
this.checkTimer.start();
this.timer = MainGame.game.time.create(false);
this.initialized = true;
}
var tut = this.tuts[this.activeIndex];
// tuts -> runningTuts
this.activeTut = tut;
// now let's execute the tut
// init
this._executeInit_(tut.init);
// run tutorial
this.runEvent(tut.event, tut.handler);
},
_checkCond_: function(condString) {
var fn = Function("return " + condString);
return fn();
},
_executeInit_: function(init) {
// Run the active tut's initializer
Function(init)();
// Send a telemetry payload
Telemetry.send({
type: 'tutorial_phase_started',
phaseName: Tutorial.activeTut.name
});
// Start the timer
this.timer.start();
},
loopingCheck: function() {
if (this.activeTut && this._checkCond_(this.activeTut.cond)) {
// Send a telemetry payload
Telemetry.send({
type: 'tutorial_phase_completed',
phaseName: Tutorial.activeTut.name,
phaseTime: this.timer.seconds
});
// Stop the timer (also resets elapsed time)
this.timer.stop();
// Update active tutorial
this.activeIndex++;
this.activeTut = this.tuts[this.activeIndex];
// init
this._executeInit_(this.activeTut.init);
// run tutorial
this.runEvent(this.activeTut.event, this.activeTut.handler);
}
},
runEvent: function(event, handler) {
console.assert(event.length === handler.length);
if (event.length === 0) return;
var e = Event.createNew();
// generate model & controller
var model = []; // an array of tables
var controller = []; // an array of arrays of functions
var fn = []; // a temp array to store arrays of functions
for (var i = 0; i < event.length; i++) {
model.push({
description:event[i].description,
buttonTexts:event[i].buttonTexts
});
// test function
controller[i] = [];
for (var j = 0; j < handler[i].length; j++) {
var handlerArray = handler[i];
let f = Function('e', handlerArray[j]);
controller[i][j] = function() { f(e); };
}
}
// now set model & controller
e.setModel(model);
e.setController(controller);
},
reminder: function(name) {
var quests = this.runningQuests.filter(function(qu) { return qu.name == name; });
console.assert(quests.length === 1);
var q = quests[0];
//console.log("So selected tut is: ", q);
// create reminder view
var view = DReminderView.createNew();
view.setModel(q.reminder.model);
view.setController(101, q.reminder.controller);
view.hide();
// create reminder button
var button = DReminderButton.createNew();
button.setReminderView(view);
// now update the start turn for view
view.setModel({startAt:q.startAt});
// let q store reminderButton
q.reminderButton = button;
},
// check every tutorial to see if it is complete
check: function(tut) {
//console.log("Checking running tut", tut);
var r = tut.reminder;
var shouldRemove = false;
// check completeness
var f = Function("return " + r.controller.check);
//console.log("f is:", f.toString());
if (f()) {
//tut.reminderButton.reminderView.suicide(); TODO: get this working
//tut.reminderButton.suicide();
// this.activeTut = null;
// Tutorial.generate();
}
},
//// Special Functions
setPointerOnBuild: function(bool) {
if (bool) {
Tut.buildButtonPointer = UIPointer.createNew(72, MainGame.game.height-80, UIPointer.DOWN, -1, null, true);
} else if (Tut.buildButtonPointer) {
Tut.buildButtonPointer.stop();
}
},
setPointerOnBuyRoad: function(bool) {
if (bool) {
Tut.buyRoadPointer = UIPointer.createNew(1020, 175, UIPointer.DOWN, -1, null, true);
} else if (Tut.buyRoadPointer) {
Tut.buyRoadPointer.stop();
}
},
setPointerOnRoadPlacement: function(bool) {
if (bool) {
Tut.roadPlacementPointer = UIPointer.createNew(128, 60, UIPointer.DOWN, -1, null, true);
MainGame.board.at(158).addChild(Tut.roadPlacementPointer);
} else if (Tut.roadPlacementPointer) {
Tut.roadPlacementPointer.stop();
}
},
setPointerOnSchool: function(bool) {
if (bool) {
Tut.schoolPointer = UIPointer.createNew(128, 20, UIPointer.DOWN, -1, null, true);
MainGame.board.at(128).addChild(Tut.schoolPointer);
} else if (Tut.schoolPointer) {
Tut.schoolPointer.stop();
}
},
setPointerOnFactory: function(bool) {
if (bool) {
Tut.factoryPointer = UIPointer.createNew(128, 20, UIPointer.DOWN, -1, null, true);
MainGame.board.at(159).addChild(Tut.factoryPointer);
} else if (Tut.factoryPointer) {
Tut.factoryPointer.stop();
}
},
setPointerOnResidences: function(bool) {
if (bool) {
Tut.buySuburbPointer = UIPointer.createNew(725, 175, UIPointer.DOWN, -1, null, true);
} else if (Tut.buySuburbPointer) {
Tut.buySuburbPointer.stop();
}
},
setPointerOnMinistryPanel: function(bool) {
if (bool) {
Tut.ministryPanelPointer = UIPointer.createNew(175, 475, UIPointer.LEFT, -1, null, true);
} else if (Tut.ministryPanelPointer) {
Tut.ministryPanelPointer.stop();
}
},
setPointerOnStatsPanel: function(bool) {
if (bool) {
Tut.statsPanelPointer = UIPointer.createNew(MainGame.game.width - 175, 475, UIPointer.RIGHT, -1, null, true);
} else if (Tut.statsPanelPointer) {
Tut.statsPanelPointer.stop();
}
},
showFunPanel: function() {
UIPointer.createNew(MainGame.game.width/2, 64, UIPointer.UP, 2000, function() { Tut.shownFunPanel = true; }, true);
},
showMoneyPanel: function() {
UIPointer.createNew(144, 64, UIPointer.UP, 2000, function() { Tut.readAboutMoney = true; }, true);
},
showStatsPanel: function() {
UIPointer.createNew(MainGame.game.width - 175, 475, UIPointer.RIGHT, 2000, function() { Tut.sawStatsPanel = true; }, true);
},
//// Sequence checks!
// Checks to see if the build menu has been opened
hasOpenedBuildMenu: function() {
return MainGame.global.buildMenuOpened;
},
boughtRoad: function() {
return Tutorial.activeTut.name === 'Roads' && MainGame.global.boughtRoad;
},
// Checks to see whether the player has built a road between any home and any lumberyard
roadsBuilt: function() {
var homes = MainGame.board.findBuilding(null, null, 'housing', null);
var lumberyards = MainGame.board.findBuilding(null, 'Commerce', null, null);
for (var i = 0; i < homes.length; i++) {
for (var k = 0; k < lumberyards.length; k++) {
if (MainGame.board.hasRoadConnect(homes[i], lumberyards[k])) {
return true;
}
}
}
return false;
},
openedSchoolBuildingDetail: function() {
//console.log('openSchoolBuildingDetail');
if (!MenuController.menuOpen) return false;
if (MenuController.leftMenusOpen.length !== 1) return false;
//console.log(MenuController);
if (MenuController.leftMenusOpen[0].bdIndex === 128) {
return true;
} else return false;
},
openedGreenTab: function() {
if (!MenuController.menuOpen) return false;
if (MenuController.leftMenusOpen[0].bdIndex === 128 && MenuController.leftMenusOpen[0].activeTab === 1) {
return true;
} else return false;
},
// Checks to see whether the player has fired the teacher
firedTeacher: function() {
var school = MainGame.board.at(128).getBuilding();
return school.people === 0;
},
// Checks to see whether the player has put staff into their factory
lumberYardHasWorkers: function() {
var lumberyards = MainGame.board.findBuilding(null, 'Commerce', null, null);
for (var i = 0; i < lumberyards.length; i++) {
var lumberyard = MainGame.board.at(lumberyards[i]).building;
if (lumberyard.people > 0) return true;
}
return false;
},
// Checks to see if the build menu has been opened
buildMenuIsOpen: function() {
return MainGame.global.buildMenuIsOpen;
},
builtNewHouse: function() {
var numHouses = MainGame.board.findBuilding(null, null, 'housing', null).length;
return Tut.numHouses < numHouses;
},
contractIsOpen: function() {
return MainGame.global.contractIsOpen;
},
hasMinister: function() {
return MainGame.population.highList().length > 0;
},
// menusIsOpen: function(string) {
// console.log(MenuController.getCurMenu());
// },
};