latterworks commited on
Commit
a5b2bab
·
verified ·
1 Parent(s): 7c76abf

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +367 -0
index.html ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Colyseus Top-Down Client</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <style>
9
+ body {
10
+ font-family: 'Inter', sans-serif;
11
+ background-color: #1a202c;
12
+ color: #e2e8f0;
13
+ display: flex;
14
+ flex-direction: column;
15
+ align-items: center;
16
+ justify-content: center;
17
+ min-height: 100vh;
18
+ margin: 0;
19
+ padding: 20px;
20
+ box-sizing: border-box;
21
+ }
22
+
23
+ canvas {
24
+ background-color: #2d3748;
25
+ border: 2px solid #4a5568;
26
+ border-radius: 10px;
27
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
28
+ display: block;
29
+ touch-action: none;
30
+ }
31
+
32
+ .container {
33
+ display: flex;
34
+ flex-direction: column;
35
+ align-items: center;
36
+ width: 100%;
37
+ max-width: 900px;
38
+ }
39
+
40
+ .controls {
41
+ display: flex;
42
+ gap: 10px;
43
+ margin-top: 20px;
44
+ flex-wrap: wrap;
45
+ justify-content: center;
46
+ }
47
+
48
+ .chat-container {
49
+ width: 100%;
50
+ max-width: 800px;
51
+ margin-top: 20px;
52
+ background-color: #2d3748;
53
+ border-radius: 10px;
54
+ padding: 15px;
55
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
56
+ }
57
+
58
+ .chat-messages {
59
+ height: 150px;
60
+ overflow-y: auto;
61
+ border: 1px solid #4a5568;
62
+ padding: 10px;
63
+ border-radius: 5px;
64
+ margin-bottom: 10px;
65
+ background-color: #1a202c;
66
+ }
67
+
68
+ .chat-input-group {
69
+ display: flex;
70
+ gap: 10px;
71
+ }
72
+
73
+ .message-box-overlay {
74
+ position: fixed;
75
+ top: 0;
76
+ left: 0;
77
+ width: 100%;
78
+ height: 100%;
79
+ background: rgba(0, 0, 0, 0.7);
80
+ display: flex;
81
+ align-items: center;
82
+ justify-content: center;
83
+ z-index: 1000;
84
+ opacity: 0;
85
+ visibility: hidden;
86
+ transition: opacity 0.3s ease, visibility 0.3s ease;
87
+ }
88
+
89
+ .message-box-overlay.show {
90
+ opacity: 1;
91
+ visibility: visible;
92
+ }
93
+
94
+ .message-box {
95
+ background: #2d3748;
96
+ padding: 25px;
97
+ border-radius: 10px;
98
+ box-shadow: 0 5px 15px rgba(0, 0, 0, 0.6);
99
+ text-align: center;
100
+ max-width: 400px;
101
+ width: 90%;
102
+ color: #e2e8f0;
103
+ }
104
+
105
+ .message-box h3 {
106
+ margin-top: 0;
107
+ color: #a0aec0;
108
+ }
109
+
110
+ .message-box button {
111
+ background-color: #4299e1;
112
+ color: white;
113
+ padding: 10px 20px;
114
+ border: none;
115
+ border-radius: 5px;
116
+ cursor: pointer;
117
+ font-size: 1rem;
118
+ transition: background-color 0.2s ease;
119
+ margin-top: 15px;
120
+ }
121
+
122
+ .message-box button:hover {
123
+ background-color: #3182ce;
124
+ }
125
+ </style>
126
+ </head>
127
+ <body class="bg-gray-900 text-gray-100 flex flex-col items-center justify-center min-h-screen p-5">
128
+ <div class="container">
129
+ <h1 class="text-3xl font-bold mb-6 text-blue-400">Colyseus Top-Down Game</h1>
130
+ <canvas id="gameCanvas" width="800" height="600" class="rounded-lg shadow-xl"></canvas>
131
+
132
+ <div class="controls mt-6">
133
+ <p class="text-lg font-semibold">Move with: <span class="text-yellow-400">Arrow Keys</span></p>
134
+ </div>
135
+
136
+ <div class="chat-container">
137
+ <h2 class="text-xl font-semibold mb-3 text-blue-300">Chat</h2>
138
+ <div id="chatMessages" class="chat-messages text-sm">
139
+ </div>
140
+ <div class="chat-input-group">
141
+ <input type="text" id="chatInput" placeholder="Type your message..."
142
+ class="flex-grow p-3 rounded-md bg-gray-700 text-gray-100 border border-gray-600 focus:outline-none focus:ring-2 focus:ring-blue-500">
143
+ <button id="sendChat"
144
+ class="px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white font-bold rounded-md shadow-lg transition duration-200 ease-in-out transform hover:scale-105">
145
+ Send
146
+ </button>
147
+ </div>
148
+ </div>
149
+ </div>
150
+
151
+ <div id="messageBoxOverlay" class="message-box-overlay">
152
+ <div class="message-box">
153
+ <h3 id="messageBoxTitle"></h3>
154
+ <p id="messageBoxContent"></p>
155
+ <button id="messageBoxClose">OK</button>
156
+ </div>
157
+ </div>
158
+
159
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/colyseus.js/0.15.10/Colyseus.min.js"></script>
160
+ <script>
161
+ function showMessageBox(title, message) {
162
+ const overlay = document.getElementById('messageBoxOverlay');
163
+ const boxTitle = document.getElementById('messageBoxTitle');
164
+ const boxContent = document.getElementById('messageBoxContent');
165
+ const closeButton = document.getElementById('messageBoxClose');
166
+
167
+ boxTitle.textContent = title;
168
+ boxContent.textContent = message;
169
+ overlay.classList.add('show');
170
+
171
+ closeButton.onclick = () => {
172
+ overlay.classList.remove('show');
173
+ };
174
+ }
175
+
176
+ const canvas = document.getElementById('gameCanvas');
177
+ const ctx = canvas.getContext('2d');
178
+
179
+ let client;
180
+ let room;
181
+ let currentPlayerId = null;
182
+
183
+ const chatMessagesDiv = document.getElementById('chatMessages');
184
+ const chatInput = document.getElementById('chatInput');
185
+ const sendChatButton = document.getElementById('sendChat');
186
+
187
+ const PLAYER_SIZE = 30;
188
+ const PLAYER_SPEED = 5;
189
+
190
+ const keys = {
191
+ ArrowUp: false,
192
+ ArrowDown: false,
193
+ ArrowLeft: false,
194
+ ArrowRight: false
195
+ };
196
+
197
+ async function connectAndJoin() {
198
+ try {
199
+ // Defensive check to ensure Colyseus is loaded
200
+ if (typeof Colyseus === 'undefined' || !Colyseus.Client) {
201
+ throw new Error("Colyseus library not loaded. Please check network connection or CDN availability.");
202
+ }
203
+
204
+ // Initialize client here, after Colyseus library is loaded
205
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
206
+ client = new Colyseus.Client(`${protocol}//${window.location.host}`);
207
+
208
+ room = await client.joinOrCreate("my_room");
209
+ currentPlayerId = room.sessionId;
210
+ showMessageBox("Connected!", `Joined room: ${room.name} as ${room.sessionId}`);
211
+ console.log("Joined successfully", room);
212
+
213
+ room.onStateChange((state) => {
214
+ console.log("New state:", state.toJSON());
215
+ drawGame(state);
216
+ });
217
+
218
+ room.state.players.onAdd = (player, sessionId) => {
219
+ console.log(`Player ${sessionId} added at (${player.x}, ${player.y})`);
220
+ player.onChange = (changes) => { };
221
+ };
222
+
223
+ room.state.players.onRemove = (player, sessionId) => {
224
+ console.log(`Player ${sessionId} removed.`);
225
+ };
226
+
227
+ room.onMessage("welcome", (message) => {
228
+ console.log("Server welcome message:", message);
229
+ });
230
+
231
+ room.onMessage("chat", (message) => {
232
+ addChatMessage(message);
233
+ });
234
+
235
+ room.onLeave((code) => {
236
+ console.log("Left room with code:", code);
237
+ showMessageBox("Disconnected", "You have been disconnected from the room. Please refresh to reconnect.");
238
+ });
239
+
240
+ } catch (e) {
241
+ console.error("Failed to connect or join:", e);
242
+ showMessageBox("Connection Error", `Failed to connect to the server or join room: ${e.message}. Make sure the server is running and the Colyseus library is loaded.`);
243
+ }
244
+ }
245
+
246
+ window.onload = connectAndJoin;
247
+
248
+ function drawGame(state) {
249
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
250
+
251
+ state.players.forEach((player, sessionId) => {
252
+ ctx.fillStyle = player.color;
253
+ ctx.strokeStyle = sessionId === currentPlayerId ? '#FFFFFF' : '#000000';
254
+ ctx.lineWidth = 2;
255
+ ctx.beginPath();
256
+ ctx.arc(player.x, player.y, PLAYER_SIZE / 2, 0, Math.PI * 2);
257
+ ctx.fill();
258
+ ctx.stroke();
259
+
260
+ ctx.fillStyle = '#FFFFFF';
261
+ ctx.font = '12px Inter';
262
+ ctx.textAlign = 'center';
263
+ ctx.textBaseline = 'middle';
264
+ ctx.fillText(sessionId.substring(0, 4), player.x, player.y + PLAYER_SIZE / 2 + 10);
265
+ });
266
+ }
267
+
268
+ document.addEventListener('keydown', (event) => {
269
+ if (event.key in keys) {
270
+ keys[event.key] = true;
271
+ sendMovementUpdate();
272
+ }
273
+ });
274
+
275
+ document.addEventListener('keyup', (event) => {
276
+ if (event.key in keys) {
277
+ keys[event.key] = false;
278
+ sendMovementUpdate();
279
+ }
280
+ });
281
+
282
+ function sendMovementUpdate() {
283
+ if (!room || !currentPlayerId) return;
284
+
285
+ const player = room.state.players.get(currentPlayerId);
286
+ if (!player) return;
287
+
288
+ let newX = player.x;
289
+ let newY = player.y;
290
+
291
+ if (keys.ArrowUp) newY -= PLAYER_SPEED;
292
+ if (keys.ArrowDown) newY += PLAYER_SPEED;
293
+ if (keys.ArrowLeft) newX -= PLAYER_SPEED;
294
+ if (keys.ArrowRight) newX += PLAYER_SPEED;
295
+
296
+ newX = Math.max(PLAYER_SIZE / 2, Math.min(canvas.width - PLAYER_SIZE / 2, newX));
297
+ newY = Math.max(PLAYER_SIZE / 2, Math.min(canvas.height - PLAYER_SIZE / 2, newY));
298
+
299
+ if (newX !== player.x || newY !== player.y) {
300
+ room.send("move", { x: newX, y: newY });
301
+ }
302
+ }
303
+
304
+ function addChatMessage(message) {
305
+ const p = document.createElement('p');
306
+ p.textContent = message;
307
+ chatMessagesDiv.appendChild(p);
308
+ chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
309
+ }
310
+
311
+ sendChatButton.addEventListener('click', () => {
312
+ const message = chatInput.value.trim();
313
+ if (message) {
314
+ room.send("chat", message);
315
+ chatInput.value = '';
316
+ }
317
+ });
318
+
319
+ chatInput.addEventListener('keypress', (event) => {
320
+ if (event.key === 'Enter') {
321
+ sendChatButton.click();
322
+ }
323
+ });
324
+
325
+ function resizeCanvas() { }
326
+ resizeCanvas();
327
+ window.addEventListener('resize', resizeCanvas);
328
+
329
+ let touchStartX = 0;
330
+ let touchStartY = 0;
331
+
332
+ canvas.addEventListener('touchstart', (e) => {
333
+ e.preventDefault();
334
+ const touch = e.touches[0];
335
+ touchStartX = touch.clientX;
336
+ touchStartY = touch.clientY;
337
+ }, { passive: false });
338
+
339
+ canvas.addEventListener('touchend', (e) => {
340
+ e.preventDefault();
341
+ const touch = e.changedTouches[0];
342
+ const endX = touch.clientX;
343
+ const endY = touch.clientY;
344
+
345
+ const deltaX = endX - touchStartX;
346
+ const deltaY = endY - touchStartY;
347
+
348
+ if (Math.abs(deltaX) > Math.abs(deltaY)) {
349
+ if (deltaX > 0) keys.ArrowRight = true;
350
+ else keys.ArrowLeft = true;
351
+ } else {
352
+ if (deltaY > 0) keys.ArrowDown = true;
353
+ else keys.ArrowUp = true;
354
+ }
355
+
356
+ sendMovementUpdate();
357
+
358
+ setTimeout(() => {
359
+ keys.ArrowUp = false;
360
+ keys.ArrowDown = false;
361
+ keys.ArrowLeft = false;
362
+ keys.ArrowRight = false;
363
+ }, 100);
364
+ }, { passive: false });
365
+ </script>
366
+ </body>
367
+ </html>