Spaces:
Sleeping
Sleeping
Update static/scripts.js
Browse files- static/scripts.js +36 -23
static/scripts.js
CHANGED
@@ -3,21 +3,20 @@ const canvas = document.getElementById('gameCanvas');
|
|
3 |
const ctx = canvas.getContext('2d');
|
4 |
const tileSize = 32;
|
5 |
|
6 |
-
//
|
7 |
-
const
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
const spriteMap = {
|
12 |
-
'w': [0, 0], // Wall
|
13 |
-
'f': [32, 0], // Floor
|
14 |
-
'player': [64, 0], // Player
|
15 |
-
'goblin': [0, 32], // Goblin
|
16 |
-
'skeleton': [0, 32], // Reuse goblin sprite for simplicity (update later if needed)
|
17 |
-
'potion': [32, 32], // Potion
|
18 |
-
'sword': [64, 32], // Sword
|
19 |
-
'door': [64, 32] // Reuse sword sprite for door (update later)
|
20 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
socket.on('update_game', (state) => {
|
23 |
drawGame(state);
|
@@ -42,16 +41,20 @@ function drawGame(state) {
|
|
42 |
// Draw visible 10x10 area centered on player
|
43 |
for (let y = 0; y < state.terrain.length; y++) {
|
44 |
for (let x = 0; x < state.terrain[y].length; x++) {
|
45 |
-
const
|
46 |
-
|
|
|
|
|
47 |
}
|
48 |
}
|
49 |
// Draw entities on top
|
50 |
for (let y = 0; y < state.entities.length; y++) {
|
51 |
for (let x = 0; x < state.entities[y].length; x++) {
|
52 |
-
if (state.entities[y][x] &&
|
53 |
-
const
|
54 |
-
|
|
|
|
|
55 |
}
|
56 |
}
|
57 |
}
|
@@ -68,7 +71,17 @@ document.getElementById('usePotion').addEventListener('click', () => {
|
|
68 |
socket.emit('use_item', 'potion');
|
69 |
});
|
70 |
|
71 |
-
// Ensure
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
const ctx = canvas.getContext('2d');
|
4 |
const tileSize = 32;
|
5 |
|
6 |
+
// Preload individual images
|
7 |
+
const images = {
|
8 |
+
'w': new Image(), 'f': new Image(), 'player': new Image(),
|
9 |
+
'goblin': new Image(), 'skeleton': new Image(), 'potion': new Image(),
|
10 |
+
'sword': new Image(), 'door': new Image()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
};
|
12 |
+
images['w'].src = '/static/images/wall.png';
|
13 |
+
images['f'].src = '/static/images/floor.png';
|
14 |
+
images['player'].src = '/static/images/player.png';
|
15 |
+
images['goblin'].src = '/static/images/goblin.png';
|
16 |
+
images['skeleton'].src = '/static/images/goblin.png'; // Reuse goblin for skeleton (update if needed)
|
17 |
+
images['potion'].src = '/static/images/potion.png';
|
18 |
+
images['sword'].src = '/static/images/sword.png';
|
19 |
+
images['door'].src = '/static/images/sword.png'; // Reuse sword for door (update if needed)
|
20 |
|
21 |
socket.on('update_game', (state) => {
|
22 |
drawGame(state);
|
|
|
41 |
// Draw visible 10x10 area centered on player
|
42 |
for (let y = 0; y < state.terrain.length; y++) {
|
43 |
for (let x = 0; x < state.terrain[y].length; x++) {
|
44 |
+
const img = images[state.terrain[y][x] || 'f']; // Default to floor if empty
|
45 |
+
if (img.complete) {
|
46 |
+
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
47 |
+
}
|
48 |
}
|
49 |
}
|
50 |
// Draw entities on top
|
51 |
for (let y = 0; y < state.entities.length; y++) {
|
52 |
for (let x = 0; x < state.entities[y].length; x++) {
|
53 |
+
if (state.entities[y][x] && images[state.entities[y][x]]) {
|
54 |
+
const img = images[state.entities[y][x]];
|
55 |
+
if (img.complete) {
|
56 |
+
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
57 |
+
}
|
58 |
}
|
59 |
}
|
60 |
}
|
|
|
71 |
socket.emit('use_item', 'potion');
|
72 |
});
|
73 |
|
74 |
+
// Ensure all images are loaded before drawing
|
75 |
+
let imagesLoaded = 0;
|
76 |
+
const totalImages = Object.keys(images).length;
|
77 |
+
for (let key in images) {
|
78 |
+
images[key].onload = () => {
|
79 |
+
imagesLoaded++;
|
80 |
+
if (imagesLoaded === totalImages) {
|
81 |
+
console.log('All images loaded');
|
82 |
+
}
|
83 |
+
};
|
84 |
+
images[key].onerror = () => {
|
85 |
+
console.error(`Failed to load image: ${images[key].src}`);
|
86 |
+
};
|
87 |
+
}
|