Repository navigation
main
/
index.ts
143 lines (118 loc) · 3.31 KB
main
/
index.ts
File metadata and controls
- Code
- Blame
143 lines (118 loc) · 3.31 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
import init, { World, Direction, GameStatus } from "snake_game";
import { rnd } from "./utils/rnd";
init().then(wasm => {
const CELL_SIZE = 20;
const WORLD_WIDTH = 8;
const snakeSpawnIdx = rnd(WORLD_WIDTH * WORLD_WIDTH);
const world = World.new(WORLD_WIDTH, snakeSpawnIdx);
const worldWidth = world.width();
const points = document.getElementById("points");
const gameStatus = document.getElementById("game-status");
const gameControlBtn = document.getElementById("game-control-btn");
const canvas = <HTMLCanvasElement> document.getElementById("snake-canvas");
const ctx = canvas.getContext("2d");
canvas.height = worldWidth * CELL_SIZE;
canvas.width = worldWidth * CELL_SIZE;
gameControlBtn.addEventListener("click", _ => {
const status = world.game_status();
if (status === undefined) {
gameControlBtn.textContent = "Playing..."
world.start_game();
play();
} else {
location.reload();
}
})
document.addEventListener("keydown", e => {
switch(e.code) {
case "ArrowUp":
world.change_snake_dir(Direction.Up);
break;
case "ArrowRight":
world.change_snake_dir(Direction.Right);
break;
case "ArrowDown":
world.change_snake_dir(Direction.Down);
break;
case "ArrowLeft":
world.change_snake_dir(Direction.Left);
break;
}
})
function drawWorld() {
ctx.beginPath();
for (let x = 0; x < worldWidth + 1; x++) {
ctx.moveTo(CELL_SIZE * x, 0);
ctx.lineTo(CELL_SIZE * x, worldWidth * CELL_SIZE)
}
for (let y = 0; y < worldWidth + 1; y++) {
ctx.moveTo(0, CELL_SIZE * y);
ctx.lineTo(worldWidth * CELL_SIZE, CELL_SIZE * y)
}
ctx.stroke();
}
function drawReward() {
const idx = world.reward_cell();
const col = idx % worldWidth;
const row = Math.floor(idx / worldWidth);
ctx.beginPath();
ctx.fillStyle = "#FF0000";
ctx.fillRect(
col * CELL_SIZE,
row * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
);
ctx.stroke();
}
function drawSnake() {
const snakeCells = new Uint32Array(
wasm.memory.buffer,
world.snake_cells(),
world.snake_length()
)
snakeCells
.slice()
.reverse()
.forEach((cellIdx, i) => {
const col = cellIdx % worldWidth;
const row = Math.floor(cellIdx / worldWidth);
ctx.fillStyle = i === snakeCells.length - 1 ? "#7878db" : "#000000";
ctx.beginPath();
ctx.fillRect(
col * CELL_SIZE,
row * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
);
})
ctx.stroke();
}
function drawGameStatus() {
gameStatus.textContent = world.game_status_text();
points.textContent = world.points().toString();
}
function paint() {
drawWorld();
drawSnake();
drawReward();
drawGameStatus();
}
function play() {
const status = world.game_status();
if (status == GameStatus.Won || status == GameStatus.Lost) {
gameControlBtn.textContent = "Re-Play";
return;
}
const fps = 3;
setTimeout(() => {
console.log("Playing!");
ctx.clearRect(0, 0, canvas.width, canvas.height);
world.step();
paint();
// the method takes a callback to invoked before the next repaint
requestAnimationFrame(play)
}, 1000 / fps)
}
paint();
})