-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain3.ts
320 lines (285 loc) · 7.88 KB
/
main3.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { emitter } from "@/composables/useEmit";
import type { User } from "@/types/general";
import type { CanvasAppOptions, Camera } from "../types";
import { loadImage, isColliding } from "./utilities";
import { drawPlayer } from "./draw";
import { updateAnimationFrame } from "./animations";
import { keyDownEventListener, keyUpEventListener } from "./keyboardEvents";
import { rightClickEventListener, wheelEventListener } from "./mouseEvents";
export async function createCanvasApp(options: CanvasAppOptions) {
const {
users,
myPlayerId,
speed,
canvas,
canvasFrameRate,
spaceMap,
myCharacterSprite,
initialSetupCompleted,
} = options;
const ctx = canvas.getContext("2d")!;
const [worldImg, characterImg, characterImgMyPlayer] = await loadImages(
spaceMap,
myCharacterSprite
);
const camera = createCamera();
let myPlayer = createMyPlayer();
const keyPressData = {
keyPressOrder: [] as string[],
pressedKeys: { w: false, a: false, s: false, d: false },
};
let animationState = "walk-down";
let animationFrame = 0;
let animationTick = 0;
let lastTime = performance.now();
let fps = 0;
const refreshInterval = 1000 / canvasFrameRate;
const userAgent = navigator.userAgent;
let mouseX = 0;
let mouseY = 0;
canvas.addEventListener("mousemove", updateMousePosition);
function updateMousePosition(e: MouseEvent) {
mouseX = e.clientX;
mouseY = e.clientY;
}
if (isInitialSetupValid()) {
startGameLoop();
} else {
waitForInitialSetup(startGameLoop);
}
const getCamera = () => {
return {
cameraX: myPlayer?.x - canvas.width / (2.2 * camera.zoomFactor),
cameraY: myPlayer.y - canvas.height / (2.2 * camera.zoomFactor),
zoomFactor: camera.zoomFactor,
};
};
// LISTENERS
keyDownEventListener(
canvas,
keyPressData.pressedKeys,
keyPressData.keyPressOrder,
() => myPlayer
);
keyUpEventListener(
canvas,
keyPressData.pressedKeys,
keyPressData.keyPressOrder,
() => myPlayer
);
rightClickEventListener(canvas, getCamera, users, myPlayerId);
wheelEventListener(canvas, camera.zoomFactor);
// Get right click move position confirmation
emitter.on("rightClickPlayerMoveConfirmed", async (user) => {
myPlayer.x = user.x - 36;
myPlayer.y = user.y - 64;
// Canvas loses focus after right click, so we need to focus it again
canvas.focus();
});
function loadImages(spaceMap: string, myCharacterSprite: string) {
return Promise.all([
loadImage(spaceMap),
loadImage(myCharacterSprite),
loadImage(myCharacterSprite),
]);
}
function createCamera(): Camera {
return {
cameraX: 200,
cameraY: 200,
zoomFactor: 1,
};
}
function createMyPlayer(): User {
return {
id: "",
userName: "",
x: 0,
y: 0,
characterSprite: "",
facingTo: "",
userStatus: "",
};
}
function isInitialSetupValid() {
return (
users.length > 0 &&
myPlayerId !== "" &&
speed !== 0 &&
spaceMap !== "" &&
initialSetupCompleted
);
}
function startGameLoop() {
gameLoop();
}
function waitForInitialSetup(callback: () => void) {
const checkConditionsBeforeLoop = setInterval(() => {
if (isInitialSetupValid()) {
callback();
clearInterval(checkConditionsBeforeLoop);
// Emit event to notify that the canvas is loaded
emitter.emit("canvasLoaded");
}
}, 0);
}
// Remove this line:
// waitForInitialSetup(gameLoop);
function gameLoop() {
clearCanvas();
myPlayer = users.find((user) => user.id === myPlayerId)!;
if (myPlayer) {
updateCamera();
drawWorld();
handlePlayerMovement();
drawOtherPlayers();
drawMyPlayer();
updateAnimation();
drawFPS();
requestNextFrame();
}
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function updateCamera() {
camera.cameraX = myPlayer.x - canvas.width / (2.2 * camera.zoomFactor);
camera.cameraY = myPlayer.y - canvas.height / (2.2 * camera.zoomFactor);
}
function drawWorld() {
ctx.drawImage(
worldImg,
0,
0,
worldImg.width,
worldImg.height,
(-camera.cameraX - worldImg.height / 2) * camera.zoomFactor,
(-camera.cameraY - worldImg.width / 2) * camera.zoomFactor,
worldImg.width * camera.zoomFactor,
worldImg.height * camera.zoomFactor
);
}
function handlePlayerMovement() {
if (keyPressData.keyPressOrder.length > 0) {
let newPlayerX = myPlayer.x;
let newPlayerY = myPlayer.y;
const lastValidKey =
keyPressData.keyPressOrder[keyPressData.keyPressOrder.length - 1];
if (
keyPressData.pressedKeys.w ||
keyPressData.pressedKeys.a ||
keyPressData.pressedKeys.s ||
keyPressData.pressedKeys.d
) {
switch (lastValidKey) {
case "w":
newPlayerY -= speed;
animationState = "walk-up";
break;
case "a":
newPlayerX -= speed;
animationState = "walk-left";
break;
case "s":
newPlayerY += speed;
animationState = "walk-down";
break;
case "d":
newPlayerX += speed;
animationState = "walk-right";
break;
}
}
if (!hasCollision(newPlayerX, newPlayerY)) {
myPlayer.x = newPlayerX;
myPlayer.y = newPlayerY;
}
}
}
function hasCollision(newPlayerX: number, newPlayerY: number): boolean {
const playerWidth = 64 * camera.zoomFactor;
const playerHeight = 64 * camera.zoomFactor;
return users.some(
(user) =>
user.id !== myPlayerId &&
isColliding(
{ x: newPlayerX, y: newPlayerY, width: playerWidth, height: playerHeight },
{ x: user.x, y: user.y, width: playerWidth, height: playerHeight }
)
);
}
function drawOtherPlayers() {
users.forEach((user) => {
if (user.id !== myPlayerId) {
const playerRect = getPlayerRect(user);
const isMouseOver = isColliding(
{ x: mouseX, y: mouseY, width: 1, height: 1 },
playerRect
);
drawPlayer(
ctx,
characterImg,
"walk-down",
1,
user,
camera.cameraX,
camera.cameraY,
camera.zoomFactor,
user.userStatus,
isMouseOver
);
}
});
}
function drawMyPlayer() {
const playerRect = getPlayerRect(myPlayer);
const isMouseOver = isColliding(
{ x: mouseX, y: mouseY, width: 1, height: 1 },
playerRect
);
drawPlayer(
ctx,
characterImgMyPlayer,
animationState,
animationFrame,
myPlayer,
camera.cameraX,
camera.cameraY,
camera.zoomFactor,
myPlayer.userStatus,
isMouseOver
);
}
function getPlayerRect(player: User) {
return {
x: (player.x - camera.cameraX - 8) * camera.zoomFactor,
y: (player.y - camera.cameraY - 8) * camera.zoomFactor,
width: 96 * camera.zoomFactor,
height: 96 * camera.zoomFactor,
};
}
function updateAnimation() {
[animationFrame, animationTick] = updateAnimationFrame(
keyPressData.pressedKeys,
animationState,
animationFrame,
animationTick
);
}
function drawFPS() {
const currentTime = performance.now();
const deltaTime = currentTime - lastTime;
lastTime = currentTime;
fps = Math.round(1000 / deltaTime);
ctx.fillStyle = "black";
ctx.font = "bold 16px Poppins";
ctx.fillText(`FPS: ${fps}`, 10, 20);
}
function requestNextFrame() {
const currentTime = performance.now();
const deltaTime = currentTime - lastTime;
const timeToWait = Math.max(0, refreshInterval - deltaTime);
setTimeout(gameLoop, timeToWait);
}
waitForInitialSetup(gameLoop);
}