-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
216 lines (182 loc) · 4.13 KB
/
index.html
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
215
216
<html>
<style>
#board {
/* width: 200px;
height: 400px; */
background: pink;
}
.row {
border: 1px solid red;
padding: 1px 0;
/*width: 210px;*/
/*height: 6px;*/
}
.cell {
border: 1px solid green;
display: inline-block;
/*padding: 1px;*/
height: 10px;
width: 10px;
}
.cell.floating {
background: black;
}
.cell.fixed {
background: red;
}
</style>
<body>
<div id="board"></div>
</body>
</div>
</hmtl>
<script type="text/javascript">
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const SQUARE_TYPES = {
FLOATING: 1,
FIXED:2,
NONE: 0
}
const BOARD_SIZE = {
width: 20,
height: 20
};
const DIRECTIONS = {
LEFT: {dX: -1, dY: 0},
DOWN: {dX: 0, dY: 1},
RIGHT: {dX: 1, dY: 0}
}
class GameBoard {
constructor(width, height) {
console.log(width, height);
this.width = width;
this.height = height;
this.board = document.getElementById('board');
for(let i = 0; i < height; i++) {
const row = document.createElement('div');
row.className = 'row';
this.board.appendChild(row);
for(let j = 0; j < width; j++) {
const c = document.createElement('span');
c.className = 'cell';
c.dataset.y = i;
c.dataset.x = j;
if([0,1,2,3].indexOf(i) >= 0 && [0,1,2].indexOf(j) >= 0)
c.className += ' floating';
row.appendChild(c);
}
}
document.onkeydown = (event) => {
switch (event.key) {
case 'a': this.move(DIRECTIONS.LEFT); break;
case 's': this.move(DIRECTIONS.DOWN); break;
case 'd': this.move(DIRECTIONS.RIGHT); break;
case 'w': this.rotate(); break;
}
}
}
rotate() {
}
changeCell(x, y, type) {
const el = this.getCell(x,y);
if(!el) return;
window.requestAnimationFrame(
() => {
el.className = 'cell ' + type;
}
);
}
getCell(x, y) {
return document.querySelector(`.row:nth-child(${y+1}) > .cell:nth-child(${x+1})`);
}
getFloating() {
return document.querySelectorAll('.cell.floating');
}
getFixed() {
return document.querySelectorAll('.cell.fixed');
}
getPos(cell) {
return {
x: parseInt(cell.dataset.x),
y: parseInt(cell.dataset.y),
}
}
canMove(direction) {
const {dX, dY} = direction;
const floatingCells = this.getFloating();
var canMove = true;
for(let i = 0; i < floatingCells.length; i++) {
const c = floatingCells[i];
const {x, y} = this.getPos(c);
const newX = parseInt(x)+dX;
const newY = parseInt(y)+dY;
if(!( newX < this.width && newX >= 0 && newY >= 0 && newY < this.height)){
canMove = false; break;
}
}
return canMove;
}
moveCell(c, dX, dY) {
const {x, y} = this.getPos(c);
if(! this.canMove({dX, dY})) return;
const newX = parseInt(x)+dX;
const newY = parseInt(y)+dY;
let newType = 'floating';
if(newY == this.height-1 || this.getCell(newX, newY + 1).className.includes('fixed')) {
newType = 'fixed';
}
this.changeCell(x,y, 'none');
this.changeCell(newX, newY, newType);
}
move(direction) {
const {dX, dY} = direction;
let floatingCells = this.getFloating();
if(direction.dX === DIRECTIONS.RIGHT.dX ) {
for(let i = floatingCells.length - 1; i >= 0; i--) {
this.moveCell(floatingCells[i], dX, dY);
}
} else {
floatingCells.forEach(c => {
this.moveCell(c, dX, dY);
})
}
if(this.getFloating().length == 0) {
this.insertBlock();
}
}
insertBlock() {
const blockWidth = 3;
const posX = getRandomInt(0, BOARD_SIZE.width)
this.changeCell(posX + posX,0,'floating');
this.changeCell(posX + posX+1,0,'floating');
this.changeCell(posX + posX+2,0,'floating');
this.changeCell(posX + posX,1,'floating');
this.changeCell(posX + posX+1,1,'floating');
}
}
class Renderer {
constructor(eBoard) {
this.target = eBoard;
}
render() {
}
}
const gb = new GameBoard(BOARD_SIZE.width, BOARD_SIZE.height);
function step(timestamp) {
console.log('step'+Math.random());
gb.move(
DIRECTIONS[Object.keys(DIRECTIONS)[getRandomInt(0,2)]]
)
gb.move(
DIRECTIONS[Object.keys(DIRECTIONS)[getRandomInt(0,2)]]
)
gb.move(
DIRECTIONS[Object.keys(DIRECTIONS)[getRandomInt(0,2)]]
)
// do something for every frame
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
</script>