-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
204 lines (186 loc) · 5.11 KB
/
Copy pathindex.ts
File metadata and controls
204 lines (186 loc) · 5.11 KB
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
const TILE_SIZE = 30;
const FPS = 30;
const SLEEP = 1000 / FPS;
enum Tile {
AIR,
FLUX,
UNBREAKABLE,
PLAYER,
STONE,
FALLING_STONE,
BOX,
FALLING_BOX,
KEY1,
LOCK1,
KEY2,
LOCK2,
}
enum Input {
UP,
DOWN,
LEFT,
RIGHT,
}
let playerx = 1;
let playery = 1;
let map: Tile[][] = [
[2, 2, 2, 2, 2, 2, 2, 2],
[2, 3, 0, 1, 1, 2, 0, 2],
[2, 4, 2, 6, 1, 2, 0, 2],
[2, 8, 4, 1, 1, 2, 0, 2],
[2, 4, 1, 1, 1, 9, 0, 2],
[2, 2, 2, 2, 2, 2, 2, 2],
];
let inputs: Input[] = [];
function remove(tile: Tile) {
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
if (map[y][x] === tile) {
map[y][x] = Tile.AIR;
}
}
}
}
function moveToTile(newx: number, newy: number) {
map[playery][playerx] = Tile.AIR;
map[newy][newx] = Tile.PLAYER;
playerx = newx;
playery = newy;
}
function moveHorizontal(dx: number) {
if (
map[playery][playerx + dx] === Tile.FLUX ||
map[playery][playerx + dx] === Tile.AIR
) {
moveToTile(playerx + dx, playery);
} else if (
(map[playery][playerx + dx] === Tile.STONE ||
map[playery][playerx + dx] === Tile.BOX) &&
map[playery][playerx + dx + dx] === Tile.AIR &&
map[playery + 1][playerx + dx] !== Tile.AIR
) {
map[playery][playerx + dx + dx] = map[playery][playerx + dx];
moveToTile(playerx + dx, playery);
} else if (map[playery][playerx + dx] === Tile.KEY1) {
remove(Tile.LOCK1);
moveToTile(playerx + dx, playery);
} else if (map[playery][playerx + dx] === Tile.KEY2) {
remove(Tile.LOCK2);
moveToTile(playerx + dx, playery);
}
}
function moveVertical(dy: number) {
if (
map[playery + dy][playerx] === Tile.FLUX ||
map[playery + dy][playerx] === Tile.AIR
) {
moveToTile(playerx, playery + dy);
} else if (map[playery + dy][playerx] === Tile.KEY1) {
remove(Tile.LOCK1);
moveToTile(playerx, playery + dy);
} else if (map[playery + dy][playerx] === Tile.KEY2) {
remove(Tile.LOCK2);
moveToTile(playerx, playery + dy);
}
}
function update() {
handleInputs();
updateMap();
}
//규칙 3: 리팩토링한 부분1
function handleInputs() {
while (inputs.length > 0) {
let current = inputs.pop();
handleInput(current);
}
}
function handleInput(input: Input) {
{
if (input === Input.LEFT) moveHorizontal(-1);
else if (input === Input.RIGHT) moveHorizontal(1);
else if (input === Input.UP) moveVertical(-1);
else if (input === Input.DOWN) moveVertical(1);
}
}
//규칙 3: 리팩토링한 부분2
function updateMap() {
for (let y = map.length - 1; y >= 0; y--) {
for (let x = 0; x < map[y].length; x++) {
updateTile(x, y);
}
}
}
function updateTile(x: number, y: number) {
if (
(map[y][x] === Tile.STONE || map[y][x] === Tile.FALLING_STONE) &&
map[y + 1][x] === Tile.AIR
) {
map[y + 1][x] = Tile.FALLING_STONE;
map[y][x] = Tile.AIR;
} else if (
(map[y][x] === Tile.BOX || map[y][x] === Tile.FALLING_BOX) &&
map[y + 1][x] === Tile.AIR
) {
map[y + 1][x] = Tile.FALLING_BOX;
map[y][x] = Tile.AIR;
} else if (map[y][x] === Tile.FALLING_STONE) {
map[y][x] = Tile.STONE;
} else if (map[y][x] === Tile.FALLING_BOX) {
map[y][x] = Tile.BOX;
}
}
function createGraphics() {
let canvas = document.getElementById("GameCanvas") as HTMLCanvasElement;
let g = canvas.getContext("2d");
g.clearRect(0, 0, canvas.width, canvas.height);
return g;
}
function draw() {
let g = createGraphics();
drawMap(g);
drawPlayer(g);
}
function drawMap(g: CanvasRenderingContext2D) {
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
if (map[y][x] === Tile.FLUX) g.fillStyle = "#ccffcc";
else if (map[y][x] === Tile.UNBREAKABLE) g.fillStyle = "#999999";
else if (map[y][x] === Tile.STONE || map[y][x] === Tile.FALLING_STONE)
g.fillStyle = "#0000cc";
else if (map[y][x] === Tile.BOX || map[y][x] === Tile.FALLING_BOX)
g.fillStyle = "#8b4513";
else if (map[y][x] === Tile.KEY1 || map[y][x] === Tile.LOCK1)
g.fillStyle = "#ffcc00";
else if (map[y][x] === Tile.KEY2 || map[y][x] === Tile.LOCK2)
g.fillStyle = "#00ccff";
if (map[y][x] !== Tile.AIR && map[y][x] !== Tile.PLAYER)
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
}
function drawPlayer(g: CanvasRenderingContext2D) {
g.fillStyle = "#ff0000";
g.fillRect(playerx * TILE_SIZE, playery * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
function gameLoop() {
let before = Date.now();
update();
draw();
let after = Date.now();
let frameTime = after - before;
let sleep = SLEEP - frameTime;
setTimeout(() => gameLoop(), sleep);
}
window.onload = () => {
gameLoop();
};
const LEFT_KEY = "ArrowLeft";
const UP_KEY = "ArrowUp";
const RIGHT_KEY = "ArrowRight";
const DOWN_KEY = "ArrowDown";
window.addEventListener("keydown", (e) => {
if (e.key === LEFT_KEY || e.key === "a") inputs.push(Input.LEFT);
else if (e.key === UP_KEY || e.key === "w") inputs.push(Input.UP);
else if (e.key === RIGHT_KEY || e.key === "d") inputs.push(Input.RIGHT);
else if (e.key === DOWN_KEY || e.key === "s") inputs.push(Input.DOWN);
});