forked from tsoding/multiplayer-game-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.mts
199 lines (187 loc) · 8.75 KB
/
client.mts
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
import * as common from './common.mjs'
import type {Player} from './common.mjs';
const DIRECTION_KEYS: {[key: string]: common.Direction} = {
'ArrowLeft' : common.Direction.Left,
'ArrowRight' : common.Direction.Right,
'ArrowUp' : common.Direction.Up,
'ArrowDown' : common.Direction.Down,
'KeyA' : common.Direction.Left,
'KeyD' : common.Direction.Right,
'KeyS' : common.Direction.Down,
'KeyW' : common.Direction.Up,
};
(async () => {
const gameCanvas = document.getElementById('game') as HTMLCanvasElement | null;
if (gameCanvas === null) throw new Error('No element with id `game`');
gameCanvas.width = common.WORLD_WIDTH;
gameCanvas.height = common.WORLD_HEIGHT;
const ctx = gameCanvas.getContext("2d");
if (ctx === null) throw new Error('2d canvas is not supported');
let ws: WebSocket | undefined = new WebSocket(`ws://${window.location.hostname}:${common.SERVER_PORT}`);
let me: Player | undefined = undefined;
const players = new Map<number, Player>();
let ping = 0;
ws.binaryType = 'arraybuffer';
ws.addEventListener("close", (event) => {
console.log("WEBSOCKET CLOSE", event)
ws = undefined
});
ws.addEventListener("error", (event) => {
// TODO: reconnect on errors
console.log("WEBSOCKET ERROR", event)
});
ws.addEventListener("message", (event) => {
// console.log('Received message', event);
if (!(event.data instanceof ArrayBuffer)) {
console.error("Received bogus-amogus message from server. Expected binary data", event);
ws?.close();
}
const view = new DataView(event.data);
if (me === undefined) {
if (common.HelloStruct.verify(view)) {
me = {
id: common.HelloStruct.id.read(view),
x: common.HelloStruct.x.read(view),
y: common.HelloStruct.y.read(view),
moving: 0,
hue: common.HelloStruct.hue.read(view)/256*360,
}
players.set(me.id, me)
} else {
console.error("Received bogus-amogus message from server. Incorrect `Hello` message.", view)
ws?.close();
}
} else {
if (common.PlayersJoinedHeaderStruct.verify(view)) {
const count = common.PlayersJoinedHeaderStruct.count(view);
for (let i = 0; i < count; ++i) {
const playerView = new DataView(event.data, common.PlayersJoinedHeaderStruct.size + i*common.PlayerStruct.size, common.PlayerStruct.size);
const id = common.PlayerStruct.id.read(playerView);
const player = players.get(id);
if (player !== undefined) {
player.x = common.PlayerStruct.x.read(playerView);
player.y = common.PlayerStruct.y.read(playerView);
player.moving = common.PlayerStruct.moving.read(playerView);
player.hue = common.PlayerStruct.hue.read(playerView)/256*360;
} else {
players.set(id, {
id,
x: common.PlayerStruct.x.read(playerView),
y: common.PlayerStruct.y.read(playerView),
moving: common.PlayerStruct.moving.read(playerView),
hue: common.PlayerStruct.hue.read(playerView)/256*360,
});
}
}
} else if (common.PlayersLeftHeaderStruct.verify(view)) {
const count = common.PlayersLeftHeaderStruct.count(view);
for (let i = 0; i < count; ++i) {
const id = common.PlayersLeftHeaderStruct.items(i).id.read(view);
players.delete(id);
}
} else if (common.PlayersMovingHeaderStruct.verify(view)) {
const count = common.PlayersMovingHeaderStruct.count(view);
for (let i = 0; i < count; ++i) {
const playerView = new DataView(event.data, common.PlayersMovingHeaderStruct.size + i*common.PlayerStruct.size, common.PlayerStruct.size);
const id = common.PlayerStruct.id.read(playerView);
const player = players.get(id);
if (player === undefined) {
console.error(`Received bogus-amogus message from server. We don't know anything about player with id ${id}`)
ws?.close();
return;
}
player.moving = common.PlayerStruct.moving.read(playerView);
player.x = common.PlayerStruct.x.read(playerView);
player.y = common.PlayerStruct.y.read(playerView);
}
} else if (common.PongStruct.verify(view)) {
ping = performance.now() - common.PongStruct.timestamp.read(view);
} else {
console.error("Received bogus-amogus message from server.", view)
ws?.close();
}
}
});
ws.addEventListener("open", (event) => {
console.log("WEBSOCKET OPEN", event)
});
const PING_COOLDOWN = 60;
let previousTimestamp = 0;
let pingCooldown = PING_COOLDOWN;
const frame = (timestamp: number) => {
const deltaTime = (timestamp - previousTimestamp)/1000;
previousTimestamp = timestamp;
ctx.fillStyle = '#202020';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
if (ws === undefined) {
const label = "Disconnected";
const size = ctx.measureText(label);
ctx.font = "48px bold";
ctx.fillStyle = 'white';
ctx.fillText(label, ctx.canvas.width/2 - size.width/2, ctx.canvas.height/2);
} else {
players.forEach((player) => {
if (me !== undefined && me.id !== player.id) {
common.updatePlayer(player, deltaTime);
ctx.fillStyle = `hsl(${player.hue} 70% 40%)`;
ctx.fillRect(player.x, player.y, common.PLAYER_SIZE, common.PLAYER_SIZE);
}
})
if (me !== undefined) {
common.updatePlayer(me, deltaTime);
ctx.fillStyle = `hsl(${me.hue} 100% 40%)`;
ctx.fillRect(me.x, me.y, common.PLAYER_SIZE, common.PLAYER_SIZE);
ctx.strokeStyle = "white";
ctx.lineWidth = 4;
ctx.beginPath()
ctx.strokeRect(me.x, me.y, common.PLAYER_SIZE, common.PLAYER_SIZE);
ctx.stroke();
}
ctx.font = "18px bold";
ctx.fillStyle = 'white';
const padding = ctx.canvas.width*0.05;
ctx.fillText(`Ping: ${ping.toFixed(2)}ms`, padding, padding);
pingCooldown -= 1;
if (pingCooldown <= 0) {
const view = new DataView(new ArrayBuffer(common.PingStruct.size));
common.PingStruct.kind.write(view, common.MessageKind.Ping);
common.PingStruct.timestamp.write(view, performance.now());
ws.send(view);
pingCooldown = PING_COOLDOWN;
}
}
window.requestAnimationFrame(frame);
}
window.requestAnimationFrame((timestamp) => {
previousTimestamp = timestamp;
window.requestAnimationFrame(frame);
});
window.addEventListener("keydown", (e) => {
if (ws !== undefined && me !== undefined) {
if (!e.repeat) {
const direction = DIRECTION_KEYS[e.code];
if (direction !== undefined) {
const view = new DataView(new ArrayBuffer(common.AmmaMovingStruct.size));
common.AmmaMovingStruct.kind.write(view, common.MessageKind.AmmaMoving);
common.AmmaMovingStruct.start.write(view, 1);
common.AmmaMovingStruct.direction.write(view, direction);
ws.send(view);
}
}
}
});
window.addEventListener("keyup", (e) => {
if (ws !== undefined && me !== undefined) {
if (!e.repeat) {
const direction = DIRECTION_KEYS[e.code];
if (direction !== undefined) {
const view = new DataView(new ArrayBuffer(common.AmmaMovingStruct.size));
common.AmmaMovingStruct.kind.write(view, common.MessageKind.AmmaMoving);
common.AmmaMovingStruct.start.write(view, 0);
common.AmmaMovingStruct.direction.write(view, direction);
ws.send(view);
}
}
}
});
})()