forked from bhavenjain/thedetectorsprojects.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslot.js
More file actions
349 lines (308 loc) · 8.81 KB
/
Copy pathslot.js
File metadata and controls
349 lines (308 loc) · 8.81 KB
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
342
343
344
345
346
347
348
349
var IMAGE_HEIGHT = 64;
var IMAGE_TOP_MARGIN = 5;
var IMAGE_BOTTOM_MARGIN = 5;
var SLOT_SEPARATOR_HEIGHT = 2;
var SLOT_HEIGHT = IMAGE_HEIGHT + IMAGE_TOP_MARGIN + IMAGE_BOTTOM_MARGIN + SLOT_SEPARATOR_HEIGHT; // how many pixels one slot image takes
var RUNTIME = 3000; // how long all slots spin before starting countdown
var SPINTIME = 1000; // how long each slot spins at minimum
var ITEM_COUNT = 6; // item count in slots
var SLOT_SPEED = 15; // how many pixels per second slots roll
var DRAW_OFFSET = 45; // how much draw offset in slot display from top
var BLURB_TBL = [ 'No win!', 'Good!', 'Excellent!', 'JACKPOT!' ];
function shuffleArray(array) {
for (i = array.length - 1; i > 0; i--) {
var j = parseInt(Math.random() * i);
var tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
// Images must be preloaded before they are used to draw into canvas
function preloadImages(images, callback) {
function _preload(asset) {
asset.img = new Image();
asset.img.src = 'img/' + asset.id + '.png';
asset.img.addEventListener(
'load',
function() {
_check();
},
false
);
asset.img.addEventListener(
'error',
function(err) {
_check(err, asset.id);
},
false
);
}
var loadc = 0;
function _check(err, id) {
if (err) {
alert('Failed to load ' + id);
}
loadc++;
if (images.length == loadc) return callback();
}
images.forEach(function(asset) {
_preload(asset);
});
}
function copyArray(array) {
var copy = [];
for (var i = 0; i < array.length; i++) {
copy.push(array[i]);
}
return copy;
}
function SlotGame() {
var game = new Game();
var items = [
{ id: 'energy-64' },
{ id: 'staff-64' },
{ id: 'cash-64' },
{ id: 'build-64' },
{ id: 'goods-64' },
{ id: 'gold-64' }
];
$('canvas').attr('height', IMAGE_HEIGHT * ITEM_COUNT * 2);
$('canvas').css('height', IMAGE_HEIGHT * ITEM_COUNT * 2);
game.items = items;
// load assets and predraw the reel canvases
preloadImages(items, function() {
// images are preloaded
// draws canvas strip
function _fill_canvas(canvas, items) {
ctx = canvas.getContext('2d');
ctx.fillStyle = '#ddd';
for (var i = 0; i < ITEM_COUNT; i++) {
var asset = items[i];
ctx.save();
ctx.shadowColor = 'rgba(0,0,0,0.5)';
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.shadowBlur = 5;
ctx.drawImage(asset.img, 3, i * SLOT_HEIGHT + IMAGE_TOP_MARGIN);
ctx.drawImage(asset.img, 3, (i + ITEM_COUNT) * SLOT_HEIGHT + IMAGE_TOP_MARGIN);
ctx.restore();
ctx.fillRect(0, i * SLOT_HEIGHT, 70, SLOT_SEPARATOR_HEIGHT);
ctx.fillRect(0, (i + ITEM_COUNT) * SLOT_HEIGHT, 70, SLOT_SEPARATOR_HEIGHT);
}
}
// Draw the canvases with shuffled arrays
game.items1 = copyArray(items);
shuffleArray(game.items1);
_fill_canvas(game.c1[0], game.items1);
game.items2 = copyArray(items);
shuffleArray(game.items2);
_fill_canvas(game.c2[0], game.items2);
game.items3 = copyArray(items);
shuffleArray(game.items3);
_fill_canvas(game.c3[0], game.items3);
game.resetOffset = (ITEM_COUNT + 3) * SLOT_HEIGHT;
game.loop();
});
$('#play').click(function(e) {
// start game on play button click
$('h1').text('Rolling!');
game.restart();
});
// Show reels for debugging
var toggleReels = 1;
$('#debug').click(function() {
toggleReels = 1 - toggleReels;
if (toggleReels) {
$('#reels').css('overflow', 'hidden');
} else {
$('#reels').css('overflow', 'visible');
}
});
}
function Game() {
// reel canvases
this.c1 = $('#canvas1');
this.c2 = $('#canvas2');
this.c3 = $('#canvas3');
// set random canvas offsets
this.offset1 = -parseInt(Math.random() * ITEM_COUNT) * SLOT_HEIGHT;
this.offset2 = -parseInt(Math.random() * ITEM_COUNT) * SLOT_HEIGHT;
this.offset3 = -parseInt(Math.random() * ITEM_COUNT) * SLOT_HEIGHT;
this.speed1 = this.speed2 = this.speed3 = 0;
this.lastUpdate = new Date();
// Needed for CSS translates
this.vendor = /webkit/i.test(navigator.appVersion)
? '-webkit'
: /firefox/i.test(navigator.userAgent)
? '-moz'
: /msie/i.test(navigator.userAgent) ? 'ms' : 'opera' in window ? '-o' : '';
this.cssTransform = this.vendor + '-transform';
this.has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix();
this.trnOpen = 'translate' + (this.has3d ? '3d(' : '(');
this.trnClose = this.has3d ? ',0)' : ')';
this.scaleOpen = 'scale' + (this.has3d ? '3d(' : '(');
this.scaleClose = this.has3d ? ',0)' : ')';
// draw the slots to initial locations
this.draw(true);
}
// Restar the game and determine the stopping locations for reels
Game.prototype.restart = function() {
this.lastUpdate = new Date();
this.speed1 = this.speed2 = this.speed3 = SLOT_SPEED;
// function locates id from items
function _find(items, id) {
for (var i = 0; i < items.length; i++) {
if (items[i].id == id) return i;
}
}
// uncomment to get always jackpot
//this.result1 = _find( this.items1, 'gold-64' );
//this.result2 = _find( this.items2, 'gold-64' );
//this.result3 = _find( this.items3, 'gold-64' );
// get random results
this.result1 = parseInt(Math.random() * this.items1.length);
this.result2 = parseInt(Math.random() * this.items2.length);
this.result3 = parseInt(Math.random() * this.items3.length);
// Clear stop locations
this.stopped1 = false;
this.stopped2 = false;
this.stopped3 = false;
// randomize reel locations
this.offset1 = -parseInt(Math.random(ITEM_COUNT)) * SLOT_HEIGHT;
this.offset2 = -parseInt(Math.random(ITEM_COUNT)) * SLOT_HEIGHT;
this.offset3 = -parseInt(Math.random(ITEM_COUNT)) * SLOT_HEIGHT;
$('#results').hide();
this.state = 1;
};
window.requestAnimFrame = (function() {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
}
);
})();
Game.prototype.loop = function() {
var that = this;
that.running = true;
(function gameLoop() {
that.update();
that.draw();
if (that.running) {
requestAnimFrame(gameLoop);
}
})();
};
Game.prototype.update = function() {
var now = new Date();
var that = this;
// Check slot status and if spun long enough stop it on result
function _check_slot(offset, result) {
if (now - that.lastUpdate > SPINTIME) {
var c = parseInt(Math.abs(offset / SLOT_HEIGHT)) % ITEM_COUNT;
if (c == result) {
if (result == 0) {
if (Math.abs(offset + ITEM_COUNT * SLOT_HEIGHT) < SLOT_SPEED * 1.5) {
return true; // done
}
} else if (Math.abs(offset + result * SLOT_HEIGHT) < SLOT_SPEED * 1.5) {
return true; // done
}
}
}
return false;
}
switch (this.state) {
case 1: // all slots spinning
if (now - this.lastUpdate > RUNTIME) {
this.state = 2;
this.lastUpdate = now;
}
break;
case 2: // slot 1
this.stopped1 = _check_slot(this.offset1, this.result1);
if (this.stopped1) {
this.speed1 = 0;
this.state++;
this.lastUpdate = now;
}
break;
case 3: // slot 1 stopped, slot 2
this.stopped2 = _check_slot(this.offset2, this.result2);
if (this.stopped2) {
this.speed2 = 0;
this.state++;
this.lastUpdate = now;
}
break;
case 4: // slot 2 stopped, slot 3
this.stopped3 = _check_slot(this.offset3, this.result3);
if (this.stopped3) {
this.speed3 = 0;
this.state++;
}
break;
case 5: // slots stopped
if (now - this.lastUpdate > 3000) {
this.state = 6;
}
break;
case 6: // check results
var ec = 0;
$('#results').show();
if (that.items1[that.result1].id == 'gold-64') {
ec++;
}
if (that.items2[that.result2].id == 'gold-64') {
ec++;
}
if (that.items3[that.result3].id == 'gold-64') {
ec++;
}
$('#multiplier').text(ec);
$('#status').text(BLURB_TBL[ec]);
this.state = 7;
break;
case 7: // game ends
break;
default:
}
this.lastupdate = now;
};
Game.prototype.draw = function(force) {
if (this.state >= 6) return;
// draw the spinning slots based on current state
for (var i = 1; i <= 3; i++) {
var resultp = 'result' + i;
var stopped = 'stopped' + i;
var speedp = 'speed' + i;
var offsetp = 'offset' + i;
var cp = 'c' + i;
if (this[stopped] || this[speedp] || force) {
if (this[stopped]) {
this[speedp] = 0;
var c = this[resultp]; // get stop location
this[offsetp] = -(c * SLOT_HEIGHT);
if (this[offsetp] + DRAW_OFFSET > 0) {
// reset back to beginning
this[offsetp] = -this.resetOffset + SLOT_HEIGHT * 3;
}
} else {
this[offsetp] += this[speedp];
if (this[offsetp] + DRAW_OFFSET > 0) {
// reset back to beginning
this[offsetp] = -this.resetOffset + SLOT_HEIGHT * 3 - DRAW_OFFSET;
}
}
// translate canvas location
this[cp].css(
this.cssTransform,
this.trnOpen + '0px, ' + (this[offsetp] + DRAW_OFFSET) + 'px' + this.trnClose
);
}
}
};