/
client.js
291 lines (258 loc) · 7.46 KB
/
client.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
// Global object to hold all our globals... bad practice? whatever
FP = window.FP || {};
FP.SPRITE_DIRECTORY = "./sprites/";
FP.FONTSHEET_DIRECTORY = "./font/";
FP.AUDIO_DIRECTORY = "./audio/";
FP.SPEED = -0.75;
FP.BLOCK_SIZE = 32;
FP.PLATFORM_CENTER = -128;
FP.HIGHSCORE_COLOR = '#cace50';
FP.MAX_BLOCK_STREAK = 3; // most same-color blocks that can show up in a row
FP.tex = {}; // holds all textures
FP.font = {}; // hold all fontsheets
var Client = IgeClass.extend({
classId: 'Client',
init: function () {
// Engine setup
ige.globalSmoothing(true);
//ige.addComponent(IgeEditorComponent);
// Setup this
var self = this;
// Wait for our textures to load before continuing
ige.on('texturesLoaded', function () {
// Create the HTML canvas
ige.createFrontBuffer(true);
ige.start(function (success) {
if (success) {
ige.viewportDepth(true);
self.setupScene();
self.setupEntities();
}
});
});
this.load();
},
setupScene: function() {
var self = this;
this.mainScene = new IgeScene2d()
.id('mainScene');
// Create the main viewport
this.vpMain = new IgeViewport()
.id('vpMain')
.autoSize(true)
.scene(this.mainScene)
.drawBounds(false)
.drawBoundsData(false)
//.drawCompositeBounds(true)
.mount(ige)
;
this.bgScene = new IgeScene2d()
.id('bgScene')
.layer(1)
.translateTo(0, 0, 0)
.mount(this.mainScene)
;
this.gameScene = new IgeScene2d()
.id('gameScene')
.layer(2)
.translateTo(0, 0, 0)
.mount(this.mainScene)
;
this.fgScene = new IgeScene2d()
.id('fgScene')
.layer(3)
.translateTo(0, 0, 0)
.mount(this.mainScene)
;
this.vpMain._oldResizeEvent = this.vpMain._resizeEvent;
this.vpMain._resizeEvent = function(event) { // transplant additional code into existing resize handler
self.vpMain._oldResizeEvent.call(self.vpMain, event);
self._resizeEvent.call(self);
}
this.vpMain._resizeEvent();
},
setupEntities: function() {
var self = this;
this.bg = new IgeEntity()
.texture(FP.tex['bg'])
.dimensionsFromCell()
.mount(this.bgScene)
;
this.fg = new IgeEntity()
.texture(FP.tex['fg'])
.dimensionsFromCell()
.mount(this.fgScene)
;
this.successMessage = new IgeEntity()
.texture(FP.tex['puzzlesuccess'])
.dimensionsFromCell()
.opacity(0)
.translateTo(-88, -15, 0)
.mount(this.gameScene)
;
this.failMessage = new IgeEntity()
.texture(FP.tex['puzzlefail'])
.dimensionsFromCell()
.opacity(0)
.translateTo(-88, -15, 0)
.mount(this.gameScene)
;
this.platform = new Platform(FP.BLOCK_SIZE, this.successMessage, this.failMessage)
.translateTo(FP.PLATFORM_CENTER, 125, 0)
.mount(this.gameScene)
;
this.stream = new BlockStream(-320, FP.PLATFORM_CENTER, 352, FP.BLOCK_SIZE, FP.SPEED)
.translateTo(0, -73, 0)
.mount(this.gameScene)
;
this.currentLevel = 0;
this.currentScore = 0;
this.levelHigh = localStorage.getItem('levelHigh') || 0;
this.scoreHigh = localStorage.getItem('scoreHigh') || 0;
this.levelText = new IgeFontEntity()
.texture(FP.font['whitenumbers'])
.textAlignX(1)
.width(200)
.translateTo(258, 15, 0)
.text("00000")
.depth(10)
.mount(this.fgScene)
;
this.levelHighText = new IgeFontEntity()
.texture(FP.font['goldnumbers'])
.textAlignX(1)
.width(200)
.translateTo(258, 66, 0)
.text(this.padScoreText(this.levelHigh))
.depth(10)
.mount(this.fgScene)
;
this.scoreText = new IgeFontEntity()
.texture(FP.font['whitenumbers'])
.textAlignX(1)
.width(200)
.translateTo(258, 153, 0)
.text("00000")
.depth(10)
.mount(this.fgScene)
;
this.scoreHighText = new IgeFontEntity()
.texture(FP.font['goldnumbers'])
.textAlignX(1)
.width(200)
.translateTo(258, 204, 0)
.text(this.padScoreText(this.scoreHigh))
.depth(10)
.mount(this.fgScene)
;
this.volumeToggle = new IgeEntity()
.texture(FP.tex['soundon'])
.dimensionsFromCell()
.mouseDown(function() {
var muted = Howler._muted;
if (muted) {
Howler.unmute();
this.texture(FP.tex['soundon']);
} else {
Howler.mute();
this.texture(FP.tex['soundoff']);
}
ige.input.stopPropagation();
})
.translateTo(300, -210, 0)
.depth(10)
.mount(this.fgScene)
;
var canvas = document.getElementById('igeFrontBuffer');
var click = function(evt) {
if (evt.target !== canvas)
return;
self.successMessage.opacity(0);
self.failMessage.opacity(0);
if (self.platform.rowCount() >= 3) {
self.platform.clearRows();
if (self.failed) {
//reset current scores
self.currentLevel = 0;
self.currentScore = 0;
self.levelText
.text(self.padScoreText(self.currentLevel))
.texture(FP.font['whitenumbers'])
;
self.scoreText
.text(self.padScoreText(self.currentScore))
.texture(FP.font['whitenumbers'])
;
}
} else {
var clearedBlocks = self.stream.clearCenterBlocks();
if (clearedBlocks) {
FP.sfx.play('flap');
self.platform.addRow(clearedBlocks[0]._type, clearedBlocks[1]._type, clearedBlocks[2]._type);
if (self.platform.rowCount() === 3) {
var linePoints = self.platform.evaluateLines();
var totalPoints = linePoints.reduce(function(acc, val) { return acc + val; }, 0);
self.platform.setPointsText(linePoints);
if (totalPoints > 0) {
FP.sfx.play('good');
self.successMessage.opacity(1);
self.failed = false;
//add to scores
self.currentLevel += 1;
self.currentScore += totalPoints;
self.levelText.text(self.padScoreText(self.currentLevel));
if (self.currentLevel >= self.levelHigh)
self.levelText.texture(FP.font['goldnumbers']);
self.scoreText.text(self.padScoreText(self.currentScore));
if (self.currentScore >= self.scoreHigh)
self.scoreText.texture(FP.font['goldnumbers']);
} else {
FP.sfx.play('fail');
self.failMessage.opacity(1);
self.failed = true;
//save highscores
if (self.currentLevel > self.levelHigh) {
self.levelHigh = self.currentLevel;
localStorage.setItem('levelHigh', self.levelHigh);
self.levelHighText.text(self.padScoreText(self.levelHigh));
}
if (self.currentScore > self.scoreHigh) {
self.scoreHigh = self.currentScore;
localStorage.setItem('scoreHigh', self.scoreHigh);
self.scoreHighText.text(self.padScoreText(self.scoreHigh));
}
}
} else {
self.platform.setPointsText(null);
}
}
}
ige.input.stopPropagation();
}
this.gameScene.mouseDown(click);
},
_resizeEvent: function() {
if (this.vpMain.resizing)
return;
var windowWidth = window.innerWidth || document.documentElement.clientWidth || d.getElementsByTagName('body')[0].clientWidth;
if (windowWidth <= 740) {
this.vpMain.resizing = true;
this.vpMain.minimumVisibleArea(740, 700);
this.vpMain.resizing = false;
} else {
delete this.vpMain._lockDimension;
this.vpMain.scaleTo(1, 1, 1);
}
},
padScoreText: function(val) {
var length = 5;
var padChar = '0';
var string = String(val);
if (string.length >= length)
return string;
var pad = Array(length - string.length + 1).join(padChar); // http://stackoverflow.com/a/1877479/2089233
string = pad + string;
return string;
}
});
if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') { module.exports = Client; }