-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameMechanics.js
216 lines (176 loc) · 5.28 KB
/
gameMechanics.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
function applyToModel(tetrimo, value) {
tetrimo.coordinates.forEach(element => {
model[element.y][element.x] = value;
});
};
// -- MOVEMENT ------
// - Move -
const MOVE_DIER = {
down: new flatVector(0, 1),
up: new flatVector(0, -1),
left: new flatVector(-1, 0),
right: new flatVector(1, 0)
};
function moveByVector(tetrimo, vector) {
tetrimo.coordinates = tetrimo.coordinates.map(element => element.addition(vector));
};
// true if it can move
function move(tetrimo, vector, apply = true) {
let result = true;
applyToModel(tetrimo, 0);
moveByVector(tetrimo, vector);
if (checkIfCollapse(tetrimo)) {
moveByVector(tetrimo, new flatVector(0,0).subtraction(vector));
result = false;
};
if (apply) applyToModel(tetrimo, tetrimo.color);
return result;
};
// true if collapse
function checkIfCollapse(tetrimo) {
let result = false;
tetrimo.coordinates.forEach(element => {
if (model[element.y] == undefined || model[element.y][element.x] == undefined || model[element.y][element.x] > 0) {
result = true;
};
});
return result;
}
// - rotation -
function spin(tetrimo, rotation, right) {
if (!right) rotation == 0? rotation = 3 : rotation = rotation -1;
tetrimo.coordinates = tetrimo.coordinates.map((element, index) => {
return element.addition(right? SPIN_FORM_VECTORS[tetrimo.name][rotation][index] : SPIN_FORM_VECTORS[tetrimo.name][rotation][index].invert());
});
if (right) return rotation == 3 ? 0 : rotation + 1;
return rotation;
};
function rotation(tetrimo, right) {
let result = true
applyToModel(tetrimo, 0);
certainRotation = spin(tetrimo, certainRotation, right);
if (checkIfCollapse(tetrimo)) {
certainRotation = spin(tetrimo, certainRotation, !right);
result = false;
}
applyToModel(tetrimo, tetrimo.color);
return result;
}
// -- Hold ----
function switchHold(player) {
if (holdable) {
applyToModel(certainTetrimo, 0);
let back = currentHold;
currentHold = certainTetrimo.name;
if (back) {
certainTetrimo = new teriomio(back);
certainRotation = 0;
} else newCertainTetrimo();
applyToModel(certainTetrimo, certainTetrimo.color)
renderOnePeace(currentHold, holdCtx, 0,0,32);
timer = 0;
if (player) holdable = false;
};
};
// -- clear Lines ----
function clearLine(line) {
for (let i = line; i > 0; i--) {
model[i] = JSON.parse(JSON.stringify(model[i - 1]))
}
model[0] = Array(model[0].length).fill(0);
};
function clearFullLines(tetrimo, addToBoard) {
let clears = 0;
let lines = [];
tetrimo.coordinates.forEach(element => {
if (!lines.includes(element.y)) lines.push(element.y);
});
lines.sort((a, b) => a - b).forEach(line => {
if (!model[line].includes(0)) {
clearLine(line);
clears ++;
}
});
if(addToBoard) points += POINTS[clears];
//lines += lines.length;
return clears;
}
//clearLine
// -- insert new tetrimo ----
const FORMS_LETTER = "IJLOSTZ"
let certainFormsLetter = FORMS_LETTER;
let following = "";
function newLetter(){
while (following.length < 7){
let letter = certainFormsLetter[Math.floor(Math.random() * certainFormsLetter.length)];
certainFormsLetter = certainFormsLetter.replace(letter, "");
if (certainFormsLetter.length == 0) certainFormsLetter = FORMS_LETTER;
following += letter;
};
let letter = following[0]
following = following.replace(letter, "");
return letter;
};
function newCertainTetrimo() {
let letter = newLetter()
certainTetrimo = new teriomio(letter);
certainRotation = 0;
holdable = true;
renderNextPeaces()
if(gameOverCheck(certainTetrimo)) gameOver();
applyToModel(certainTetrimo, certainTetrimo.color)
};
newCertainTetrimo();
// -- game Over ----
function gameOver(){
alert("GAME OVER - points: " + points);
resetGame();
}
function gameOverCheck (tetrimo){
let check = false;
tetrimo.coordinates.forEach(element => {
if (model[element.y][element.x] > 0) check = true
});
return check;
}
function resetGame () {
currentHold = undefined;
model = Array(20).fill().map(() => (Array(10).fill(0)));
certainFormsLetter = FORMS_LETTER;
points = 0;
newCertainTetrimo();
}
// -- preview of the next turn ----
function preview (tetrimo){
let future = new teriomio (tetrimo.name);
future.coordinates = tetrimo.coordinates.map(element=>{
return new flatVector(element.x, element.y);
});
while(move(future, MOVE_DIER.down, false));
applyToModel(tetrimo, tetrimo.color);
previewVisualise(future);
return future;
}
// -- Fall Down ----
let timer = 0;
function fallDown() {
if (!move(certainTetrimo, MOVE_DIER.down)) {
timer++;
if (timer > 6) {
clearFullLines(certainTetrimo, true);
newCertainTetrimo();
timer = 0
}
}
}
let fallingIntervall;
let fallingSwitch = true;
function switchFalling(){
if (fallingSwitch){
fallingIntervall = setInterval(fallDown, 1000/fallingSpeed);
}
else {
clearInterval(fallingIntervall);
}
fallingSwitch = !fallingSwitch;
};