-
Notifications
You must be signed in to change notification settings - Fork 9
/
common.mts
238 lines (216 loc) · 7.33 KB
/
common.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
export const SERVER_PORT = 6970;
export const WORLD_FACTOR = 200;
export const WORLD_WIDTH = 4*WORLD_FACTOR;
export const WORLD_HEIGHT = 3*WORLD_FACTOR;
export const PLAYER_SIZE = 30;
export const PLAYER_SPEED = 500;
export enum Direction {
Left = 0,
Right,
Up,
Down,
Count,
}
export type Vector2 = {x: number, y: number};
export const DIRECTION_VECTORS: Vector2[] = (() => {
console.assert(Direction.Count == 4, "The definition of Direction have changed");
const vectors = Array(Direction.Count);
vectors[Direction.Left] = {x: -1, y: 0};
vectors[Direction.Right] = {x: 1, y: 0};
vectors[Direction.Up] = {x: 0, y: -1};
vectors[Direction.Down] = {x: 0, y: 1};
return vectors;
})()
export interface Player {
id: number,
x: number,
y: number,
moving: number,
hue: number,
}
export enum MessageKind {
Hello,
PlayerJoined,
PlayerLeft,
PlayerMoving,
AmmaMoving,
Ping,
Pong,
}
interface Field {
offset: number,
size: number,
read(view: DataView): number;
write(view: DataView, value: number): void;
}
export const UINT8_SIZE = 1;
export const UINT16_SIZE = 2;
export const UINT32_SIZE = 4;
export const FLOAT32_SIZE = 4;
function allocUint8Field(allocator: { size: number }): Field {
const offset = allocator.size;
const size = UINT8_SIZE;
allocator.size += size;
return {
offset,
size,
read: (view) => view.getUint8(offset),
write: (view, value) => view.setUint8(offset, value)
}
}
function allocUint16Field(allocator: { size: number }): Field {
const offset = allocator.size;
const size = UINT16_SIZE;
allocator.size += size;
return {
offset,
size,
read: (view) => view.getUint16(offset),
write: (view, value) => view.setUint16(offset, value)
}
}
function allocUint32Field(allocator: { size: number }): Field {
const offset = allocator.size;
const size = UINT32_SIZE;
allocator.size += size;
return {
offset,
size,
read: (view) => view.getUint32(offset, true),
write: (view, value) => view.setUint32(offset, value, true)
}
}
function allocFloat32Field(allocator: { size: number }): Field {
const offset = allocator.size;
const size = FLOAT32_SIZE;
allocator.size += size;
return {
offset,
size,
read: (view) => view.getFloat32(offset, true),
write: (view, value) => view.setFloat32(offset, value, true)
}
}
function verifier(kindField: Field, kind: number, size: number): (view: DataView) => boolean {
return (view) =>
view.byteLength == size &&
kindField.read(view) == kind
}
export const PingStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const timestamp = allocUint32Field(allocator);
const size = allocator.size;
const verify = verifier(kind, MessageKind.Ping, size);
return {kind, timestamp, size, verify}
})();
export const PongStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const timestamp = allocUint32Field(allocator);
const size = allocator.size;
const verify = verifier(kind, MessageKind.Pong, size);
return {kind, timestamp, size, verify}
})();
export const HelloStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const id = allocUint32Field(allocator);
const x = allocFloat32Field(allocator);
const y = allocFloat32Field(allocator);
const hue = allocUint8Field(allocator);
const size = allocator.size;
const verify = verifier(kind, MessageKind.Hello, size);
return {kind, id, x, y, hue, size, verify}
})();
export const AmmaMovingStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const direction = allocUint8Field(allocator);
const start = allocUint8Field(allocator);
const size = allocator.size;
const verify = verifier(kind, MessageKind.AmmaMoving, size);
return {kind, direction, start, size, verify}
})();
// [kind] [count] [id] [x] [y] [moving] [id] [x] [y] [moving] [id] [x] [y] [moving]
// ^
export const PlayerStruct = (() => {
const allocator = { size: 0 };
const id = allocUint32Field(allocator);
const x = allocFloat32Field(allocator);
const y = allocFloat32Field(allocator);
const hue = allocUint8Field(allocator);
const moving = allocUint8Field(allocator);
const size = allocator.size;
return {id, x, y, hue, moving, size};
})();
export const PlayersJoinedHeaderStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const size = allocator.size;
const itemSize = PlayerStruct.size;
const verify = (view: DataView) =>
view.byteLength >= size &&
(view.byteLength - size)%itemSize === 0 &&
kind.read(view) == MessageKind.PlayerJoined;
const count = (view: DataView) => (view.byteLength - size)/itemSize
return {kind, count, size, verify};
})();
export const PlayersMovingHeaderStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const size = allocator.size;
const itemSize = PlayerStruct.size;
const verify = (view: DataView) =>
view.byteLength >= size &&
(view.byteLength - size)%itemSize === 0 &&
kind.read(view) == MessageKind.PlayerMoving;
const count = (view: DataView) => (view.byteLength - size)/itemSize;
return {kind, count, size, verify};
})();
export const PlayersLeftHeaderStruct = (() => {
const allocator = { size: 0 };
const kind = allocUint8Field(allocator);
const headerSize = allocator.size;
const itemSize = UINT32_SIZE;
const items = (index: number) => {
return {
id: {
read: (view: DataView): number => view.getUint32(headerSize + index*itemSize, true),
write: (view: DataView, value: number): void => view.setUint32(headerSize + index*itemSize, value, true)
}
}
}
const verify = (view: DataView) =>
view.byteLength >= headerSize &&
(view.byteLength - headerSize)%itemSize === 0 &&
kind.read(view) === MessageKind.PlayerLeft;
const allocateAndInit = (countItems: number): DataView => {
const buffer = new ArrayBuffer(headerSize + itemSize*countItems);
const view = new DataView(buffer);
kind.write(view, MessageKind.PlayerLeft);
return view;
}
const count = (view: DataView) => (view.byteLength - headerSize)/itemSize
return {kind, count, items, itemSize, headerSize, verify, allocateAndInit};
})();
function properMod(a: number, b: number): number {
return (a%b + b)%b;
}
export function updatePlayer(player: Player, deltaTime: number) {
let dx = 0;
let dy = 0;
for (let dir = 0; dir < Direction.Count; dir += 1) {
if ((player.moving>>dir)&1) {
dx += DIRECTION_VECTORS[dir].x;
dy += DIRECTION_VECTORS[dir].y;
}
}
const l = dx*dx + dy*dy;
if (l !== 0) {
dx /= l;
dy /= l;
}
player.x = properMod(player.x + dx*PLAYER_SPEED*deltaTime, WORLD_WIDTH);
player.y = properMod(player.y + dy*PLAYER_SPEED*deltaTime, WORLD_HEIGHT);
}