-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
206 lines (189 loc) · 7.31 KB
/
main.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
"use strict";
let Transitions = {
fadeIn: "fadeIn",
running: "running",
fadeOut: "fadeOut",
showDialog: "showDialog",
hideDialog: "hideDialog",
preFadeIn: "preFadeIn", // a one-tick transition before fading in (likely after a fadeOut)
preRunning: "preRunning" // a one-tick transition before running (likely after a showDialog)
}
let game = {
// These objects are expected to be on the form (* are optional): { container, onTick*, onKeyDown*, onKeyUp*, onKeyPress*, onShow* }
menuObj: null,
aboutObj: null,
gameSetupObj: null,
gameObj: null,
currentState: "menuObj", // A reference to the *Obj letiables above (it is a reference so we can recreate the *Obj in a different resolution if needed)
nextState: null,
currentTransition: Transitions.preFadeIn,
fadingLayer: null
};
gameEngine.onInit = function onInit(stage, assets, context) {
let wepList = readEncodedFile(assets.getResult("wep", true));
let itemList = readEncodedFile(assets.getResult("item", true));
let typeList = readEncodedTypeFile(assets.getResult("type", true));
let configList = readConfigFile(assets.getResult("config", false));
createGameObjects(assets);
if (game.currentState) {
// <begin> FOR TESTING:
if (game.currentState === "gameObj") {
game.gameObj.setPlayerNames(["test1", "test2", "test3", "test4"]);
}
// <end> FOR TESTING
let currentObj = game[game.currentState];
stage.addChild(currentObj.container);
if (currentObj.onShow) {
currentObj.onShow();
}
}
// FadingLayer is just a black layer put on top of the stage content. The alpha channel then controls the fading in/out.
game.fadingLayer = new createjs.Shape();
game.fadingLayer.graphics.beginFill("black").drawRect(0, 0, GET_MAX_X, GET_MAX_Y).endFill();
game.fadingLayer.cache(0, 0, GET_MAX_X, GET_MAX_Y);
stage.addChild(game.fadingLayer);
function createGameObjects(assets) {
game.menuObj = createMenu(onSelectMainMenuItem, assets);
game.aboutObj = createAbout(onExitAbout, assets);
game.gameSetupObj = createGameSetup(onGameSetupDone, onExitGameSetup, assets);
game.gameObj = createGame(wepList, onExitGame, assets, context);
function onSelectMainMenuItem(menuItemIndex) {
switch (menuItemIndex) {
case 0: // Start Game
game.currentTransition = Transitions.fadeOut;
game.nextState = "gameSetupObj";
break;
case 1: // Options
game.currentTransition = Transitions.fadeOut;
// TODO: set game.nextState based on selected item
break;
case 2: // About
game.currentTransition = Transitions.showDialog;
game.nextState = "aboutObj";
break;
case 3: // Exit
game.currentTransition = Transitions.fadeOut;
// TODO: Figure out what to do when exiting
break;
}
}
function onExitAbout() {
game.currentTransition = Transitions.hideDialog;
game.nextState = "menuObj";
}
function onExitGameSetup() {
game.currentTransition = Transitions.fadeOut;
game.nextState = "menuObj";
}
function onGameSetupDone(playerNames) {
game.currentTransition = Transitions.fadeOut;
game.nextState = "gameObj";
game.gameObj.setPlayerNames(playerNames);
}
function onExitGame() {
game.currentTransition = Transitions.fadeOut;
game.nextState = "menuObj";
}
}
}
gameEngine.onTick = function onTick(stage, deltaInSeconds) {
let fadingDuration = 0.4;
switch(game.currentTransition) {
case Transitions.running:
if (game.currentState) {
let currentObj = game[game.currentState];
if (currentObj.onTick) {
currentObj.onTick(stage, deltaInSeconds);
}
}
break;
case Transitions.fadeIn:
let decrement = deltaInSeconds / fadingDuration;
if (game.fadingLayer.alpha - decrement > 0) {
game.fadingLayer.alpha -= decrement;
}
else {
game.fadingLayer.alpha = 0;
game.currentTransition = Transitions.running;
}
break;
case Transitions.fadeOut:
let increment = deltaInSeconds / fadingDuration;
if (game.fadingLayer.alpha + increment < 1) {
game.fadingLayer.alpha += increment;
}
else {
game.fadingLayer.alpha = 1;
if (changeToNextState(stage)) {
game.currentTransition = Transitions.preFadeIn;
}
else {
game.currentTransition = Transitions.fadeIn;
}
}
break;
case Transitions.showDialog:
case Transitions.hideDialog:
if (changeToNextState(stage)) {
game.currentTransition = Transitions.preRunning;
}
else {
game.currentTransition = Transitions.running;
}
break;
case Transitions.preFadeIn:
game.currentTransition = Transitions.fadeIn;
break;
case Transitions.preRunning:
game.currentTransition = Transitions.running;
break;
}
function changeToNextState(stage) {
let onShowCalled = false;
if (game.nextState) {
// remove the current container, unless we are showing a dialog
if (game.currentTransition !== Transitions.showDialog) {
let currentObj = game[game.currentState];
stage.removeChild(currentObj.container);
}
// add the new container, unless we are hiding a dialog
if (game.currentTransition !== Transitions.hideDialog) {
let nextObj = game[game.nextState];
let lastIndex = stage.numChildren - 1;
stage.addChildAt(nextObj.container, lastIndex); // adds the container "under" the fadingLayer
// trigger onShow(), if available
if (nextObj.onShow) {
nextObj.onShow();
onShowCalled = true;
}
}
game.currentState = game.nextState;
game.nextState = null;
}
return onShowCalled;
}
}
gameEngine.onKeyDown = function onKeyDown(stage, key) {
if (game.currentState) {
let currentObj = game[game.currentState];
if (currentObj.onKeyDown) {
currentObj.onKeyDown(stage, key);
}
}
}
gameEngine.onKeyUp = function onKeyUp(stage, key) {
if (game.currentState) {
let currentObj = game[game.currentState];
if (currentObj.onKeyUp) {
currentObj.onKeyUp(stage, key);
}
}
}
gameEngine.onKeyPress = function onKeyPress(stage, key) {
if (game.currentState) {
let currentObj = game[game.currentState];
if (currentObj.onKeyPress) {
currentObj.onKeyPress(stage, key);
}
}
}