-
Notifications
You must be signed in to change notification settings - Fork 2
/
Physics.js
186 lines (166 loc) · 4.9 KB
/
Physics.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
var Physics = (function() {
"use strict";
var Physics = {};
// Move the block on the board. This method never modifies data or keys.
Physics.moveBlock = function(block, data, keys) {
var shift = 0;
var drop = 0;
var turn = 0;
var moved = false;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key === Action.RIGHT) {
shift++;
} else if (key === Action.LEFT) {
shift--;
} else if (key === Action.DOWN) {
drop += 1;
} else if (key === Action.ROTATE_CW && block.rotates) {
turn = 1;
} else if (key === Action.ROTATE_CCW && block.rotates) {
turn = -1;
}
}
if (shift !== 0) {
block.x += shift;
if (this.checkBlock(block, data) === Constants.OK) {
moved = true;
} else {
block.x -= shift;
}
}
if (turn !== 0) {
block.angle = (block.angle + turn + 4) % 4;
var trans = new Point(0, 0);
while (this.checkBlock(block, data) === Constants.LEFTEDGE) {
block.x++;
trans.x++;
}
while (this.checkBlock(block, data) === Constants.RIGHTEDGE) {
block.x--;
trans.x--;
}
while (this.checkBlock(block, data) === Constants.TOPEDGE) {
block.y++;
trans.y++;
}
if (this.checkBlock(block, data) === Constants.OK) {
moved = true;
} else if (block.shoveaways > 0 && this.shoveaway(block, data, shift)) {
block.shoveaways--;
moved = true;
} else {
block.x -= trans.x;
block.y -= trans.y;
block.angle = (block.angle - turn + 4) % 4;
}
}
if (moved) {
block.rowsFree = this.calculateRowsFree(block, data);
block.localStickFrames = Constants.LOCALSTICKFRAMES;
}
if (block.rowsFree > 0) {
block.localStickFrames = Constants.LOCALSTICKFRAMES;
block.globalStickFrames = Constants.GLOBALSTICKFRAMES;
// Drop the block if gravity is on or if a DOWN key were pressed.
drop = Math.min(drop, block.rowsFree);
block.y += drop;
block.rowsFree -= drop;
} else {
block.globalStickFrames--;
if (!moved) {
block.localStickFrames--;
}
}
}
// Tries to shove the block away from obstructing squares and the bottom edge.
// Returns true and leaves the block in its new position on success.
// Leaves the block's position unmodified on failure.
Physics.shoveaway = function(block, data, hint) {
// In the absence of a hint, prefer to shove left over shoving right.
hint = (hint > 0 ? 1 : -1);
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 3; j++) {
if (this.checkBlock(block, data) === Constants.OK) {
return true;
}
block.x += (j === 1 ? -2*hint : hint);
}
if (i === 0) {
block.y++;
} else if (i === 1) {
block.y -= 2;
} else {
block.y--;
}
}
block.y += 3;
return false;
}
// Places the block onto the board and removes full rows from the board.
// Returns the number of rows removed.
Physics.placeBlock = function(block, data) {
var offsets = block.getOffsets();
for (var i = 0; i < offsets.length; i++) {
var offset = offsets[i];
data[offset.y][offset.x] = block.color;
}
return this.removeRows(data);
}
// Modifies data and returns the number of rows cleared from it.
Physics.removeRows = function(data) {
var numRowsCleared = 0;
for (var i = Constants.ROWS - 1; i >= 0; i--) {
var isRowFull = true;
for (var j = 0; j < Constants.COLS; j++) {
if (data[i][j] === 0) {
isRowFull = false;
}
}
if (isRowFull) {
for (j = 0; j < Constants.COLS; j++) {
data[i][j] = 0;
}
numRowsCleared++;
} else if (numRowsCleared > 0) {
for (j = 0; j < Constants.COLS; j++) {
data[i + numRowsCleared][j] = data[i][j];
data[i][j] = 0;
}
}
}
return numRowsCleared;
}
// Returns the number of rows that the given block could drop on this board.
// Mutates block in the middle of the function but restores it by the end.
Physics.calculateRowsFree = function(block, data) {
var result = 0;
while (this.checkBlock(block, data) === Constants.OK) {
result++;
block.y++;
}
block.y -= result;
return result - 1;
}
// Returns OK if the block is in a valid position. Otherwise, returns the
// code for the highest-priority placement rule that the block breaks.
Physics.checkBlock = function(block, data) {
var offsets = block.getOffsets();
var status = Constants.OK;
for (var i = 0; i < offsets.length; i++) {
if (offsets[i].x < 0) {
status = Math.min(Constants.LEFTEDGE, status);
} else if (offsets[i].x >= Constants.COLS) {
status = Math.min(Constants.RIGHTEDGE, status);
} else if (offsets[i].y < 0) {
status = Math.min(Constants.TOPEDGE, status);
} else if (offsets[i].y >= Constants.ROWS) {
status = Math.min(Constants.BOTTOMEDGE, status);
} else if (data[offsets[i].y][offsets[i].x] !== 0) {
status = Math.min(Constants.OVERLAP, status);
}
}
return status;
}
return Physics;
})();