Nafiul Huda commited on
Update public/index.html
Browse files- public/index.html +627 -541
public/index.html
CHANGED
|
@@ -1,660 +1,746 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
</head>
|
| 30 |
<body>
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
const
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
const
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
record: () => playTone(500, 1000, 0.5, 'sine')
|
| 80 |
-
};
|
| 81 |
-
|
| 82 |
-
function playTone(freqStart, freqEnd, dur, type, vol=0.05) {
|
| 83 |
-
if (!actx) return;
|
| 84 |
-
const osc = actx.createOscillator();
|
| 85 |
-
const gain = actx.createGain();
|
| 86 |
-
osc.type = type;
|
| 87 |
-
osc.frequency.setValueAtTime(freqStart, actx.currentTime);
|
| 88 |
-
osc.frequency.exponentialRampToValueAtTime(freqEnd, actx.currentTime + dur);
|
| 89 |
-
gain.gain.setValueAtTime(vol, actx.currentTime);
|
| 90 |
-
gain.gain.exponentialRampToValueAtTime(0.01, actx.currentTime + dur);
|
| 91 |
-
osc.connect(gain);
|
| 92 |
-
gain.connect(actx.destination);
|
| 93 |
-
osc.start();
|
| 94 |
-
osc.stop(actx.currentTime + dur);
|
| 95 |
-
}
|
| 96 |
|
| 97 |
-
//
|
| 98 |
-
const
|
| 99 |
-
const ctx = canvas.getContext('2d', { alpha: false });
|
| 100 |
-
canvas.width = W;
|
| 101 |
-
canvas.height = H;
|
| 102 |
|
| 103 |
-
|
| 104 |
-
const s = Math.min(innerWidth / W, innerHeight / H);
|
| 105 |
-
canvas.style.width = W * s + 'px';
|
| 106 |
-
canvas.style.height = H * s + 'px';
|
| 107 |
-
}
|
| 108 |
-
resize();
|
| 109 |
-
onresize = resize;
|
| 110 |
-
|
| 111 |
-
// === ELEMENTS ===
|
| 112 |
-
const menuScreen = document.getElementById('menuScreen');
|
| 113 |
-
const overScreen = document.getElementById('overScreen');
|
| 114 |
-
const nameInput = document.getElementById('nameInput');
|
| 115 |
-
const finalScoreEl = document.getElementById('finalScore');
|
| 116 |
-
const newRecordEl = document.getElementById('newRecord');
|
| 117 |
-
const menuLB = document.getElementById('menuLB');
|
| 118 |
-
const overLB = document.getElementById('overLB');
|
| 119 |
-
|
| 120 |
-
// === STATE ===
|
| 121 |
-
let socket, myId;
|
| 122 |
let state = 'menu';
|
| 123 |
-
let
|
| 124 |
-
let
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
//
|
| 130 |
-
let
|
| 131 |
-
let platforms = [];
|
| 132 |
-
let
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
// Input
|
| 139 |
-
let keys = {
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
platforms.push({ x: W/2 - PLAT_W/2, y: H - 100, type: 0, w: PLAT_W });
|
| 150 |
-
highestGenY = H - 100;
|
| 151 |
-
}
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
monsters.push({
|
| 180 |
-
x: rand(50, W - 50),
|
| 181 |
-
y: highestGenY - rand(40, 100),
|
| 182 |
-
vx: rand(1, 3) * (Math.random() < 0.5 ? 1 : -1),
|
| 183 |
-
angle: 0
|
| 184 |
-
});
|
| 185 |
-
}
|
| 186 |
-
}
|
| 187 |
|
| 188 |
-
//
|
| 189 |
-
|
| 190 |
-
|
|
|
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
});
|
| 198 |
}
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
}
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
}
|
| 221 |
|
| 222 |
-
function
|
| 223 |
-
for
|
| 224 |
particles.push({
|
| 225 |
x, y,
|
| 226 |
-
vx: (Math.random()
|
| 227 |
-
vy: (Math.random()
|
| 228 |
life: 1,
|
| 229 |
-
|
| 230 |
-
size: rand(3, 6)
|
| 231 |
});
|
| 232 |
}
|
| 233 |
}
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
//
|
| 245 |
-
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
|
| 252 |
// Wrap
|
| 253 |
-
if
|
| 254 |
-
if
|
| 255 |
|
| 256 |
-
//
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
if (player.x + PLAYER_W * 0.7 > p.x && player.x + PLAYER_W * 0.3 < p.x + p.w &&
|
| 268 |
-
pBottom >= p.y && pBottom <= p.y + p.w/2 + player.vy) { // relaxed y collision
|
| 269 |
-
|
| 270 |
-
// SPIKE DEATH
|
| 271 |
-
if (p.type === 4) {
|
| 272 |
-
die();
|
| 273 |
-
return;
|
| 274 |
-
}
|
| 275 |
-
|
| 276 |
-
// BREAKABLE
|
| 277 |
-
if (p.type === 2) {
|
| 278 |
-
p.broken = true;
|
| 279 |
-
sfx.break();
|
| 280 |
-
spawnParticles(p.x + p.w/2, p.y, '#a64', 8);
|
| 281 |
-
continue;
|
| 282 |
}
|
| 283 |
|
| 284 |
-
|
| 285 |
-
player.y = p.y - PLAYER_H;
|
| 286 |
-
player.vy = JUMP_FORCE;
|
| 287 |
-
sfx.jump();
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
| 295 |
} else {
|
| 296 |
-
|
|
|
|
| 297 |
}
|
|
|
|
| 298 |
}
|
| 299 |
}
|
| 300 |
}
|
| 301 |
-
|
| 302 |
-
//
|
| 303 |
-
for
|
| 304 |
-
if
|
| 305 |
-
|
| 306 |
-
if (p.x < 0 || p.x > W - p.w) p.vx *= -1;
|
| 307 |
-
}
|
| 308 |
-
if (p.springAnim > 0) p.springAnim--;
|
| 309 |
-
}
|
| 310 |
-
|
| 311 |
-
for (const m of monsters) {
|
| 312 |
-
m.x += m.vx;
|
| 313 |
-
if (m.x < 0 || m.x > W - 40) m.vx *= -1;
|
| 314 |
-
m.angle += 0.1;
|
| 315 |
-
|
| 316 |
-
// Monster Collision
|
| 317 |
-
const dx = (player.x + PLAYER_W/2) - (m.x);
|
| 318 |
-
const dy = (player.y + PLAYER_H/2) - (m.y);
|
| 319 |
-
const dist = Math.sqrt(dx*dx + dy*dy);
|
| 320 |
-
if (dist < 30) {
|
| 321 |
-
die();
|
| 322 |
-
return;
|
| 323 |
}
|
| 324 |
}
|
| 325 |
-
|
| 326 |
// Score
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
genPlatforms();
|
| 332 |
}
|
| 333 |
|
| 334 |
-
//
|
| 335 |
-
if
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
//
|
| 338 |
-
for
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
}
|
|
|
|
|
|
|
| 344 |
|
| 345 |
-
//
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
y: Math.round(player.y),
|
| 351 |
-
vx: player.vx,
|
| 352 |
-
vy: player.vy,
|
| 353 |
-
score: player.score,
|
| 354 |
-
color: player.color,
|
| 355 |
-
alive: player.alive,
|
| 356 |
-
facingRight: player.facingRight
|
| 357 |
-
});
|
| 358 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
}
|
| 360 |
|
| 361 |
-
function
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
//
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
// Parallax Stars
|
| 375 |
-
ctx.fillStyle = 'rgba(255,255,255,0.3)';
|
| 376 |
-
for(let i=0; i<50; i++) {
|
| 377 |
-
// pseudo random stars based on i
|
| 378 |
-
let x = (i * 137) % W;
|
| 379 |
-
let y = ((i * 311) - camY * (0.1 + (i%5)/10)) % H;
|
| 380 |
-
if (y < 0) y += H;
|
| 381 |
ctx.beginPath();
|
| 382 |
-
ctx.arc(x,
|
| 383 |
ctx.fill();
|
| 384 |
}
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
let col = '#0f8'; // normal
|
| 394 |
-
if (p.type === 1) col = '#0af'; // move
|
| 395 |
-
if (p.type === 2) col = '#a64'; // break
|
| 396 |
-
if (p.type === 4) col = '#666'; // spike platform base
|
| 397 |
-
|
| 398 |
-
// Shadow
|
| 399 |
-
ctx.fillStyle = 'rgba(0,0,0,0.5)';
|
| 400 |
-
ctx.fillRect(p.x + 4, sy + 4, p.w, PLAT_H);
|
| 401 |
|
| 402 |
-
|
| 403 |
-
ctx.fillStyle = col;
|
| 404 |
-
ctx.fillRect(p.x, sy, p.w, PLAT_H);
|
| 405 |
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
ctx.fillStyle = '#
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
| 415 |
ctx.beginPath();
|
| 416 |
-
|
| 417 |
-
ctx.moveTo(
|
| 418 |
-
ctx.lineTo(
|
| 419 |
-
ctx.lineTo(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
}
|
| 421 |
ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
}
|
| 423 |
|
| 424 |
-
|
| 425 |
-
if (p.type === 3) {
|
| 426 |
-
ctx.fillStyle = '#fd0';
|
| 427 |
-
ctx.fillRect(p.x + p.w/2 - 10, sy - 6 - p.springAnim, 20, 6 + p.springAnim);
|
| 428 |
-
}
|
| 429 |
}
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
|
|
|
|
|
|
| 438 |
|
| 439 |
-
ctx.fillStyle = '#f24';
|
| 440 |
ctx.beginPath();
|
| 441 |
-
ctx.arc(
|
| 442 |
ctx.fill();
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
ctx.fillStyle = '#fff';
|
|
|
|
| 445 |
ctx.beginPath();
|
| 446 |
-
ctx.arc(
|
|
|
|
| 447 |
ctx.fill();
|
|
|
|
| 448 |
ctx.fillStyle = '#000';
|
|
|
|
| 449 |
ctx.beginPath();
|
| 450 |
-
ctx.arc(
|
| 451 |
-
ctx.
|
| 452 |
-
// Wings
|
| 453 |
-
ctx.strokeStyle = '#f24';
|
| 454 |
-
ctx.lineWidth = 3;
|
| 455 |
-
ctx.beginPath();
|
| 456 |
-
ctx.moveTo(m.x-10, sy+bob); ctx.lineTo(m.x-28, sy+bob-10);
|
| 457 |
-
ctx.moveTo(m.x+10, sy+bob); ctx.lineTo(m.x+28, sy+bob-10);
|
| 458 |
-
ctx.stroke();
|
| 459 |
-
}
|
| 460 |
-
|
| 461 |
-
// Particles
|
| 462 |
-
for (const p of particles) {
|
| 463 |
-
ctx.globalAlpha = p.life;
|
| 464 |
-
ctx.fillStyle = p.color;
|
| 465 |
-
ctx.beginPath();
|
| 466 |
-
ctx.arc(p.x, p.y - camY, p.size, 0, Math.PI*2);
|
| 467 |
ctx.fill();
|
|
|
|
|
|
|
| 468 |
}
|
| 469 |
-
|
| 470 |
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
o.dx += (o.x - o.dx) * 0.2; // Smooth Lerp
|
| 475 |
-
o.dy += (o.y - o.dy) * 0.2;
|
| 476 |
-
if (o.dy - camY > -100 && o.dy - camY < H+100) {
|
| 477 |
-
drawPlayer(o.dx, o.dy - camY, o.c, o.f, o.n, false);
|
| 478 |
-
}
|
| 479 |
-
});
|
| 480 |
|
| 481 |
-
|
| 482 |
-
if (player.alive) {
|
| 483 |
-
drawPlayer(player.x, player.y - camY, player.color, player.facingRight, 'YOU', true);
|
| 484 |
-
}
|
| 485 |
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
ctx.strokeStyle = '#000';
|
| 491 |
-
ctx.lineWidth = 4;
|
| 492 |
-
ctx.textAlign = 'left';
|
| 493 |
-
ctx.strokeText(player.score, 20, 50);
|
| 494 |
-
ctx.fillText(player.score, 20, 50);
|
| 495 |
-
}
|
| 496 |
-
}
|
| 497 |
-
|
| 498 |
-
function drawPlayer(x, y, color, facingRight, name, isMe) {
|
| 499 |
-
const cx = x + PLAYER_W/2;
|
| 500 |
-
const cy = y + PLAYER_H/2;
|
| 501 |
|
| 502 |
-
// Shadow
|
| 503 |
-
ctx.fillStyle = 'rgba(0,0,0,0.3)';
|
| 504 |
ctx.beginPath();
|
| 505 |
-
ctx.
|
| 506 |
-
ctx.fill();
|
| 507 |
-
|
| 508 |
-
// Body
|
| 509 |
-
ctx.fillStyle = color;
|
| 510 |
-
ctx.beginPath();
|
| 511 |
-
// Squish effect on landing could go here
|
| 512 |
-
ctx.roundRect(x, y, PLAYER_W, PLAYER_H, 10);
|
| 513 |
ctx.fill();
|
| 514 |
|
| 515 |
-
//
|
| 516 |
-
ctx.fillStyle = '#
|
| 517 |
-
|
|
|
|
| 518 |
ctx.beginPath();
|
| 519 |
-
ctx.
|
|
|
|
| 520 |
ctx.fill();
|
| 521 |
|
| 522 |
-
ctx.fillStyle = '#
|
| 523 |
ctx.beginPath();
|
| 524 |
-
ctx.
|
| 525 |
-
ctx.
|
| 526 |
ctx.fill();
|
| 527 |
|
| 528 |
-
// Jetpack/Backpack
|
| 529 |
-
ctx.fillStyle = '#555';
|
| 530 |
-
ctx.fillRect(cx - 6 - faceX*2.5, y + 10, 12, 20);
|
| 531 |
-
|
| 532 |
// Name
|
| 533 |
-
if
|
| 534 |
ctx.fillStyle = '#fff';
|
| 535 |
-
ctx.font = '
|
| 536 |
ctx.textAlign = 'center';
|
| 537 |
-
ctx.
|
|
|
|
|
|
|
| 538 |
}
|
|
|
|
|
|
|
| 539 |
}
|
| 540 |
|
| 541 |
-
function
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
}
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
}
|
| 557 |
-
|
| 558 |
-
menuScreen.classList.add('hidden');
|
| 559 |
-
overScreen.classList.remove('hidden');
|
| 560 |
}
|
| 561 |
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
|
|
|
|
|
|
| 567 |
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
|
|
|
| 571 |
|
| 572 |
-
|
| 573 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
}
|
| 575 |
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
if(
|
| 583 |
-
|
| 584 |
-
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
//
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
const w = rect.width;
|
| 596 |
-
|
| 597 |
-
if (x < w/2) { keys.left = true; keys.right = false; }
|
| 598 |
-
else { keys.right = true; keys.left = false; }
|
| 599 |
-
};
|
| 600 |
-
canvas.addEventListener('touchstart', handleTouch, {passive: false});
|
| 601 |
-
canvas.addEventListener('touchmove', handleTouch, {passive: false});
|
| 602 |
-
canvas.addEventListener('touchend', () => { keys.left = keys.right = false; });
|
| 603 |
-
|
| 604 |
-
document.getElementById('playBtn').onclick = startGame;
|
| 605 |
-
document.getElementById('retryBtn').onclick = startGame;
|
| 606 |
-
|
| 607 |
-
// === SOCKET ===
|
| 608 |
-
socket = io();
|
| 609 |
-
socket.on('init', d => {
|
| 610 |
-
renderLB(d.leaderboard, menuLB);
|
| 611 |
-
});
|
| 612 |
|
| 613 |
-
socket.on('
|
| 614 |
-
const
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
|
|
|
| 618 |
|
| 619 |
-
if
|
| 620 |
-
|
| 621 |
-
others.set(p.id, {
|
| 622 |
-
dx: p.x, dy: p.y, // interpolation vars
|
| 623 |
-
...p
|
| 624 |
-
});
|
| 625 |
} else {
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
o.x = p.x; o.y = p.y;
|
| 629 |
-
o.n = p.n; o.c = p.c; o.f = p.f;
|
| 630 |
}
|
| 631 |
-
}
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
if (!currentIds.has(id)) others.delete(id);
|
| 635 |
}
|
| 636 |
});
|
| 637 |
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
});
|
| 642 |
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
}
|
| 650 |
|
| 651 |
-
//
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
}
|
|
|
|
|
|
|
|
|
|
| 657 |
loop();
|
| 658 |
-
|
|
|
|
|
|
|
| 659 |
</body>
|
| 660 |
</html>
|
|
|
|
| 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,maximum-scale=1,user-scalable=no">
|
| 6 |
+
<title>Neon Jump</title>
|
| 7 |
+
<style>
|
| 8 |
+
*{margin:0;padding:0;box-sizing:border-box;touch-action:manipulation}
|
| 9 |
+
body{background:#05050a;display:flex;justify-content:center;align-items:center;min-height:100vh;font-family:system-ui,sans-serif;overflow:hidden}
|
| 10 |
+
#wrap{position:relative}
|
| 11 |
+
canvas{display:block;border:2px solid #0ff;box-shadow:0 0 30px #0ff4}
|
| 12 |
+
.ui{position:absolute;top:0;left:0;right:0;bottom:0;display:flex;flex-direction:column;justify-content:center;align-items:center;background:rgba(5,5,15,.9);color:#fff;text-align:center;transition:opacity .3s}
|
| 13 |
+
.ui.hidden{opacity:0;pointer-events:none}
|
| 14 |
+
h1{font-size:2.5em;color:#0ff;text-shadow:0 0 20px #0ff;margin-bottom:20px}
|
| 15 |
+
h1 span{color:#f0f}
|
| 16 |
+
input{padding:12px 20px;font-size:1.1em;background:rgba(0,255,255,.1);border:2px solid #0ff;color:#fff;border-radius:8px;outline:none;width:200px;margin-bottom:15px;text-align:center}
|
| 17 |
+
input::placeholder{color:#888}
|
| 18 |
+
button{padding:14px 40px;font-size:1.2em;background:linear-gradient(135deg,#0ff,#08f);border:none;color:#000;border-radius:8px;cursor:pointer;font-weight:700;transition:transform .2s,box-shadow .2s}
|
| 19 |
+
button:hover{transform:scale(1.05);box-shadow:0 0 25px #0ff}
|
| 20 |
+
.lb{margin-top:25px;background:rgba(0,0,0,.4);padding:15px 25px;border-radius:10px;border:1px solid #0ff4;max-height:200px;overflow-y:auto}
|
| 21 |
+
.lb h2{color:#0ff;font-size:1.1em;margin-bottom:10px}
|
| 22 |
+
.lb-row{display:flex;justify-content:space-between;padding:4px 0;color:#aaa;font-size:.9em}
|
| 23 |
+
.lb-row:nth-child(2){color:gold}
|
| 24 |
+
.lb-row:nth-child(3){color:#c0c0c0}
|
| 25 |
+
.lb-row:nth-child(4){color:#cd7f32}
|
| 26 |
+
#go h1{color:#f44;text-shadow:0 0 20px #f44}
|
| 27 |
+
#score{font-size:2em;color:#0ff;margin:15px 0}
|
| 28 |
+
#record{color:gold;font-size:1.3em;animation:pulse .4s infinite alternate}
|
| 29 |
+
@keyframes pulse{to{opacity:.5}}
|
| 30 |
+
.hidden-el{display:none!important}
|
| 31 |
+
</style>
|
| 32 |
</head>
|
| 33 |
<body>
|
| 34 |
+
<div id="wrap">
|
| 35 |
+
<canvas id="c"></canvas>
|
| 36 |
+
<div id="menu" class="ui">
|
| 37 |
+
<h1>🚀 NEON<span>JUMP</span></h1>
|
| 38 |
+
<input id="name" placeholder="Enter name" maxlength="12" autocomplete="off">
|
| 39 |
+
<button id="play">PLAY</button>
|
| 40 |
+
<div class="lb"><h2>🏆 LEADERBOARD</h2><div id="lb1"></div></div>
|
| 41 |
+
</div>
|
| 42 |
+
<div id="go" class="ui hidden">
|
| 43 |
+
<h1>GAME OVER</h1>
|
| 44 |
+
<div id="score">0</div>
|
| 45 |
+
<div id="record" class="hidden-el">🎉 NEW RECORD!</div>
|
| 46 |
+
<button id="retry">RETRY</button>
|
| 47 |
+
<div class="lb"><h2>🏆 LEADERBOARD</h2><div id="lb2"></div></div>
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
<script src="/socket.io/socket.io.js"></script>
|
| 51 |
+
<script>
|
| 52 |
+
(function(){
|
| 53 |
+
"use strict";
|
| 54 |
+
|
| 55 |
+
const $ = s => document.querySelector(s);
|
| 56 |
+
const canvas = $('#c'), ctx = canvas.getContext('2d');
|
| 57 |
+
const W = 400, H = 650;
|
| 58 |
+
canvas.width = W; canvas.height = H;
|
| 59 |
+
|
| 60 |
+
const socket = io();
|
| 61 |
+
|
| 62 |
+
// Audio
|
| 63 |
+
let audio;
|
| 64 |
+
function initAudio(){ if(!audio) audio = new (window.AudioContext||window.webkitAudioContext)(); }
|
| 65 |
+
|
| 66 |
+
function sound(type){
|
| 67 |
+
if(!audio) return;
|
| 68 |
+
const o = audio.createOscillator();
|
| 69 |
+
const g = audio.createGain();
|
| 70 |
+
o.connect(g); g.connect(audio.destination);
|
| 71 |
+
const t = audio.currentTime;
|
| 72 |
+
|
| 73 |
+
if(type==='jump'){
|
| 74 |
+
o.type='sine'; o.frequency.setValueAtTime(350,t);
|
| 75 |
+
o.frequency.exponentialRampToValueAtTime(700,t+.08);
|
| 76 |
+
g.gain.setValueAtTime(.25,t); g.gain.exponentialRampToValueAtTime(.01,t+.1);
|
| 77 |
+
o.start(t); o.stop(t+.1);
|
| 78 |
+
} else if(type==='spring'){
|
| 79 |
+
o.type='sine'; o.frequency.setValueAtTime(500,t);
|
| 80 |
+
o.frequency.exponentialRampToValueAtTime(1400,t+.15);
|
| 81 |
+
g.gain.setValueAtTime(.3,t); g.gain.exponentialRampToValueAtTime(.01,t+.2);
|
| 82 |
+
o.start(t); o.stop(t+.2);
|
| 83 |
+
} else if(type==='break'){
|
| 84 |
+
o.type='sawtooth'; o.frequency.setValueAtTime(180,t);
|
| 85 |
+
o.frequency.exponentialRampToValueAtTime(40,t+.15);
|
| 86 |
+
g.gain.setValueAtTime(.15,t); g.gain.exponentialRampToValueAtTime(.01,t+.15);
|
| 87 |
+
o.start(t); o.stop(t+.15);
|
| 88 |
+
} else if(type==='die'){
|
| 89 |
+
o.type='sawtooth'; o.frequency.setValueAtTime(250,t);
|
| 90 |
+
o.frequency.exponentialRampToValueAtTime(30,t+.4);
|
| 91 |
+
g.gain.setValueAtTime(.35,t); g.gain.exponentialRampToValueAtTime(.01,t+.4);
|
| 92 |
+
o.start(t); o.stop(t+.4);
|
| 93 |
+
} else if(type==='record'){
|
| 94 |
+
o.type='sine';
|
| 95 |
+
[523,659,784,1047].forEach((f,i)=>{ o.frequency.setValueAtTime(f,t+i*.12); });
|
| 96 |
+
g.gain.setValueAtTime(.25,t); g.gain.setValueAtTime(.25,t+.4);
|
| 97 |
+
g.gain.exponentialRampToValueAtTime(.01,t+.55);
|
| 98 |
+
o.start(t); o.stop(t+.55);
|
| 99 |
+
}
|
| 100 |
}
|
| 101 |
|
| 102 |
+
// Constants
|
| 103 |
+
const GRAVITY = 0.45;
|
| 104 |
+
const JUMP = -13;
|
| 105 |
+
const SPRING = -21;
|
| 106 |
+
const SPEED = 6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
// Types
|
| 109 |
+
const T_NORM=0, T_MOVE=1, T_BREAK=2, T_SPRING=3, T_SPIKE=4;
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
// State
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
let state = 'menu';
|
| 113 |
+
let pName = '', pColor = '#0ff';
|
| 114 |
+
let best = 0, lb = [];
|
| 115 |
+
|
| 116 |
+
// Player
|
| 117 |
+
let P = { x:0, y:0, vx:0, vy:0, w:28, h:28, score:0, maxY:0, alive:true };
|
| 118 |
+
|
| 119 |
+
// World
|
| 120 |
+
let camY = 0, targetCam = 0;
|
| 121 |
+
let platforms = [], enemies = [], particles = [];
|
| 122 |
+
let platId = 0, enemyId = 0;
|
| 123 |
+
|
| 124 |
+
// Stars
|
| 125 |
+
const stars = Array.from({length:80}, () => ({
|
| 126 |
+
x: Math.random()*W,
|
| 127 |
+
y: Math.random()*8000,
|
| 128 |
+
sz: Math.random()*2+.5,
|
| 129 |
+
sp: Math.random()*.4+.1,
|
| 130 |
+
br: Math.random()*.5+.3
|
| 131 |
+
}));
|
| 132 |
+
|
| 133 |
+
// Others
|
| 134 |
+
let others = {};
|
| 135 |
|
| 136 |
// Input
|
| 137 |
+
let keys = {l:false, r:false};
|
| 138 |
+
let touch = {l:false, r:false};
|
| 139 |
+
|
| 140 |
+
// Platform creation
|
| 141 |
+
function mkPlat(x, y, type){
|
| 142 |
+
return {
|
| 143 |
+
id: platId++, x, y, w: type===T_SPRING?55:75, h:14,
|
| 144 |
+
type, broken:false, dir:1, sx:x, range: 60+Math.random()*40
|
| 145 |
+
};
|
| 146 |
+
}
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
function genPlat(baseY){
|
| 149 |
+
const diff = Math.min(1, Math.abs(baseY)/6000);
|
| 150 |
+
const x = 30 + Math.random()*(W-130);
|
| 151 |
+
let type = T_NORM;
|
| 152 |
+
const r = Math.random();
|
| 153 |
+
|
| 154 |
+
if(r < .12*diff) type = T_SPIKE;
|
| 155 |
+
else if(r < .12*diff+.14) type = T_BREAK;
|
| 156 |
+
else if(r < .12*diff+.28) type = T_MOVE;
|
| 157 |
+
else if(r < .12*diff+.38) type = T_SPRING;
|
| 158 |
+
|
| 159 |
+
return mkPlat(x, baseY, type);
|
| 160 |
+
}
|
| 161 |
|
| 162 |
+
function initPlats(){
|
| 163 |
+
platforms = [];
|
| 164 |
+
platId = 0;
|
| 165 |
+
|
| 166 |
+
// Start platform
|
| 167 |
+
platforms.push(mkPlat(W/2-37, H-60, T_NORM));
|
| 168 |
+
|
| 169 |
+
let y = H - 140;
|
| 170 |
+
let lastX = W/2;
|
| 171 |
+
|
| 172 |
+
while(y > -600){
|
| 173 |
+
// Ensure reachability: max horizontal jump ~120px, vertical ~200px
|
| 174 |
+
const gap = 70 + Math.random()*50;
|
| 175 |
+
y -= gap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
// Constrain x to be reachable
|
| 178 |
+
const maxDx = 130;
|
| 179 |
+
const minX = Math.max(20, lastX - maxDx);
|
| 180 |
+
const maxX = Math.min(W - 95, lastX + maxDx);
|
| 181 |
|
| 182 |
+
const p = genPlat(y);
|
| 183 |
+
p.x = minX + Math.random()*(maxX - minX);
|
| 184 |
+
lastX = p.x + p.w/2;
|
| 185 |
+
|
| 186 |
+
platforms.push(p);
|
|
|
|
| 187 |
}
|
| 188 |
+
}
|
| 189 |
|
| 190 |
+
function genPlatsAbove(){
|
| 191 |
+
let minY = Math.min(...platforms.map(p=>p.y));
|
| 192 |
+
const target = camY - 700;
|
| 193 |
+
|
| 194 |
+
let lastPlat = platforms.find(p=>p.y===minY);
|
| 195 |
+
let lastX = lastPlat ? lastPlat.x + lastPlat.w/2 : W/2;
|
| 196 |
+
|
| 197 |
+
while(minY > target){
|
| 198 |
+
const gap = 70 + Math.random()*50;
|
| 199 |
+
minY -= gap;
|
| 200 |
+
|
| 201 |
+
const maxDx = 130;
|
| 202 |
+
const mnX = Math.max(20, lastX - maxDx);
|
| 203 |
+
const mxX = Math.min(W - 95, lastX + maxDx);
|
| 204 |
+
|
| 205 |
+
const p = genPlat(minY);
|
| 206 |
+
p.x = mnX + Math.random()*(mxX - mnX);
|
| 207 |
+
lastX = p.x + p.w/2;
|
| 208 |
+
|
| 209 |
+
platforms.push(p);
|
| 210 |
+
}
|
| 211 |
}
|
| 212 |
|
| 213 |
+
function genEnemies(){
|
| 214 |
+
if(enemies.length > 8) return;
|
| 215 |
+
const minE = enemies.length ? Math.min(...enemies.map(e=>e.y)) : camY;
|
| 216 |
+
const diff = Math.min(1, Math.abs(camY)/4000);
|
| 217 |
+
|
| 218 |
+
if(minE > camY - 400 && Math.random() < .25*diff){
|
| 219 |
+
enemies.push({
|
| 220 |
+
id: enemyId++,
|
| 221 |
+
x: Math.random()*(W-40),
|
| 222 |
+
y: camY - 500 - Math.random()*300,
|
| 223 |
+
w: 38, h: 38,
|
| 224 |
+
vx: (1 + Math.random()*2) * (Math.random()<.5?1:-1),
|
| 225 |
+
ph: Math.random()*Math.PI*2
|
| 226 |
+
});
|
| 227 |
+
}
|
| 228 |
}
|
| 229 |
|
| 230 |
+
function spawnParts(x, y, col, n){
|
| 231 |
+
for(let i=0; i<n; i++){
|
| 232 |
particles.push({
|
| 233 |
x, y,
|
| 234 |
+
vx: (Math.random()-.5)*10,
|
| 235 |
+
vy: (Math.random()-.5)*10 - 3,
|
| 236 |
life: 1,
|
| 237 |
+
col
|
|
|
|
| 238 |
});
|
| 239 |
}
|
| 240 |
}
|
| 241 |
|
| 242 |
+
function initGame(){
|
| 243 |
+
P = { x:W/2-14, y:H-100, vx:0, vy:0, w:28, h:28, score:0, maxY:0, alive:true };
|
| 244 |
+
camY = 0; targetCam = 0;
|
| 245 |
+
enemies = []; particles = [];
|
| 246 |
+
initPlats();
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
function die(){
|
| 250 |
+
if(!P.alive) return;
|
| 251 |
+
P.alive = false;
|
| 252 |
+
sound('die');
|
| 253 |
+
spawnParts(P.x+P.w/2, P.y+P.h/2, '#0ff', 20);
|
| 254 |
+
|
| 255 |
+
const isRecord = P.score > best;
|
| 256 |
+
if(isRecord){ best = P.score; sound('record'); }
|
| 257 |
+
|
| 258 |
+
socket.emit('s', {n:pName, s:P.score});
|
| 259 |
|
| 260 |
+
setTimeout(()=>{
|
| 261 |
+
state = 'over';
|
| 262 |
+
$('#go').classList.remove('hidden');
|
| 263 |
+
$('#score').textContent = P.score;
|
| 264 |
+
$('#record').classList.toggle('hidden-el', !isRecord);
|
| 265 |
+
renderLB('lb2');
|
| 266 |
+
}, 400);
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
function update(){
|
| 270 |
+
if(state !== 'play') return;
|
| 271 |
|
| 272 |
+
// Input
|
| 273 |
+
let dir = 0;
|
| 274 |
+
if(keys.l || touch.l) dir = -1;
|
| 275 |
+
if(keys.r || touch.r) dir = 1;
|
| 276 |
|
| 277 |
+
P.vx = dir * SPEED;
|
| 278 |
+
P.vy += GRAVITY;
|
| 279 |
+
P.x += P.vx;
|
| 280 |
+
P.y += P.vy;
|
| 281 |
|
| 282 |
// Wrap
|
| 283 |
+
if(P.x < -P.w) P.x = W;
|
| 284 |
+
if(P.x > W) P.x = -P.w;
|
| 285 |
|
| 286 |
+
// Platform collision
|
| 287 |
+
if(P.vy > 0){
|
| 288 |
+
for(const pl of platforms){
|
| 289 |
+
if(pl.broken) continue;
|
| 290 |
+
|
| 291 |
+
const feet = P.y + P.h;
|
| 292 |
+
const wasAbove = feet - P.vy <= pl.y;
|
| 293 |
+
|
| 294 |
+
if(P.x+P.w > pl.x && P.x < pl.x+pl.w && feet >= pl.y && feet <= pl.y+pl.h+10 && wasAbove){
|
| 295 |
+
if(pl.type === T_SPIKE){
|
| 296 |
+
die(); return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
}
|
| 298 |
|
| 299 |
+
P.y = pl.y - P.h;
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
+
if(pl.type === T_SPRING){
|
| 302 |
+
P.vy = SPRING;
|
| 303 |
+
sound('spring');
|
| 304 |
+
} else if(pl.type === T_BREAK){
|
| 305 |
+
P.vy = JUMP;
|
| 306 |
+
pl.broken = true;
|
| 307 |
+
sound('break');
|
| 308 |
+
spawnParts(pl.x+pl.w/2, pl.y, '#964B00', 8);
|
| 309 |
} else {
|
| 310 |
+
P.vy = JUMP;
|
| 311 |
+
sound('jump');
|
| 312 |
}
|
| 313 |
+
break;
|
| 314 |
}
|
| 315 |
}
|
| 316 |
}
|
| 317 |
+
|
| 318 |
+
// Enemy collision
|
| 319 |
+
for(const e of enemies){
|
| 320 |
+
if(P.x+P.w > e.x+5 && P.x < e.x+e.w-5 && P.y+P.h > e.y+5 && P.y < e.y+e.h-5){
|
| 321 |
+
die(); return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
}
|
| 323 |
}
|
| 324 |
+
|
| 325 |
// Score
|
| 326 |
+
const worldY = -P.y;
|
| 327 |
+
if(worldY > P.maxY){
|
| 328 |
+
P.maxY = worldY;
|
| 329 |
+
P.score = Math.floor(worldY/10);
|
|
|
|
| 330 |
}
|
| 331 |
|
| 332 |
+
// Fall death
|
| 333 |
+
if(P.y > camY + H + 150){
|
| 334 |
+
die(); return;
|
| 335 |
+
}
|
| 336 |
|
| 337 |
+
// Update platforms
|
| 338 |
+
for(const pl of platforms){
|
| 339 |
+
if(pl.type === T_MOVE && !pl.broken){
|
| 340 |
+
pl.x += pl.dir * 2.5;
|
| 341 |
+
if(pl.x > pl.sx + pl.range || pl.x < pl.sx - pl.range) pl.dir *= -1;
|
| 342 |
+
}
|
| 343 |
}
|
| 344 |
+
platforms = platforms.filter(p => p.y < camY + H + 100);
|
| 345 |
+
genPlatsAbove();
|
| 346 |
|
| 347 |
+
// Update enemies
|
| 348 |
+
for(const e of enemies){
|
| 349 |
+
e.x += e.vx;
|
| 350 |
+
if(e.x < -e.w) e.x = W;
|
| 351 |
+
if(e.x > W) e.x = -e.w;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
}
|
| 353 |
+
enemies = enemies.filter(e => e.y < camY + H + 100 && e.y > camY - 800);
|
| 354 |
+
genEnemies();
|
| 355 |
+
|
| 356 |
+
// Particles
|
| 357 |
+
for(const p of particles){
|
| 358 |
+
p.x += p.vx;
|
| 359 |
+
p.y += p.vy;
|
| 360 |
+
p.vy += .25;
|
| 361 |
+
p.life -= .025;
|
| 362 |
+
}
|
| 363 |
+
particles = particles.filter(p => p.life > 0);
|
| 364 |
+
|
| 365 |
+
// Camera
|
| 366 |
+
if(P.y < camY + H*.35){
|
| 367 |
+
targetCam = P.y - H*.35;
|
| 368 |
+
}
|
| 369 |
+
camY += (targetCam - camY) * .12;
|
| 370 |
+
|
| 371 |
+
// Network
|
| 372 |
+
socket.emit('u', {
|
| 373 |
+
x: Math.round(P.x),
|
| 374 |
+
y: Math.round(P.y),
|
| 375 |
+
vx: Math.round(P.vx*10)/10,
|
| 376 |
+
vy: Math.round(P.vy*10)/10,
|
| 377 |
+
s: P.score,
|
| 378 |
+
a: P.alive
|
| 379 |
+
});
|
| 380 |
}
|
| 381 |
|
| 382 |
+
function drawBG(){
|
| 383 |
+
const grd = ctx.createLinearGradient(0,0,0,H);
|
| 384 |
+
grd.addColorStop(0,'#08081a');
|
| 385 |
+
grd.addColorStop(1,'#180828');
|
| 386 |
+
ctx.fillStyle = grd;
|
| 387 |
+
ctx.fillRect(0,0,W,H);
|
| 388 |
+
|
| 389 |
+
// Stars parallax
|
| 390 |
+
for(const s of stars){
|
| 391 |
+
const sy = ((s.y - camY*s.sp) % (H+100)) - 50;
|
| 392 |
+
ctx.globalAlpha = s.br;
|
| 393 |
+
ctx.fillStyle = '#fff';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
ctx.beginPath();
|
| 395 |
+
ctx.arc(s.x, sy, s.sz, 0, Math.PI*2);
|
| 396 |
ctx.fill();
|
| 397 |
}
|
| 398 |
+
ctx.globalAlpha = 1;
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
function drawPlats(){
|
| 402 |
+
for(const pl of platforms){
|
| 403 |
+
if(pl.broken) continue;
|
| 404 |
+
const sy = pl.y - camY;
|
| 405 |
+
if(sy < -30 || sy > H+30) continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
+
ctx.save();
|
|
|
|
|
|
|
| 408 |
|
| 409 |
+
if(pl.type === T_NORM){
|
| 410 |
+
ctx.fillStyle = '#0f0';
|
| 411 |
+
ctx.shadowColor = '#0f0';
|
| 412 |
+
ctx.shadowBlur = 12;
|
| 413 |
+
roundRect(pl.x, sy, pl.w, pl.h, 4);
|
| 414 |
+
} else if(pl.type === T_MOVE){
|
| 415 |
+
ctx.fillStyle = '#00bfff';
|
| 416 |
+
ctx.shadowColor = '#00bfff';
|
| 417 |
+
ctx.shadowBlur = 12;
|
| 418 |
+
roundRect(pl.x, sy, pl.w, pl.h, 4);
|
| 419 |
+
ctx.fillStyle = '#fff';
|
| 420 |
ctx.beginPath();
|
| 421 |
+
if(pl.dir > 0){
|
| 422 |
+
ctx.moveTo(pl.x+pl.w-8, sy+pl.h/2);
|
| 423 |
+
ctx.lineTo(pl.x+pl.w-14, sy+4);
|
| 424 |
+
ctx.lineTo(pl.x+pl.w-14, sy+pl.h-4);
|
| 425 |
+
} else {
|
| 426 |
+
ctx.moveTo(pl.x+8, sy+pl.h/2);
|
| 427 |
+
ctx.lineTo(pl.x+14, sy+4);
|
| 428 |
+
ctx.lineTo(pl.x+14, sy+pl.h-4);
|
| 429 |
}
|
| 430 |
ctx.fill();
|
| 431 |
+
} else if(pl.type === T_BREAK){
|
| 432 |
+
ctx.fillStyle = '#8B4513';
|
| 433 |
+
ctx.shadowColor = '#8B4513';
|
| 434 |
+
ctx.shadowBlur = 6;
|
| 435 |
+
roundRect(pl.x, sy, pl.w, pl.h, 3);
|
| 436 |
+
ctx.strokeStyle = '#5a3010';
|
| 437 |
+
ctx.lineWidth = 1.5;
|
| 438 |
+
ctx.beginPath();
|
| 439 |
+
ctx.moveTo(pl.x+15, sy); ctx.lineTo(pl.x+25, sy+pl.h);
|
| 440 |
+
ctx.moveTo(pl.x+45, sy); ctx.lineTo(pl.x+35, sy+pl.h);
|
| 441 |
+
ctx.stroke();
|
| 442 |
+
} else if(pl.type === T_SPRING){
|
| 443 |
+
ctx.fillStyle = '#0f0';
|
| 444 |
+
roundRect(pl.x, sy, pl.w, pl.h, 3);
|
| 445 |
+
ctx.fillStyle = '#ff0';
|
| 446 |
+
ctx.shadowColor = '#ff0';
|
| 447 |
+
ctx.shadowBlur = 10;
|
| 448 |
+
const sw = 22, sh = 16;
|
| 449 |
+
ctx.fillRect(pl.x+pl.w/2-sw/2, sy-sh+2, sw, sh);
|
| 450 |
+
ctx.fillStyle = '#aa0';
|
| 451 |
+
for(let i=0; i<3; i++){
|
| 452 |
+
ctx.fillRect(pl.x+pl.w/2-sw/2+2, sy-sh+4+i*5, sw-4, 2);
|
| 453 |
+
}
|
| 454 |
+
} else if(pl.type === T_SPIKE){
|
| 455 |
+
ctx.fillStyle = '#333';
|
| 456 |
+
roundRect(pl.x, sy, pl.w, pl.h, 2);
|
| 457 |
+
ctx.fillStyle = '#f00';
|
| 458 |
+
ctx.shadowColor = '#f00';
|
| 459 |
+
ctx.shadowBlur = 8;
|
| 460 |
+
const spikes = 5;
|
| 461 |
+
const sw = pl.w/spikes;
|
| 462 |
+
for(let i=0; i<spikes; i++){
|
| 463 |
+
ctx.beginPath();
|
| 464 |
+
ctx.moveTo(pl.x + i*sw + sw/2, sy-10);
|
| 465 |
+
ctx.lineTo(pl.x + i*sw + 2, sy);
|
| 466 |
+
ctx.lineTo(pl.x + i*sw + sw - 2, sy);
|
| 467 |
+
ctx.closePath();
|
| 468 |
+
ctx.fill();
|
| 469 |
+
}
|
| 470 |
}
|
| 471 |
|
| 472 |
+
ctx.restore();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
}
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
function roundRect(x,y,w,h,r){
|
| 477 |
+
ctx.beginPath();
|
| 478 |
+
ctx.moveTo(x+r,y);
|
| 479 |
+
ctx.lineTo(x+w-r,y);
|
| 480 |
+
ctx.quadraticCurveTo(x+w,y,x+w,y+r);
|
| 481 |
+
ctx.lineTo(x+w,y+h-r);
|
| 482 |
+
ctx.quadraticCurveTo(x+w,y+h,x+w-r,y+h);
|
| 483 |
+
ctx.lineTo(x+r,y+h);
|
| 484 |
+
ctx.quadraticCurveTo(x,y+h,x,y+h-r);
|
| 485 |
+
ctx.lineTo(x,y+r);
|
| 486 |
+
ctx.quadraticCurveTo(x,y,x+r,y);
|
| 487 |
+
ctx.closePath();
|
| 488 |
+
ctx.fill();
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
function drawEnemies(){
|
| 492 |
+
const t = Date.now()/1000;
|
| 493 |
+
for(const e of enemies){
|
| 494 |
+
const sy = e.y - camY + Math.sin(t*3 + e.ph)*5;
|
| 495 |
+
if(sy < -50 || sy > H+50) continue;
|
| 496 |
|
| 497 |
+
ctx.save();
|
| 498 |
+
ctx.fillStyle = '#f0f';
|
| 499 |
+
ctx.shadowColor = '#f0f';
|
| 500 |
+
ctx.shadowBlur = 15;
|
| 501 |
|
|
|
|
| 502 |
ctx.beginPath();
|
| 503 |
+
ctx.arc(e.x+e.w/2, sy+e.h/2, e.w/2, 0, Math.PI*2);
|
| 504 |
ctx.fill();
|
| 505 |
+
|
| 506 |
+
// Tentacles
|
| 507 |
+
ctx.strokeStyle = '#f0f';
|
| 508 |
+
ctx.lineWidth = 3;
|
| 509 |
+
for(let i=0; i<3; i++){
|
| 510 |
+
const tx = e.x + 8 + i*11;
|
| 511 |
+
ctx.beginPath();
|
| 512 |
+
ctx.moveTo(tx, sy+e.h-5);
|
| 513 |
+
ctx.quadraticCurveTo(tx + Math.sin(t*4+i)*5, sy+e.h+10, tx, sy+e.h+15);
|
| 514 |
+
ctx.stroke();
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
// Eyes
|
| 518 |
ctx.fillStyle = '#fff';
|
| 519 |
+
ctx.shadowBlur = 0;
|
| 520 |
ctx.beginPath();
|
| 521 |
+
ctx.arc(e.x+12, sy+14, 7, 0, Math.PI*2);
|
| 522 |
+
ctx.arc(e.x+26, sy+14, 7, 0, Math.PI*2);
|
| 523 |
ctx.fill();
|
| 524 |
+
|
| 525 |
ctx.fillStyle = '#000';
|
| 526 |
+
const ex = e.vx > 0 ? 2 : -2;
|
| 527 |
ctx.beginPath();
|
| 528 |
+
ctx.arc(e.x+12+ex, sy+15, 3, 0, Math.PI*2);
|
| 529 |
+
ctx.arc(e.x+26+ex, sy+15, 3, 0, Math.PI*2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
ctx.fill();
|
| 531 |
+
|
| 532 |
+
ctx.restore();
|
| 533 |
}
|
| 534 |
+
}
|
| 535 |
|
| 536 |
+
function drawPlayer(p, local){
|
| 537 |
+
const sy = p.y - camY;
|
| 538 |
+
if(sy < -50 || sy > H+50) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
|
| 540 |
+
const col = local ? '#0ff' : (p.c || '#f80');
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
+
ctx.save();
|
| 543 |
+
ctx.fillStyle = col;
|
| 544 |
+
ctx.shadowColor = col;
|
| 545 |
+
ctx.shadowBlur = 18;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
|
|
|
|
|
|
| 547 |
ctx.beginPath();
|
| 548 |
+
ctx.arc(p.x + p.w/2, sy + p.h/2, p.w/2, 0, Math.PI*2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
ctx.fill();
|
| 550 |
|
| 551 |
+
// Face
|
| 552 |
+
ctx.fillStyle = '#fff';
|
| 553 |
+
ctx.shadowBlur = 0;
|
| 554 |
+
const ex = (p.vx||0) > 0 ? 4 : ((p.vx||0) < 0 ? -4 : 0);
|
| 555 |
ctx.beginPath();
|
| 556 |
+
ctx.arc(p.x + p.w/2 - 4 + ex, sy + p.h/2 - 3, 4, 0, Math.PI*2);
|
| 557 |
+
ctx.arc(p.x + p.w/2 + 4 + ex, sy + p.h/2 - 3, 4, 0, Math.PI*2);
|
| 558 |
ctx.fill();
|
| 559 |
|
| 560 |
+
ctx.fillStyle = '#000';
|
| 561 |
ctx.beginPath();
|
| 562 |
+
ctx.arc(p.x + p.w/2 - 4 + ex, sy + p.h/2 - 2, 2, 0, Math.PI*2);
|
| 563 |
+
ctx.arc(p.x + p.w/2 + 4 + ex, sy + p.h/2 - 2, 2, 0, Math.PI*2);
|
| 564 |
ctx.fill();
|
| 565 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
// Name
|
| 567 |
+
if(!local && p.n){
|
| 568 |
ctx.fillStyle = '#fff';
|
| 569 |
+
ctx.font = '11px system-ui';
|
| 570 |
ctx.textAlign = 'center';
|
| 571 |
+
ctx.shadowColor = '#000';
|
| 572 |
+
ctx.shadowBlur = 4;
|
| 573 |
+
ctx.fillText(p.n, p.x+p.w/2, sy-8);
|
| 574 |
}
|
| 575 |
+
|
| 576 |
+
ctx.restore();
|
| 577 |
}
|
| 578 |
|
| 579 |
+
function drawOthers(){
|
| 580 |
+
const now = Date.now();
|
| 581 |
+
for(const id in others){
|
| 582 |
+
const o = others[id];
|
| 583 |
+
if(!o.a) continue;
|
| 584 |
+
|
| 585 |
+
// Client-side prediction with velocity
|
| 586 |
+
const dt = (now - o.t) / 1000;
|
| 587 |
+
const predX = o.x + o.vx * dt * 60;
|
| 588 |
+
const predY = o.y + o.vy * dt * 60;
|
| 589 |
+
|
| 590 |
+
// Lerp render position
|
| 591 |
+
o.rx = o.rx || o.x;
|
| 592 |
+
o.ry = o.ry || o.y;
|
| 593 |
+
o.rx += (predX - o.rx) * .15;
|
| 594 |
+
o.ry += (predY - o.ry) * .15;
|
| 595 |
+
|
| 596 |
+
// Wrap
|
| 597 |
+
if(o.rx < -30) o.rx = W;
|
| 598 |
+
if(o.rx > W) o.rx = -30;
|
| 599 |
+
|
| 600 |
+
drawPlayer({x:o.rx, y:o.ry, w:28, h:28, vx:o.vx, n:o.n, c:o.c}, false);
|
| 601 |
}
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
function drawParticles(){
|
| 605 |
+
for(const p of particles){
|
| 606 |
+
const sy = p.y - camY;
|
| 607 |
+
ctx.globalAlpha = p.life;
|
| 608 |
+
ctx.fillStyle = p.col;
|
| 609 |
+
ctx.fillRect(p.x-3, sy-3, 6, 6);
|
| 610 |
}
|
| 611 |
+
ctx.globalAlpha = 1;
|
|
|
|
|
|
|
| 612 |
}
|
| 613 |
|
| 614 |
+
function drawUI(){
|
| 615 |
+
ctx.fillStyle = '#fff';
|
| 616 |
+
ctx.font = 'bold 22px system-ui';
|
| 617 |
+
ctx.textAlign = 'left';
|
| 618 |
+
ctx.shadowColor = '#0ff';
|
| 619 |
+
ctx.shadowBlur = 8;
|
| 620 |
+
ctx.fillText(P.score, 12, 30);
|
| 621 |
|
| 622 |
+
ctx.font = '13px system-ui';
|
| 623 |
+
ctx.fillStyle = '#888';
|
| 624 |
+
ctx.shadowBlur = 0;
|
| 625 |
+
ctx.fillText(`Best: ${best}`, 12, 48);
|
| 626 |
|
| 627 |
+
// Touch zones indicator
|
| 628 |
+
ctx.globalAlpha = .15;
|
| 629 |
+
ctx.strokeStyle = '#fff';
|
| 630 |
+
ctx.setLineDash([5,5]);
|
| 631 |
+
ctx.beginPath();
|
| 632 |
+
ctx.moveTo(W/2, H-100);
|
| 633 |
+
ctx.lineTo(W/2, H);
|
| 634 |
+
ctx.stroke();
|
| 635 |
+
ctx.setLineDash([]);
|
| 636 |
+
ctx.globalAlpha = 1;
|
| 637 |
}
|
| 638 |
|
| 639 |
+
function draw(){
|
| 640 |
+
drawBG();
|
| 641 |
+
drawPlats();
|
| 642 |
+
drawEnemies();
|
| 643 |
+
drawParticles();
|
| 644 |
+
drawOthers();
|
| 645 |
+
if(P.alive && state==='play'){
|
| 646 |
+
drawPlayer(P, true);
|
| 647 |
+
drawUI();
|
| 648 |
+
}
|
| 649 |
+
}
|
| 650 |
|
| 651 |
+
// Loop
|
| 652 |
+
function loop(){
|
| 653 |
+
update();
|
| 654 |
+
draw();
|
| 655 |
+
requestAnimationFrame(loop);
|
| 656 |
+
}
|
| 657 |
+
|
| 658 |
+
// Socket
|
| 659 |
+
socket.on('l', d => { lb = d || []; renderLB('lb1'); renderLB('lb2'); });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
|
| 661 |
+
socket.on('p', list => {
|
| 662 |
+
const now = Date.now();
|
| 663 |
+
const ids = new Set();
|
| 664 |
+
for(const p of list){
|
| 665 |
+
ids.add(p.i);
|
| 666 |
+
if(p.i === socket.id) continue;
|
| 667 |
|
| 668 |
+
if(!others[p.i]){
|
| 669 |
+
others[p.i] = {...p, rx:p.x, ry:p.y, t:now};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
} else {
|
| 671 |
+
Object.assign(others[p.i], p);
|
| 672 |
+
others[p.i].t = now;
|
|
|
|
|
|
|
| 673 |
}
|
| 674 |
+
}
|
| 675 |
+
for(const id in others){
|
| 676 |
+
if(!ids.has(id)) delete others[id];
|
|
|
|
| 677 |
}
|
| 678 |
});
|
| 679 |
|
| 680 |
+
function renderLB(id){
|
| 681 |
+
const el = $('#'+id);
|
| 682 |
+
if(!el) return;
|
| 683 |
+
el.innerHTML = lb.length ? lb.map((e,i) =>
|
| 684 |
+
`<div class="lb-row"><span>${i+1}. ${e.n}</span><span>${e.s}</span></div>`
|
| 685 |
+
).join('') : '<div style="color:#555">No scores yet</div>';
|
| 686 |
+
}
|
| 687 |
+
|
| 688 |
+
// Controls
|
| 689 |
+
document.addEventListener('keydown', e => {
|
| 690 |
+
if(e.key==='ArrowLeft'||e.key==='a') keys.l = true;
|
| 691 |
+
if(e.key==='ArrowRight'||e.key==='d') keys.r = true;
|
| 692 |
+
});
|
| 693 |
+
document.addEventListener('keyup', e => {
|
| 694 |
+
if(e.key==='ArrowLeft'||e.key==='a') keys.l = false;
|
| 695 |
+
if(e.key==='ArrowRight'||e.key==='d') keys.r = false;
|
| 696 |
});
|
| 697 |
|
| 698 |
+
canvas.addEventListener('touchstart', e => {
|
| 699 |
+
e.preventDefault(); initAudio();
|
| 700 |
+
handleTouch(e.touches);
|
| 701 |
+
}, {passive:false});
|
| 702 |
+
canvas.addEventListener('touchmove', e => {
|
| 703 |
+
e.preventDefault();
|
| 704 |
+
handleTouch(e.touches);
|
| 705 |
+
}, {passive:false});
|
| 706 |
+
canvas.addEventListener('touchend', e => {
|
| 707 |
+
e.preventDefault();
|
| 708 |
+
handleTouch(e.touches);
|
| 709 |
+
}, {passive:false});
|
| 710 |
+
|
| 711 |
+
function handleTouch(touches){
|
| 712 |
+
touch.l = false; touch.r = false;
|
| 713 |
+
const rect = canvas.getBoundingClientRect();
|
| 714 |
+
for(const t of touches){
|
| 715 |
+
const x = (t.clientX - rect.left) / rect.width * W;
|
| 716 |
+
if(x < W/2) touch.l = true;
|
| 717 |
+
else touch.r = true;
|
| 718 |
+
}
|
| 719 |
}
|
| 720 |
|
| 721 |
+
// Start
|
| 722 |
+
$('#play').onclick = () => { initAudio(); startGame(); };
|
| 723 |
+
$('#retry').onclick = () => { $('#go').classList.add('hidden'); startGame(); };
|
| 724 |
+
$('#name').onkeypress = e => { if(e.key==='Enter'){ initAudio(); startGame(); } };
|
| 725 |
+
|
| 726 |
+
function startGame(){
|
| 727 |
+
pName = $('#name').value.trim() || 'Player';
|
| 728 |
+
pColor = `hsl(${Math.random()*360},80%,60%)`;
|
| 729 |
+
|
| 730 |
+
$('#menu').classList.add('hidden');
|
| 731 |
+
$('#go').classList.add('hidden');
|
| 732 |
+
|
| 733 |
+
initGame();
|
| 734 |
+
state = 'play';
|
| 735 |
+
|
| 736 |
+
socket.emit('j', {n:pName, c:pColor, x:P.x, y:P.y});
|
| 737 |
}
|
| 738 |
+
|
| 739 |
+
renderLB('lb1');
|
| 740 |
+
draw();
|
| 741 |
loop();
|
| 742 |
+
|
| 743 |
+
})();
|
| 744 |
+
</script>
|
| 745 |
</body>
|
| 746 |
</html>
|