-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
165 lines (143 loc) · 4.31 KB
/
index.ts
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
import init, { World, Direction, GameStatus } from "jxlust-snake-game";
import { rng } from "./utils/rng";
init().then((wasm) => {
const INTERVAL_TIME = 1000 / 4;
const WORLD_WIDTH = 16;
const CELL_SIZE = 20; //cell size 10
// const snakeSpawnIndex = Date.now() % (WORLD_WIDTH * WORLD_WIDTH);
const snakeSpawnIndex = rng(WORLD_WIDTH * WORLD_WIDTH);
const world = World.new(WORLD_WIDTH, snakeSpawnIndex);
const worldWidth = world.width();
const playIdDom = document.getElementById("playId");
const statusIdDom = document.getElementById("statusId");
const pointsIdDom = document.getElementById("pointsId");
let rafId: number;
const canvas = <HTMLCanvasElement>document.getElementById("snake-canvas");
canvas.width = CELL_SIZE * worldWidth;
canvas.height = CELL_SIZE * worldWidth;
const ctx = canvas.getContext("2d");
// debugger;
document.addEventListener("keydown", (e) => {
// console.log(e.code);
switch (e.code) {
case "ArrowUp":
world.change_snake_dir(Direction.Up);
break;
case "ArrowDown":
world.change_snake_dir(Direction.Down);
break;
case "ArrowRight":
world.change_snake_dir(Direction.Right);
break;
case "ArrowLeft":
world.change_snake_dir(Direction.Left);
break;
}
});
playIdDom.addEventListener("click", (_) => {
let gameStatus = world.game_status();
if (gameStatus === undefined) {
world.start_game();
loopStart();
playIdDom.innerText = "Playing";
} else {
window.location.reload();
}
});
function drawWorld() {
ctx.beginPath();
// ctx.strokeStyle = ["blue","red","yellow"].at(colorIndex);
ctx.strokeStyle = "skyblue";
ctx.lineWidth = 1;
for (let x = 0; x < worldWidth + 1; x++) {
ctx.moveTo(x * CELL_SIZE, 0);
ctx.lineTo(x * CELL_SIZE, CELL_SIZE * worldWidth);
}
for (let y = 0; y < worldWidth + 1; y++) {
ctx.moveTo(0, y * CELL_SIZE);
ctx.lineTo(CELL_SIZE * worldWidth, CELL_SIZE * y);
}
ctx.stroke();
}
function drawSnake() {
const snakeCellsPtr = world.snake_cells();
const snakeLength = world.snake_length();
const snakeCells = new Int32Array(
wasm.memory.buffer,
snakeCellsPtr,
snakeLength
); //usize = 4 bytes = 4 * 8
// debugger;
// const snakeIndex = world.snake_header();
//way1: filter
//way2: slice + reverse
snakeCells
// .filter((idx, i) => !(i > 0 && idx === snakeCells[0]))
.slice()
.reverse() //i === snakeCells.length - 1
.forEach((cellIdx, i) => {
let color = i === snakeCells.length - 1 ? "#a8f8f8" : "#000000";
ctxFillCell(cellIdx, color);
});
//way3: fill header again
// let headerIndex = snakeCells[0];
// ctxFillCell(headerIndex, "#a8f8f8");
world.step();
}
function ctxFillCell(index: number, color?: string) {
const row = (index / worldWidth) | 0;
const col = index % worldWidth;
ctx.beginPath();
color && (ctx.fillStyle = color);
ctx.fillRect(col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE);
ctx.stroke();
}
function drawFoodCell() {
let foodIndex = world.food_cell();
if (foodIndex >= 0) {
ctxFillCell(foodIndex, "#ff0000");
}
}
function drawStatusText() {
if (isGameEnd()) {
playIdDom.textContent = "Re-Play";
}
statusIdDom.textContent = world.game_status_text();
pointsIdDom.textContent = world.points().toString();
}
function isGameEnd() {
return (
world.game_status() === GameStatus.Won ||
world.game_status() === GameStatus.Lost
);
}
// debugger;
function updatedView() {
console.log("update view");
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawWorld();
drawSnake();
drawFoodCell();
drawStatusText();
}
let startTime: number;
function loopCallback(timestamp: number) {
if (!startTime) {
startTime = timestamp;
}
let diffTime = timestamp - startTime;
if (isGameEnd()) {
window.cancelAnimationFrame(rafId);
} else {
if (diffTime >= INTERVAL_TIME || diffTime === 0) {
startTime = timestamp;
updatedView();
}
rafId = window.requestAnimationFrame(loopCallback);
}
}
function loopStart() {
window.requestAnimationFrame(loopCallback);
}
updatedView();
});