-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslotsController.js
162 lines (144 loc) · 5.07 KB
/
slotsController.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
function SlotController() {
let reels = [];
let reelPositions = [0,0,0,0,0];
let currSymbols = [];
// populate reels
for ( var i = 0; i < NUM_OF_REELS; i++ ) {
var r = new Reel({
position: {
x: REELS_X+(i*(REEL_WIDTH+REEL_SPACING)),
y: REELS_Y,
},
width: REEL_WIDTH,
symbolHeight: SYMBOL_HEIGHT,
});
reels.push(r);
}
function showPositions(positions) {
for ( var i = 0; i < Math.min(positions.length,reels.length,NUM_OF_REELS); i++ ) {
let symbols = getSymbolsAtPosition(i,positions[i]);
currSymbols[i] = symbols;
reels[i].display(symbols,(positions[i]%1));
}
}
let _isSpinning = false;
let spinStartTime;
let lastSpinUpdateTime;
let spinId;
function spin() {
if ( isResultSpinning ) return;
resetSymbolDisplay();
_isSpinning = true;
spinStartTime = lastSpinUpdateTime = performance.now();
spinId = window.requestAnimationFrame(doSpin);
}
this.spin = spin.bind(this);
function isSpinning() {
return _isSpinning;
}
this.isSpinning = isSpinning.bind(this);
function doSpin(e) {
let msPassed = e - lastSpinUpdateTime;
for ( var i = 0; i < reelPositions.length; i++ ) {
if ( e-spinStartTime > i*SPIN_DELAY_PER_REEL ) {
reelPositions[i] += msPassed/1000*SPIN_SPEED;
if ( reelPositions[i] > REEL_SYMBOL_DATA[i].length ) {
reelPositions[i] -= REEL_SYMBOL_DATA[i].length;
}
}
}
showPositions(reelPositions);
lastSpinUpdateTime = e;
if ( _isSpinning ) {
spinId = window.requestAnimationFrame(doSpin);
}
}
showPositions(reelPositions);
let endSpinSymbols = [];
let endSpinPositions = [];
let isResultSpinning = false;
let wins;
function stopSpin(result) {
_isSpinning = false;
window.cancelAnimationFrame(spinId);
if ( result ) {
let resultPositions = result.reelPositions;
wins = result.wins;
isResultSpinning = true;
// create new list of symbols, current + result
for ( var i = 0; i < Math.min(resultPositions.length,reels.length,NUM_OF_REELS); i++ ) {
endSpinSymbols[i] = currSymbols[i].concat(getSymbolsAtPosition(i,resultPositions[i]));
endSpinPositions[i] = reelPositions[i]%1;
}
// start new frame function for showing new symbols
spinId = window.requestAnimationFrame(doStopToResult);
reelPositions = resultPositions.concat();
}
}
this.stopSpin = stopSpin.bind(this);
function doStopToResult(e) {
let msPassed = e - lastSpinUpdateTime;
let endSpinComplete = true;
for ( var i = 0; i < endSpinPositions.length; i++ ) {
if ( endSpinPositions[i] < endSpinSymbols[i].length-NUM_OF_ROWS-1 ) {
endSpinPositions[i] += msPassed/1000*SPIN_SPEED;
endSpinComplete = false;
}
else {
endSpinPositions[i] = endSpinSymbols[i].length-NUM_OF_ROWS-1;
}
reels[i].display(endSpinSymbols[i],endSpinPositions[i]);
}
lastSpinUpdateTime = e;
if ( !endSpinComplete && isResultSpinning ) {
spinId = window.requestAnimationFrame(doStopToResult);
}
else {
isResultSpinning = false;
if ( wins && wins.length ) {
showWins();
}
}
}
let isShowingWins = false;
let currWinIndex;
let winLineTimerId;
function showWins() {
currWinIndex = 0;
showWinLine(wins[currWinIndex].lineId,wins[currWinIndex].length);
}
function showWinLine(lineId,length) {
fadeAllSymbols();
let winLine = WIN_LINES[lineId];
for ( var r = 0; r < length; r++ ) {
reels[r].applyFilters(winLine[r],getWinFilters(r));
}
clearTimeout(winLineTimerId);
winLineTimerId = setTimeout(()=>{
currWinIndex++;
if ( currWinIndex >= wins.length ) {
currWinIndex = 0;
}
showWinLine(wins[currWinIndex].lineId,wins[currWinIndex].length);
},WIN_LINE_DURATION);
}
function getWinFilters(index) {
let winFilter = new PIXI.filters.AdvancedBloomFilter({bloomScale:0.5,threshold:0.5});
createjs.Tween.get(winFilter)
.wait(index*WIN_LINE_DURATION/10)
.to({bloomScale:1,brightness:1.5,threshold:0.25}, WIN_LINE_DURATION/4)
.to({bloomScale:0.5,brightness:1,threshold:0.5}, WIN_LINE_DURATION/4);
return [winFilter];
}
function fadeAllSymbols() {
for ( var r = 0; r < NUM_OF_REELS; r++ ) {
reels[r].fade();
}
}
function resetSymbolDisplay() {
clearTimeout(winLineTimerId);
for ( var i = 0; i < reels.length; i++ ) {
reels[i].clearFilters();
}
}
};