forked from cgolden15/tetris
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScoreTracker.js
213 lines (190 loc) · 4.81 KB
/
ScoreTracker.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
function ScoreTracker(scoreOutput, linesOutput, levelOutput, tickerOutput) {
this.level = 1;
this.score = 0;
this.linesRemaining = ScoreTracker.levelLines(this.level);
this.scoreOutput = scoreOutput;
this.linesOutput = linesOutput;
this.levelOutput = levelOutput;
this.tickerOutput = tickerOutput;
this.curCombo = -1;
this.lastWasBonus = false;
this.backToBackCount = 0;
this.isGameWon = false;
this.outputScore();
this.outputLines();
this.outputLevel();
}
ScoreTracker.levelLines = function (level) {
return level*5;
};
ScoreTracker.prototype.updateScore = function(config) {
var linesCleared = 0,
isBonus = false,
scoreDiff = 0,
tickerLines = [],
i;
if (config.miniT) {
// mini t spin, 1 for no lines, 2 for 1 line
tickerLines.push("T Spin Mini");
linesCleared += 1;
scoreDiff += 100 * this.level;
if (config.lines === 1) {
linesCleared += 1;
scoreDiff += 100 * this.level;
}
} else if (config.normalT) {
// normal t spin, bonus for eveything but 0 lines
switch (config.lines) {
case 0:
tickerLines.push("T Spin");
linesCleared += 4;
scoreDiff += 400 * this.level;
break;
case 1:
tickerLines.push("T Spin Single");
linesCleared += 8;
isBonus = true;
scoreDiff += 800 * this.level;
break;
case 2:
tickerLines.push("T Spin Double");
linesCleared += 12;
isBonus = true;
scoreDiff += 1200 * this.level;
break;
case 3:
tickerLines.push("T SPIN TRIPLE");
linesCleared += 16;
isBonus = true;
scoreDiff += 1600 * this.level;
break;
}
} else if (config.lines > 0) {
// plain old line clears
switch (config.lines) {
case 1:
tickerLines.push("Single");
linesCleared += 1;
scoreDiff += 100 * this.level;
break;
case 2:
tickerLines.push("Double");
linesCleared += 3;
scoreDiff += 300 * this.level;
break;
case 3:
tickerLines.push("Triple");
linesCleared += 5;
scoreDiff += 500 * this.level;
break;
case 4:
tickerLines.push("TETRIS");
linesCleared += 8;
isBonus = true;
scoreDiff += 800 * this.level;
break;
}
}
// apply the combo
if (linesCleared > 0) {
this.curCombo += 1;
linesCleared += Math.floor(this.curCombo * 0.5);
scoreDiff += 50 * this.curCombo * this.level;
if (this.curCombo >= 1) {
tickerLines.push("Combo x" + this.curCombo);
}
} else {
this.curCombo = -1;
}
// apply back-to-back bonus
if (this.lastWasBonus && isBonus) {
tickerLines.push("Back-to-Back");
this.backToBackCount += 1;
linesCleared = Math.floor(linesCleared * 1.5);
scoreDiff += this.backToBackCount * 0.5 * scoreDiff;
} else {
this.backToBackCount = 0;
}
// only update the last bonus state if a single through triple was gotten
if (config.lines > 0) {
this.lastWasBonus = isBonus;
}
// apply the lines cleared
this.linesRemaining -= linesCleared;
if (this.linesRemaining <= 0) {
if (this.level < 15) {
this.level += 1;
this.linesRemaining = ScoreTracker.levelLines(this.level);
} else {
this.isGameWon = true;
}
this.outputLevel();
}
if (linesCleared > 0) {
this.outputLines();
}
this.score += scoreDiff;
this.outputScore();
if (tickerLines.length === 0) {
this.tickerOutput.addLine("");
} else {
for (i = 0; i < tickerLines.length; i += 1) {
this.tickerOutput.addLine(tickerLines[i]);
}
}
};
ScoreTracker.prototype.softDrop = function() {
this.score += 1;
};
ScoreTracker.prototype.hardDrop = function(dist) {
this.score += 2 * dist;
};
ScoreTracker.prototype.getLinesRemaining = function() { return this.linesRemaining; };
ScoreTracker.prototype.getScore = function() { return this.score; };
ScoreTracker.prototype.getLevel = function() { return this.level; };
ScoreTracker.prototype.getLevelPeriod = function() {
var periods = [
1000,
800,
600,
470,
380,
250,
200,
160,
130,
90,
50,
27,
20,
15,
10
],
res = periods[(this.level < periods.length) ? this.level : periods.length - 1];
return res;
};
ScoreTracker.prototype.gameWon = function() {
return this.isGameWon;
};
ScoreTracker.prototype.getResults = function() {
return {
score: this.score,
level: this.level,
won: this.isGameWon
};
};
ScoreTracker.prototype.outputScore = function() {
this.scoreOutput.addLine("Score:");
this.scoreOutput.addLine("" + this.score);
this.scoreOutput.addLine("");
};
ScoreTracker.prototype.outputLines = function() {
this.linesOutput.addLine("Lines:");
this.linesOutput.addLine("" + this.linesRemaining);
this.linesOutput.addLine("");
};
ScoreTracker.prototype.outputLevel = function() {
this.levelOutput.addLine("Level:");
this.levelOutput.addLine("" + this.level);
this.levelOutput.addLine("");
};