broadfield-dev commited on
Commit
d09f4c3
·
verified ·
1 Parent(s): 312acdf

Update static/scripts.js

Browse files
Files changed (1) hide show
  1. static/scripts.js +112 -3
static/scripts.js CHANGED
@@ -1,5 +1,8 @@
1
  const socket = io("https://broadfield-dev-dungeon-game.hf.space", {
2
- transports: ['websocket'] // Force WebSocket transport
 
 
 
3
  });
4
 
5
  socket.on('connect', () => {
@@ -7,7 +10,23 @@ socket.on('connect', () => {
7
  });
8
 
9
  socket.on('connect_error', (error) => {
10
- console.error('Connection error:', error);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  });
12
 
13
  socket.on('disconnect', (reason) => {
@@ -28,8 +47,98 @@ socket.on('update_game', (state) => {
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
  });
 
1
  const socket = io("https://broadfield-dev-dungeon-game.hf.space", {
2
+ transports: ['websocket', 'polling'], // Try both WebSocket and polling
3
+ reconnection: true,
4
+ reconnectionAttempts: 5,
5
+ reconnectionDelay: 1000
6
  });
7
 
8
  socket.on('connect', () => {
 
10
  });
11
 
12
  socket.on('connect_error', (error) => {
13
+ console.error('Connection error:', error.message || error);
14
+ });
15
+
16
+ socket.on('connect_timeout', (timeout) => {
17
+ console.error('Connection timeout after', timeout, 'ms');
18
+ });
19
+
20
+ socket.on('reconnect_attempt', (attempt) => {
21
+ console.log('Reconnection attempt #', attempt);
22
+ });
23
+
24
+ socket.on('reconnect_error', (error) => {
25
+ console.error('Reconnection error:', error.message || error);
26
+ });
27
+
28
+ socket.on('reconnect_failed', () => {
29
+ console.error('Reconnection failed after all attempts');
30
  });
31
 
32
  socket.on('disconnect', (reason) => {
 
47
  });
48
 
49
  socket.on('message', (msg) => {
50
+ console.log('Received message:', msg);
51
  const messagesDiv = document.getElementById('messages');
52
  messagesDiv.innerHTML += `<p>${msg}</p>`;
53
  messagesDiv.scrollTop = messagesDiv.scrollHeight;
54
+ });
55
+
56
+ const canvas = document.getElementById('gameCanvas');
57
+ console.log('Canvas:', canvas);
58
+ if (!canvas) console.error('Canvas element not found');
59
+ const ctx = canvas.getContext('2d');
60
+ console.log('Context:', ctx);
61
+ if (!ctx) console.error('Canvas context not available');
62
+ const tileSize = 32;
63
+
64
+ const images = {
65
+ 'w': new Image(), 'f': new Image(), 'player': new Image(),
66
+ 'goblin': new Image(), 'skeleton': new Image(), 'potion': new Image(),
67
+ 'sword': new Image(), 'door': new Image()
68
+ };
69
+ images['w'].src = '/static/wall.png';
70
+ images['f'].src = '/static/floor.png';
71
+ images['player'].src = '/static/player.png';
72
+ images['goblin'].src = '/static/goblin.png';
73
+ images['skeleton'].src = '/static/skeleton.png';
74
+ images['potion'].src = '/static/potion.png';
75
+ images['sword'].src = '/static/sword.png';
76
+ images['door'].src = '/static/door.png';
77
+
78
+ let allImagesLoaded = false;
79
+ let imagesLoaded = 0;
80
+ const totalImages = Object.keys(images).length;
81
+ for (let key in images) {
82
+ images[key].onload = () => {
83
+ imagesLoaded++;
84
+ console.log(`Loaded image: ${images[key].src}`);
85
+ if (imagesLoaded === totalImages) {
86
+ console.log('All images loaded');
87
+ allImagesLoaded = true;
88
+ if (window.currentState) drawGame(window.currentState);
89
+ }
90
+ };
91
+ images[key].onerror = () => {
92
+ console.error(`Failed to load image: ${images[key].src}`);
93
+ };
94
+ console.log(`Attempting to load: ${images[key].src}`);
95
+ }
96
+
97
+ function drawGame(state) {
98
+ console.log('Drawing game with state:', state);
99
+ ctx.fillStyle = 'red'; // Test with a red background
100
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
101
+ if (!state.terrain || !state.entities) {
102
+ console.warn('State missing terrain or entities:', state);
103
+ return;
104
+ }
105
+ for (let y = 0; y < state.terrain.length; y++) {
106
+ for (let x = 0; x < state.terrain[y].length; x++) {
107
+ const terrainType = state.terrain[y][x] || 'f';
108
+ console.log(`Terrain at (${x},${y}): ${terrainType}`);
109
+ const img = images[terrainType];
110
+ if (img && img.complete) {
111
+ console.log(`Drawing ${terrainType} at (${x},${y})`);
112
+ ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
113
+ } else {
114
+ console.warn(`Image not ready for ${terrainType}`);
115
+ }
116
+ }
117
+ }
118
+ for (let y = 0; y < state.entities.length; y++) {
119
+ for (let x = 0; x < state.entities[y].length; x++) {
120
+ const entityType = state.entities[y][x];
121
+ if (entityType) {
122
+ console.log(`Entity at (${x},${y}): ${entityType}`);
123
+ const img = images[entityType];
124
+ if (img && img.complete) {
125
+ console.log(`Drawing ${entityType} at (${x},${y})`);
126
+ ctx.drawImage(img, x * tileSize, y * tileSize, tileSize, tileSize);
127
+ } else {
128
+ console.warn(`Image not ready for ${entityType}`);
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ document.addEventListener('keydown', (e) => {
136
+ const directions = { 'ArrowUp': 'up', 'ArrowDown': 'down', 'ArrowLeft': 'left', 'ArrowRight': 'right' };
137
+ if (directions[e.key]) {
138
+ socket.emit('move', directions[e.key]);
139
+ }
140
+ });
141
+
142
+ document.getElementById('usePotion').addEventListener('click', () => {
143
+ socket.emit('use_item', 'potion');
144
  });