Spaces:
Sleeping
Sleeping
Update static/scripts.js
Browse files- static/scripts.js +30 -43
static/scripts.js
CHANGED
@@ -1,22 +1,13 @@
|
|
1 |
-
const socket = io("
|
2 |
|
3 |
-
socket.on('connect', () =>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
socket.on('connect_error', (error) => {
|
8 |
-
console.error('Connection error:', error);
|
9 |
-
});
|
10 |
-
|
11 |
-
socket.on('disconnect', () => {
|
12 |
-
console.log('Disconnected from the server.');
|
13 |
-
});
|
14 |
|
15 |
const canvas = document.getElementById('gameCanvas');
|
16 |
const ctx = canvas.getContext('2d');
|
17 |
const tileSize = 32;
|
18 |
|
19 |
-
// Preload individual images
|
20 |
const images = {
|
21 |
'w': new Image(), 'f': new Image(), 'player': new Image(),
|
22 |
'goblin': new Image(), 'skeleton': new Image(), 'potion': new Image(),
|
@@ -26,21 +17,36 @@ images['w'].src = '/static/wall.png';
|
|
26 |
images['f'].src = '/static/floor.png';
|
27 |
images['player'].src = '/static/player.png';
|
28 |
images['goblin'].src = '/static/goblin.png';
|
29 |
-
images['skeleton'].src = '/static/
|
30 |
images['potion'].src = '/static/potion.png';
|
31 |
images['sword'].src = '/static/sword.png';
|
32 |
-
images['door'].src = '/static/door.png';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
socket.on('update_game', (state) => {
|
35 |
-
|
|
|
|
|
36 |
document.getElementById('health').textContent = state.health;
|
37 |
document.getElementById('attack').textContent = state.attack;
|
38 |
document.getElementById('inventory').textContent = state.inventory.join(', ');
|
39 |
document.getElementById('level').textContent = state.level;
|
40 |
document.getElementById('usePotion').disabled = !state.inventory.includes('potion');
|
41 |
-
if (state.health <= 0)
|
42 |
-
alert('Game Over! Refresh to restart.');
|
43 |
-
}
|
44 |
});
|
45 |
|
46 |
socket.on('message', (msg) => {
|
@@ -51,21 +57,19 @@ socket.on('message', (msg) => {
|
|
51 |
|
52 |
function drawGame(state) {
|
53 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
54 |
-
// Draw visible 10x10 area centered on player
|
55 |
for (let y = 0; y < state.terrain.length; y++) {
|
56 |
for (let x = 0; x < state.terrain[y].length; x++) {
|
57 |
-
const img = images[state.terrain[y][x] || 'f'];
|
58 |
-
if (img.complete) {
|
59 |
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
60 |
}
|
61 |
}
|
62 |
}
|
63 |
-
// Draw entities on top
|
64 |
for (let y = 0; y < state.entities.length; y++) {
|
65 |
for (let x = 0; x < state.entities[y].length; x++) {
|
66 |
if (state.entities[y][x] && images[state.entities[y][x]]) {
|
67 |
const img = images[state.entities[y][x]];
|
68 |
-
if (img.complete) {
|
69 |
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
70 |
}
|
71 |
}
|
@@ -75,26 +79,9 @@ function drawGame(state) {
|
|
75 |
|
76 |
document.addEventListener('keydown', (e) => {
|
77 |
const directions = { 'ArrowUp': 'up', 'ArrowDown': 'down', 'ArrowLeft': 'left', 'ArrowRight': 'right' };
|
78 |
-
if (directions[e.key])
|
79 |
-
socket.emit('move', directions[e.key]);
|
80 |
-
}
|
81 |
});
|
82 |
|
83 |
document.getElementById('usePotion').addEventListener('click', () => {
|
84 |
socket.emit('use_item', 'potion');
|
85 |
-
});
|
86 |
-
|
87 |
-
// Ensure all images are loaded before drawing
|
88 |
-
let imagesLoaded = 0;
|
89 |
-
const totalImages = Object.keys(images).length;
|
90 |
-
for (let key in images) {
|
91 |
-
images[key].onload = () => {
|
92 |
-
imagesLoaded++;
|
93 |
-
if (imagesLoaded === totalImages) {
|
94 |
-
console.log('All images loaded');
|
95 |
-
}
|
96 |
-
};
|
97 |
-
images[key].onerror = () => {
|
98 |
-
console.error(`Failed to load image: ${images[key].src}`);
|
99 |
-
};
|
100 |
-
}
|
|
|
1 |
+
const socket = io("http://broadfield-dev-dungeon-game.hf.space:7860"); // Adjust for deployment
|
2 |
|
3 |
+
socket.on('connect', () => console.log('Connected to the server!'));
|
4 |
+
socket.on('connect_error', (error) => console.error('Connection error:', error));
|
5 |
+
socket.on('disconnect', () => console.log('Disconnected from the server.'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
const canvas = document.getElementById('gameCanvas');
|
8 |
const ctx = canvas.getContext('2d');
|
9 |
const tileSize = 32;
|
10 |
|
|
|
11 |
const images = {
|
12 |
'w': new Image(), 'f': new Image(), 'player': new Image(),
|
13 |
'goblin': new Image(), 'skeleton': new Image(), 'potion': new Image(),
|
|
|
17 |
images['f'].src = '/static/floor.png';
|
18 |
images['player'].src = '/static/player.png';
|
19 |
images['goblin'].src = '/static/goblin.png';
|
20 |
+
images['skeleton'].src = '/static/skeleton.png'; // Update if separate image
|
21 |
images['potion'].src = '/static/potion.png';
|
22 |
images['sword'].src = '/static/sword.png';
|
23 |
+
images['door'].src = '/static/door.png';
|
24 |
+
|
25 |
+
let allImagesLoaded = false;
|
26 |
+
let imagesLoaded = 0;
|
27 |
+
const totalImages = Object.keys(images).length;
|
28 |
+
for (let key in images) {
|
29 |
+
images[key].onload = () => {
|
30 |
+
imagesLoaded++;
|
31 |
+
if (imagesLoaded === totalImages) {
|
32 |
+
console.log('All images loaded');
|
33 |
+
allImagesLoaded = true;
|
34 |
+
if (window.currentState) drawGame(window.currentState);
|
35 |
+
}
|
36 |
+
};
|
37 |
+
images[key].onerror = () => console.error(`Failed to load image: ${images[key].src}`);
|
38 |
+
}
|
39 |
|
40 |
socket.on('update_game', (state) => {
|
41 |
+
window.currentState = state;
|
42 |
+
if (allImagesLoaded) drawGame(state);
|
43 |
+
else console.log('Waiting for images to load...');
|
44 |
document.getElementById('health').textContent = state.health;
|
45 |
document.getElementById('attack').textContent = state.attack;
|
46 |
document.getElementById('inventory').textContent = state.inventory.join(', ');
|
47 |
document.getElementById('level').textContent = state.level;
|
48 |
document.getElementById('usePotion').disabled = !state.inventory.includes('potion');
|
49 |
+
if (state.health <= 0) alert('Game Over! Refresh to restart.');
|
|
|
|
|
50 |
});
|
51 |
|
52 |
socket.on('message', (msg) => {
|
|
|
57 |
|
58 |
function drawGame(state) {
|
59 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
60 |
for (let y = 0; y < state.terrain.length; y++) {
|
61 |
for (let x = 0; x < state.terrain[y].length; x++) {
|
62 |
+
const img = images[state.terrain[y][x] || 'f'];
|
63 |
+
if (img && img.complete) {
|
64 |
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
65 |
}
|
66 |
}
|
67 |
}
|
|
|
68 |
for (let y = 0; y < state.entities.length; y++) {
|
69 |
for (let x = 0; x < state.entities[y].length; x++) {
|
70 |
if (state.entities[y][x] && images[state.entities[y][x]]) {
|
71 |
const img = images[state.entities[y][x]];
|
72 |
+
if (img && img.complete) {
|
73 |
ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
|
74 |
}
|
75 |
}
|
|
|
79 |
|
80 |
document.addEventListener('keydown', (e) => {
|
81 |
const directions = { 'ArrowUp': 'up', 'ArrowDown': 'down', 'ArrowLeft': 'left', 'ArrowRight': 'right' };
|
82 |
+
if (directions[e.key]) socket.emit('move', directions[e.key]);
|
|
|
|
|
83 |
});
|
84 |
|
85 |
document.getElementById('usePotion').addEventListener('click', () => {
|
86 |
socket.emit('use_item', 'potion');
|
87 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|