-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblockor.js
214 lines (211 loc) · 5.95 KB
/
blockor.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
class Blockor {
constructor(settings) {
Object.assign(this, settings)
this.rotation = 0
if (!this.position) this.position = createVector(0, 0, 0)
this.lastPosition = createVector(-1, -1, -1)
if (!this.orientation) this.orientation = createVector()
this.pressing = new Set()
}
update() {
push()
let position = this.position
let orientation = this.orientation
let drawX = position.x * 50
let drawY = position.y * 50
let drawZ = position.z * 50
if (this.oddRotZ()) {
drawX = (position.x + 0.5) * 50
drawY = (position.y + 0.5) * 50
}
else {
if (this.oddRotX()) {
drawZ = (position.z + 0.5) * 50
drawY = (position.y + 0.5) * 50
}
else {
drawX = (position.x) * 50
drawY = (position.y) * 50
}
}
translate(drawX, drawY, drawZ)
rotateX(orientation.x)
rotateY(orientation.y)
rotateZ(orientation.z)
fill("red")
box(50, 100, 50)
pop()
}
oddRotZ() {
return (this.orientation.z / 90) % 2
}
oddRotX() {
return (this.orientation.x / 90) % 2
}
isStanding(orientation) {
return this.oddRotX() === this.oddRotZ()
}
keyCheck() {
if(this.game.menuOpen) return
if (this.game.win || this.game.dead) return
let orientation = this.orientation
let position = this.position
let pressing = false
if(this.lastKeyFrame === frameCount){
setTimeout(this.keyCheck.bind(this), 1000/frameRate())
return
}
this.lastKeyFrame = frameCount
if (keyIsDown(this.keys.right)&&!this.pressing.has(this.keys.right)) {
this.pressing.add(this.keys.right)
this.moveTo('right')
}
if (keyIsDown(this.keys.left)&&!this.pressing.has(this.keys.left)) {
this.pressing.add(this.keys.left)
this.moveTo('left')
}
if (keyIsDown(this.keys.up)&&!this.pressing.has(this.keys.up)) {
this.pressing.add(this.keys.up)
this.moveTo('up')
}
if (keyIsDown(this.keys.down)&&!this.pressing.has(this.keys.down)) {
this.pressing.add(this.keys.down)
this.moveTo('down')
}
if ( keyIsDown(this.keys.swap)&&!this.pressing.has(this.keys.swap) && this.half) {
console.log("swap")
this.pressing.add(this.keys.swap)
if(!this.justSwapped) {
this.game.blockor = this.otherBlockor
this.game.blockor.justSwapped = true
this.game.blockor.otherBlockor = this
this.otherBlockor = null
keyPressed = game.blockor.keyCheck.bind(this.game.blockor)
keyReleased = game.blockor.keyClear.bind(this.game.blockor)
}
}
}
keyClear() {
this.pressing.delete(keyCode)
if(keyCode === this.keys.swap) {
this.justSwapped = false
console.log("clearing swap key")
}
}
moveTo(direction) {
let orientation = this.orientation
let position = this.position
if (direction === 'right') {
if (this.oddRotX()) {
position.x += 1
} else {
orientation.z -= 90
if (this.oddRotZ()) position.x += 1
else position.x += 2
}
}
if (direction === 'left') {
if (this.oddRotX()) {
position.x -= 1
} else {
orientation.z += 90
if (this.oddRotZ()) position.x -= 2
else position.x -= 1
}
}
if (direction === 'up') {
if (this.oddRotZ()) {
position.z -= 1
} else {
orientation.x -= 90
if (this.oddRotX()) position.z -= 2
else position.z -= 1
}
}
if (direction === 'down') {
if (this.oddRotZ()) {
position.z += 1
} else {
orientation.x += 90
if (this.oddRotX()) position.z += 1
else position.z += 2
}
}
}
}
class HalfBlockor extends Blockor {
constructor(settings) {
super(settings)
this.half = true
}
update() {
push()
let position = this.position
let orientation = this.orientation
let drawX = position.x * 50
let drawY = position.y * 50 + 25
let drawZ = position.z * 50
translate(drawX, drawY, drawZ)
rotateX(orientation.x)
rotateY(orientation.y)
rotateZ(orientation.z)
if (this.otherBlockor) fill("red")
else fill("#c96363")
box(50, 50, 50)
pop()
if (this.otherBlockor) {
this.otherBlockor.update()
let sameX = this.position.x === this.otherBlockor.position.x
let nextX = abs(this.position.x - this.otherBlockor.position.x) === 1
let sameZ = this.position.z === this.otherBlockor.position.z
let nextZ = abs(this.position.z - this.otherBlockor.position.z) === 1
let adjacent = (sameX && nextZ) || (sameZ && nextX)
if (adjacent) this.merge()
}
}
merge() {
document.getElementById("level").innerText = "Stage: " + this.game.levelN
let orientation = createVector(90, 0, 0)
if (this.position.x < this.otherBlockor.position.x) {
orientation = createVector(0, 0, 90)
}
if (this.position.x > this.otherBlockor.position.x) {
orientation = createVector(0, 0, -90)
this.position.x -= 1
}
if (this.position.z < this.otherBlockor.position.z) {
orientation = createVector(90, 0, 0)
}
if (this.position.z > this.otherBlockor.position.z) {
orientation = createVector(-90, 0, 0)
this.position.z -= 1
}
this.game.blockor = new Blockor({
keys: { right: 39, left: 37, up: 38, down: 40, swap: 32 },
game: this,
position: createVector(this.position.x, this.position.y, this.position.z),
orientation: orientation
})
keyPressed = game.blockor.keyCheck.bind(this.game.blockor)
keyReleased = game.blockor.keyClear.bind(this.game.blockor)
}
moveTo(direction) {
let orientation = this.orientation
let position = this.position
if (direction === 'right') {
position.x += 1
}
if (direction === 'left') {
position.x -= 1
}
if (direction === 'up') {
position.z -= 1
}
if (direction === 'down') {
position.z += 1
}
}
isStanding(orientation) {
return false
}
}