-
Notifications
You must be signed in to change notification settings - Fork 0
/
current_tetramino.js
55 lines (46 loc) · 1.75 KB
/
current_tetramino.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
class Current_tetramino extends Tetramino {
check_move(dx, dy, field, cell_index){
return !(this.cells[cell_index].x + dx < 0 || this.cells[cell_index].y + dy< 0 ||
this.cells[cell_index].x +dx >= field_width || this.cells[cell_index].y + dy >= field_height ||
field.field[this.cells[cell_index].y+dy][this.cells[cell_index].x+dx] == "1")
}
move_sides(dx, dy, field){
for(let i = 0; i < size_of_tetramino; i++){
if(!this.check_move(dx, dy, field, i)){
if(dy == 1){
document.dispatchEvent(my_event)
}
return false
}
}
for (let i = 0; i < size_of_tetramino; i++) {
this.cells[i].set_x(this.cells[i].x + dx)
this.cells[i].set_y(this.cells[i].y + dy)
}
return true
}
rotate(field){
let matrix_size = this.matrix.length - 1
let x, y, dx, dy
for(let i = 0; i < size_of_tetramino; i++){
y = this.cells[i].tetramino_x
x = matrix_size - this.cells[i].tetramino_y
dx = x - this.cells[i].tetramino_x
dy = y - this.cells[i].tetramino_y
if(!this.check_move(dx, dy, field, i)){
return false
}
}
for(let i = 0; i < size_of_tetramino; i++){
y = this.cells[i].tetramino_x
x = matrix_size - this.cells[i].tetramino_y
dx = x - this.cells[i].tetramino_x
dy = y - this.cells[i].tetramino_y
this.cells[i].tetramino_y = y
this.cells[i].tetramino_x = x
this.cells[i].set_x(this.cells[i].x + dx)
this.cells[i].set_y(this.cells[i].y + dy)
}
return true
}
}