-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.js
111 lines (96 loc) · 2.32 KB
/
blocks.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
class Blocks {
static #BLOCKS = [
[
[[1, 1, 1, 1]],
],
[
[
[1, 0, 0],
[1, 1, 1]
]
], [
[
[0, 0, 1],
[1, 1, 1]
]
], [
[
[0, 1, 0],
[1, 1, 1]
]
], [
[
[1, 1],
[1, 1]
]
], [
[
[1, 1, 0],
[0, 1, 1]
]
], [
[
[0, 1, 1],
[1, 1, 0]
]
],
]
static {
for (let i = 0; i < this.#BLOCKS.length; i++) {
for (let j = 1; j < 4; j++) {
this.#BLOCKS[i].unshift(this.#transpose(this.#BLOCKS[i][0]))
}
}
}
static #transpose(a) {
let temp = new Array(a[0].length); // number of columns
for (let i = 0; i < temp.length; i++) {
temp[i] = [];
}
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[0].length; j++) {
temp[j][i] = a[i][a[i].length - 1 - j];
}
}
return temp;
}
static new() {
const blockType = Math.floor(Math.random() * this.#BLOCKS.length)
const blockVariance = Math.floor(Math.random() * this.#BLOCKS[blockType].length)
return new Block(blockType, blockVariance)
}
static get(blockType, blockVariance) {
return this.#BLOCKS[blockType][blockVariance]
}
static next(blockType, blockVariance) {
return (blockVariance + 1) % this.#BLOCKS[blockType].length
}
}
class Block {
#type
#variance
constructor(_type, _variance) {
this.#type = _type
this.#variance = _variance
this.x = 0
this.y = 0
}
rotate() {
this.#variance = Blocks.next(this.#type, this.#variance)
}
getWidth() {
return Blocks.get(this.#type, this.#variance)[0].length
}
getHeight() {
return Blocks.get(this.#type, this.#variance).length
}
getRight() {
return this.x + this.getWidth()
}
setRight(value) {
this.x = value - this.getWidth()
}
data() {
return Blocks.get(this.#type, this.#variance)
}
}