-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
50 lines (41 loc) · 854 Bytes
/
block.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
class Block {
constructor (x, y, col) {
this.x = x;
this.y = y;
this.col = col;
this.entity = new BlockEntity(this)
}
// Are 2 blocks touching
touches(other) {
return (this.y === other.y && Math.abs(this.x - other.x) === 1) ||
(this.x === other.x && Math.abs(this.y - other.y) === 1)
}
moveDown() {
this.y ++;
this.entity.ty = this.y;
}
moveLeft () {
this.x = this.entity.x = this.x - 1;
}
moveRight () {
this.x = this.entity.x = this.x + 1;
}
}
class BlockEntity {
constructor (block) {
this.x = block.x;
this.y = block.y
this.ty = block.y;
this.vy = 0;
this.g = 0.02;
}
update () {
if(this.y < this.ty) {
this.vy += this.g;
this.y = Math.min(this.ty, this.y + this.vy)
} else {
this.y = this.ty;
this.vy = 0;
}
}
}