Spaces:
Sleeping
Sleeping
Update static/scripts.js
Browse files- static/scripts.js +11 -80
static/scripts.js
CHANGED
@@ -1,54 +1,24 @@
|
|
1 |
const socket = io("https://broadfield-dev-dungeon-game.hf.space", {
|
2 |
-
transports: ['websocket'] // Force WebSocket
|
3 |
});
|
4 |
|
5 |
-
socket.on('connect', () =>
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
const canvas = document.getElementById('gameCanvas');
|
10 |
-
const ctx = canvas.getContext('2d');
|
11 |
-
const tileSize = 32;
|
12 |
-
|
13 |
-
const images = {
|
14 |
-
'w': new Image(), 'f': new Image(), 'player': new Image(),
|
15 |
-
'goblin': new Image(), 'skeleton': new Image(), 'potion': new Image(),
|
16 |
-
'sword': new Image(), 'door': new Image()
|
17 |
-
};
|
18 |
-
images['w'].src = '/static/wall.png';
|
19 |
-
images['f'].src = '/static/floor.png';
|
20 |
-
images['player'].src = '/static/player.png';
|
21 |
-
images['goblin'].src = '/static/goblin.png';
|
22 |
-
images['skeleton'].src = '/static/goblin.png';
|
23 |
-
images['potion'].src = '/static/potion.png';
|
24 |
-
images['sword'].src = '/static/sword.png';
|
25 |
-
images['door'].src = '/static/door.png';
|
26 |
-
|
27 |
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
for (let key in images) {
|
33 |
-
images[key].onload = () => {
|
34 |
-
imagesLoaded++;
|
35 |
-
if (imagesLoaded === totalImages) {
|
36 |
-
console.log('All images loaded');
|
37 |
-
allImagesLoaded = true;
|
38 |
-
if (window.currentState) drawGame(window.currentState);
|
39 |
-
}
|
40 |
-
};
|
41 |
-
images[key].onerror = () => console.error(`Failed to load image: ${images[key].src}`);
|
42 |
-
}
|
43 |
|
44 |
socket.on('update_game', (state) => {
|
45 |
console.log('Received game state:', state);
|
46 |
window.currentState = state;
|
47 |
if (allImagesLoaded) drawGame(state);
|
48 |
else console.log('Waiting for images to load...');
|
49 |
-
window.currentState = state;
|
50 |
-
if (allImagesLoaded) drawGame(state);
|
51 |
-
else console.log('Waiting for images to load...');
|
52 |
document.getElementById('health').textContent = state.health;
|
53 |
document.getElementById('attack').textContent = state.attack;
|
54 |
document.getElementById('inventory').textContent = state.inventory.join(', ');
|
@@ -58,47 +28,8 @@ socket.on('update_game', (state) => {
|
|
58 |
});
|
59 |
|
60 |
socket.on('message', (msg) => {
|
|
|
61 |
const messagesDiv = document.getElementById('messages');
|
62 |
messagesDiv.innerHTML += `<p>${msg}</p>`;
|
63 |
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
64 |
-
});
|
65 |
-
|
66 |
-
function drawGame(state) {
|
67 |
-
console.log('Drawing game with state:', state); // Log the entire state
|
68 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
69 |
-
for (let y = 0; y < state.terrain.length; y++) {
|
70 |
-
for (let x = 0; x < state.terrain[y].length; x++) {
|
71 |
-
const terrainType = state.terrain[y][x] || 'f';
|
72 |
-
console.log(`Terrain at (${x},${y}): ${terrainType}`);
|
73 |
-
const img = images[terrainType];
|
74 |
-
if (img && img.complete) {
|
75 |
-
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
76 |
-
} else {
|
77 |
-
console.warn(`Image not ready for ${terrainType}`);
|
78 |
-
}
|
79 |
-
}
|
80 |
-
}
|
81 |
-
for (let y = 0; y < state.entities.length; y++) {
|
82 |
-
for (let x = 0; x < state.entities[y].length; x++) {
|
83 |
-
const entityType = state.entities[y][x];
|
84 |
-
if (entityType) {
|
85 |
-
console.log(`Entity at (${x},${y}): ${entityType}`);
|
86 |
-
const img = images[entityType];
|
87 |
-
if (img && img.complete) {
|
88 |
-
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
89 |
-
} else {
|
90 |
-
console.warn(`Image not ready for ${entityType}`);
|
91 |
-
}
|
92 |
-
}
|
93 |
-
}
|
94 |
-
}
|
95 |
-
}
|
96 |
-
|
97 |
-
document.addEventListener('keydown', (e) => {
|
98 |
-
const directions = { 'ArrowUp': 'up', 'ArrowDown': 'down', 'ArrowLeft': 'left', 'ArrowRight': 'right' };
|
99 |
-
if (directions[e.key]) socket.emit('move', directions[e.key]);
|
100 |
-
});
|
101 |
-
|
102 |
-
document.getElementById('usePotion').addEventListener('click', () => {
|
103 |
-
socket.emit('use_item', 'potion');
|
104 |
});
|
|
|
1 |
const socket = io("https://broadfield-dev-dungeon-game.hf.space", {
|
2 |
+
transports: ['websocket'] // Force WebSocket transport
|
3 |
});
|
4 |
|
5 |
+
socket.on('connect', () => {
|
6 |
+
console.log('Connected to the server! Socket ID:', socket.id);
|
7 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
socket.on('connect_error', (error) => {
|
10 |
+
console.error('Connection error:', error);
|
11 |
+
});
|
12 |
|
13 |
+
socket.on('disconnect', (reason) => {
|
14 |
+
console.log('Disconnected from the server. Reason:', reason);
|
15 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
socket.on('update_game', (state) => {
|
18 |
console.log('Received game state:', state);
|
19 |
window.currentState = state;
|
20 |
if (allImagesLoaded) drawGame(state);
|
21 |
else console.log('Waiting for images to load...');
|
|
|
|
|
|
|
22 |
document.getElementById('health').textContent = state.health;
|
23 |
document.getElementById('attack').textContent = state.attack;
|
24 |
document.getElementById('inventory').textContent = state.inventory.join(', ');
|
|
|
28 |
});
|
29 |
|
30 |
socket.on('message', (msg) => {
|
31 |
+
console.log('Received message:', msg); // Log messages to ensure they’re working
|
32 |
const messagesDiv = document.getElementById('messages');
|
33 |
messagesDiv.innerHTML += `<p>${msg}</p>`;
|
34 |
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
});
|