-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw.ts
168 lines (154 loc) · 3.88 KB
/
draw.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
import { characterAnimations } from "./animations";
import type { User } from "@/types/general";
import { loadImage } from "./utilities";
import shadowImage from "../images/shadow.png";
// Load images
const [shadowSprite] = await Promise.all([loadImage(shadowImage)]);
function drawShadow(
ctx: CanvasRenderingContext2D,
shadowX: number,
shadowY: number,
zoomFactor: number,
isMouseOver: boolean
): void {
// Draw shadow using shadow sprite
let shadowSize = 48;
ctx.drawImage(
shadowSprite,
shadowX + 8,
shadowY + 24,
shadowSize * zoomFactor,
shadowSize * zoomFactor
);
if (isMouseOver) {
// Make shadow 4px bigger when mouse is over
shadowSize = 52;
}
}
function drawCharacter(
ctx: CanvasRenderingContext2D,
characterImg: HTMLImageElement,
animation: string,
frame: number,
shadowX: number,
shadowY: number,
zoomFactor: number
): void {
const frameCoords = characterAnimations[animation][frame];
ctx.drawImage(
characterImg,
frameCoords[0],
frameCoords[1],
16,
16,
shadowX,
shadowY,
64 * zoomFactor,
64 * zoomFactor
);
}
// Draw player name
function drawPlayerNameBackground(
ctx: CanvasRenderingContext2D,
player: User,
zoomFactor: number,
backgroundX: number,
backgroundY: number
): void {
ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
ctx.font = `${14 * zoomFactor}px Poppins`;
const userNameTextWidth = ctx.measureText(player.userName).width;
const padding = 10 * zoomFactor;
const backgroundHeight = 20 * zoomFactor;
const backgroundWidth = userNameTextWidth + padding * 3;
ctx.beginPath();
// Left half circle
ctx.arc(
backgroundX + backgroundHeight / 2,
backgroundY + backgroundHeight / 2,
backgroundHeight / 2,
Math.PI / 2,
(3 * Math.PI) / 2
);
// Right half circle
ctx.arc(
backgroundX + backgroundWidth - backgroundHeight / 2,
backgroundY + backgroundHeight / 2,
backgroundHeight / 2,
(3 * Math.PI) / 2,
Math.PI / 2
);
ctx.closePath();
ctx.fill();
}
// Draw status icon
function drawUserStatusIcon(
ctx: CanvasRenderingContext2D,
userStatus: string,
zoomFactor: number,
statusX: number,
statusY: number
): void {
const statusRadius = 5 * zoomFactor;
switch (userStatus) {
case "online":
ctx.fillStyle = "#2CC56F";
break;
case "busy":
ctx.fillStyle = "orange";
break;
case "away":
ctx.fillStyle = "gray";
break;
default:
ctx.fillStyle = "gray";
}
ctx.beginPath();
ctx.arc(statusX, statusY, statusRadius, 0, 2 * Math.PI);
ctx.fill();
}
function drawPlayerName(
ctx: CanvasRenderingContext2D,
player: User,
zoomFactor: number,
backgroundX: number,
backgroundY: number,
padding: number
): void {
ctx.fillStyle = "white";
ctx.fillText(
player.userName,
backgroundX + 10 + padding,
backgroundY * zoomFactor + 15
);
}
export function drawPlayer(
ctx: CanvasRenderingContext2D,
characterImg: HTMLImageElement,
animation: string,
frame: number,
player: User,
cameraX: number,
cameraY: number,
zoomFactor: number,
userStatus: string,
isMouseOver: boolean
): void {
const shadowX = (player.x - cameraX - 4) * zoomFactor;
const shadowY = (player.y - cameraY - 4) * zoomFactor;
drawShadow(ctx, shadowX, shadowY, zoomFactor, isMouseOver);
drawCharacter(ctx, characterImg, animation, frame, shadowX, shadowY, zoomFactor);
const backgroundX =
(player.x - cameraX - ctx.measureText(player.userName).width / 16) * zoomFactor;
const backgroundY = (player.y - cameraY - characterImg.height / 4) * zoomFactor;
drawPlayerNameBackground(ctx, player, zoomFactor, backgroundX, backgroundY);
const padding = 10 * zoomFactor;
drawUserStatusIcon(
ctx,
userStatus,
zoomFactor,
backgroundX + padding,
backgroundY + (20 * zoomFactor) / 2
);
drawPlayerName(ctx, player, zoomFactor, backgroundX, backgroundY, padding);
}