Nafiul Huda commited on
Update public/index.html
Browse files- public/index.html +360 -722
public/index.html
CHANGED
|
@@ -1,746 +1,384 @@
|
|
| 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
|
| 6 |
-
<title>
|
| 7 |
-
<style>
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 178 |
-
const maxDx = 130;
|
| 179 |
-
const minX = Math.max(20, lastX - maxDx);
|
| 180 |
-
const maxX = Math.min(W - 95, lastX + maxDx);
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 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 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 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 |
-
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 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
}
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 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 |
-
|
| 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 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 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 |
-
|
| 431 |
-
|
| 432 |
-
ctx.
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
ctx.shadowColor = '#ff0';
|
| 447 |
-
|
| 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.
|
| 465 |
-
ctx.
|
| 466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
ctx.closePath();
|
| 468 |
ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
}
|
|
|
|
|
|
|
|
|
|
| 470 |
}
|
| 471 |
-
|
| 472 |
-
|
| 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>
|
|
|
|
| 1 |
+
mi<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Nebula Survivors</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { margin: 0; overflow: hidden; background: #050510; font-family: 'Segoe UI', Tahoma, sans-serif; color: white; user-select: none; }
|
| 9 |
+
canvas { display: block; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
#ui { position: absolute; pointer-events: none; width: 100%; height: 100%; top: 0; left: 0; }
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
/* Start Screen */
|
| 14 |
+
#startScreen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); display: flex; flex-direction: column; justify-content: center; align-items: center; pointer-events: auto; z-index: 10; }
|
| 15 |
+
h1 { font-size: 60px; margin: 0; background: linear-gradient(to right, #00f260, #0575E6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; text-shadow: 0 0 30px rgba(0,242,96,0.3); }
|
| 16 |
+
input { font-size: 20px; padding: 10px 20px; border-radius: 30px; border: 2px solid #444; background: #111; color: white; outline: none; text-align: center; margin: 20px; width: 250px; }
|
| 17 |
+
button { font-size: 22px; padding: 12px 40px; border-radius: 30px; border: none; background: linear-gradient(45deg, #00f260, #0575E6); color: white; cursor: pointer; font-weight: bold; transition: transform 0.1s; }
|
| 18 |
+
button:hover { transform: scale(1.05); box-shadow: 0 0 20px rgba(5, 117, 230, 0.5); }
|
| 19 |
|
| 20 |
+
/* HUD */
|
| 21 |
+
#statOverlay { position: absolute; bottom: 20px; left: 20px; pointer-events: auto; }
|
| 22 |
+
.upg-btn { display: block; background: rgba(0,0,0,0.6); border: 1px solid #444; color: #aaa; padding: 8px 15px; margin-top: 5px; cursor: pointer; border-radius: 5px; font-size: 14px; transition: 0.2s; width: 180px; text-align: left; }
|
| 23 |
+
.upg-btn:hover { background: rgba(255,255,255,0.1); color: #fff; border-color: #fff; }
|
| 24 |
+
.upg-avail { border-color: #0f0; color: #0f0; box-shadow: 0 0 10px rgba(0,255,0,0.2); }
|
| 25 |
+
.upg-avail:hover { background: rgba(0,255,0,0.2); }
|
| 26 |
+
|
| 27 |
+
#scoreBoard { position: absolute; top: 20px; right: 20px; text-align: right; }
|
| 28 |
+
.lvl-badge { background: #0575E6; padding: 5px 10px; border-radius: 10px; font-weight: bold; font-size: 14px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
/* Notifications */
|
| 31 |
+
#notif { position: absolute; top: 20%; left: 50%; transform: translateX(-50%); font-size: 24px; color: gold; text-shadow: 0 0 10px black; opacity: 0; transition: opacity 0.5s; font-weight: bold; }
|
| 32 |
+
</style>
|
| 33 |
+
</head>
|
| 34 |
+
<body>
|
| 35 |
+
<canvas id="gameCanvas"></canvas>
|
| 36 |
+
|
| 37 |
+
<div id="ui">
|
| 38 |
+
<div id="scoreBoard">
|
| 39 |
+
<div id="scoreVal" style="font-size: 24px; font-weight: bold;">0</div>
|
| 40 |
+
<div style="font-size: 14px; color: #888;">Score</div>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
<div id="statOverlay" style="display:none;">
|
| 44 |
+
<div style="font-weight:bold; margin-bottom:5px; color:#ddd;">UPGRADES (<span id="pts">0</span>)</div>
|
| 45 |
+
<div class="upg-btn" onclick="upgrade('spd')">[1] Speed</div>
|
| 46 |
+
<div class="upg-btn" onclick="upgrade('dmg')">[2] Damage</div>
|
| 47 |
+
<div class="upg-btn" onclick="upgrade('rld')">[3] Reload</div>
|
| 48 |
+
<div class="upg-btn" onclick="upgrade('reg')">[4] Regen</div>
|
| 49 |
+
</div>
|
| 50 |
|
| 51 |
+
<div id="notif">LEVEL UP!</div>
|
| 52 |
+
</div>
|
| 53 |
+
|
| 54 |
+
<div id="startScreen">
|
| 55 |
+
<h1>NEBULA SURVIVORS</h1>
|
| 56 |
+
<p style="color:#888; margin-bottom: 20px;">WASD to Move | Mouse to Aim | Click to Shoot</p>
|
| 57 |
+
<input type="text" id="nameInput" placeholder="Pilot Name" maxlength="12">
|
| 58 |
+
<button onclick="startGame()">LAUNCH</button>
|
| 59 |
+
</div>
|
| 60 |
+
|
| 61 |
+
<script src="/socket.io/socket.io.js"></script>
|
| 62 |
+
<script>
|
| 63 |
+
// === CONFIG ===
|
| 64 |
+
const canvas = document.getElementById('gameCanvas');
|
| 65 |
+
const ctx = canvas.getContext('2d');
|
| 66 |
+
const socket = io();
|
| 67 |
+
|
| 68 |
+
// Audio Context
|
| 69 |
+
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
| 70 |
+
const actx = new AudioContext();
|
| 71 |
+
|
| 72 |
+
// Game State
|
| 73 |
+
let myId = null;
|
| 74 |
+
let mapSize = 3000;
|
| 75 |
+
let me = null;
|
| 76 |
+
let players = []; // Array for rendering
|
| 77 |
+
let bullets = [];
|
| 78 |
+
let asteroids = [];
|
| 79 |
+
let orbs = [];
|
| 80 |
|
| 81 |
+
let camX = 0, camY = 0;
|
| 82 |
+
let mouseX = 0, mouseY = 0;
|
| 83 |
+
|
| 84 |
+
// Inputs
|
| 85 |
+
const keys = { w: false, a: false, s: false, d: false, space: false };
|
| 86 |
+
let isMouseDown = false;
|
| 87 |
+
|
| 88 |
+
// Visuals
|
| 89 |
+
let stars = [];
|
| 90 |
+
let particles = [];
|
| 91 |
+
|
| 92 |
+
// Resize
|
| 93 |
+
function resize() {
|
| 94 |
+
canvas.width = window.innerWidth;
|
| 95 |
+
canvas.height = window.innerHeight;
|
| 96 |
+
}
|
| 97 |
+
window.onresize = resize;
|
| 98 |
+
resize();
|
| 99 |
+
|
| 100 |
+
// Generate Background Stars
|
| 101 |
+
for(let i=0; i<200; i++) {
|
| 102 |
+
stars.push({
|
| 103 |
+
x: Math.random() * mapSize,
|
| 104 |
+
y: Math.random() * mapSize,
|
| 105 |
+
z: Math.random() * 2 + 0.5 // Depth
|
| 106 |
+
});
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
// === SOUND SYSTEM (Synthesizer) ===
|
| 110 |
+
function playSound(type) {
|
| 111 |
+
if(actx.state === 'suspended') actx.resume();
|
| 112 |
+
const osc = actx.createOscillator();
|
| 113 |
+
const gain = actx.createGain();
|
| 114 |
+
osc.connect(gain);
|
| 115 |
+
gain.connect(actx.destination);
|
| 116 |
+
|
| 117 |
+
const now = actx.currentTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
if (type === 'shoot') {
|
| 120 |
+
osc.type = 'triangle';
|
| 121 |
+
osc.frequency.setValueAtTime(400, now);
|
| 122 |
+
osc.frequency.exponentialRampToValueAtTime(100, now + 0.1);
|
| 123 |
+
gain.gain.setValueAtTime(0.1, now);
|
| 124 |
+
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.1);
|
| 125 |
+
osc.start(now);
|
| 126 |
+
osc.stop(now + 0.1);
|
| 127 |
+
} else if (type === 'exp') { // Explosion
|
| 128 |
+
osc.type = 'sawtooth';
|
| 129 |
+
osc.frequency.setValueAtTime(100, now);
|
| 130 |
+
osc.frequency.exponentialRampToValueAtTime(10, now + 0.2);
|
| 131 |
+
gain.gain.setValueAtTime(0.2, now);
|
| 132 |
+
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.2);
|
| 133 |
+
osc.start(now);
|
| 134 |
+
osc.stop(now + 0.2);
|
| 135 |
+
} else if (type === 'lvl') { // Level Up
|
| 136 |
+
osc.type = 'sine';
|
| 137 |
+
osc.frequency.setValueAtTime(440, now);
|
| 138 |
+
osc.frequency.setValueAtTime(660, now + 0.1);
|
| 139 |
+
osc.frequency.setValueAtTime(880, now + 0.2);
|
| 140 |
+
gain.gain.setValueAtTime(0.1, now);
|
| 141 |
+
gain.gain.linearRampToValueAtTime(0, now + 0.4);
|
| 142 |
+
osc.start(now);
|
| 143 |
+
osc.stop(now + 0.4);
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// === SOCKET HANDLERS ===
|
| 148 |
+
socket.on('init', (data) => {
|
| 149 |
+
myId = data.id;
|
| 150 |
+
mapSize = data.mapSize;
|
| 151 |
+
});
|
| 152 |
+
|
| 153 |
+
socket.on('u', (pack) => {
|
| 154 |
+
// Smooth Interpolation could go here, but for now we just update
|
| 155 |
+
players = pack.p;
|
| 156 |
|
| 157 |
+
// Reconstruct bullets from flattened array [x,y, x,y...]
|
| 158 |
+
bullets = [];
|
| 159 |
+
for(let i=0; i<pack.b.length; i+=2) {
|
| 160 |
+
bullets.push({x: pack.b[i], y: pack.b[i+1]});
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
asteroids = pack.a;
|
| 164 |
+
orbs = pack.o;
|
| 165 |
+
|
| 166 |
+
// Find myself
|
| 167 |
+
me = players.find(p => p.id === myId);
|
| 168 |
+
if (me) {
|
| 169 |
+
// Update HUD
|
| 170 |
+
document.getElementById('scoreVal').innerText = me.nm + " | Lvl " + me.lvl;
|
| 171 |
+
document.getElementById('pts').innerText = me.pts;
|
| 172 |
|
| 173 |
+
// Toggle Upgrade Buttons
|
| 174 |
+
const btns = document.getElementsByClassName('upg-btn');
|
| 175 |
+
for(let btn of btns) {
|
| 176 |
+
if(me.pts > 0) btn.classList.add('upg-avail');
|
| 177 |
+
else btn.classList.remove('upg-avail');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
}
|
|
|
|
| 179 |
}
|
| 180 |
+
});
|
| 181 |
+
|
| 182 |
+
socket.on('levelup', () => {
|
| 183 |
+
const n = document.getElementById('notif');
|
| 184 |
+
n.style.opacity = 1;
|
| 185 |
+
playSound('lvl');
|
| 186 |
+
setTimeout(() => n.style.opacity = 0, 2000);
|
| 187 |
+
});
|
| 188 |
+
|
| 189 |
+
socket.on('dead', () => {
|
| 190 |
+
particles.push({x: me.x, y: me.y, vx:0, vy:0, color:'red', life:50, size:50}); // Big boom visual
|
| 191 |
+
playSound('exp');
|
| 192 |
+
});
|
| 193 |
+
|
| 194 |
+
// === INPUTS ===
|
| 195 |
+
function startGame() {
|
| 196 |
+
const name = document.getElementById('nameInput').value || 'Pilot';
|
| 197 |
+
socket.emit('join', name);
|
| 198 |
+
document.getElementById('startScreen').style.display = 'none';
|
| 199 |
+
document.getElementById('statOverlay').style.display = 'block';
|
| 200 |
+
|
| 201 |
+
// Start sending inputs
|
| 202 |
+
setInterval(() => {
|
| 203 |
+
if(!me) return;
|
| 204 |
+
const angle = Math.atan2(mouseY - canvas.height/2, mouseX - canvas.width/2);
|
| 205 |
+
|
| 206 |
+
socket.emit('input', {
|
| 207 |
+
u: keys.w,
|
| 208 |
+
d: keys.s,
|
| 209 |
+
l: keys.a,
|
| 210 |
+
r: keys.d,
|
| 211 |
+
s: isMouseDown,
|
| 212 |
+
angle: angle
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
if(isMouseDown) playSound('shoot');
|
| 216 |
+
}, 1000/30); // 30hz Input rate
|
| 217 |
}
|
| 218 |
+
|
| 219 |
+
function upgrade(stat) {
|
| 220 |
+
socket.emit('upgrade', stat);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
}
|
| 222 |
+
|
| 223 |
+
window.onkeydown = (e) => {
|
| 224 |
+
if(e.key === 'w' || e.key === 'ArrowUp') keys.w = true;
|
| 225 |
+
if(e.key === 's' || e.key === 'ArrowDown') keys.s = true;
|
| 226 |
+
if(e.key === 'a' || e.key === 'ArrowLeft') keys.a = true;
|
| 227 |
+
if(e.key === 'd' || e.key === 'ArrowRight') keys.d = true;
|
| 228 |
+
|
| 229 |
+
if(e.key === '1') upgrade('spd');
|
| 230 |
+
if(e.key === '2') upgrade('dmg');
|
| 231 |
+
if(e.key === '3') upgrade('rld');
|
| 232 |
+
if(e.key === '4') upgrade('reg');
|
| 233 |
+
};
|
| 234 |
+
|
| 235 |
+
window.onkeyup = (e) => {
|
| 236 |
+
if(e.key === 'w' || e.key === 'ArrowUp') keys.w = false;
|
| 237 |
+
if(e.key === 's' || e.key === 'ArrowDown') keys.s = false;
|
| 238 |
+
if(e.key === 'a' || e.key === 'ArrowLeft') keys.a = false;
|
| 239 |
+
if(e.key === 'd' || e.key === 'ArrowRight') keys.d = false;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
window.onmousemove = (e) => { mouseX = e.clientX; mouseY = e.clientY; };
|
| 243 |
+
window.onmousedown = () => isMouseDown = true;
|
| 244 |
+
window.onmouseup = () => isMouseDown = false;
|
| 245 |
+
|
| 246 |
+
// === RENDER LOOP ===
|
| 247 |
+
function draw() {
|
| 248 |
+
// Clear
|
| 249 |
+
ctx.fillStyle = '#050510';
|
| 250 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 251 |
+
|
| 252 |
+
// Camera follow
|
| 253 |
+
if (me) {
|
| 254 |
+
// Smooth camera
|
| 255 |
+
camX += (me.x - canvas.width/2 - camX) * 0.1;
|
| 256 |
+
camY += (me.y - canvas.height/2 - camY) * 0.1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
}
|
| 258 |
+
|
| 259 |
+
ctx.save();
|
| 260 |
+
ctx.translate(-camX, -camY);
|
| 261 |
+
|
| 262 |
+
// Draw Background Stars (Parallax)
|
| 263 |
+
ctx.fillStyle = '#fff';
|
| 264 |
+
stars.forEach(s => {
|
| 265 |
+
const px = (s.x - camX * (s.z * 0.1)) % mapSize;
|
| 266 |
+
const py = (s.y - camY * (s.z * 0.1)) % mapSize;
|
| 267 |
+
// Wrap visual
|
| 268 |
+
const wrapX = px < 0 ? px + mapSize : px;
|
| 269 |
+
const wrapY = py < 0 ? py + mapSize : py;
|
| 270 |
+
|
| 271 |
+
ctx.globalAlpha = s.z * 0.5;
|
| 272 |
+
ctx.beginPath();
|
| 273 |
+
ctx.arc(wrapX, wrapY, s.z, 0, Math.PI*2);
|
| 274 |
+
ctx.fill();
|
| 275 |
+
});
|
| 276 |
+
ctx.globalAlpha = 1;
|
| 277 |
+
|
| 278 |
+
// Draw Map Borders
|
| 279 |
+
ctx.strokeStyle = '#f00';
|
| 280 |
+
ctx.lineWidth = 5;
|
| 281 |
+
ctx.strokeRect(0,0, mapSize, mapSize);
|
| 282 |
+
|
| 283 |
+
// Draw Orbs
|
| 284 |
+
orbs.forEach(o => {
|
| 285 |
+
ctx.fillStyle = `hsl(${Date.now()/20 % 360}, 100%, 50%)`;
|
| 286 |
+
ctx.shadowBlur = 10;
|
| 287 |
+
ctx.shadowColor = ctx.fillStyle;
|
| 288 |
+
ctx.beginPath();
|
| 289 |
+
ctx.arc(o.x, o.y, 4, 0, Math.PI*2);
|
| 290 |
+
ctx.fill();
|
| 291 |
+
ctx.shadowBlur = 0;
|
| 292 |
+
});
|
| 293 |
+
|
| 294 |
+
// Draw Asteroids
|
| 295 |
+
ctx.fillStyle = '#334';
|
| 296 |
+
ctx.strokeStyle = '#556';
|
| 297 |
+
ctx.lineWidth = 3;
|
| 298 |
+
asteroids.forEach(a => {
|
| 299 |
+
ctx.beginPath();
|
| 300 |
+
ctx.arc(a.x, a.y, a.r, 0, Math.PI*2);
|
| 301 |
+
ctx.fill();
|
| 302 |
+
ctx.stroke();
|
| 303 |
+
});
|
| 304 |
+
|
| 305 |
+
// Draw Bullets
|
| 306 |
+
ctx.fillStyle = '#ff9';
|
| 307 |
+
ctx.shadowBlur = 5;
|
| 308 |
ctx.shadowColor = '#ff0';
|
| 309 |
+
bullets.forEach(b => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
ctx.beginPath();
|
| 311 |
+
ctx.arc(b.x, b.y, 3, 0, Math.PI*2);
|
| 312 |
+
ctx.fill();
|
| 313 |
+
});
|
| 314 |
+
ctx.shadowBlur = 0;
|
| 315 |
+
|
| 316 |
+
// Draw Players
|
| 317 |
+
players.forEach(p => {
|
| 318 |
+
const isMe = p.id === myId;
|
| 319 |
+
|
| 320 |
+
ctx.save();
|
| 321 |
+
ctx.translate(p.x, p.y);
|
| 322 |
+
ctx.rotate(p.a);
|
| 323 |
+
|
| 324 |
+
// Ship Body
|
| 325 |
+
ctx.fillStyle = isMe ? '#00f260' : '#f05053';
|
| 326 |
+
ctx.strokeStyle = '#fff';
|
| 327 |
+
ctx.lineWidth = 2;
|
| 328 |
+
|
| 329 |
+
ctx.beginPath();
|
| 330 |
+
ctx.moveTo(15, 0);
|
| 331 |
+
ctx.lineTo(-10, 10);
|
| 332 |
+
ctx.lineTo(-5, 0);
|
| 333 |
+
ctx.lineTo(-10, -10);
|
| 334 |
ctx.closePath();
|
| 335 |
ctx.fill();
|
| 336 |
+
ctx.stroke();
|
| 337 |
+
|
| 338 |
+
// Engine Flame
|
| 339 |
+
if(Math.random() > 0.5) {
|
| 340 |
+
ctx.fillStyle = '#fa0';
|
| 341 |
+
ctx.beginPath();
|
| 342 |
+
ctx.moveTo(-12, 0);
|
| 343 |
+
ctx.lineTo(-20, 5);
|
| 344 |
+
ctx.lineTo(-20, -5);
|
| 345 |
+
ctx.fill();
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
ctx.restore();
|
| 349 |
+
|
| 350 |
+
// Health Bar
|
| 351 |
+
const hpPct = p.hp / p.mhp;
|
| 352 |
+
ctx.fillStyle = '#300';
|
| 353 |
+
ctx.fillRect(p.x - 20, p.y + 25, 40, 6);
|
| 354 |
+
ctx.fillStyle = '#0f0';
|
| 355 |
+
ctx.fillRect(p.x - 20, p.y + 25, 40 * hpPct, 6);
|
| 356 |
+
|
| 357 |
+
// Name
|
| 358 |
+
ctx.fillStyle = '#fff';
|
| 359 |
+
ctx.font = '12px Arial';
|
| 360 |
+
ctx.textAlign = 'center';
|
| 361 |
+
ctx.fillText(p.nm, p.x, p.y - 25);
|
| 362 |
+
});
|
| 363 |
+
|
| 364 |
+
// Particles (Explosions)
|
| 365 |
+
for(let i=particles.length-1; i>=0; i--) {
|
| 366 |
+
const p = particles[i];
|
| 367 |
+
p.life--;
|
| 368 |
+
p.size *= 0.95;
|
| 369 |
+
ctx.globalAlpha = p.life / 20;
|
| 370 |
+
ctx.fillStyle = p.color;
|
| 371 |
+
ctx.beginPath();
|
| 372 |
+
ctx.arc(p.x, p.y, p.size, 0, Math.PI*2);
|
| 373 |
+
ctx.fill();
|
| 374 |
+
if(p.life <= 0) particles.splice(i, 1);
|
| 375 |
}
|
| 376 |
+
|
| 377 |
+
ctx.restore();
|
| 378 |
+
requestAnimationFrame(draw);
|
| 379 |
}
|
| 380 |
+
|
| 381 |
+
draw();
|
| 382 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
</body>
|
| 384 |
</html>
|