Nafiul Huda commited on
Update public/index.html
Browse files- public/index.html +521 -1091
public/index.html
CHANGED
|
@@ -2,1119 +2,549 @@
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width,
|
| 6 |
-
<
|
| 7 |
-
<meta name="mobile-web-app-capable" content="yes">
|
| 8 |
-
<title>Multiplayer Jump!</title>
|
| 9 |
<style>
|
| 10 |
-
*
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
position: fixed;
|
| 30 |
-
top: 0;
|
| 31 |
-
left: 0;
|
| 32 |
-
width: 100%;
|
| 33 |
-
height: 100%;
|
| 34 |
-
display: flex;
|
| 35 |
-
justify-content: center;
|
| 36 |
-
align-items: center;
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
#gameCanvas {
|
| 40 |
-
background: #1a1a2e;
|
| 41 |
-
image-rendering: pixelated;
|
| 42 |
-
image-rendering: crisp-edges;
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
#ui {
|
| 46 |
-
position: fixed;
|
| 47 |
-
top: 0;
|
| 48 |
-
left: 0;
|
| 49 |
-
width: 100%;
|
| 50 |
-
height: 100%;
|
| 51 |
-
display: flex;
|
| 52 |
-
justify-content: center;
|
| 53 |
-
align-items: center;
|
| 54 |
-
pointer-events: none;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
#ui > * {
|
| 58 |
-
pointer-events: auto;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
.menu {
|
| 62 |
-
background: rgba(20, 20, 40, 0.95);
|
| 63 |
-
border: 2px solid #4a4a6a;
|
| 64 |
-
border-radius: 16px;
|
| 65 |
-
padding: 30px;
|
| 66 |
-
text-align: center;
|
| 67 |
-
color: white;
|
| 68 |
-
max-width: 320px;
|
| 69 |
-
width: 90%;
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
.menu h1 {
|
| 73 |
-
font-size: 28px;
|
| 74 |
-
margin-bottom: 20px;
|
| 75 |
-
background: linear-gradient(45deg, #00ff88, #00aaff);
|
| 76 |
-
-webkit-background-clip: text;
|
| 77 |
-
-webkit-text-fill-color: transparent;
|
| 78 |
-
background-clip: text;
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
.menu input {
|
| 82 |
-
width: 100%;
|
| 83 |
-
padding: 12px;
|
| 84 |
-
font-size: 16px;
|
| 85 |
-
border: 2px solid #4a4a6a;
|
| 86 |
-
border-radius: 8px;
|
| 87 |
-
background: #2a2a4a;
|
| 88 |
-
color: white;
|
| 89 |
-
margin-bottom: 15px;
|
| 90 |
-
text-align: center;
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
.menu input:focus {
|
| 94 |
-
outline: none;
|
| 95 |
-
border-color: #00ff88;
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
.menu button {
|
| 99 |
-
width: 100%;
|
| 100 |
-
padding: 14px;
|
| 101 |
-
font-size: 18px;
|
| 102 |
-
font-weight: bold;
|
| 103 |
-
border: none;
|
| 104 |
-
border-radius: 8px;
|
| 105 |
-
background: linear-gradient(45deg, #00ff88, #00aaff);
|
| 106 |
-
color: #1a1a2e;
|
| 107 |
-
cursor: pointer;
|
| 108 |
-
transition: transform 0.2s, box-shadow 0.2s;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
.menu button:hover {
|
| 112 |
-
transform: scale(1.02);
|
| 113 |
-
box-shadow: 0 0 20px rgba(0, 255, 136, 0.4);
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
.menu button:active {
|
| 117 |
-
transform: scale(0.98);
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
.hidden {
|
| 121 |
-
display: none !important;
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
#leaderboardList {
|
| 125 |
-
text-align: left;
|
| 126 |
-
margin: 15px 0;
|
| 127 |
-
max-height: 200px;
|
| 128 |
-
overflow-y: auto;
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
.leaderboard-entry {
|
| 132 |
-
display: flex;
|
| 133 |
-
justify-content: space-between;
|
| 134 |
-
padding: 8px;
|
| 135 |
-
border-bottom: 1px solid #3a3a5a;
|
| 136 |
-
font-size: 14px;
|
| 137 |
-
}
|
| 138 |
-
|
| 139 |
-
.leaderboard-entry:nth-child(1) .rank { color: gold; }
|
| 140 |
-
.leaderboard-entry:nth-child(2) .rank { color: silver; }
|
| 141 |
-
.leaderboard-entry:nth-child(3) .rank { color: #cd7f32; }
|
| 142 |
-
|
| 143 |
-
.controls-hint {
|
| 144 |
-
font-size: 12px;
|
| 145 |
-
color: #8a8aaa;
|
| 146 |
-
margin-top: 15px;
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
-
#connectionStatus {
|
| 150 |
-
position: fixed;
|
| 151 |
-
top: 10px;
|
| 152 |
-
left: 50%;
|
| 153 |
-
transform: translateX(-50%);
|
| 154 |
-
padding: 8px 16px;
|
| 155 |
-
border-radius: 20px;
|
| 156 |
-
font-size: 12px;
|
| 157 |
-
color: white;
|
| 158 |
-
z-index: 100;
|
| 159 |
-
transition: opacity 0.3s;
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
.status-connected {
|
| 163 |
-
background: rgba(0, 200, 100, 0.8);
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
.status-connecting {
|
| 167 |
-
background: rgba(255, 200, 0, 0.8);
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
.status-disconnected {
|
| 171 |
-
background: rgba(255, 50, 50, 0.8);
|
| 172 |
-
}
|
| 173 |
</style>
|
| 174 |
</head>
|
| 175 |
<body>
|
| 176 |
-
<
|
| 177 |
-
<canvas id="gameCanvas"></canvas>
|
| 178 |
-
</div>
|
| 179 |
-
|
| 180 |
-
<div id="connectionStatus" class="status-connecting">Connecting...</div>
|
| 181 |
|
| 182 |
-
<div id="
|
| 183 |
-
<div
|
| 184 |
<h1>π JUMP!</h1>
|
| 185 |
-
<input type="text" id="
|
| 186 |
-
<button id="
|
| 187 |
-
<div id="
|
| 188 |
-
<div class="
|
| 189 |
-
β β or Touch sides to move<br>
|
| 190 |
-
Jump on platforms to climb!
|
| 191 |
-
</div>
|
| 192 |
</div>
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
| 195 |
<h1>GAME OVER</h1>
|
| 196 |
-
<
|
| 197 |
-
<
|
| 198 |
-
<button id="
|
| 199 |
-
<div id="
|
| 200 |
</div>
|
| 201 |
</div>
|
| 202 |
|
| 203 |
<script src="/socket.io/socket.io.js"></script>
|
| 204 |
<script>
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
// Get platforms in range
|
| 359 |
-
function getPlatformsInRange(minY, maxY) {
|
| 360 |
-
const minChunk = Math.floor(-maxY / CHUNK_SIZE);
|
| 361 |
-
const maxChunk = Math.floor(-minY / CHUNK_SIZE) + 1;
|
| 362 |
-
const platforms = [];
|
| 363 |
-
|
| 364 |
-
for (let i = Math.max(0, minChunk); i <= maxChunk; i++) {
|
| 365 |
-
if (!platformCache.has(i)) {
|
| 366 |
-
platformCache.set(i, generateChunk(i));
|
| 367 |
-
}
|
| 368 |
-
platforms.push(...platformCache.get(i));
|
| 369 |
-
}
|
| 370 |
-
|
| 371 |
-
return platforms.filter(p => !brokenPlatforms.has(p.id) && !p.broken);
|
| 372 |
-
}
|
| 373 |
-
|
| 374 |
-
// Create jump particles
|
| 375 |
-
function createJumpParticles(x, y, color, isSpring) {
|
| 376 |
-
const count = isSpring ? 20 : 10;
|
| 377 |
-
for (let i = 0; i < count; i++) {
|
| 378 |
-
particles.push({
|
| 379 |
-
x: x + PLAYER_WIDTH / 2,
|
| 380 |
-
y: y + PLAYER_HEIGHT,
|
| 381 |
-
vx: (Math.random() - 0.5) * 8,
|
| 382 |
-
vy: Math.random() * 3 + 1,
|
| 383 |
-
life: 1,
|
| 384 |
-
decay: 0.02 + Math.random() * 0.02,
|
| 385 |
-
size: Math.random() * 6 + 2,
|
| 386 |
-
color: isSpring ? '#ffff00' : color
|
| 387 |
-
});
|
| 388 |
-
}
|
| 389 |
-
}
|
| 390 |
-
|
| 391 |
-
// Update particles
|
| 392 |
-
function updateParticles() {
|
| 393 |
-
for (let i = particles.length - 1; i >= 0; i--) {
|
| 394 |
-
const p = particles[i];
|
| 395 |
-
p.x += p.vx;
|
| 396 |
-
p.y += p.vy;
|
| 397 |
-
p.vy += 0.1;
|
| 398 |
-
p.life -= p.decay;
|
| 399 |
|
| 400 |
-
if (p.
|
| 401 |
-
|
|
|
|
|
|
|
| 402 |
}
|
| 403 |
-
}
|
| 404 |
-
}
|
| 405 |
-
|
| 406 |
-
// Resize canvas
|
| 407 |
-
function resizeCanvas() {
|
| 408 |
-
const container = document.getElementById('gameContainer');
|
| 409 |
-
const containerWidth = container.clientWidth;
|
| 410 |
-
const containerHeight = container.clientHeight;
|
| 411 |
-
|
| 412 |
-
const scaleX = containerWidth / GAME_WIDTH;
|
| 413 |
-
const scaleY = containerHeight / GAME_HEIGHT;
|
| 414 |
-
const scale = Math.min(scaleX, scaleY);
|
| 415 |
-
|
| 416 |
-
canvas.style.width = `${GAME_WIDTH * scale}px`;
|
| 417 |
-
canvas.style.height = `${GAME_HEIGHT * scale}px`;
|
| 418 |
-
}
|
| 419 |
-
|
| 420 |
-
window.addEventListener('resize', resizeCanvas);
|
| 421 |
-
resizeCanvas();
|
| 422 |
-
|
| 423 |
-
// Client-side prediction
|
| 424 |
-
function predictLocalPlayer() {
|
| 425 |
-
if (!localPlayer.alive) return;
|
| 426 |
-
|
| 427 |
-
// Apply input
|
| 428 |
-
if (inputState.left) {
|
| 429 |
-
localPlayer.vx = -MOVE_SPEED;
|
| 430 |
-
localPlayer.facingRight = false;
|
| 431 |
-
} else if (inputState.right) {
|
| 432 |
-
localPlayer.vx = MOVE_SPEED;
|
| 433 |
-
localPlayer.facingRight = true;
|
| 434 |
-
} else {
|
| 435 |
-
localPlayer.vx *= 0.85;
|
| 436 |
-
}
|
| 437 |
-
|
| 438 |
-
// Apply gravity
|
| 439 |
-
localPlayer.vy += GRAVITY;
|
| 440 |
-
localPlayer.vy = Math.min(localPlayer.vy, MAX_FALL_SPEED);
|
| 441 |
-
|
| 442 |
-
const prevY = localPlayer.y;
|
| 443 |
-
|
| 444 |
-
// Update position
|
| 445 |
-
localPlayer.x += localPlayer.vx;
|
| 446 |
-
localPlayer.y += localPlayer.vy;
|
| 447 |
-
|
| 448 |
-
// Screen wrap
|
| 449 |
-
if (localPlayer.x < -PLAYER_WIDTH) {
|
| 450 |
-
localPlayer.x = GAME_WIDTH;
|
| 451 |
-
} else if (localPlayer.x > GAME_WIDTH) {
|
| 452 |
-
localPlayer.x = -PLAYER_WIDTH;
|
| 453 |
-
}
|
| 454 |
-
|
| 455 |
-
// Platform collision
|
| 456 |
-
if (localPlayer.vy > 0) {
|
| 457 |
-
const platforms = getPlatformsInRange(
|
| 458 |
-
localPlayer.y - GAME_HEIGHT,
|
| 459 |
-
localPlayer.y + GAME_HEIGHT
|
| 460 |
-
);
|
| 461 |
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
brokenPlatforms.add(platform.id);
|
| 467 |
-
}
|
| 468 |
-
|
| 469 |
-
const isSpring = platform.type === PLATFORM_SPRING;
|
| 470 |
-
localPlayer.y = platform.y - PLAYER_HEIGHT;
|
| 471 |
-
localPlayer.vy = isSpring ? SPRING_FORCE : JUMP_FORCE;
|
| 472 |
-
|
| 473 |
-
createJumpParticles(localPlayer.x, localPlayer.y, localPlayer.color, isSpring);
|
| 474 |
-
break;
|
| 475 |
-
}
|
| 476 |
-
}
|
| 477 |
-
}
|
| 478 |
-
|
| 479 |
-
// Update score
|
| 480 |
-
const newScore = Math.floor(-localPlayer.y / 10);
|
| 481 |
-
if (newScore > localPlayer.score) {
|
| 482 |
-
localPlayer.score = newScore;
|
| 483 |
-
if (localPlayer.y < localPlayer.highestY) {
|
| 484 |
-
localPlayer.highestY = localPlayer.y;
|
| 485 |
-
}
|
| 486 |
-
}
|
| 487 |
-
|
| 488 |
-
// Check death
|
| 489 |
-
const deathLine = cameraY + GAME_HEIGHT + 100;
|
| 490 |
-
if (localPlayer.y > deathLine) {
|
| 491 |
-
localPlayer.alive = false;
|
| 492 |
-
onGameOver();
|
| 493 |
-
}
|
| 494 |
-
}
|
| 495 |
-
|
| 496 |
-
function checkPlatformCollision(player, platform, prevY) {
|
| 497 |
-
const playerBottom = player.y + PLAYER_HEIGHT;
|
| 498 |
-
const playerPrevBottom = prevY + PLAYER_HEIGHT;
|
| 499 |
-
|
| 500 |
-
return player.x + PLAYER_WIDTH > platform.x &&
|
| 501 |
-
player.x < platform.x + platform.width &&
|
| 502 |
-
playerPrevBottom <= platform.y &&
|
| 503 |
-
playerBottom >= platform.y &&
|
| 504 |
-
playerBottom <= platform.y + platform.height + 10;
|
| 505 |
-
}
|
| 506 |
-
|
| 507 |
-
// Update moving platforms
|
| 508 |
-
function updatePlatforms() {
|
| 509 |
-
for (const [, platforms] of platformCache) {
|
| 510 |
-
for (const platform of platforms) {
|
| 511 |
-
if (platform.type === PLATFORM_MOVING && !platform.broken) {
|
| 512 |
-
platform.x += platform.moveSpeed * platform.moveDir;
|
| 513 |
-
|
| 514 |
-
if (platform.x <= 20 || platform.x >= GAME_WIDTH - PLATFORM_WIDTH - 20) {
|
| 515 |
-
platform.moveDir *= -1;
|
| 516 |
-
}
|
| 517 |
-
}
|
| 518 |
-
}
|
| 519 |
-
}
|
| 520 |
-
}
|
| 521 |
-
|
| 522 |
-
// Interpolate other players
|
| 523 |
-
function interpolateOtherPlayers(serverState) {
|
| 524 |
-
for (const playerData of serverState.players) {
|
| 525 |
-
if (playerData.id === playerId) {
|
| 526 |
-
// Reconcile local player with server
|
| 527 |
-
localPlayer.score = playerData.score;
|
| 528 |
-
localPlayer.color = playerData.color;
|
| 529 |
-
localPlayer.alive = playerData.alive;
|
| 530 |
-
|
| 531 |
-
// Smooth correction if too far off
|
| 532 |
-
const dx = playerData.x - localPlayer.x;
|
| 533 |
-
const dy = playerData.y - localPlayer.y;
|
| 534 |
-
if (Math.abs(dx) > 50 || Math.abs(dy) > 50) {
|
| 535 |
-
localPlayer.x += dx * 0.3;
|
| 536 |
-
localPlayer.y += dy * 0.3;
|
| 537 |
-
}
|
| 538 |
-
} else {
|
| 539 |
-
// Interpolate other players
|
| 540 |
-
if (!otherPlayers.has(playerData.id)) {
|
| 541 |
-
otherPlayers.set(playerData.id, {
|
| 542 |
-
...playerData,
|
| 543 |
-
targetX: playerData.x,
|
| 544 |
-
targetY: playerData.y
|
| 545 |
-
});
|
| 546 |
-
}
|
| 547 |
-
|
| 548 |
-
const other = otherPlayers.get(playerData.id);
|
| 549 |
-
other.targetX = playerData.x;
|
| 550 |
-
other.targetY = playerData.y;
|
| 551 |
-
other.score = playerData.score;
|
| 552 |
-
other.alive = playerData.alive;
|
| 553 |
-
other.color = playerData.color;
|
| 554 |
-
other.username = playerData.username;
|
| 555 |
-
other.facingRight = playerData.facingRight;
|
| 556 |
-
}
|
| 557 |
-
}
|
| 558 |
-
|
| 559 |
-
// Remove disconnected players
|
| 560 |
-
const activeIds = new Set(serverState.players.map(p => p.id));
|
| 561 |
-
for (const [id] of otherPlayers) {
|
| 562 |
-
if (!activeIds.has(id)) {
|
| 563 |
-
otherPlayers.delete(id);
|
| 564 |
-
}
|
| 565 |
-
}
|
| 566 |
-
|
| 567 |
-
// Update broken platforms
|
| 568 |
-
for (const id of serverState.brokenPlatforms) {
|
| 569 |
-
brokenPlatforms.add(id);
|
| 570 |
-
}
|
| 571 |
-
}
|
| 572 |
-
|
| 573 |
-
// Update camera
|
| 574 |
-
function updateCamera() {
|
| 575 |
-
targetCameraY = localPlayer.highestY - GAME_HEIGHT * 0.4;
|
| 576 |
-
targetCameraY = Math.min(targetCameraY, 0);
|
| 577 |
-
cameraY += (targetCameraY - cameraY) * 0.1;
|
| 578 |
-
}
|
| 579 |
-
|
| 580 |
-
// Draw functions
|
| 581 |
-
function drawBackground() {
|
| 582 |
-
// Gradient background
|
| 583 |
-
const gradient = ctx.createLinearGradient(0, 0, 0, GAME_HEIGHT);
|
| 584 |
-
gradient.addColorStop(0, '#0a0a2e');
|
| 585 |
-
gradient.addColorStop(0.5, '#1a1a3e');
|
| 586 |
-
gradient.addColorStop(1, '#2a2a4e');
|
| 587 |
-
ctx.fillStyle = gradient;
|
| 588 |
-
ctx.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
|
| 589 |
-
|
| 590 |
-
// Parallax grid layers
|
| 591 |
-
ctx.strokeStyle = 'rgba(100, 100, 150, 0.1)';
|
| 592 |
-
ctx.lineWidth = 1;
|
| 593 |
-
|
| 594 |
-
// Far layer
|
| 595 |
-
const gridSize1 = 80;
|
| 596 |
-
const offset1 = (-cameraY * 0.3) % gridSize1;
|
| 597 |
-
for (let y = offset1; y < GAME_HEIGHT; y += gridSize1) {
|
| 598 |
-
ctx.beginPath();
|
| 599 |
-
ctx.moveTo(0, y);
|
| 600 |
-
ctx.lineTo(GAME_WIDTH, y);
|
| 601 |
-
ctx.stroke();
|
| 602 |
-
}
|
| 603 |
-
for (let x = 0; x < GAME_WIDTH; x += gridSize1) {
|
| 604 |
-
ctx.beginPath();
|
| 605 |
-
ctx.moveTo(x, 0);
|
| 606 |
-
ctx.lineTo(x, GAME_HEIGHT);
|
| 607 |
-
ctx.stroke();
|
| 608 |
-
}
|
| 609 |
-
|
| 610 |
-
// Near layer
|
| 611 |
-
ctx.strokeStyle = 'rgba(100, 100, 150, 0.05)';
|
| 612 |
-
const gridSize2 = 40;
|
| 613 |
-
const offset2 = (-cameraY * 0.6) % gridSize2;
|
| 614 |
-
for (let y = offset2; y < GAME_HEIGHT; y += gridSize2) {
|
| 615 |
-
ctx.beginPath();
|
| 616 |
-
ctx.moveTo(0, y);
|
| 617 |
-
ctx.lineTo(GAME_WIDTH, y);
|
| 618 |
-
ctx.stroke();
|
| 619 |
-
}
|
| 620 |
-
|
| 621 |
-
// Stars
|
| 622 |
-
const starRng = new SeededRandom(42);
|
| 623 |
-
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
|
| 624 |
-
for (let i = 0; i < 50; i++) {
|
| 625 |
-
const x = starRng.nextRange(0, GAME_WIDTH);
|
| 626 |
-
const baseY = starRng.nextRange(0, GAME_HEIGHT * 3);
|
| 627 |
-
const y = (baseY - cameraY * 0.1) % GAME_HEIGHT;
|
| 628 |
-
const size = starRng.nextRange(1, 3);
|
| 629 |
-
ctx.beginPath();
|
| 630 |
-
ctx.arc(x, y, size, 0, Math.PI * 2);
|
| 631 |
-
ctx.fill();
|
| 632 |
}
|
| 633 |
}
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
let color, shadowColor;
|
| 643 |
-
switch (platform.type) {
|
| 644 |
-
case PLATFORM_MOVING:
|
| 645 |
-
color = '#4488ff';
|
| 646 |
-
shadowColor = '#2266cc';
|
| 647 |
-
break;
|
| 648 |
-
case PLATFORM_BREAKABLE:
|
| 649 |
-
color = '#aa6633';
|
| 650 |
-
shadowColor = '#774422';
|
| 651 |
-
break;
|
| 652 |
-
case PLATFORM_SPRING:
|
| 653 |
-
color = '#ffcc00';
|
| 654 |
-
shadowColor = '#cc9900';
|
| 655 |
-
break;
|
| 656 |
-
default:
|
| 657 |
-
color = '#44dd66';
|
| 658 |
-
shadowColor = '#22aa44';
|
| 659 |
-
}
|
| 660 |
-
|
| 661 |
-
// Shadow
|
| 662 |
-
ctx.fillStyle = shadowColor;
|
| 663 |
-
ctx.beginPath();
|
| 664 |
-
ctx.roundRect(platform.x, screenY + 4, platform.width, platform.height, 6);
|
| 665 |
-
ctx.fill();
|
| 666 |
-
|
| 667 |
-
// Main platform
|
| 668 |
-
ctx.fillStyle = color;
|
| 669 |
-
ctx.beginPath();
|
| 670 |
-
ctx.roundRect(platform.x, screenY, platform.width, platform.height, 6);
|
| 671 |
-
ctx.fill();
|
| 672 |
-
|
| 673 |
-
// Highlight
|
| 674 |
-
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
|
| 675 |
-
ctx.beginPath();
|
| 676 |
-
ctx.roundRect(platform.x + 4, screenY + 2, platform.width - 8, 4, 2);
|
| 677 |
-
ctx.fill();
|
| 678 |
-
|
| 679 |
-
// Spring coil
|
| 680 |
-
if (platform.type === PLATFORM_SPRING) {
|
| 681 |
-
ctx.strokeStyle = '#ffee00';
|
| 682 |
-
ctx.lineWidth = 3;
|
| 683 |
-
const cx = platform.x + platform.width / 2;
|
| 684 |
-
ctx.beginPath();
|
| 685 |
-
ctx.moveTo(cx - 10, screenY);
|
| 686 |
-
for (let i = 0; i < 4; i++) {
|
| 687 |
-
ctx.lineTo(cx + (i % 2 === 0 ? 10 : -10), screenY - 5 - i * 4);
|
| 688 |
-
}
|
| 689 |
-
ctx.stroke();
|
| 690 |
-
}
|
| 691 |
-
|
| 692 |
-
ctx.restore();
|
| 693 |
}
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
ctx.beginPath();
|
| 742 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 743 |
ctx.stroke();
|
| 744 |
-
|
| 745 |
-
// Name tag (for other players or local)
|
| 746 |
-
const name = isLocal ? 'YOU' : (player.username || 'Player');
|
| 747 |
-
ctx.font = 'bold 14px Segoe UI, Arial';
|
| 748 |
-
ctx.textAlign = 'center';
|
| 749 |
-
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
| 750 |
-
ctx.fillText(name, x + 1, screenY - 11);
|
| 751 |
-
ctx.fillStyle = isLocal ? '#00ff88' : 'white';
|
| 752 |
-
ctx.fillText(name, x, screenY - 12);
|
| 753 |
-
|
| 754 |
-
ctx.restore();
|
| 755 |
-
}
|
| 756 |
-
|
| 757 |
-
function shadeColor(color, percent) {
|
| 758 |
-
// Handle HSL colors
|
| 759 |
-
if (color.startsWith('hsl')) {
|
| 760 |
-
const match = color.match(/hsl\((\d+),\s*(\d+)%,\s*(\d+)%\)/);
|
| 761 |
-
if (match) {
|
| 762 |
-
const h = parseInt(match[1]);
|
| 763 |
-
const s = parseInt(match[2]);
|
| 764 |
-
const l = Math.max(0, Math.min(100, parseInt(match[3]) + percent));
|
| 765 |
-
return `hsl(${h}, ${s}%, ${l}%)`;
|
| 766 |
-
}
|
| 767 |
-
}
|
| 768 |
-
// Handle hex colors
|
| 769 |
-
const num = parseInt(color.replace('#', ''), 16);
|
| 770 |
-
const amt = Math.round(2.55 * percent);
|
| 771 |
-
const R = Math.max(0, Math.min(255, (num >> 16) + amt));
|
| 772 |
-
const G = Math.max(0, Math.min(255, (num >> 8 & 0x00FF) + amt));
|
| 773 |
-
const B = Math.max(0, Math.min(255, (num & 0x0000FF) + amt));
|
| 774 |
-
return `#${(0x1000000 + R * 0x10000 + G * 0x100 + B).toString(16).slice(1)}`;
|
| 775 |
-
}
|
| 776 |
-
|
| 777 |
-
function drawParticles() {
|
| 778 |
-
for (const p of particles) {
|
| 779 |
-
const screenY = p.y - cameraY;
|
| 780 |
-
ctx.globalAlpha = p.life;
|
| 781 |
-
ctx.fillStyle = p.color;
|
| 782 |
-
ctx.beginPath();
|
| 783 |
-
ctx.arc(p.x, screenY, p.size, 0, Math.PI * 2);
|
| 784 |
-
ctx.fill();
|
| 785 |
-
}
|
| 786 |
-
ctx.globalAlpha = 1;
|
| 787 |
-
}
|
| 788 |
-
|
| 789 |
-
function drawHUD() {
|
| 790 |
-
// Score
|
| 791 |
-
ctx.font = 'bold 32px Segoe UI, Arial';
|
| 792 |
-
ctx.textAlign = 'left';
|
| 793 |
-
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
| 794 |
-
ctx.fillText(`${localPlayer.score}`, 22, 47);
|
| 795 |
-
ctx.fillStyle = '#ffffff';
|
| 796 |
-
ctx.fillText(`${localPlayer.score}`, 20, 45);
|
| 797 |
-
|
| 798 |
-
// High score
|
| 799 |
-
ctx.font = '16px Segoe UI, Arial';
|
| 800 |
-
ctx.fillStyle = '#aaaacc';
|
| 801 |
-
ctx.fillText(`Best: ${Math.max(personalBest, localPlayer.score)}`, 20, 70);
|
| 802 |
-
|
| 803 |
-
// Player count
|
| 804 |
-
const playerCount = otherPlayers.size + 1;
|
| 805 |
-
ctx.textAlign = 'right';
|
| 806 |
-
ctx.fillStyle = '#88aaff';
|
| 807 |
-
ctx.fillText(`${playerCount} online`, GAME_WIDTH - 20, 45);
|
| 808 |
-
}
|
| 809 |
-
|
| 810 |
-
// Main game loop
|
| 811 |
-
function gameLoop() {
|
| 812 |
-
if (gameState === 'playing') {
|
| 813 |
-
// Update
|
| 814 |
-
predictLocalPlayer();
|
| 815 |
-
updatePlatforms();
|
| 816 |
-
updateParticles();
|
| 817 |
-
updateCamera();
|
| 818 |
-
|
| 819 |
-
// Interpolate other players
|
| 820 |
-
for (const [, other] of otherPlayers) {
|
| 821 |
-
other.x += (other.targetX - other.x) * 0.2;
|
| 822 |
-
other.y += (other.targetY - other.y) * 0.2;
|
| 823 |
-
}
|
| 824 |
-
|
| 825 |
-
// Send input to server
|
| 826 |
-
inputSeq++;
|
| 827 |
-
socket.emit('input', {
|
| 828 |
-
left: inputState.left,
|
| 829 |
-
right: inputState.right,
|
| 830 |
-
seq: inputSeq
|
| 831 |
-
});
|
| 832 |
-
}
|
| 833 |
-
|
| 834 |
-
// Draw
|
| 835 |
-
drawBackground();
|
| 836 |
-
|
| 837 |
-
if (gameState === 'playing') {
|
| 838 |
-
// Draw platforms
|
| 839 |
-
const platforms = getPlatformsInRange(
|
| 840 |
-
cameraY - 100,
|
| 841 |
-
cameraY + GAME_HEIGHT + 100
|
| 842 |
-
);
|
| 843 |
-
for (const platform of platforms) {
|
| 844 |
-
drawPlatform(platform);
|
| 845 |
-
}
|
| 846 |
-
|
| 847 |
-
// Draw other players
|
| 848 |
-
for (const [, other] of otherPlayers) {
|
| 849 |
-
if (other.alive) {
|
| 850 |
-
drawPlayer(other, false);
|
| 851 |
-
}
|
| 852 |
-
}
|
| 853 |
-
|
| 854 |
-
// Draw particles
|
| 855 |
-
drawParticles();
|
| 856 |
-
|
| 857 |
-
// Draw local player
|
| 858 |
-
if (localPlayer.alive) {
|
| 859 |
-
drawPlayer(localPlayer, true);
|
| 860 |
-
}
|
| 861 |
-
|
| 862 |
-
// Draw HUD
|
| 863 |
-
drawHUD();
|
| 864 |
-
}
|
| 865 |
-
|
| 866 |
-
requestAnimationFrame(gameLoop);
|
| 867 |
-
}
|
| 868 |
-
|
| 869 |
-
// Input handling
|
| 870 |
-
function handleKeyDown(e) {
|
| 871 |
-
if (e.key === 'ArrowLeft' || e.key === 'a' || e.key === 'A') {
|
| 872 |
-
inputState.left = true;
|
| 873 |
-
}
|
| 874 |
-
if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') {
|
| 875 |
-
inputState.right = true;
|
| 876 |
-
}
|
| 877 |
-
}
|
| 878 |
-
|
| 879 |
-
function handleKeyUp(e) {
|
| 880 |
-
if (e.key === 'ArrowLeft' || e.key === 'a' || e.key === 'A') {
|
| 881 |
-
inputState.left = false;
|
| 882 |
-
}
|
| 883 |
-
if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') {
|
| 884 |
-
inputState.right = false;
|
| 885 |
-
}
|
| 886 |
-
}
|
| 887 |
-
|
| 888 |
-
function handleTouchStart(e) {
|
| 889 |
-
e.preventDefault();
|
| 890 |
-
updateTouchInput(e.touches);
|
| 891 |
-
}
|
| 892 |
-
|
| 893 |
-
function handleTouchMove(e) {
|
| 894 |
-
e.preventDefault();
|
| 895 |
-
updateTouchInput(e.touches);
|
| 896 |
-
}
|
| 897 |
-
|
| 898 |
-
function handleTouchEnd(e) {
|
| 899 |
-
e.preventDefault();
|
| 900 |
-
if (e.touches.length === 0) {
|
| 901 |
-
inputState.left = false;
|
| 902 |
-
inputState.right = false;
|
| 903 |
-
} else {
|
| 904 |
-
updateTouchInput(e.touches);
|
| 905 |
-
}
|
| 906 |
-
}
|
| 907 |
-
|
| 908 |
-
function updateTouchInput(touches) {
|
| 909 |
-
inputState.left = false;
|
| 910 |
-
inputState.right = false;
|
| 911 |
-
|
| 912 |
-
const rect = canvas.getBoundingClientRect();
|
| 913 |
-
const centerX = rect.left + rect.width / 2;
|
| 914 |
-
|
| 915 |
-
for (const touch of touches) {
|
| 916 |
-
if (touch.clientX < centerX) {
|
| 917 |
-
inputState.left = true;
|
| 918 |
-
} else {
|
| 919 |
-
inputState.right = true;
|
| 920 |
-
}
|
| 921 |
-
}
|
| 922 |
-
}
|
| 923 |
-
|
| 924 |
-
document.addEventListener('keydown', handleKeyDown);
|
| 925 |
-
document.addEventListener('keyup', handleKeyUp);
|
| 926 |
-
canvas.addEventListener('touchstart', handleTouchStart, { passive: false });
|
| 927 |
-
canvas.addEventListener('touchmove', handleTouchMove, { passive: false });
|
| 928 |
-
canvas.addEventListener('touchend', handleTouchEnd, { passive: false });
|
| 929 |
-
|
| 930 |
-
// Game over
|
| 931 |
-
function onGameOver() {
|
| 932 |
-
gameState = 'gameover';
|
| 933 |
-
|
| 934 |
-
// Update personal best
|
| 935 |
-
if (localPlayer.score > personalBest) {
|
| 936 |
-
personalBest = localPlayer.score;
|
| 937 |
-
localStorage.setItem('jumpHighScore', personalBest.toString());
|
| 938 |
-
highScoreText.textContent = 'π NEW HIGH SCORE! π';
|
| 939 |
-
} else {
|
| 940 |
-
highScoreText.textContent = `Your best: ${personalBest}`;
|
| 941 |
-
}
|
| 942 |
-
|
| 943 |
-
finalScore.textContent = `Score: ${localPlayer.score}`;
|
| 944 |
-
renderLeaderboard(gameOverLeaderboard);
|
| 945 |
-
|
| 946 |
-
menuScreen.classList.add('hidden');
|
| 947 |
-
gameOverScreen.classList.remove('hidden');
|
| 948 |
-
}
|
| 949 |
-
|
| 950 |
-
// Render leaderboard
|
| 951 |
-
function renderLeaderboard(container) {
|
| 952 |
-
container.innerHTML = '<div style="margin-top: 15px; font-size: 14px; color: #aaa;">Top Scores</div>';
|
| 953 |
-
|
| 954 |
-
for (let i = 0; i < Math.min(5, leaderboard.length); i++) {
|
| 955 |
-
const entry = leaderboard[i];
|
| 956 |
-
const div = document.createElement('div');
|
| 957 |
-
div.className = 'leaderboard-entry';
|
| 958 |
-
div.innerHTML = `
|
| 959 |
-
<span class="rank">#${i + 1}</span>
|
| 960 |
-
<span>${escapeHtml(entry.username)}</span>
|
| 961 |
-
<span>${entry.score}</span>
|
| 962 |
-
`;
|
| 963 |
-
container.appendChild(div);
|
| 964 |
-
}
|
| 965 |
-
}
|
| 966 |
-
|
| 967 |
-
function escapeHtml(text) {
|
| 968 |
-
const div = document.createElement('div');
|
| 969 |
-
div.textContent = text;
|
| 970 |
-
return div.innerHTML;
|
| 971 |
-
}
|
| 972 |
-
|
| 973 |
-
// Socket connection
|
| 974 |
-
function connectSocket() {
|
| 975 |
-
socket = io({
|
| 976 |
-
reconnection: true,
|
| 977 |
-
reconnectionAttempts: Infinity,
|
| 978 |
-
reconnectionDelay: 1000,
|
| 979 |
-
reconnectionDelayMax: 5000
|
| 980 |
-
});
|
| 981 |
-
|
| 982 |
-
socket.on('connect', () => {
|
| 983 |
-
updateConnectionStatus('connected', 'Connected');
|
| 984 |
-
|
| 985 |
-
if (gameState === 'playing' || gameState === 'gameover') {
|
| 986 |
-
// Rejoin game
|
| 987 |
-
const username = localStorage.getItem('jumpUsername') || 'Player';
|
| 988 |
-
socket.emit('join', { username });
|
| 989 |
-
}
|
| 990 |
-
});
|
| 991 |
-
|
| 992 |
-
socket.on('disconnect', () => {
|
| 993 |
-
updateConnectionStatus('disconnected', 'Disconnected');
|
| 994 |
-
});
|
| 995 |
-
|
| 996 |
-
socket.on('connect_error', () => {
|
| 997 |
-
updateConnectionStatus('disconnected', 'Connection Error');
|
| 998 |
-
});
|
| 999 |
-
|
| 1000 |
-
socket.on('init', (data) => {
|
| 1001 |
-
playerId = data.playerId;
|
| 1002 |
-
worldSeed = data.worldSeed;
|
| 1003 |
-
leaderboard = data.leaderboard || [];
|
| 1004 |
-
|
| 1005 |
-
// Clear caches
|
| 1006 |
-
platformCache.clear();
|
| 1007 |
-
brokenPlatforms.clear();
|
| 1008 |
-
|
| 1009 |
-
renderLeaderboard(leaderboardList);
|
| 1010 |
-
});
|
| 1011 |
-
|
| 1012 |
-
socket.on('state', (state) => {
|
| 1013 |
-
if (gameState === 'playing') {
|
| 1014 |
-
interpolateOtherPlayers(state);
|
| 1015 |
-
}
|
| 1016 |
-
});
|
| 1017 |
-
|
| 1018 |
-
socket.on('leaderboard', (data) => {
|
| 1019 |
-
leaderboard = data;
|
| 1020 |
-
renderLeaderboard(leaderboardList);
|
| 1021 |
-
});
|
| 1022 |
-
}
|
| 1023 |
-
|
| 1024 |
-
function updateConnectionStatus(status, text) {
|
| 1025 |
-
connectionStatus.textContent = text;
|
| 1026 |
-
connectionStatus.className = `status-${status}`;
|
| 1027 |
-
|
| 1028 |
-
if (status === 'connected') {
|
| 1029 |
-
setTimeout(() => {
|
| 1030 |
-
connectionStatus.style.opacity = '0';
|
| 1031 |
-
}, 2000);
|
| 1032 |
-
} else {
|
| 1033 |
-
connectionStatus.style.opacity = '1';
|
| 1034 |
-
}
|
| 1035 |
-
}
|
| 1036 |
-
|
| 1037 |
-
// Start game
|
| 1038 |
-
function startGame() {
|
| 1039 |
-
const username = usernameInput.value.trim() || 'Player';
|
| 1040 |
-
localStorage.setItem('jumpUsername', username);
|
| 1041 |
-
|
| 1042 |
-
// Reset local player
|
| 1043 |
-
localPlayer = {
|
| 1044 |
-
x: GAME_WIDTH / 2 - PLAYER_WIDTH / 2,
|
| 1045 |
-
y: 0,
|
| 1046 |
-
vx: 0,
|
| 1047 |
-
vy: 0,
|
| 1048 |
-
score: 0,
|
| 1049 |
-
highestY: 0,
|
| 1050 |
-
alive: true,
|
| 1051 |
-
facingRight: true,
|
| 1052 |
-
color: '#00ff88'
|
| 1053 |
-
};
|
| 1054 |
-
|
| 1055 |
-
// Reset camera
|
| 1056 |
-
cameraY = 0;
|
| 1057 |
-
targetCameraY = 0;
|
| 1058 |
-
|
| 1059 |
-
// Clear caches
|
| 1060 |
-
platformCache.clear();
|
| 1061 |
-
brokenPlatforms.clear();
|
| 1062 |
-
particles = [];
|
| 1063 |
-
otherPlayers.clear();
|
| 1064 |
-
|
| 1065 |
-
// Join server
|
| 1066 |
-
socket.emit('join', { username });
|
| 1067 |
-
|
| 1068 |
-
// Hide menus
|
| 1069 |
-
menuScreen.classList.add('hidden');
|
| 1070 |
-
gameOverScreen.classList.add('hidden');
|
| 1071 |
-
|
| 1072 |
-
gameState = 'playing';
|
| 1073 |
-
}
|
| 1074 |
-
|
| 1075 |
-
function restartGame() {
|
| 1076 |
-
socket.emit('restart');
|
| 1077 |
-
|
| 1078 |
-
// Reset local player
|
| 1079 |
-
localPlayer = {
|
| 1080 |
-
x: GAME_WIDTH / 2 - PLAYER_WIDTH / 2,
|
| 1081 |
-
y: 0,
|
| 1082 |
-
vx: 0,
|
| 1083 |
-
vy: 0,
|
| 1084 |
-
score: 0,
|
| 1085 |
-
highestY: 0,
|
| 1086 |
-
alive: true,
|
| 1087 |
-
facingRight: true,
|
| 1088 |
-
color: localPlayer.color
|
| 1089 |
-
};
|
| 1090 |
-
|
| 1091 |
-
// Reset camera
|
| 1092 |
-
cameraY = 0;
|
| 1093 |
-
targetCameraY = 0;
|
| 1094 |
-
|
| 1095 |
-
// Clear particles
|
| 1096 |
-
particles = [];
|
| 1097 |
-
|
| 1098 |
-
// Hide game over screen
|
| 1099 |
-
gameOverScreen.classList.add('hidden');
|
| 1100 |
-
|
| 1101 |
-
gameState = 'playing';
|
| 1102 |
}
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1118 |
</script>
|
| 1119 |
</body>
|
| 1120 |
</html>
|
|
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
|
| 6 |
+
<title>Jump!</title>
|
|
|
|
|
|
|
| 7 |
<style>
|
| 8 |
+
*{margin:0;padding:0;box-sizing:border-box;touch-action:none;user-select:none}
|
| 9 |
+
html,body{width:100%;height:100%;overflow:hidden;background:#0a0a1a;font-family:Arial,sans-serif}
|
| 10 |
+
#C{position:fixed;top:50%;left:50%;transform:translate(-50%,-50%)}
|
| 11 |
+
.menu{position:fixed;top:0;left:0;width:100%;height:100%;display:flex;justify-content:center;align-items:center;background:rgba(0,0,0,0.85);z-index:10}
|
| 12 |
+
.menu.hidden{display:none}
|
| 13 |
+
.box{background:#1a1a2e;border:2px solid #333;border-radius:16px;padding:24px;text-align:center;color:#fff;width:90%;max-width:340px}
|
| 14 |
+
.box h1{font-size:32px;margin-bottom:16px;background:linear-gradient(45deg,#0f8,#0af);-webkit-background-clip:text;-webkit-text-fill-color:transparent}
|
| 15 |
+
.box input{width:100%;padding:12px;font-size:16px;border:2px solid #444;border-radius:8px;background:#252540;color:#fff;margin-bottom:12px;text-align:center}
|
| 16 |
+
.box input:focus{outline:none;border-color:#0f8}
|
| 17 |
+
.box button{width:100%;padding:14px;font-size:18px;font-weight:bold;border:none;border-radius:8px;background:linear-gradient(45deg,#0f8,#0af);color:#000;cursor:pointer}
|
| 18 |
+
.box button:active{transform:scale(0.98)}
|
| 19 |
+
.lb{margin-top:16px;text-align:left;max-height:180px;overflow-y:auto}
|
| 20 |
+
.lb-title{font-size:14px;color:#888;margin-bottom:8px;text-align:center}
|
| 21 |
+
.lb-row{display:flex;justify-content:space-between;padding:6px 8px;border-bottom:1px solid #333;font-size:13px}
|
| 22 |
+
.lb-row:nth-child(2){color:gold}
|
| 23 |
+
.lb-row:nth-child(3){color:silver}
|
| 24 |
+
.lb-row:nth-child(4){color:#cd7f32}
|
| 25 |
+
.score-box{font-size:28px;margin:12px 0;color:#0f8}
|
| 26 |
+
.sub{font-size:13px;color:#888;margin-top:12px}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
</style>
|
| 28 |
</head>
|
| 29 |
<body>
|
| 30 |
+
<canvas id="C"></canvas>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
<div id="menuScreen" class="menu">
|
| 33 |
+
<div class="box">
|
| 34 |
<h1>π JUMP!</h1>
|
| 35 |
+
<input type="text" id="nameInput" placeholder="Your name" maxlength="12">
|
| 36 |
+
<button id="playBtn">PLAY</button>
|
| 37 |
+
<div class="lb" id="menuLB"></div>
|
| 38 |
+
<div class="sub">Arrow keys / Touch to move</div>
|
|
|
|
|
|
|
|
|
|
| 39 |
</div>
|
| 40 |
+
</div>
|
| 41 |
+
|
| 42 |
+
<div id="overScreen" class="menu hidden">
|
| 43 |
+
<div class="box">
|
| 44 |
<h1>GAME OVER</h1>
|
| 45 |
+
<div class="score-box" id="finalScore">0</div>
|
| 46 |
+
<div id="newRecord" style="color:#ff0;margin-bottom:12px"></div>
|
| 47 |
+
<button id="retryBtn">PLAY AGAIN</button>
|
| 48 |
+
<div class="lb" id="overLB"></div>
|
| 49 |
</div>
|
| 50 |
</div>
|
| 51 |
|
| 52 |
<script src="/socket.io/socket.io.js"></script>
|
| 53 |
<script>
|
| 54 |
+
// === CONFIG ===
|
| 55 |
+
const W = 540, H = 960;
|
| 56 |
+
const GRAVITY = 0.4, JUMP = -14, SPRING_JUMP = -22;
|
| 57 |
+
const PLAYER_W = 36, PLAYER_H = 44;
|
| 58 |
+
const PLAT_W = 70, PLAT_H = 14;
|
| 59 |
+
const SPEED = 6;
|
| 60 |
+
|
| 61 |
+
// === SETUP ===
|
| 62 |
+
const canvas = document.getElementById('C');
|
| 63 |
+
const ctx = canvas.getContext('2d');
|
| 64 |
+
canvas.width = W;
|
| 65 |
+
canvas.height = H;
|
| 66 |
+
|
| 67 |
+
function resize() {
|
| 68 |
+
const s = Math.min(innerWidth / W, innerHeight / H);
|
| 69 |
+
canvas.style.width = W * s + 'px';
|
| 70 |
+
canvas.style.height = H * s + 'px';
|
| 71 |
+
}
|
| 72 |
+
resize();
|
| 73 |
+
onresize = resize;
|
| 74 |
+
|
| 75 |
+
// === ELEMENTS ===
|
| 76 |
+
const menuScreen = document.getElementById('menuScreen');
|
| 77 |
+
const overScreen = document.getElementById('overScreen');
|
| 78 |
+
const nameInput = document.getElementById('nameInput');
|
| 79 |
+
const playBtn = document.getElementById('playBtn');
|
| 80 |
+
const retryBtn = document.getElementById('retryBtn');
|
| 81 |
+
const finalScoreEl = document.getElementById('finalScore');
|
| 82 |
+
const newRecordEl = document.getElementById('newRecord');
|
| 83 |
+
const menuLB = document.getElementById('menuLB');
|
| 84 |
+
const overLB = document.getElementById('overLB');
|
| 85 |
+
|
| 86 |
+
// === STATE ===
|
| 87 |
+
let socket, myId;
|
| 88 |
+
let state = 'menu';
|
| 89 |
+
let leaderboard = [];
|
| 90 |
+
let others = [];
|
| 91 |
+
let highScore = parseInt(localStorage.getItem('hs')) || 0;
|
| 92 |
+
let name = localStorage.getItem('name') || '';
|
| 93 |
+
nameInput.value = name;
|
| 94 |
+
|
| 95 |
+
// Player
|
| 96 |
+
let player = null;
|
| 97 |
+
let platforms = [];
|
| 98 |
+
let particles = [];
|
| 99 |
+
let camY = 0;
|
| 100 |
+
let seed = 12345;
|
| 101 |
+
|
| 102 |
+
// Input
|
| 103 |
+
let keys = { left: false, right: false };
|
| 104 |
+
|
| 105 |
+
// === SEEDED RANDOM ===
|
| 106 |
+
function seededRand(s) {
|
| 107 |
+
return function() {
|
| 108 |
+
s = (s * 16807) % 2147483647;
|
| 109 |
+
return (s - 1) / 2147483646;
|
| 110 |
+
};
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// === GENERATE PLATFORMS ===
|
| 114 |
+
function genPlatforms() {
|
| 115 |
+
platforms = [];
|
| 116 |
+
const rand = seededRand(seed);
|
| 117 |
+
let y = H - 100;
|
| 118 |
+
|
| 119 |
+
// Starting platform
|
| 120 |
+
platforms.push({ x: W/2 - PLAT_W/2, y: y, type: 0, broken: false });
|
| 121 |
+
y -= 60;
|
| 122 |
+
|
| 123 |
+
while (y > -50000) {
|
| 124 |
+
const difficulty = Math.min(Math.abs(y) / 20000, 1);
|
| 125 |
+
const gap = 50 + rand() * (60 + difficulty * 40);
|
| 126 |
+
y -= gap;
|
| 127 |
+
|
| 128 |
+
const x = 30 + rand() * (W - PLAT_W - 60);
|
| 129 |
+
let type = 0; // normal
|
| 130 |
+
|
| 131 |
+
if (Math.abs(y) > 2000) {
|
| 132 |
+
const r = rand();
|
| 133 |
+
if (r < 0.08) type = 2; // breakable
|
| 134 |
+
else if (r < 0.15) type = 1; // moving
|
| 135 |
+
else if (r < 0.22) type = 3; // spring
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
platforms.push({
|
| 139 |
+
x, y, type,
|
| 140 |
+
broken: false,
|
| 141 |
+
dir: rand() > 0.5 ? 1 : -1,
|
| 142 |
+
speed: 1 + rand() * 2,
|
| 143 |
+
ox: x
|
| 144 |
+
});
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
// === INIT PLAYER ===
|
| 149 |
+
function initPlayer(saved) {
|
| 150 |
+
player = {
|
| 151 |
+
x: saved?.x ?? W/2 - PLAYER_W/2,
|
| 152 |
+
y: saved?.y ?? H - 150,
|
| 153 |
+
vx: 0,
|
| 154 |
+
vy: 0,
|
| 155 |
+
score: saved?.score ?? 0,
|
| 156 |
+
maxY: saved?.y ?? H - 150,
|
| 157 |
+
alive: true,
|
| 158 |
+
color: saved?.color ?? `hsl(${Math.random()*360},70%,60%)`,
|
| 159 |
+
facingRight: true
|
| 160 |
+
};
|
| 161 |
+
camY = Math.min(0, player.y - H * 0.4);
|
| 162 |
+
genPlatforms();
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// === PARTICLES ===
|
| 166 |
+
function spawnParticles(x, y, color, n) {
|
| 167 |
+
for (let i = 0; i < n; i++) {
|
| 168 |
+
particles.push({
|
| 169 |
+
x, y,
|
| 170 |
+
vx: (Math.random() - 0.5) * 8,
|
| 171 |
+
vy: Math.random() * 3,
|
| 172 |
+
life: 1,
|
| 173 |
+
color,
|
| 174 |
+
size: 2 + Math.random() * 4
|
| 175 |
+
});
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// === UPDATE ===
|
| 180 |
+
function update() {
|
| 181 |
+
if (state !== 'playing' || !player.alive) return;
|
| 182 |
+
|
| 183 |
+
// Input
|
| 184 |
+
if (keys.left) { player.vx = -SPEED; player.facingRight = false; }
|
| 185 |
+
else if (keys.right) { player.vx = SPEED; player.facingRight = true; }
|
| 186 |
+
else player.vx *= 0.85;
|
| 187 |
+
|
| 188 |
+
// Physics
|
| 189 |
+
player.vy += GRAVITY;
|
| 190 |
+
player.vy = Math.min(player.vy, 18);
|
| 191 |
+
player.x += player.vx;
|
| 192 |
+
player.y += player.vy;
|
| 193 |
+
|
| 194 |
+
// Wrap
|
| 195 |
+
if (player.x < -PLAYER_W) player.x = W;
|
| 196 |
+
if (player.x > W) player.x = -PLAYER_W;
|
| 197 |
+
|
| 198 |
+
// Platform collision (only falling)
|
| 199 |
+
if (player.vy > 0) {
|
| 200 |
+
for (const p of platforms) {
|
| 201 |
+
if (p.broken) continue;
|
| 202 |
+
if (p.y < camY - 100 || p.y > camY + H + 100) continue;
|
| 203 |
+
|
| 204 |
+
if (player.x + PLAYER_W > p.x && player.x < p.x + PLAT_W &&
|
| 205 |
+
player.y + PLAYER_H >= p.y && player.y + PLAYER_H <= p.y + PLAT_H + player.vy + 2) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
+
if (p.type === 2) { // breakable
|
| 208 |
+
p.broken = true;
|
| 209 |
+
spawnParticles(p.x + PLAT_W/2, p.y, '#a63', 8);
|
| 210 |
+
continue;
|
| 211 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
player.y = p.y - PLAYER_H;
|
| 214 |
+
player.vy = p.type === 3 ? SPRING_JUMP : JUMP;
|
| 215 |
+
spawnParticles(player.x + PLAYER_W/2, player.y + PLAYER_H, player.color, p.type === 3 ? 15 : 8);
|
| 216 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
}
|
| 218 |
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
// Update moving platforms
|
| 222 |
+
for (const p of platforms) {
|
| 223 |
+
if (p.type === 1 && !p.broken) {
|
| 224 |
+
p.x += p.speed * p.dir;
|
| 225 |
+
if (p.x < 20 || p.x > W - PLAT_W - 20) p.dir *= -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// Score
|
| 230 |
+
if (player.y < player.maxY) {
|
| 231 |
+
player.maxY = player.y;
|
| 232 |
+
player.score = Math.floor((H - 150 - player.y) / 10);
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
// Camera
|
| 236 |
+
const targetCam = Math.min(0, player.y - H * 0.4);
|
| 237 |
+
camY += (targetCam - camY) * 0.1;
|
| 238 |
+
|
| 239 |
+
// Death
|
| 240 |
+
if (player.y > camY + H + 100) {
|
| 241 |
+
player.alive = false;
|
| 242 |
+
gameOver();
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// Particles
|
| 246 |
+
for (let i = particles.length - 1; i >= 0; i--) {
|
| 247 |
+
const p = particles[i];
|
| 248 |
+
p.x += p.vx;
|
| 249 |
+
p.y += p.vy;
|
| 250 |
+
p.vy += 0.15;
|
| 251 |
+
p.life -= 0.03;
|
| 252 |
+
if (p.life <= 0) particles.splice(i, 1);
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
// Send to server
|
| 256 |
+
if (socket?.connected) {
|
| 257 |
+
socket.emit('update', {
|
| 258 |
+
name: name,
|
| 259 |
+
x: player.x,
|
| 260 |
+
y: player.y,
|
| 261 |
+
vy: player.vy,
|
| 262 |
+
score: player.score,
|
| 263 |
+
color: player.color,
|
| 264 |
+
alive: player.alive,
|
| 265 |
+
facingRight: player.facingRight
|
| 266 |
+
});
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
// === DRAW ===
|
| 271 |
+
function draw() {
|
| 272 |
+
// Background
|
| 273 |
+
ctx.fillStyle = '#0f0f23';
|
| 274 |
+
ctx.fillRect(0, 0, W, H);
|
| 275 |
+
|
| 276 |
+
// Grid
|
| 277 |
+
ctx.strokeStyle = 'rgba(50,50,80,0.3)';
|
| 278 |
+
ctx.lineWidth = 1;
|
| 279 |
+
const gridY = (-camY * 0.3) % 50;
|
| 280 |
+
for (let y = gridY; y < H; y += 50) {
|
| 281 |
+
ctx.beginPath();
|
| 282 |
+
ctx.moveTo(0, y);
|
| 283 |
+
ctx.lineTo(W, y);
|
| 284 |
+
ctx.stroke();
|
| 285 |
+
}
|
| 286 |
+
for (let x = 0; x < W; x += 50) {
|
| 287 |
+
ctx.beginPath();
|
| 288 |
+
ctx.moveTo(x, 0);
|
| 289 |
+
ctx.lineTo(x, H);
|
| 290 |
+
ctx.stroke();
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
// Stars
|
| 294 |
+
ctx.fillStyle = 'rgba(255,255,255,0.4)';
|
| 295 |
+
const starRand = seededRand(99);
|
| 296 |
+
for (let i = 0; i < 40; i++) {
|
| 297 |
+
const sx = starRand() * W;
|
| 298 |
+
const sy = ((starRand() * H * 3) - camY * 0.1) % H;
|
| 299 |
+
ctx.beginPath();
|
| 300 |
+
ctx.arc(sx, sy, starRand() * 2 + 1, 0, Math.PI * 2);
|
| 301 |
+
ctx.fill();
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
if (state !== 'playing') return;
|
| 305 |
+
|
| 306 |
+
// Platforms
|
| 307 |
+
for (const p of platforms) {
|
| 308 |
+
if (p.broken) continue;
|
| 309 |
+
const sy = p.y - camY;
|
| 310 |
+
if (sy < -50 || sy > H + 50) continue;
|
| 311 |
+
|
| 312 |
+
const colors = ['#4d4', '#48f', '#a63', '#fd0'];
|
| 313 |
+
ctx.fillStyle = colors[p.type];
|
| 314 |
+
ctx.beginPath();
|
| 315 |
+
ctx.roundRect(p.x, sy, PLAT_W, PLAT_H, 6);
|
| 316 |
+
ctx.fill();
|
| 317 |
+
|
| 318 |
+
// Spring
|
| 319 |
+
if (p.type === 3) {
|
| 320 |
+
ctx.strokeStyle = '#fc0';
|
| 321 |
+
ctx.lineWidth = 3;
|
| 322 |
ctx.beginPath();
|
| 323 |
+
const cx = p.x + PLAT_W/2;
|
| 324 |
+
ctx.moveTo(cx - 8, sy);
|
| 325 |
+
ctx.lineTo(cx + 8, sy - 6);
|
| 326 |
+
ctx.lineTo(cx - 8, sy - 12);
|
| 327 |
+
ctx.lineTo(cx + 8, sy - 18);
|
| 328 |
ctx.stroke();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
// Other players
|
| 333 |
+
for (const o of others) {
|
| 334 |
+
if (o.id === myId) continue;
|
| 335 |
+
const sy = o.y - camY;
|
| 336 |
+
if (sy < -100 || sy > H + 100) continue;
|
| 337 |
+
drawPlayer(o.x, sy, o.color, o.facingRight, o.name, false);
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
// Particles
|
| 341 |
+
for (const p of particles) {
|
| 342 |
+
ctx.globalAlpha = p.life;
|
| 343 |
+
ctx.fillStyle = p.color;
|
| 344 |
+
ctx.beginPath();
|
| 345 |
+
ctx.arc(p.x, p.y - camY, p.size, 0, Math.PI * 2);
|
| 346 |
+
ctx.fill();
|
| 347 |
+
}
|
| 348 |
+
ctx.globalAlpha = 1;
|
| 349 |
+
|
| 350 |
+
// Local player
|
| 351 |
+
if (player.alive) {
|
| 352 |
+
drawPlayer(player.x, player.y - camY, player.color, player.facingRight, 'YOU', true);
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
// HUD
|
| 356 |
+
ctx.fillStyle = '#fff';
|
| 357 |
+
ctx.font = 'bold 28px Arial';
|
| 358 |
+
ctx.textAlign = 'left';
|
| 359 |
+
ctx.fillText(player.score, 20, 40);
|
| 360 |
+
|
| 361 |
+
ctx.font = '14px Arial';
|
| 362 |
+
ctx.fillStyle = '#888';
|
| 363 |
+
ctx.fillText(`Best: ${Math.max(highScore, player.score)}`, 20, 60);
|
| 364 |
+
|
| 365 |
+
ctx.textAlign = 'right';
|
| 366 |
+
ctx.fillStyle = '#6af';
|
| 367 |
+
ctx.fillText(`${others.length} online`, W - 20, 40);
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
function drawPlayer(x, y, color, facingRight, label, isLocal) {
|
| 371 |
+
const cx = x + PLAYER_W/2;
|
| 372 |
+
const cy = y + PLAYER_H/2;
|
| 373 |
+
|
| 374 |
+
// Shadow
|
| 375 |
+
ctx.fillStyle = 'rgba(0,0,0,0.3)';
|
| 376 |
+
ctx.beginPath();
|
| 377 |
+
ctx.ellipse(cx, y + PLAYER_H + 4, PLAYER_W/2, 6, 0, 0, Math.PI * 2);
|
| 378 |
+
ctx.fill();
|
| 379 |
+
|
| 380 |
+
// Body
|
| 381 |
+
ctx.fillStyle = color;
|
| 382 |
+
ctx.beginPath();
|
| 383 |
+
ctx.ellipse(cx, cy, PLAYER_W/2, PLAYER_H/2, 0, 0, Math.PI * 2);
|
| 384 |
+
ctx.fill();
|
| 385 |
+
|
| 386 |
+
// Eyes
|
| 387 |
+
const ex = facingRight ? 4 : -4;
|
| 388 |
+
ctx.fillStyle = '#fff';
|
| 389 |
+
ctx.beginPath();
|
| 390 |
+
ctx.ellipse(cx - 7 + ex, cy - 6, 7, 9, 0, 0, Math.PI * 2);
|
| 391 |
+
ctx.ellipse(cx + 7 + ex, cy - 6, 7, 9, 0, 0, Math.PI * 2);
|
| 392 |
+
ctx.fill();
|
| 393 |
+
|
| 394 |
+
ctx.fillStyle = '#222';
|
| 395 |
+
ctx.beginPath();
|
| 396 |
+
ctx.arc(cx - 5 + ex * 1.5, cy - 6, 3.5, 0, Math.PI * 2);
|
| 397 |
+
ctx.arc(cx + 9 + ex * 1.5, cy - 6, 3.5, 0, Math.PI * 2);
|
| 398 |
+
ctx.fill();
|
| 399 |
+
|
| 400 |
+
// Mouth
|
| 401 |
+
ctx.strokeStyle = '#222';
|
| 402 |
+
ctx.lineWidth = 2;
|
| 403 |
+
ctx.beginPath();
|
| 404 |
+
ctx.arc(cx + ex * 0.5, cy + 8, 6, 0.2, Math.PI - 0.2);
|
| 405 |
+
ctx.stroke();
|
| 406 |
+
|
| 407 |
+
// Label
|
| 408 |
+
ctx.font = 'bold 12px Arial';
|
| 409 |
+
ctx.textAlign = 'center';
|
| 410 |
+
ctx.fillStyle = isLocal ? '#0f8' : '#fff';
|
| 411 |
+
ctx.fillText(label, cx, y - 8);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// === GAME OVER ===
|
| 415 |
+
function gameOver() {
|
| 416 |
+
state = 'gameover';
|
| 417 |
+
|
| 418 |
+
const score = player.score;
|
| 419 |
+
finalScoreEl.textContent = score;
|
| 420 |
+
|
| 421 |
+
if (score > highScore) {
|
| 422 |
+
highScore = score;
|
| 423 |
+
localStorage.setItem('hs', highScore);
|
| 424 |
+
newRecordEl.textContent = 'π NEW RECORD! π';
|
| 425 |
+
} else {
|
| 426 |
+
newRecordEl.textContent = '';
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
if (socket?.connected) {
|
| 430 |
+
socket.emit('gameover', { name, score });
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
renderLB(overLB);
|
| 434 |
+
menuScreen.classList.add('hidden');
|
| 435 |
+
overScreen.classList.remove('hidden');
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
// === RENDER LEADERBOARD ===
|
| 439 |
+
function renderLB(el) {
|
| 440 |
+
el.innerHTML = '<div class="lb-title">π Leaderboard</div>';
|
| 441 |
+
for (let i = 0; i < Math.min(8, leaderboard.length); i++) {
|
| 442 |
+
const e = leaderboard[i];
|
| 443 |
+
el.innerHTML += `<div class="lb-row"><span>#${i+1} ${escapeHtml(e.name)}</span><span>${e.score}</span></div>`;
|
| 444 |
+
}
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
function escapeHtml(t) {
|
| 448 |
+
return t.replace(/</g, '<').replace(/>/g, '>');
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
// === START GAME ===
|
| 452 |
+
function startGame(saved) {
|
| 453 |
+
name = nameInput.value.trim() || 'Anon';
|
| 454 |
+
localStorage.setItem('name', name);
|
| 455 |
+
|
| 456 |
+
initPlayer(saved);
|
| 457 |
+
particles = [];
|
| 458 |
+
state = 'playing';
|
| 459 |
+
|
| 460 |
+
menuScreen.classList.add('hidden');
|
| 461 |
+
overScreen.classList.add('hidden');
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
// === INPUT ===
|
| 465 |
+
onkeydown = e => {
|
| 466 |
+
if (e.key === 'ArrowLeft' || e.key === 'a') keys.left = true;
|
| 467 |
+
if (e.key === 'ArrowRight' || e.key === 'd') keys.right = true;
|
| 468 |
+
};
|
| 469 |
+
onkeyup = e => {
|
| 470 |
+
if (e.key === 'ArrowLeft' || e.key === 'a') keys.left = false;
|
| 471 |
+
if (e.key === 'ArrowRight' || e.key === 'd') keys.right = false;
|
| 472 |
+
};
|
| 473 |
+
|
| 474 |
+
canvas.ontouchstart = canvas.ontouchmove = e => {
|
| 475 |
+
e.preventDefault();
|
| 476 |
+
keys.left = keys.right = false;
|
| 477 |
+
const rect = canvas.getBoundingClientRect();
|
| 478 |
+
const cx = rect.left + rect.width / 2;
|
| 479 |
+
for (const t of e.touches) {
|
| 480 |
+
if (t.clientX < cx) keys.left = true;
|
| 481 |
+
else keys.right = true;
|
| 482 |
+
}
|
| 483 |
+
};
|
| 484 |
+
canvas.ontouchend = e => {
|
| 485 |
+
e.preventDefault();
|
| 486 |
+
keys.left = keys.right = false;
|
| 487 |
+
const rect = canvas.getBoundingClientRect();
|
| 488 |
+
const cx = rect.left + rect.width / 2;
|
| 489 |
+
for (const t of e.touches) {
|
| 490 |
+
if (t.clientX < cx) keys.left = true;
|
| 491 |
+
else keys.right = true;
|
| 492 |
+
}
|
| 493 |
+
};
|
| 494 |
+
|
| 495 |
+
// === BUTTONS ===
|
| 496 |
+
playBtn.onclick = () => startGame(null);
|
| 497 |
+
retryBtn.onclick = () => startGame(null);
|
| 498 |
+
|
| 499 |
+
nameInput.onkeypress = e => { if (e.key === 'Enter') startGame(null); };
|
| 500 |
+
|
| 501 |
+
// === SOCKET ===
|
| 502 |
+
function connectSocket() {
|
| 503 |
+
socket = io({ reconnection: true, reconnectionDelay: 1000 });
|
| 504 |
+
|
| 505 |
+
socket.on('connect', () => {
|
| 506 |
+
myId = socket.id;
|
| 507 |
+
console.log('Connected');
|
| 508 |
+
});
|
| 509 |
+
|
| 510 |
+
socket.on('init', data => {
|
| 511 |
+
leaderboard = data.leaderboard || [];
|
| 512 |
+
renderLB(menuLB);
|
| 513 |
+
|
| 514 |
+
// Resume saved game
|
| 515 |
+
if (data.savedPlayer && data.savedPlayer.score > 0 && state === 'menu') {
|
| 516 |
+
nameInput.value = data.savedPlayer.name || name;
|
| 517 |
+
name = nameInput.value;
|
| 518 |
+
// Auto-resume option - uncomment to auto-start
|
| 519 |
+
// startGame(data.savedPlayer);
|
| 520 |
+
}
|
| 521 |
+
});
|
| 522 |
+
|
| 523 |
+
socket.on('players', data => {
|
| 524 |
+
others = data;
|
| 525 |
+
});
|
| 526 |
+
|
| 527 |
+
socket.on('leaderboard', data => {
|
| 528 |
+
leaderboard = data;
|
| 529 |
+
renderLB(menuLB);
|
| 530 |
+
renderLB(overLB);
|
| 531 |
+
});
|
| 532 |
+
|
| 533 |
+
socket.on('disconnect', () => {
|
| 534 |
+
console.log('Disconnected, reconnecting...');
|
| 535 |
+
});
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
// === GAME LOOP ===
|
| 539 |
+
function loop() {
|
| 540 |
+
update();
|
| 541 |
+
draw();
|
| 542 |
+
requestAnimationFrame(loop);
|
| 543 |
+
}
|
| 544 |
+
|
| 545 |
+
// === INIT ===
|
| 546 |
+
connectSocket();
|
| 547 |
+
loop();
|
| 548 |
</script>
|
| 549 |
</body>
|
| 550 |
</html>
|