-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.js
136 lines (107 loc) · 2.75 KB
/
engine.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
// Helper funciton
// creddit goes to https://medium.com/front-end-weekly/matrix-rotation-%EF%B8%8F-6550397f16ab
const flipMatrix = matrix => (
matrix[0].map((column, index) => (
matrix.map(row => row[index])
))
);
const rotateMatrix = matrix => (
flipMatrix(matrix.reverse())
);
const rotateMatrixCounterClockwise = matrix => (
flipMatrix(matrix).reverse()
);
const flipMatrixCounterClockwise = matrix => (
rotateMatrix(matrix).reverse()
);
function render_tetromino(grid, xoffset = 0, yoffset = 0, callback) {
let endx = grid.length - 1
let endy = grid[0].length - 1
let stepx = height / box_width
let stepy = height / box_height
// generates serise of numbers with a offset value like 10,20,30, where the step is 10
for (let x = 0, i = 0; x <= endx * stepx; i += 1, x = i * stepx) {
for (let y = 0, j = 0; y <= endy * stepy; j += 1, y = j * stepy) {
if (grid[i][j] === 1) {
// allows for setting offset of all the rectangle's to offset by n rectangles width and hieght
callback(x + xoffset * stepx + center_line, y + yoffset * stepy, stepx, stepy)
}
}
}
}
function rotate_2d_grid(tetronimoe) {
return _.zip.apply(_, tetronimoe)
}
function copy_to_screen(tetronimo, grid, xoffset = 0, yoffset = 0) {
if (xoffset < 0) throw (`xoffset is less than 0, xoffset=${xoffset}`)
if (yoffset < 0) throw (`yoffset is less than 0, yoffset=${yoffset}`)
for (var x = 0; x <= tetronimo.length - 1; x += 1) {
for (var y = 0; y <= tetronimo[x].length - 1; y += 1) {
try{
if (tetronimo[x][y] === 1) {
grid[x + xoffset][y + yoffset] = tetronimo[x][y]
}}catch(e){
console.error("outofbounds")
}
}
}
return grid
}
function render_grid(gamegrid, callback) {
let endx = gamegrid.length - 1
let endy = gamegrid[0].length - 1
let stepx = height / box_width
let stepy = height / box_height
for (let x = 0, i = 0; x <= endx * stepx; i += 1, x = i * stepx) {
for (let y = 0, j = 0; y <= endy * stepy; j += 1, y = j * stepy) {
if (gamegrid[i][j] === 1) {
callback(x + center_line, y, stepx, stepy)
}
}
}
}
function get_tetronimoes() {
return ({
"i": [
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
],
"j": [
[0, 1, 0],
[0, 1, 0],
[1, 1, 0],
],
"l": [
[0, 1, 0],
[0, 1, 0],
[0, 1, 1],
],
"o": [
[1, 1],
[1, 1],
],
"s": [
[0, 0, 0],
[0, 1, 1],
[1, 1, 0],
],
"t": [
[0, 0, 0],
[1, 1, 1],
[0, 1, 0],
],
"z": [
[0, 0, 0],
[1, 1, 0],
[0, 1, 1],
],
"test": [
[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
],
})
}