965 lines
51 KiB
HTML
965 lines
51 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Body Snatcher</title>
|
|
<style>
|
|
html,body{margin:0;padding:0;background:#05050c;color:#cfcfe0;font-family:monospace;}
|
|
#wrap{display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:100vh;padding:12px;box-sizing:border-box;}
|
|
canvas{background:#0a0a14;border:1px solid #2a2a44;image-rendering:pixelated;display:block;}
|
|
#hint{margin-top:8px;font-size:12px;color:#6272a4;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="wrap">
|
|
<canvas id="game" width="800" height="624"></canvas>
|
|
<div id="hint">Click the canvas, then use the keys shown on the title screen.</div>
|
|
</div>
|
|
<script>
|
|
(function(){
|
|
'use strict';
|
|
|
|
// ===== Constants =====
|
|
var TILE=32, COLS=25, ROWS=18, HUD_H=48;
|
|
var W=COLS*TILE, H=ROWS*TILE+HUD_H; // 800 x 624
|
|
var GHOST_BASE=15, GHOST_T1=20;
|
|
var TIMER_STEP=0.1, LOOP_MS=100; // 1 tick == 0.1s == one loop slice
|
|
var VISION=5; // fog radius (chebyshev)
|
|
var SAVE_KEY='bodysnatcher_save';
|
|
|
|
// ===== Tile factory =====
|
|
function tFloor(){return {type:'floor',solid:false};}
|
|
function tWall(){return {type:'wall',solid:true};}
|
|
function tExit(){return {type:'exit',solid:false};}
|
|
function tDoor(){return {type:'door',solid:false};}
|
|
function tShop(){return {type:'shop',solid:false};}
|
|
function tChest(){return {type:'chest',solid:true};}
|
|
|
|
// ===== Enemy base stats (dual: AI + possessed share these numbers) =====
|
|
var ENEMY_STATS={
|
|
brute:{hp:8,damage:3,speed:2},
|
|
skitterer:{hp:2,damage:1,speed:1},
|
|
sentinel:{hp:4,damage:1,speed:1,ranged:true,melee:false},
|
|
shielder:{hp:5,damage:2,speed:2},
|
|
bomber:{hp:3,damage:3,speed:1},
|
|
healer:{hp:3,damage:1,speed:1},
|
|
phaser:{hp:4,damage:2,speed:1},
|
|
turret:{hp:4,damage:1,speed:0,ranged:true},
|
|
charger:{hp:5,damage:3,speed:1},
|
|
siren:{hp:3,damage:1,speed:2},
|
|
mimic:{hp:4,damage:3,speed:1},
|
|
leech:{hp:4,damage:1,speed:2}
|
|
};
|
|
var ENEMY_COLORS={brute:'#ff5555',skitterer:'#ff9933',sentinel:'#bd93f9',shielder:'#5c8aff',
|
|
bomber:'#a0202a',healer:'#50fa7b',phaser:'#8be9fd',turret:'#999999',charger:'#f1fa8c',
|
|
siren:'#ff79c6',mimic:'#b58147',leech:'#7a3d9a'};
|
|
var ESSENCE_DROPS={brute:3,skitterer:1,sentinel:3,shielder:4,bomber:2,healer:5,phaser:4,turret:5,charger:4,siren:3,mimic:4,leech:4};
|
|
var AI_DAMAGE={brute:2,skitterer:1,sentinel:1,shielder:2,bomber:3,healer:1,phaser:2,turret:1,charger:3,siren:1,mimic:2,leech:1};
|
|
var FACE_DELTA={up:{dx:0,dy:-1},down:{dx:0,dy:1},left:{dx:-1,dy:0},right:{dx:1,dy:0}};
|
|
|
|
// ===== Live state =====
|
|
var G={
|
|
state:'title', floor:1, essence:0, upgrades:{},
|
|
achievements:{firstPossession:false,bodyHopper:false,pacifistGhost:false,essenceHoarder:false,
|
|
deepDiver:false,escapeArtist:false,bomberman:false,dualWield:false},
|
|
player:{x:3,y:3,ghost:true,timer:GHOST_BASE,host:null,body:null,facing:'down',hp:1},
|
|
enemies:[], projectiles:[], grid:null, explored:null, log:[], shop:null
|
|
};
|
|
var _setupTick=false; // first world-step after entering play costs no timer
|
|
function pushLog(m){ G.log.push(m); if(G.log.length>60) G.log.shift(); }
|
|
|
|
// ===== Canvas (tolerates missing/null/mock context) =====
|
|
var _canvas=null;
|
|
function getCanvas(){ try{ if(!_canvas) _canvas=document.getElementById('game'); return _canvas; }catch(e){ return null; } }
|
|
|
|
// ===== Audio (Web Audio, tolerates mock constructor) =====
|
|
var _ac=null;
|
|
function audioCtx(){ try{ if(!_ac){ var AC=window.AudioContext||window.webkitAudioContext; if(AC) _ac=new AC(); } return _ac; }catch(e){ return null; } }
|
|
function beep(freq,dur,type,vol){
|
|
try{
|
|
var ac=audioCtx(); if(!ac) return;
|
|
if(ac.resume){ try{ ac.resume(); }catch(e){} }
|
|
var o=ac.createOscillator(), g=ac.createGain();
|
|
o.type=type||'square'; o.frequency.value=freq||440; g.gain.value=vol||0.05;
|
|
o.connect(g); g.connect(ac.destination);
|
|
var t=(typeof ac.currentTime==='number')?ac.currentTime:0;
|
|
o.start(t); o.stop(t+(dur||0.1));
|
|
}catch(e){}
|
|
}
|
|
// ===== Game feel (VISUAL ONLY: never affects tick/state; safe under mock ctx) =====
|
|
var _parts=[], _shake={t:0,mag:0}, _vLast=0;
|
|
function nowMs(){ try{ return (typeof performance!=='undefined'&&performance.now)?performance.now():Date.now(); }catch(e){ return Date.now(); } }
|
|
function shake(mag,dur){ _shake.mag=Math.max(_shake.mag,mag||4); _shake.t=Math.max(_shake.t,dur||0.25); }
|
|
function burst(px,py,color,n,spd){
|
|
try{ var c=color||'#fff'; n=n||8; spd=spd||2;
|
|
for(var i=0;i<n;i++){ var a=Math.random()*Math.PI*2, s=(0.4+Math.random())*spd*60;
|
|
_parts.push({x:px,y:py,vx:Math.cos(a)*s,vy:Math.sin(a)*s-20,life:0.4+Math.random()*0.4,max:0.8,color:c,size:2+Math.random()*2}); } }catch(e){}
|
|
}
|
|
function flashEntity(o){ try{ o._fl=nowMs(); }catch(e){} }
|
|
function isFlashing(o){ try{ return !!(o&&o._fl&&(nowMs()-o._fl)<100); }catch(e){ return false; } }
|
|
function tileCenter(x,y){ return {px:x*TILE+TILE/2, py:HUD_H+y*TILE+TILE/2}; }
|
|
function updateVisuals(){
|
|
var now=nowMs(); if(!_vLast) _vLast=now;
|
|
var dt=Math.min(0.05,(now-_vLast)/1000); _vLast=now;
|
|
if(_shake.t>0){ _shake.t=Math.max(0,_shake.t-dt); if(_shake.t<=0) _shake.mag=0; }
|
|
if(_parts.length){ var keep=[]; for(var i=0;i<_parts.length;i++){ var p=_parts[i]; p.x+=p.vx*dt; p.y+=p.vy*dt; p.vy+=120*dt; p.life-=dt; if(p.life>0) keep.push(p); } _parts=keep; }
|
|
}
|
|
// ponytail: render detects tile changes -> animates last-pos->cur over 150ms (no movement-code edits)
|
|
function visualPos(e){
|
|
var now=nowMs();
|
|
if(e._tx===undefined){ e._tx=e.x; e._ty=e.y; e._fx=e.x; e._fy=e.y; e._t0=now-150; }
|
|
if(e.x!==e._tx||e.y!==e._ty){ e._fx=e._tx; e._fy=e._ty; e._tx=e.x; e._ty=e.y; e._t0=now; }
|
|
var t=Math.max(0,Math.min(1,(now-e._t0)/150)); t=1-(1-t)*(1-t);
|
|
return {x:e._fx+(e._tx-e._fx)*t, y:e._fy+(e._ty-e._fy)*t};
|
|
}
|
|
|
|
// ===== Dungeon (procgen: random walk + room carving; different every call) =====
|
|
function buildDungeon(n){
|
|
n=n||1; G.grid=[]; G.explored=[];
|
|
for(var y=0;y<ROWS;y++){ var row=[],er=[]; for(var x=0;x<COLS;x++){ row.push(tWall()); er.push(false); } G.grid.push(row); G.explored.push(er); }
|
|
var rooms=[], target=5+Math.floor(Math.random()*4), tries=0; // 5-8 rooms
|
|
while(rooms.length<target && tries<90){
|
|
tries++;
|
|
var rw=4+Math.floor(Math.random()*5), rh=4+Math.floor(Math.random()*3); // 4-8 x 4-6
|
|
var rx=1+Math.floor(Math.random()*Math.max(1,COLS-rw-2)), ry=1+Math.floor(Math.random()*Math.max(1,ROWS-rh-2));
|
|
var overlap=false;
|
|
for(var i=0;i<rooms.length;i++){ var o=rooms[i]; if(rx<o.x+o.w+1&&rx+rw+1>o.x&&ry<o.y+o.h+1&&ry+rh+1>o.y){ overlap=true; break; } }
|
|
if(overlap) continue;
|
|
var room={x:rx,y:ry,w:rw,h:rh,cx:Math.floor(rx+rw/2),cy:Math.floor(ry+rh/2)};
|
|
for(var yy=ry;yy<ry+rh;yy++) for(var xx=rx;xx<rx+rw;xx++) if(G.grid[yy]) G.grid[yy][xx]=tFloor();
|
|
rooms.push(room);
|
|
}
|
|
if(!rooms.length){ var rw=6,rh=5,rx=2,ry=2; for(var yy=ry;yy<ry+rh;yy++) for(var xx=rx;xx<rx+rw;xx++) G.grid[yy][xx]=tFloor(); rooms.push({x:rx,y:ry,w:rw,h:rh,cx:5,cy:4}); }
|
|
for(var i=1;i<rooms.length;i++){ var a=rooms[i-1],b=rooms[i]; if(Math.random()<0.5){ carveH(a.cx,b.cx,a.cy); carveV(a.cy,b.cy,b.cx); } else { carveV(a.cy,b.cy,a.cx); carveH(a.cx,b.cx,b.cy); } } // L-corridors, never diagonal
|
|
G._spawn={x:rooms[0].cx,y:rooms[0].cy};
|
|
var far=rooms[rooms.length-1], farD=-1; // exit in farthest room from spawn
|
|
for(var i=1;i<rooms.length;i++){ var dd=manhattan(rooms[i].cx,rooms[i].cy,G._spawn.x,G._spawn.y); if(dd>farD){ farD=dd; far=rooms[i]; } }
|
|
if(G.grid[far.cy]&&G.grid[far.cy][far.cx]) G.grid[far.cy][far.cx]=tExit();
|
|
G._exit={x:far.cx,y:far.cy}; // remember exit pos (survives tile edits / fog)
|
|
var nChests=1+Math.floor(Math.random()*2); // 1-2 chests
|
|
for(var c=0;c<nChests;c++){ var ri=1+Math.floor(Math.random()*Math.max(1,rooms.length-1)); var r=rooms[Math.min(ri,rooms.length-1)]; var cx=r.x+1+Math.floor(Math.random()*Math.max(1,r.w-2)), cy=r.y+1+Math.floor(Math.random()*Math.max(1,r.h-2)); if(G.grid[cy]&&G.grid[cy][cx]&&G.grid[cy][cx].type==='floor') G.grid[cy][cx]=tChest(); }
|
|
}
|
|
function carveH(x1,x2,y){ var a=Math.min(x1,x2),b=Math.max(x1,x2); for(var x=a;x<=b;x++) if(G.grid[y]&&G.grid[y][x]) G.grid[y][x]=tFloor(); }
|
|
function carveV(y1,y2,x){ var a=Math.min(y1,y2),b=Math.max(y1,y2); for(var y=a;y<=b;y++) if(G.grid[y]&&G.grid[y][x]) G.grid[y][x]=tFloor(); }
|
|
function floorEnemyPool(n){ var p=['brute','skitterer','sentinel']; if(n>=4)p=p.concat(['shielder','bomber','charger']); if(n>=7)p=p.concat(['healer','phaser','siren','turret']); if(n>=10)p=p.concat(['mimic','leech']); return p; }
|
|
function populateFloor(n){
|
|
G.enemies=[]; G.projectiles=[];
|
|
var count=4+(n||1), pool=floorEnemyPool(n||1), placed=0, t=0;
|
|
var sx=(G._spawn&&typeof G._spawn.x==='number')?G._spawn.x:G.player.x, sy=(G._spawn&&typeof G._spawn.y==='number')?G._spawn.y:G.player.y;
|
|
while(placed<count && t<count*60){
|
|
t++;
|
|
var x=1+Math.floor(Math.random()*(COLS-2)), y=1+Math.floor(Math.random()*(ROWS-2));
|
|
var tile=G.grid[y]&&G.grid[y][x]; if(!tile||tile.solid) continue;
|
|
if((x===sx&&y===sy)||(x===G.player.x&&y===G.player.y)) continue;
|
|
if(manhattan(x,y,sx,sy)<6) continue; // ponytail: keep spawn clear (anchor on G._spawn, set by buildDungeon); movement/possession tests depend on it
|
|
if(tile.type==='exit'||tile.type==='chest') continue;
|
|
if(enemyAt(x,y)) continue;
|
|
addEnemy(pool[Math.floor(Math.random()*pool.length)],x,y); placed++;
|
|
}
|
|
}
|
|
function applyMeta1(){
|
|
if(!(G.upgrades&&G.upgrades.meta1)||!G.player.ghost||!G.enemies.length) return;
|
|
var e=G.enemies[Math.floor(Math.random()*G.enemies.length)], k=G.enemies.indexOf(e); if(k<0)return;
|
|
G.enemies.splice(k,1); e.x=G.player.x; e.y=G.player.y;
|
|
G.player.host=e; G.player.body=e; G.player.ghost=false; G.player.hp=e.hp; G.state='possessed';
|
|
G._possessedTypes=G._possessedTypes||{}; G._possessedTypes[e.type]=true; applyOverclock();
|
|
}
|
|
function applyOverclock(){
|
|
if(!(G.upgrades&&G.upgrades.body2)||!G.player.host||G.player.host._oc) return;
|
|
var h=G.player.host; h._oc=5; h._ocBaseMax=h.maxHp; h._ocBaseDmg=h.damage;
|
|
h.maxHp=Math.max(1,Math.round(h.maxHp*1.5)); h.hp=h.maxHp; h.damage=Math.max(1,Math.round(h.damage*1.5));
|
|
}
|
|
|
|
function inBounds(x,y){ return x>=0&&x<COLS&&y>=0&&y<ROWS; }
|
|
function isSolidAt(x,y){ if(!inBounds(x,y))return true; var t=G.grid[y][x]; return !!(t&&t.solid); }
|
|
function visionRadius(){ return (G.upgrades&&G.upgrades.meta2)?8:VISION; }
|
|
function isVisible(x,y){ return Math.max(Math.abs(x-G.player.x),Math.abs(y-G.player.y))<=visionRadius(); }
|
|
function updateFog(){
|
|
if(!G.explored) return;
|
|
for(var y=0;y<ROWS;y++) for(var x=0;x<COLS;x++) if(isVisible(x,y)) G.explored[y][x]=true;
|
|
}
|
|
function ghostTimerMax(){ return (G.upgrades&&G.upgrades.ghost1)?GHOST_T1:GHOST_BASE; }
|
|
|
|
function placePlayerSpawn(){
|
|
var sx=G._spawn?G._spawn.x:3, sy=G._spawn?G._spawn.y:3;
|
|
if(isSolidAt(sx,sy)){ outer: for(var y=1;y<ROWS;y++) for(var x=1;x<COLS;x++){ if(G.grid[y][x]&&!G.grid[y][x].solid){ sx=x; sy=y; break outer; } } }
|
|
G.player.x=sx; G.player.y=sy;
|
|
G.player.ghost=true; G.player.host=null; G.player.body=null;
|
|
G.player.facing='down'; G.player.hp=1; G.player.timer=ghostTimerMax();
|
|
updateFog();
|
|
}
|
|
|
|
// ===== Run / floor control =====
|
|
G.start=function(){
|
|
G.state='title'; G.floor=1; G.essence=0; G.upgrades={};
|
|
G.enemies=[]; G.projectiles=[]; G.log=[]; G.shop=null; G._tookHostDmg=false;
|
|
G.player={x:3,y:3,ghost:true,timer:ghostTimerMax(),host:null,body:null,facing:'down',hp:1};
|
|
_setupTick=false; buildDungeon(1); updateFog();
|
|
};
|
|
function startGame(){
|
|
G.floor=1; G.essence=0; G.upgrades={};
|
|
G.enemies=[]; G.projectiles=[]; G.log=[]; G.shop=null; G._possessedTypes={}; G._pendingFloor=null; G._retained=null; G._tookHostDmg=false;
|
|
G.player={x:3,y:3,ghost:true,timer:ghostTimerMax(),host:null,body:null,facing:'down',hp:1};
|
|
buildDungeon(1); placePlayerSpawn(); populateFloor(1); applyMeta1(); _setupTick=true; G.state='playing';
|
|
pushLog('Run started. Find a body to possess.'); beep(660,0.08,'square');
|
|
}
|
|
G.newRun=function(){ startGame(); };
|
|
G.setFloor=function(n){
|
|
G.floor=n||1; G.projectiles=[]; G.shop=null; G._pendingFloor=null;
|
|
G.player={x:3,y:3,ghost:true,timer:ghostTimerMax(),host:null,body:null,facing:'down',hp:1};
|
|
buildDungeon(n); placePlayerSpawn(); populateFloor(n); applyMeta1(); _setupTick=true; G.state='playing';
|
|
if(G.floor>=7) G.achievements.deepDiver=true; if(G.floor>=3&&!G._tookHostDmg) G.achievements.pacifistGhost=true;
|
|
};
|
|
|
|
// ===== Input =====
|
|
function normKey(k){ if(k===' '||k==='Space')return 'Space'; return k; }
|
|
function dirFromKey(k){
|
|
switch(k){
|
|
case 'ArrowUp':case 'w':case 'W':return {dx:0,dy:-1,face:'up'};
|
|
case 'ArrowDown':case 's':case 'S':return {dx:0,dy:1,face:'down'};
|
|
case 'ArrowLeft':case 'a':case 'A':return {dx:-1,dy:0,face:'left'};
|
|
case 'ArrowRight':case 'd':case 'D':return {dx:1,dy:0,face:'right'};
|
|
}
|
|
return null;
|
|
}
|
|
function tryMove(d){
|
|
var host=G.player.host;
|
|
G.player.facing=d.face; // face even on a blocked bump
|
|
if(host&&host.rooted) return; // rooted bodies (turret) cannot move
|
|
var nx=G.player.x+d.dx, ny=G.player.y+d.dy;
|
|
if(!inBounds(nx,ny)) return;
|
|
var ghostPhasing=G.player.ghost&&G.upgrades&&G.upgrades.ghost3;
|
|
if(isSolidAt(nx,ny)&&!ghostPhasing) return; // ghost T3 phases walls
|
|
G.player.x=nx; G.player.y=ny; updateFog();
|
|
if(host){ host.x=nx; host.y=ny; }
|
|
var t=G.grid[ny]&&G.grid[ny][nx];
|
|
if(t&&t.type==='exit') descend();
|
|
}
|
|
function descend(){
|
|
if(G.floor>=10){ G.state='won'; G.achievements.escapeArtist=true; beep(880,0.22,'triangle'); G.save(); return; }
|
|
G._pendingFloor=G.floor+1; _openShop(false); pushLog('Floor cleared! Shop time.'); beep(600,0.1,'sine'); G.save();
|
|
}
|
|
function closeShop(){
|
|
G.shop=null;
|
|
if(G._pendingFloor){ var f=G._pendingFloor; G._pendingFloor=null; G.floor=f; if(G.floor>=7)G.achievements.deepDiver=true; if(G.floor>=3&&!G._tookHostDmg)G.achievements.pacifistGhost=true; buildDungeon(f); placePlayerSpawn(); populateFloor(f); applyMeta1(); _setupTick=true; G.state='playing'; pushLog('Descended to floor '+G.floor+'.'); beep(520,0.1,'sine'); G.save(); }
|
|
else { G.state=(G.player&&G.player.ghost)?'playing':'possessed'; }
|
|
}
|
|
function rotateFacing(dir){
|
|
var order=['up','right','down','left'];
|
|
var i=order.indexOf(G.player.facing); if(i<0) i=0;
|
|
G.player.facing=order[(i+dir+4)%4];
|
|
}
|
|
function ghostDash(dir){ // Ghost T4: 3-tile dash while incorporeal (phases walls)
|
|
var order=['up','right','down','left']; var i=order.indexOf(G.player.facing); if(i<0)i=0;
|
|
G.player.facing=order[(i+dir+4)%4];
|
|
var fd=FACE_DELTA[G.player.facing]||FACE_DELTA.down, x=G.player.x,y=G.player.y;
|
|
for(var s=0;s<3;s++){ var nx=x+fd.dx,ny=y+fd.dy; if(!inBounds(nx,ny))break; x=nx; y=ny; }
|
|
G.player.x=x; G.player.y=y; updateFog(); beep(740,0.08,'sine');
|
|
}
|
|
|
|
G.input=function(key){
|
|
var k=normKey(key);
|
|
try{
|
|
if(G.state==='title'){ if(k==='Enter'||k==='Space') startGame(); return; }
|
|
if(G.state==='won'||G.state==='lost'){ if(k==='Enter') G.start(); return; }
|
|
if(G.state==='paused'){ if(k==='Escape'||k==='Enter'){ G.state=G._prev||'playing'; return; } if(k==='s'||k==='S'){ G.save(); pushLog('Saved.'); return; } if(k==='q'||k==='Q'){ G.save(); G.start(); return; } return; }
|
|
if(G.state==='shop'){ handleShopInput(k); return; }
|
|
if(k==='Escape'){ G._prev=G.state; G.state='paused'; return; }
|
|
var d=dirFromKey(k);
|
|
if(d){ tryMove(d); return; }
|
|
if(k==='q'||k==='Q'){ if(G.player.ghost&&G.upgrades&&G.upgrades.ghost4) ghostDash(-1); else rotateFacing(-1); return; }
|
|
if(k==='e'||k==='E'){ if(G.player.ghost&&G.upgrades&&G.upgrades.ghost4) ghostDash(1); else rotateFacing(1); return; }
|
|
if(k==='Space'){
|
|
if(G.player.ghost) doPossess();
|
|
else doHostAction();
|
|
return;
|
|
}
|
|
}catch(e){ pushLog('input error: '+e); }
|
|
};
|
|
|
|
// ===== Deterministic world step =====
|
|
function manhattan(ax,ay,bx,by){ return Math.abs(ax-bx)+Math.abs(ay-by); }
|
|
function sign(n){ return n>0?1:(n<0?-1:0); }
|
|
function faceFromDelta(dx,dy){ if(Math.abs(dx)>=Math.abs(dy)) return dx>0?'right':'left'; return dy>0?'down':'up'; }
|
|
function dirToFace(e,p){ var dx=p.x-e.x,dy=p.y-e.y; if(dx===0&&dy===0) return e.facing||'down'; if(Math.abs(dx)>=Math.abs(dy)) return dx>0?'right':'left'; return dy>0?'down':'up'; }
|
|
function enemyAt(x,y){ for(var i=0;i<G.enemies.length;i++){ var e=G.enemies[i]; if(e.hp>0&&e.x===x&&e.y===y) return e; } return null; }
|
|
function possessRange(){ return (G.upgrades&&G.upgrades.ghost2)?2:1; }
|
|
|
|
function findPossessTarget(){
|
|
var p=G.player, r=possessRange();
|
|
var fd=FACE_DELTA[p.facing]||FACE_DELTA.down;
|
|
var pref=enemyAt(p.x+fd.dx,p.y+fd.dy); if(pref) return pref;
|
|
for(var dy=-r;dy<=r;dy++) for(var dx=-r;dx<=r;dx++){
|
|
if(dx===0&&dy===0) continue;
|
|
if(Math.max(Math.abs(dx),Math.abs(dy))>r) continue; // chebyshev radius: also grabs a diagonally-placed target
|
|
var e=enemyAt(p.x+dx,p.y+dy); if(e) return e;
|
|
}
|
|
return null;
|
|
}
|
|
function doPossess(){
|
|
var e=findPossessTarget();
|
|
if(!e){ pushLog('No enemy in range to possess.'); beep(200,0.05,'square'); return; }
|
|
G.enemies=G.enemies.filter(function(en){return en!==e;});
|
|
e.x=G.player.x; e.y=G.player.y;
|
|
G.player.host=e; G.player.body=e; G.player.ghost=false; G.player.hp=e.hp;
|
|
G.state='possessed';
|
|
if(G.upgrades&&G.upgrades.body1&&G._retained){ e.maxHp+=2; e.hp+=2; } // ponytail: Body T1 retain = small bonus from remembered host
|
|
G._retained=e.type;
|
|
applyOverclock(); // Body T2 overclock on fresh possession
|
|
G._possessedTypes=G._possessedTypes||{}; G._possessedTypes[e.type]=true;
|
|
G.achievements.firstPossession=true;
|
|
if(Object.keys(G._possessedTypes).length>=5) G.achievements.bodyHopper=true;
|
|
pushLog('Possessed a '+e.type+'.'); var pc=tileCenter(G.player.x,G.player.y); burst(pc.px,pc.py,'#8be9fd',14,2); beep(540,0.12,'sine'); G.save();
|
|
}
|
|
function bodyActionLabel(host){
|
|
if(!host) return 'Attack';
|
|
switch(host.type){
|
|
case 'sentinel': case 'turret': return 'Shoot';
|
|
case 'bomber': return 'Detonate';
|
|
case 'healer': return 'Heal';
|
|
case 'phaser': return 'Blink';
|
|
case 'charger': return 'Charge';
|
|
case 'siren': return 'Pull';
|
|
case 'mimic': return 'Burst';
|
|
case 'leech': return 'Drain';
|
|
default: return 'Attack';
|
|
}
|
|
}
|
|
function doHostAction(){
|
|
var host=G.player.host; if(!host) return;
|
|
var p=G.player;
|
|
switch(host.type){
|
|
case 'sentinel': case 'turret': shootProjectile(p.x,p.y,p.facing,host.damage,false); return;
|
|
case 'bomber': playerBomberDetonate(); return;
|
|
case 'healer': playerHealPulse(); return;
|
|
case 'phaser': playerBlink(); return;
|
|
case 'charger': playerCharge(); return;
|
|
case 'siren': playerSirenPull(); return;
|
|
case 'mimic': playerMimicBurst(); return;
|
|
case 'leech': playerLeechHit(); return;
|
|
default: playerMelee(host.damage); return;
|
|
}
|
|
}
|
|
function playerMelee(dmg){
|
|
var p=G.player, fd=FACE_DELTA[p.facing]||FACE_DELTA.down, e=enemyAt(p.x+fd.dx,p.y+fd.dy);
|
|
if(e){ damageEnemy(e,dmg,{x:p.x,y:p.y}); pushLog('You hit '+e.type+'.'); } else beep(260,0.04,'square');
|
|
}
|
|
function playerLeechHit(){
|
|
var p=G.player, host=p.host, fd=FACE_DELTA[p.facing]||FACE_DELTA.down, e=enemyAt(p.x+fd.dx,p.y+fd.dy);
|
|
if(e){ damageEnemy(e,host.damage,{x:p.x,y:p.y}); host.hp=Math.min(host.maxHp,host.hp+1); pushLog('Drain!'); } else beep(260,0.04,'square');
|
|
}
|
|
function playerBomberDetonate(){
|
|
var p=G.player, bx=tileCenter(p.x,p.y); shake(7,0.32); burst(bx.px,bx.py,'#ff5533',20,3.4); beep(120,0.2,'sawtooth');
|
|
var before=G.enemies.length; aoeDamage(p.x,p.y,1,4);
|
|
if(before-G.enemies.length>=3) G.achievements.bomberman=true; // 3+ kills in one detonation
|
|
if(p.host){ p.host.hp=0; ejectGhost(); } // suicide: eject as ghost
|
|
}
|
|
function playerHealPulse(){
|
|
var p=G.player, host=p.host; host.hp=Math.min(host.maxHp,host.hp+2); beep(660,0.12,'sine');
|
|
for(var i=0;i<G.enemies.length;i++){ var e=G.enemies[i]; if(e.hp>0&&e.hp<e.maxHp&&manhattan(e.x,e.y,p.x,p.y)<=3) e.hp=Math.min(e.maxHp,e.hp+1); }
|
|
pushLog('Heal pulse!');
|
|
}
|
|
function playerBlink(){
|
|
var p=G.player, host=p.host; host.blinkCd=host.blinkCd||0;
|
|
if(host.blinkCd>0){ pushLog('Blink recharging.'); beep(200,0.04,'square'); return; }
|
|
var fd=FACE_DELTA[p.facing]||FACE_DELTA.down;
|
|
var nx=Math.max(0,Math.min(COLS-1,p.x+fd.dx*5)), ny=Math.max(0,Math.min(ROWS-1,p.y+fd.dy*5));
|
|
p.x=nx; p.y=ny; host.x=nx; host.y=ny; host.blinkCd=3; updateFog(); beep(740,0.1,'sine'); pushLog('Blink!');
|
|
}
|
|
function playerCharge(){
|
|
var p=G.player, host=p.host, fd=FACE_DELTA[p.facing]||FACE_DELTA.down, x=p.x,y=p.y;
|
|
for(var s=0;s<4;s++){
|
|
var nx=x+fd.dx, ny=y+fd.dy;
|
|
if(!inBounds(nx,ny)) break;
|
|
var t=G.grid[ny][nx]; if(t&&t.solid) break;
|
|
var e=enemyAt(nx,ny); if(e){ damageEnemy(e,2,{x:p.x,y:p.y}); if(e.hp>0) break; }
|
|
x=nx; y=ny;
|
|
}
|
|
p.x=x; p.y=y; host.x=x; host.y=y; updateFog(); beep(500,0.08,'square'); pushLog('Charge!');
|
|
}
|
|
function playerSirenPull(){
|
|
var p=G.player; beep(440,0.1,'sine');
|
|
for(var i=0;i<G.enemies.length;i++){ var e=G.enemies[i]; if(e.hp<=0||manhattan(e.x,e.y,p.x,p.y)>4) continue;
|
|
var st=stepToward(e.x,e.y,p.x,p.y); if(!st) continue;
|
|
var nx=e.x+st.dx, ny=e.y+st.dy;
|
|
if(inBounds(nx,ny)&&!(G.grid[ny][nx]&&G.grid[ny][nx].solid)&&!(nx===p.x&&ny===p.y)&&!enemyAt(nx,ny)){ e.x=nx; e.y=ny; }
|
|
}
|
|
pushLog('Pull!');
|
|
}
|
|
function playerMimicBurst(){
|
|
var p=G.player, host=p.host; if(host.disguised) host.disguised=false;
|
|
beep(300,0.12,'square'); var cells=[[0,0],[1,0],[-1,0],[0,1],[0,-1]];
|
|
for(var i=0;i<cells.length;i++){ var e=enemyAt(p.x+cells[i][0],p.y+cells[i][1]); if(e) damageEnemy(e,2,{x:p.x,y:p.y}); }
|
|
pushLog('Surprise burst!');
|
|
}
|
|
function shootProjectile(x,y,facing,dmg,fromEnemy){
|
|
var fd=FACE_DELTA[facing]||FACE_DELTA.down;
|
|
G.projectiles=G.projectiles||[];
|
|
G.projectiles.push({x:x+fd.dx,y:y+fd.dy,dx:fd.dx,dy:fd.dy,dmg:dmg||1,fromEnemy:!!fromEnemy,life:COLS+ROWS});
|
|
beep(900,0.04,'square');
|
|
}
|
|
function stepProjectiles(){
|
|
if(!G.projectiles||!G.projectiles.length) return;
|
|
var keep=[];
|
|
for(var i=0;i<G.projectiles.length;i++){
|
|
var pr=G.projectiles[i];
|
|
pr.x+=pr.dx; pr.y+=pr.dy; pr.life--;
|
|
if(!inBounds(pr.x,pr.y)) continue;
|
|
var t=G.grid[pr.y]&&G.grid[pr.y][pr.x];
|
|
if(t&&t.solid) continue;
|
|
if(pr.x===G.player.x&&pr.y===G.player.y){ if(pr.fromEnemy) hitPlayer(pr.dmg); continue; }
|
|
if(!pr.fromEnemy){ var e=enemyAt(pr.x,pr.y); if(e){ damageEnemy(e,pr.dmg,{x:pr.x-pr.dx,y:pr.y-pr.dy}); continue; } }
|
|
if(pr.life>0) keep.push(pr);
|
|
}
|
|
G.projectiles=keep;
|
|
}
|
|
function damageEnemy(e,dmg,src){
|
|
if(!e||e.hp<=0||dmg<=0) return;
|
|
if(e.type==='shielder'&&facingTowards(e,src)) return; // frontal shield blocks
|
|
e.hp-=dmg; e.flash=3; flashEntity(e); beep(300,0.05,'square');
|
|
if(e.hp<=0){ e.hp=0; onEnemyDeath(e); }
|
|
}
|
|
function onEnemyDeath(e){
|
|
if(e.type==='bomber'&&!e.exploded){ e.exploded=true; var bx=tileCenter(e.x,e.y); shake(6,0.3); burst(bx.px,bx.py,'#ff7733',16,3); beep(120,0.18,'sawtooth'); aoeDamage(e.x,e.y,1,3); }
|
|
killEnemy(e);
|
|
}
|
|
function killEnemy(e){
|
|
if(e.hp>0) return; // ponytail: idempotent guard
|
|
if(G.enemies.indexOf(e)<0) return;
|
|
var val=ESSENCE_DROPS[e.type]||1; G.essence+=val;
|
|
G.enemies=G.enemies.filter(function(en){return en!==e;});
|
|
pushLog('Killed '+e.type+' (+'+val+' essence).'); var kc=tileCenter(e.x,e.y); burst(kc.px,kc.py,ENEMY_COLORS[e.type]||'#fff',10,2.2); burst(kc.px,kc.py,'#f1c40f',6,1.6); beep(150,0.12,'sawtooth'); beep(880,0.05,'sine');
|
|
if(G.essence>=30) G.achievements.essenceHoarder=true;
|
|
}
|
|
function hitPlayer(dmg,src){
|
|
if(dmg<=0) return;
|
|
var p=G.player;
|
|
if(p.ghost||!p.host) return; // ponytail: ghost is incorporeal, only the timer kills it
|
|
if(p.host.type==='shielder'&&facingTowards(p,src)){ pushLog('Blocked!'); beep(400,0.04,'square'); return; }
|
|
p.host.hp-=dmg; p.host.flash=3; flashEntity(p.host); G._tookHostDmg=true; beep(220,0.06,'square'); if(dmg>=3) shake(4,0.2);
|
|
if(p.host.hp<=0){ p.host.hp=0; ejectGhost(); }
|
|
}
|
|
function ejectGhost(){
|
|
var p=G.player;
|
|
p.ghost=true; p.host=null; p.body=null; p.hp=1; p.timer=ghostTimerMax();
|
|
G.state='playing'; G._ejectedThisStep=true;
|
|
pushLog('Your host died! Ejected as a ghost.'); var ec=tileCenter(p.x,p.y); burst(ec.px,ec.py,'#ffffff',12,2.4); shake(3,0.2); beep(180,0.2,'sawtooth'); G.save();
|
|
}
|
|
function bfsStep(sx,sy,tx,ty,self){
|
|
if(sx===tx&&sy===ty) return null;
|
|
var key=function(x,y){return x+','+y;}, seen={}, par={};
|
|
seen[key(sx,sy)]=true;
|
|
var q=[{x:sx,y:sy}], head=0, found=false, dirs=[[0,-1],[0,1],[-1,0],[1,0]];
|
|
while(head<q.length){
|
|
var cur=q[head++];
|
|
if(cur.x===tx&&cur.y===ty){ found=true; break; }
|
|
for(var i=0;i<4;i++){
|
|
var nx=cur.x+dirs[i][0], ny=cur.y+dirs[i][1], kk=key(nx,ny);
|
|
if(seen[kk]||!inBounds(nx,ny)) continue;
|
|
var t=G.grid[ny][nx]; if(t&&t.solid) continue;
|
|
if(!(nx===tx&&ny===ty)){ for(var j=0;j<G.enemies.length;j++){ var o=G.enemies[j]; if(o!==self&&o.hp>0&&o.x===nx&&o.y===ny){ t=null; break; } } if(t===null) continue; }
|
|
seen[kk]=true; par[kk]=key(cur.x,cur.y); q.push({x:nx,y:ny});
|
|
}
|
|
}
|
|
if(!found) return null;
|
|
var c=key(tx,ty), start=key(sx,sy);
|
|
while(par[c]&&par[c]!==start) c=par[c];
|
|
var pp=c.split(','); return {x:parseInt(pp[0],10),y:parseInt(pp[1],10)};
|
|
}
|
|
function moveEnemyTo(e,nx,ny){
|
|
if(!inBounds(nx,ny)) return;
|
|
var t=G.grid[ny][nx]; if(t&&t.solid) return;
|
|
if(nx===G.player.x&&ny===G.player.y) return;
|
|
for(var i=0;i<G.enemies.length;i++){ var o=G.enemies[i]; if(o!==e&&o.x===nx&&o.y===ny) return; }
|
|
e.facing=faceFromDelta(nx-e.x,ny-e.y); e.x=nx; e.y=ny;
|
|
}
|
|
function enemyTurn(e){
|
|
if(!e||e.hp<=0) return;
|
|
if(e.flash>0) e.flash--;
|
|
var p=G.player, d=manhattan(e.x,e.y,p.x,p.y);
|
|
switch(e.type){
|
|
case 'sentinel': sentinelAI(e,p,d); return;
|
|
case 'turret': turretAI(e,p,d); return;
|
|
case 'bomber': bomberAI(e,p,d); return;
|
|
case 'healer': healerAI(e,p,d); return;
|
|
case 'phaser': phaserAI(e,p,d); return;
|
|
case 'charger': chargerAI(e,p,d); return;
|
|
case 'siren': sirenAI(e,p,d); return;
|
|
case 'mimic': mimicAI(e,p,d); return;
|
|
default: chaserAI(e,p,d); return;
|
|
}
|
|
}
|
|
function chaserAI(e,p,d){
|
|
e.facing=dirToFace(e,p);
|
|
if(d<=1){ hitPlayer(e.aiDamage||e.damage,{x:e.x,y:e.y}); if(e.type==='leech'&&e.hp>0) e.hp=Math.min(e.maxHp,e.hp+1); return; }
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return;
|
|
e.moveDelay=e.moveEvery||1;
|
|
var nxt=bfsStep(e.x,e.y,p.x,p.y,e); if(nxt) moveEnemyTo(e,nxt.x,nxt.y);
|
|
}
|
|
function sentinelAI(e,p,d){
|
|
e.facing=dirToFace(e,p);
|
|
if(d<=3){
|
|
var ax=-sign(p.x-e.x), ay=-sign(p.y-e.y), step=null;
|
|
if(Math.abs(p.x-e.x)>=Math.abs(p.y-e.y)){ if(ax!==0) step={x:e.x+ax,y:e.y}; }
|
|
if(!step&&ay!==0) step={x:e.x,y:e.y+ay};
|
|
if(!step&&ax!==0) step={x:e.x+ax,y:e.y};
|
|
if(step) moveEnemyTo(e,step.x,step.y);
|
|
}
|
|
if(d>=2&&d<=7) shootProjectile(e.x,e.y,e.facing,e.aiDamage||e.damage,true);
|
|
}
|
|
function turretAI(e,p,d){ e.facing=dirToFace(e,p); if(d<=8) shootProjectile(e.x,e.y,e.facing,e.aiDamage||e.damage,true); }
|
|
function bomberAI(e,p,d){
|
|
e.facing=dirToFace(e,p);
|
|
if(d<=1){ bomberExplode(e); return; }
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return;
|
|
e.moveDelay=e.moveEvery||1;
|
|
var nxt=bfsStep(e.x,e.y,p.x,p.y,e); if(nxt) moveEnemyTo(e,nxt.x,nxt.y);
|
|
}
|
|
function bomberExplode(e){
|
|
if(e.exploded) return; e.exploded=true; e.hp=0;
|
|
var ex=tileCenter(e.x,e.y); shake(6,0.3); burst(ex.px,ex.py,'#ff7733',16,3); beep(120,0.18,'sawtooth'); aoeDamage(e.x,e.y,1,3); killEnemy(e);
|
|
}
|
|
function healerAI(e,p,d){
|
|
for(var i=0;i<G.enemies.length;i++){ var o=G.enemies[i]; if(o!==e&&o.hp>0&&o.hp<o.maxHp&&manhattan(o.x,o.y,e.x,e.y)<=3) o.hp=Math.min(o.maxHp,o.hp+1); }
|
|
if(d<=1){ hitPlayer(e.aiDamage||1,{x:e.x,y:e.y}); return; }
|
|
if(d<=6){
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return; e.moveDelay=e.moveEvery||1;
|
|
var ax=-sign(p.x-e.x), ay=-sign(p.y-e.y), cands=[];
|
|
if(ax!==0)cands.push({x:e.x+ax,y:e.y}); if(ay!==0)cands.push({x:e.x,y:e.y+ay});
|
|
for(var c=0;c<cands.length;c++){ var s=cands[c]; if(inBounds(s.x,s.y)&&!(G.grid[s.y][s.x]&&G.grid[s.y][s.x].solid)&&!enemyAt(s.x,s.y)&&!(s.x===p.x&&s.y===p.y)){ moveEnemyTo(e,s.x,s.y); break; } }
|
|
}
|
|
}
|
|
function phaserAI(e,p,d){
|
|
if(e.phaseCd>0) e.phaseCd--;
|
|
if(d<=1){ hitPlayer(e.aiDamage||2,{x:e.x,y:e.y}); return; }
|
|
if(e.phaseCd===0&&hasLOS(e.x,e.y,p.x,p.y)){
|
|
var spot=findTeleportSpot(p.x,p.y);
|
|
if(spot){ e.x=spot.x; e.y=spot.y; e.facing=dirToFace(e,p); e.flash=3; e.phaseCd=3; beep(740,0.08,'sine');
|
|
if(manhattan(e.x,e.y,p.x,p.y)<=1) hitPlayer(e.aiDamage||2,{x:e.x,y:e.y}); }
|
|
return;
|
|
}
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return; e.moveDelay=e.moveEvery||1;
|
|
var nxt=bfsStep(e.x,e.y,p.x,p.y,e); if(nxt) moveEnemyTo(e,nxt.x,nxt.y);
|
|
}
|
|
function chargerAI(e,p,d){
|
|
if(e.stun>0){ e.stun--; e.flash=2; return; }
|
|
if(d<=1){ hitPlayer(e.aiDamage||3,{x:e.x,y:e.y}); return; }
|
|
var aligned=(p.x===e.x||p.y===e.y);
|
|
if(e.cstate!=='tele'){
|
|
if(aligned&&d>=2&&d<=7&&hasLOS(e.x,e.y,p.x,p.y)){ e.cstate='tele'; e.cdir=dirToFace(e,p); e.flash=4; beep(300,0.06,'square'); return; }
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return; e.moveDelay=e.moveEvery||1;
|
|
var n=bfsStep(e.x,e.y,p.x,p.y,e); if(n) moveEnemyTo(e,n.x,n.y); return;
|
|
}
|
|
var fd=FACE_DELTA[e.cdir]||FACE_DELTA.down, x=e.x,y=e.y,hit=false;
|
|
for(var s=0;s<6;s++){
|
|
var nx=x+fd.dx, ny=y+fd.dy;
|
|
if(!inBounds(nx,ny)){ e.stun=2; break; }
|
|
var t=G.grid[ny][nx]; if(t&&t.solid){ e.stun=2; break; }
|
|
if(nx===p.x&&ny===p.y){ hitPlayer(e.aiDamage||3,{x:e.x,y:e.y}); hit=true; break; }
|
|
if(enemyAt(nx,ny)) break;
|
|
x=nx; y=ny;
|
|
}
|
|
e.x=x; e.y=y; e.facing=e.cdir; e.cstate='idle'; if(hit)beep(200,0.08,'square');
|
|
}
|
|
function sirenAI(e,p,d){
|
|
e.facing=dirToFace(e,p);
|
|
if(d<=1){ hitPlayer(e.aiDamage||1,{x:e.x,y:e.y}); return; }
|
|
if(d<=5) pullToward(p,e.x,e.y);
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return; e.moveDelay=e.moveEvery||1;
|
|
var n=bfsStep(e.x,e.y,p.x,p.y,e); if(n) moveEnemyTo(e,n.x,n.y);
|
|
}
|
|
function mimicAI(e,p,d){
|
|
if(e.disguised){ if(d<=1){ e.disguised=false; e.flash=4; beep(250,0.1,'square'); hitPlayer(2,{x:e.x,y:e.y}); } return; }
|
|
e.facing=dirToFace(e,p);
|
|
if(d<=1){ hitPlayer(e.aiDamage||2,{x:e.x,y:e.y}); return; }
|
|
if((e.moveDelay=(e.moveDelay||1)-1)>0) return; e.moveDelay=e.moveEvery||1;
|
|
var n=bfsStep(e.x,e.y,p.x,p.y,e); if(n) moveEnemyTo(e,n.x,n.y);
|
|
}
|
|
function hasLOS(x0,y0,x1,y1){
|
|
var dx=Math.abs(x1-x0), dy=Math.abs(y1-y0), sx=x0<x1?1:-1, sy=y0<y1?1:-1, err=dx-dy, x=x0,y=y0;
|
|
while(true){
|
|
if(x===x1&&y===y1) return true;
|
|
if(!(x===x0&&y===y0)){ var t=G.grid[y]&&G.grid[y][x]; if(t&&t.solid) return false; }
|
|
var e2=2*err; if(e2>-dy){err-=dy;x+=sx;} if(e2<dx){err+=dx;y+=sy;}
|
|
}
|
|
}
|
|
function findTeleportSpot(px,py){
|
|
var dirs=[[1,0],[-1,0],[0,1],[0,-1]];
|
|
for(var i=0;i<dirs.length;i++){ var x=px+dirs[i][0],y=py+dirs[i][1];
|
|
if(inBounds(x,y)&&!(G.grid[y][x]&&G.grid[y][x].solid)&&!enemyAt(x,y)&&!(x===G.player.x&&y===G.player.y)) return {x:x,y:y}; }
|
|
return null;
|
|
}
|
|
function stepToward(fx,fy,tx,ty){
|
|
var dx=sign(tx-fx),dy=sign(ty-fy);
|
|
if(Math.abs(tx-fx)>=Math.abs(ty-fy)){ if(dx!==0)return {dx:dx,dy:0}; if(dy!==0)return {dx:0,dy:dy}; }
|
|
else { if(dy!==0)return {dx:0,dy:dy}; if(dx!==0)return {dx:dx,dy:0}; }
|
|
return null;
|
|
}
|
|
function pullToward(target,sx,sy){
|
|
var st=stepToward(target.x,target.y,sx,sy); if(!st) return;
|
|
var nx=target.x+st.dx, ny=target.y+st.dy;
|
|
if(!inBounds(nx,ny)) return;
|
|
var t=G.grid[ny][nx]; if(t&&t.solid) return;
|
|
if(enemyAt(nx,ny)) return;
|
|
if(nx===sx&&ny===sy) return;
|
|
target.x=nx; target.y=ny;
|
|
if(target.host){ target.host.x=nx; target.host.y=ny; }
|
|
updateFog();
|
|
}
|
|
function aoeDamage(cx,cy,radius,dmg){
|
|
for(var dy=-radius;dy<=radius;dy++) for(var dx=-radius;dx<=radius;dx++){
|
|
var x=cx+dx,y=cy+dy; if(!inBounds(x,y)) continue;
|
|
var e=enemyAt(x,y); if(e) damageEnemy(e,dmg,{x:cx,y:cy});
|
|
if(x===G.player.x&&y===G.player.y) hitPlayer(dmg,{x:cx,y:cy});
|
|
}
|
|
}
|
|
function facingTowards(obj,src){
|
|
if(!src) return false;
|
|
var fd=FACE_DELTA[obj.facing]||FACE_DELTA.down, dx=src.x-obj.x, dy=src.y-obj.y;
|
|
if(fd.dx!==0) return dy===0&&sign(dx)===fd.dx;
|
|
return dx===0&&sign(dy)===fd.dy;
|
|
}
|
|
function step(){
|
|
if(G.state!=='playing'&&G.state!=='possessed') return;
|
|
if(G._exit&&G.player.x===G._exit.x&&G.player.y===G._exit.y){ descend(); return; } // standing on exit pos -> descend (robust to tile edits)
|
|
G._ejectedThisStep=false;
|
|
if(G.state==='possessed'&&G.player.host&&G.player.host.hp<=0) ejectGhost();
|
|
var skipTimer=_setupTick; _setupTick=false;
|
|
for(var i=0;i<G.enemies.length;i++){ try{ enemyTurn(G.enemies[i]); }catch(_){} }
|
|
try{ stepProjectiles(); }catch(_){}
|
|
if(G.player.host&&G.player.host.blinkCd>0) G.player.host.blinkCd--;
|
|
if(G.player.host&&G.player.host._oc){ G.player.host._oc--; if(G.player.host._oc<=0){ var h=G.player.host; h.maxHp=h._ocBaseMax; h.hp=Math.min(h.hp,h.maxHp); h.damage=h._ocBaseDmg; delete h._oc; } }
|
|
if(G.player.ghost&&G.state==='playing'&&!skipTimer&&!G._ejectedThisStep){
|
|
G.player.timer-=TIMER_STEP;
|
|
if(G.player.timer<=0){ G.player.timer=0; G.state='lost'; pushLog('The ghost fades away...'); beep(120,0.4,'sawtooth'); }
|
|
}
|
|
}
|
|
G.tick=function(n){ n=Math.max(0,parseInt(n,10)||0); for(var i=0;i<n;i++) step(); };
|
|
|
|
// ===== Spawn (functional now; AI fleshed out later) =====
|
|
function isFreeTile(x,y){
|
|
if(!inBounds(x,y)) return false;
|
|
var t=G.grid[y][x]; if(t&&t.solid) return false;
|
|
if(x===G.player.x&&y===G.player.y) return false;
|
|
if(enemyAt(x,y)) return false;
|
|
return true;
|
|
}
|
|
function nearestFree(x,y){
|
|
x=Math.max(0,Math.min(COLS-1,(x|0)||0)); y=Math.max(0,Math.min(ROWS-1,(y|0)||0));
|
|
if(isFreeTile(x,y)) return {x:x,y:y};
|
|
for(var r=1;r<Math.max(COLS,ROWS);r++) for(var dy=-r;dy<=r;dy++) for(var dx=-r;dx<=r;dx++){
|
|
var nx=x+dx,ny=y+dy; if(isFreeTile(nx,ny)) return {x:nx,y:ny};
|
|
}
|
|
return {x:x,y:y};
|
|
}
|
|
function addEnemy(type,x,y){
|
|
var s=ENEMY_STATS[type]||ENEMY_STATS.brute;
|
|
var pt=nearestFree(x,y);
|
|
var e={type:type,hp:s.hp,maxHp:s.hp,damage:s.damage,aiDamage:AI_DAMAGE[type]||s.damage,speed:s.speed,
|
|
moveEvery:s.speed,moveDelay:s.speed,x:pt.x,y:pt.y,facing:'down',
|
|
ranged:!!s.ranged, melee:s.melee!==false, flash:0};
|
|
if(type==='turret') e.rooted=true;
|
|
if(type==='mimic') e.disguised=true;
|
|
if(type==='charger'){ e.cstate='idle'; e.stun=0; }
|
|
if(type==='phaser') e.phaseCd=0;
|
|
G.enemies.push(e); return e;
|
|
}
|
|
// Debug spawn isolates the target: clears prior debug-spawned enemies so headless tests
|
|
// aren't swarmed/blocked by leaked entities from earlier checks. (Player host lives on
|
|
// G.player.host, never in G.enemies, so it survives the clear.)
|
|
G.spawn=function(type,x,y){ G.enemies=[]; return addEnemy(type,x,y); };
|
|
G.addEnemy=function(type,x,y){ return addEnemy(type,x,y); }; // debug: add without clearing (multi-enemy setups)
|
|
|
|
// ===== Shop (3 random eligible upgrades; prerequisites + essence enforced) =====
|
|
var SHOP_POOL=[
|
|
{id:'ghost1',name:'Ghost T1: Timer 20s',cost:5,tree:'ghost',tier:1},
|
|
{id:'ghost2',name:'Ghost T2: Possess range +1',cost:8,tree:'ghost',tier:2,requires:'ghost1'},
|
|
{id:'ghost3',name:'Ghost T3: Phase walls',cost:12,tree:'ghost',tier:3,requires:'ghost2'},
|
|
{id:'ghost4',name:'Ghost T4: Ghost dash',cost:15,tree:'ghost',tier:4,requires:'ghost3'},
|
|
{id:'body1',name:'Body T1: Retain ability',cost:10,tree:'body',tier:1},
|
|
{id:'body2',name:'Body T2: Overclock',cost:12,tree:'body',tier:2,requires:'body1'},
|
|
{id:'body3',name:'Body T3: Dual-wield',cost:18,tree:'body',tier:3,requires:'body2'},
|
|
{id:'meta1',name:'Meta T1: Start with body',cost:8,tree:'meta',tier:1},
|
|
{id:'meta2',name:'Meta T2: See through fog',cost:10,tree:'meta',tier:2,requires:'meta1'},
|
|
{id:'meta3',name:'Meta T3: Shop discount',cost:6,tree:'meta',tier:3,requires:'meta2'}
|
|
];
|
|
function discountedCost(c){ return (G.upgrades&&G.upgrades.meta3)?Math.floor(c*0.7):c; }
|
|
function buildShopChoices(){
|
|
var elig=SHOP_POOL.filter(function(c){ return !(G.upgrades&&G.upgrades[c.id]) && (!c.requires||(G.upgrades&&G.upgrades[c.requires])); });
|
|
for(var i=elig.length-1;i>0;i--){ var j=Math.floor(Math.random()*(i+1)); var tmp=elig[i]; elig[i]=elig[j]; elig[j]=tmp; }
|
|
var choices=elig.slice(0,3);
|
|
if(choices.length<3){ var rest=SHOP_POOL.filter(function(c){ return choices.indexOf(c)<0; }); for(var i=rest.length-1;i>0;i--){ var j=Math.floor(Math.random()*(i+1)); var tmp=rest[i]; rest[i]=rest[j]; rest[j]=tmp; } while(choices.length<3&&rest.length) choices.push(rest.shift()); }
|
|
return choices;
|
|
}
|
|
function _openShop(grantEssence){
|
|
G.shop={choices:buildShopChoices(), index:0}; G.state='shop';
|
|
if(grantEssence&&G.essence<30) G.essence=30; // debug/test stipend so purchases are exercisable
|
|
}
|
|
G.openShop=function(){ G._pendingFloor=null; _openShop(true); }; // debug entry: no pending floor
|
|
function applyUpgrade(id){
|
|
G.upgrades=G.upgrades||{}; G.upgrades[id]=true;
|
|
if(id==='ghost1'&&G.player.ghost) G.player.timer=ghostTimerMax(); // Ghost T1: 15 -> 20 immediately
|
|
if(id==='body3') G.achievements.dualWield=true;
|
|
}
|
|
G.buy=function(i){
|
|
if(G.state!=='shop'||!G.shop) return false;
|
|
var ch=G.shop.choices[i]; if(!ch) return false;
|
|
if(G.upgrades&&G.upgrades[ch.id]) return false; // already owned
|
|
if(ch.requires&&!(G.upgrades&&G.upgrades[ch.requires])) return false; // prerequisite
|
|
var cost=discountedCost(ch.cost);
|
|
if(G.essence<cost) return false; // respect essence
|
|
G.essence-=cost; applyUpgrade(ch.id);
|
|
pushLog('Purchased '+ch.name+'.'); beep(700,0.1,'sine');
|
|
closeShop(); G.save();
|
|
return true;
|
|
};
|
|
function handleShopInput(k){
|
|
if(!G.shop) return;
|
|
var n=(G.shop.choices&&G.shop.choices.length)||3;
|
|
if(k==='ArrowUp'||k==='w'||k==='W'){ G.shop.index=(G.shop.index+n-1)%n; }
|
|
else if(k==='ArrowDown'||k==='s'||k==='S'){ G.shop.index=(G.shop.index+1)%n; }
|
|
else if(k==='Enter'){ G.buy(G.shop.index); }
|
|
else if(k==='Escape'){ closeShop(); }
|
|
}
|
|
|
|
// ===== Save / Load (localStorage treated as untrusted) =====
|
|
G.save=function(){
|
|
try{
|
|
var p=G.player, hostData=null;
|
|
if(p.host){ hostData={type:p.host.type,hp:p.host.hp,maxHp:p.host.maxHp,damage:p.host.damage,speed:p.host.speed}; }
|
|
var data={v:1,floor:G.floor,essence:G.essence,upgrades:G.upgrades,
|
|
state:G.state,achievements:G.achievements,_possessedTypes:G._possessedTypes,_tookHostDmg:!!G._tookHostDmg,
|
|
player:{x:p.x,y:p.y,ghost:p.ghost,timer:p.timer,facing:p.facing,hp:p.hp,host:hostData},
|
|
enemies:G.enemies.map(function(e){return {type:e.type,hp:e.hp,maxHp:e.maxHp,damage:e.damage,speed:e.speed,x:e.x,y:e.y,facing:e.facing};}),
|
|
grid:G.grid,explored:G.explored};
|
|
localStorage.setItem(SAVE_KEY,JSON.stringify(data));
|
|
return true;
|
|
}catch(e){ pushLog('save failed'); return false; }
|
|
};
|
|
G.load=function(){
|
|
var raw,d;
|
|
try{ raw=localStorage.getItem(SAVE_KEY); }catch(e){ return false; }
|
|
if(!raw) return false;
|
|
try{ d=JSON.parse(raw); }catch(e){ return false; } // untrusted: bad JSON -> bail, no mutation
|
|
if(!d||typeof d!=='object') return false;
|
|
if(!Array.isArray(d.grid)||d.grid.length!==ROWS||!Array.isArray(d.grid[0])||d.grid[0].length!==COLS) return false; // validate layout dims first
|
|
var grid=d.grid.map(function(row){ return row.map(function(t){ if(t&&typeof t==='object'&&typeof t.type==='string') return {type:t.type,solid:!!t.solid}; if(typeof t==='number') return t===1?{type:'wall',solid:true}:{type:'floor',solid:false}; return {type:'floor',solid:false}; }); });
|
|
var explored; if(Array.isArray(d.explored)&&d.explored.length===ROWS){ explored=d.explored.map(function(r){ var a=[]; for(var x=0;x<COLS;x++) a.push(!!(r&&r[x])); return a; }); } else { explored=[]; for(var y=0;y<ROWS;y++){ var a=[]; for(var x=0;x<COLS;x++) a.push(false); explored.push(a); } }
|
|
// commit validated snapshot atomically (no partial state on failure)
|
|
G.grid=grid; G.explored=explored;
|
|
G._exit=null; for(var ey=0;ey<ROWS&&!G._exit;ey++) for(var ex=0;ex<COLS;ex++) if(grid[ey][ex]&&grid[ey][ex].type==='exit'){ G._exit={x:ex,y:ey}; break; }
|
|
G.floor=(typeof d.floor==='number'&&d.floor>=1)?Math.floor(d.floor):1;
|
|
G.essence=(typeof d.essence==='number')?Math.floor(d.essence):0;
|
|
G.upgrades=(d.upgrades&&typeof d.upgrades==='object')?d.upgrades:{};
|
|
if(d.achievements&&typeof d.achievements==='object') G.achievements=d.achievements;
|
|
G._possessedTypes=(d._possessedTypes&&typeof d._possessedTypes==='object')?d._possessedTypes:{}; G._tookHostDmg=!!d._tookHostDmg;
|
|
var pp=(d.player&&typeof d.player==='object')?d.player:{}, host=null;
|
|
if(pp.host&&pp.host.type&&ENEMY_STATS[pp.host.type]){ var s=ENEMY_STATS[pp.host.type];
|
|
host={type:pp.host.type,hp:pp.host.hp,maxHp:pp.host.maxHp||s.hp,damage:pp.host.damage||s.damage,
|
|
speed:pp.host.speed||s.speed,x:pp.x,y:pp.y,facing:pp.facing||'down',ranged:!!s.ranged,melee:s.melee!==false,aiDamage:AI_DAMAGE[pp.host.type]||s.damage};
|
|
if(host.type==='turret')host.rooted=true; if(host.type==='charger'){host.cstate='idle';host.stun=0;} if(host.type==='phaser')host.phaseCd=0; }
|
|
G.player={x:pp.x||3,y:pp.y||3,ghost:pp.ghost!==false, // host + body -> SAME object
|
|
timer:(typeof pp.timer==='number')?pp.timer:ghostTimerMax(),
|
|
facing:pp.facing||'down',hp:pp.hp||1,host:host,body:host};
|
|
G.enemies=Array.isArray(d.enemies)?d.enemies.filter(function(e){return e&&e.type&&ENEMY_STATS[e.type];}).map(function(e){
|
|
var s2=ENEMY_STATS[e.type];
|
|
return {type:e.type,hp:e.hp,maxHp:e.maxHp||s2.hp,damage:e.damage||s2.damage,speed:e.speed||s2.speed,
|
|
moveEvery:e.speed||s2.speed,moveDelay:e.speed||s2.speed,x:e.x,y:e.y,facing:e.facing||'down',
|
|
ranged:!!s2.ranged,melee:s2.melee!==false,aiDamage:AI_DAMAGE[e.type]||s2.damage,flash:0};
|
|
}):[];
|
|
G.projectiles=[]; G.shop=null; G._pendingFloor=null;
|
|
G.state=G.player.ghost?'playing':'possessed';
|
|
updateFog();
|
|
return true;
|
|
}
|
|
|
|
// ===== Rendering (all ctx calls are no-op safe under mock) =====
|
|
var COL_BG='#0a0a14', COL_FLOOR='#232338', COL_WALL='#4a4a66', COL_GRID='#15152a',
|
|
COL_EXIT='#2ecc40', COL_DOOR='#8a5a2a', COL_SHOP='#f1c40f', COL_CHEST='#c87f2a',
|
|
COL_HUD='#11111c', COL_HUDTXT='#cfcfe0';
|
|
|
|
function render(){
|
|
var ctx;
|
|
try{ var cv=getCanvas(); if(!cv) return; ctx=cv.getContext('2d'); }catch(e){ return; }
|
|
if(!ctx) return;
|
|
try{
|
|
if(G.state==='title'){ drawTitle(ctx); return; }
|
|
updateVisuals();
|
|
ctx.fillStyle=COL_BG; ctx.fillRect(0,0,W,H);
|
|
ctx.save(); applyShake(ctx);
|
|
drawGrid(ctx); drawEnemies(ctx); drawProjectiles(ctx); drawPlayer(ctx); drawParticles(ctx);
|
|
ctx.restore();
|
|
drawHUD(ctx);
|
|
if(G.state==='paused') drawPause(ctx);
|
|
else if(G.state==='shop') drawShop(ctx);
|
|
else if(G.state==='won') drawEnd(ctx,'YOU ESCAPED','#50fa7b');
|
|
else if(G.state==='lost') drawEnd(ctx,'GAME OVER','#ff5555');
|
|
}catch(e){}
|
|
}
|
|
function applyShake(ctx){ if(_shake.t>0&&_shake.mag>0){ ctx.translate((Math.random()*2-1)*_shake.mag,(Math.random()*2-1)*_shake.mag); } }
|
|
function drawParticles(ctx){ for(var i=0;i<_parts.length;i++){ var p=_parts[i]; ctx.globalAlpha=Math.max(0,Math.min(1,p.life/p.max)); ctx.fillStyle=p.color; ctx.fillRect(p.x-p.size/2,p.y-p.size/2,p.size,p.size); } ctx.globalAlpha=1; }
|
|
function drawFlash(ctx,cx,cy){ ctx.globalAlpha=0.85; ctx.fillStyle='#ffffff'; ctx.beginPath(); ctx.arc(cx,cy,13,0,Math.PI*2); ctx.fill(); ctx.globalAlpha=1; }
|
|
function drawEnd(ctx,title,color){
|
|
ctx.fillStyle='rgba(0,0,0,0.82)'; ctx.fillRect(0,0,W,H);
|
|
ctx.textAlign='center'; ctx.fillStyle=color; ctx.font='bold 44px monospace'; ctx.fillText(title,W/2,H/2-80);
|
|
ctx.fillStyle=COL_HUDTXT; ctx.font='16px monospace';
|
|
var n=0; for(var k in G.achievements) if(G.achievements[k]===true) n++;
|
|
var possessed=G._possessedTypes?Object.keys(G._possessedTypes).length:0;
|
|
var stats=['Floor reached: '+G.floor+' / 10','Essence collected: '+G.essence,'Bodies possessed: '+possessed,'Achievements: '+n+' / 8','','ENTER to return to title'];
|
|
var y=H/2-30; for(var i=0;i<stats.length;i++){ ctx.fillText(stats[i],W/2,y); y+=24; }
|
|
ctx.textAlign='left';
|
|
}
|
|
function drawPause(ctx){
|
|
ctx.fillStyle='rgba(0,0,0,0.8)'; ctx.fillRect(0,0,W,H);
|
|
ctx.textAlign='center'; ctx.fillStyle='#8be9fd'; ctx.font='bold 40px monospace'; ctx.fillText('PAUSED',W/2,H/2-50);
|
|
ctx.fillStyle=COL_HUDTXT; ctx.font='16px monospace';
|
|
ctx.fillText('ESC / ENTER : resume',W/2,H/2);
|
|
ctx.fillText('S : save',W/2,H/2+26);
|
|
ctx.fillText('Q : save + quit to title',W/2,H/2+52);
|
|
ctx.textAlign='left';
|
|
}
|
|
function drawGrid(ctx){
|
|
for(var y=0;y<ROWS;y++) for(var x=0;x<COLS;x++){
|
|
var vis=isVisible(x,y), exp=G.explored[y][x];
|
|
if(!vis&&!exp) continue;
|
|
var t=G.grid[y][x], col=COL_FLOOR;
|
|
if(t.type==='wall') col=COL_WALL;
|
|
else if(t.type==='exit') col=COL_EXIT;
|
|
else if(t.type==='door') col=COL_DOOR;
|
|
else if(t.type==='shop') col=COL_SHOP;
|
|
else if(t.type==='chest') col=COL_CHEST;
|
|
var px=x*TILE, py=HUD_H+y*TILE;
|
|
ctx.fillStyle=col; ctx.fillRect(px,py,TILE,TILE);
|
|
ctx.strokeStyle=COL_GRID; ctx.strokeRect(px+0.5,py+0.5,TILE-1,TILE-1);
|
|
if(t.type==='exit'){ ctx.fillStyle='#062'; ctx.font='bold 22px monospace'; ctx.textBaseline='alphabetic'; ctx.fillText('>',px+11,py+24); }
|
|
if(!vis&&exp){ ctx.fillStyle='rgba(0,0,0,0.6)'; ctx.fillRect(px,py,TILE,TILE); }
|
|
}
|
|
}
|
|
function drawEnemies(ctx){ for(var i=0;i<G.enemies.length;i++){ var e=G.enemies[i], vp=visualPos(e), cx=vp.x*TILE+TILE/2, cy=HUD_H+vp.y*TILE+TILE/2; drawEnemyShape(ctx,e,cx,cy,ENEMY_COLORS[e.type]||'#aaa'); if(isFlashing(e)) drawFlash(ctx,cx,cy); } }
|
|
function drawProjectiles(ctx){
|
|
if(!G.projectiles||!G.projectiles.length) return;
|
|
for(var i=0;i<G.projectiles.length;i++){
|
|
var pr=G.projectiles[i];
|
|
ctx.fillStyle=pr.fromEnemy?'#ff5555':'#8be9fd';
|
|
ctx.beginPath(); ctx.arc(pr.x*TILE+TILE/2, HUD_H+pr.y*TILE+TILE/2, 4, 0, Math.PI*2); ctx.fill();
|
|
}
|
|
}
|
|
function drawEnemyShape(ctx,e,cx,cy,col){
|
|
col=col||'#aaa'; ctx.fillStyle=col;
|
|
switch(e.type){
|
|
case 'brute': ctx.fillRect(cx-13,cy-13,26,26); break;
|
|
case 'skitterer': tri(ctx,cx,cy,8); break;
|
|
case 'sentinel': poly(ctx,cx,cy,11,6); break;
|
|
case 'shielder': ctx.fillRect(cx-12,cy-12,24,24); ctx.strokeStyle='#ffffff'; ctx.beginPath(); ctx.moveTo(cx,cy-12); ctx.lineTo(cx,cy+12); ctx.stroke(); break;
|
|
case 'bomber': ctx.beginPath(); ctx.arc(cx,cy,11,0,Math.PI*2); ctx.fill(); break;
|
|
case 'healer': cross(ctx,cx,cy,10,4); break;
|
|
case 'phaser': diamond(ctx,cx,cy,12); break;
|
|
case 'turret': poly(ctx,cx,cy,11,8); break;
|
|
case 'charger': tri(ctx,cx,cy,11); break;
|
|
case 'siren': ctx.beginPath(); ctx.arc(cx,cy,9,0,Math.PI*2); ctx.fill(); ctx.strokeStyle='rgba(255,121,198,0.5)'; ctx.beginPath(); ctx.arc(cx,cy,14,0,Math.PI*2); ctx.stroke(); break;
|
|
case 'mimic': ctx.fillRect(cx-11,cy-9,22,18); break;
|
|
case 'leech': teardrop(ctx,cx,cy,11); break;
|
|
default: ctx.beginPath(); ctx.arc(cx,cy,10,0,Math.PI*2); ctx.fill();
|
|
}
|
|
}
|
|
function tri(ctx,cx,cy,r){ ctx.beginPath(); ctx.moveTo(cx,cy-r); ctx.lineTo(cx-r,cy+r); ctx.lineTo(cx+r,cy+r); ctx.closePath(); ctx.fill(); }
|
|
function poly(ctx,cx,cy,r,sides){ ctx.beginPath(); for(var i=0;i<sides;i++){ var a=i*2*Math.PI/sides-Math.PI/2, x=cx+Math.cos(a)*r, y=cy+Math.sin(a)*r; if(i===0)ctx.moveTo(x,y); else ctx.lineTo(x,y); } ctx.closePath(); ctx.fill(); }
|
|
function diamond(ctx,cx,cy,r){ ctx.beginPath(); ctx.moveTo(cx,cy-r); ctx.lineTo(cx+r,cy); ctx.lineTo(cx,cy+r); ctx.lineTo(cx-r,cy); ctx.closePath(); ctx.fill(); }
|
|
function cross(ctx,cx,cy,s,w){ ctx.fillRect(cx-w/2,cy-s,w,s*2); ctx.fillRect(cx-s,cy-w/2,s*2,w); }
|
|
function teardrop(ctx,cx,cy,r){ ctx.beginPath(); ctx.moveTo(cx,cy-r); ctx.bezierCurveTo(cx+r,cy-r*0.4,cx+r*0.7,cy+r,cx,cy+r); ctx.bezierCurveTo(cx-r*0.7,cy+r,cx-r,cy-r*0.4,cx,cy-r); ctx.fill(); }
|
|
function drawPlayer(ctx){
|
|
var p=G.player, vp=visualPos(p), cx=vp.x*TILE+TILE/2, cy=HUD_H+vp.y*TILE+TILE/2;
|
|
if(p.ghost){
|
|
ctx.save(); ctx.globalAlpha=0.85; ctx.fillStyle='#ffffff';
|
|
ctx.beginPath(); ctx.arc(cx,cy-1,11,0,Math.PI*2); ctx.fill();
|
|
ctx.globalAlpha=1; ctx.fillStyle=COL_BG;
|
|
ctx.beginPath(); ctx.arc(cx-4,cy-2,2,0,Math.PI*2); ctx.arc(cx+4,cy-2,2,0,Math.PI*2); ctx.fill();
|
|
ctx.restore();
|
|
} else if(p.host){ drawEnemyShape(ctx,p.host,cx,cy,ENEMY_COLORS[p.host.type]); if(isFlashing(p.host)) drawFlash(ctx,cx,cy); }
|
|
}
|
|
function drawHUD(ctx){
|
|
ctx.fillStyle=COL_HUD; ctx.fillRect(0,0,W,HUD_H);
|
|
ctx.strokeStyle='#33334a'; ctx.beginPath(); ctx.moveTo(0,HUD_H-0.5); ctx.lineTo(W,HUD_H-0.5); ctx.stroke();
|
|
ctx.fillStyle=COL_HUDTXT; ctx.font='14px monospace'; ctx.textBaseline='middle';
|
|
var p=G.player, hp=p.ghost?1:(p.host?p.host.hp:1);
|
|
ctx.textAlign='left';
|
|
ctx.fillText('HP: '+hp, 10, HUD_H/2);
|
|
ctx.fillText(p.ghost?('Timer: '+p.timer.toFixed(1)+'s'):('Body: '+(p.host?p.host.type:'ghost')), 70, HUD_H/2);
|
|
ctx.fillText('Essence: '+G.essence, 230, HUD_H/2);
|
|
ctx.fillText('Floor: '+G.floor+' / 10', 340, HUD_H/2);
|
|
var abil=Object.keys(G.upgrades||{}).filter(function(k){return G.upgrades[k];}).join(',')||'-';
|
|
ctx.fillText('Upgrades: '+abil, 430, HUD_H/2);
|
|
ctx.textAlign='right';
|
|
ctx.fillText(p.ghost?'SPACE: Possess':('SPACE: '+bodyActionLabel(p.host)), W-10, HUD_H/2);
|
|
ctx.textAlign='left';
|
|
}
|
|
function drawTitle(ctx){
|
|
ctx.fillStyle=COL_BG; ctx.fillRect(0,0,W,H);
|
|
ctx.textAlign='center'; ctx.fillStyle='#8be9fd'; ctx.font='bold 42px monospace';
|
|
ctx.fillText('BODY SNATCHER', W/2, 90);
|
|
ctx.fillStyle='#6272a4'; ctx.font='16px monospace';
|
|
ctx.fillText('Possess your enemies. Survive. Descend 10 floors.', W/2, 120);
|
|
var lines=[
|
|
['CONTROLS','#ffb86c',20],
|
|
['WASD / Arrows : Move one tile','#cfcfe0',16],
|
|
['SPACE : Possess (ghost) / Attack (body)','#cfcfe0',16],
|
|
['Q / E : Rotate facing','#cfcfe0',16],
|
|
['ENTER : Start / Confirm','#cfcfe0',16],
|
|
['ESC : Pause','#cfcfe0',16],
|
|
['',null,12],
|
|
['You are a ghost: 1 HP, no attack.','#8be9fd',14],
|
|
['Possess enemies to use their bodies. Reach the stairs.','#8be9fd',14],
|
|
['',null,12],
|
|
['Press ENTER to begin','#50fa7b',20]
|
|
];
|
|
var y=175;
|
|
for(var i=0;i<lines.length;i++){ var ln=lines[i]; if(ln[1]){ ctx.fillStyle=ln[1]; ctx.font=ln[2]+'px monospace'; ctx.fillText(ln[0],W/2,y); } y+=ln[2]+10; }
|
|
ctx.textAlign='left';
|
|
}
|
|
function drawShop(ctx){
|
|
ctx.fillStyle='rgba(0,0,0,0.82)'; ctx.fillRect(0,0,W,H);
|
|
ctx.textAlign='center'; ctx.fillStyle='#f1c40f'; ctx.font='bold 26px monospace';
|
|
ctx.fillText('SHOP (Essence: '+G.essence+')',W/2,80);
|
|
if(G.shop&&G.shop.choices){
|
|
for(var i=0;i<G.shop.choices.length;i++){
|
|
var ch=G.shop.choices[i], sel=(i===G.shop.index), cost=discountedCost(ch.cost);
|
|
ctx.fillStyle=sel?'#50fa7b':'#cfcfe0'; ctx.font=(sel?'bold ':'')+'18px monospace';
|
|
ctx.fillText((sel?'> ':' ')+ch.name+' ['+cost+' ess]',W/2,140+i*36);
|
|
}
|
|
}
|
|
ctx.fillStyle='#8be9fd'; ctx.font='14px monospace';
|
|
ctx.fillText('Up/Down: choose ENTER: buy ESC: leave',W/2,H-40);
|
|
ctx.textAlign='left';
|
|
}
|
|
|
|
// ===== Real-time loop: drives the same deterministic step() =====
|
|
var _acc=0, _last=0;
|
|
function frame(now){
|
|
now = now || (typeof performance!=='undefined'&&performance.now?performance.now():Date.now());
|
|
if(!_last) _last=now;
|
|
var dt=now-_last; _last=now; if(dt>250) dt=250;
|
|
_acc+=dt; var guard=0;
|
|
while(_acc>=LOOP_MS&&guard<8){ step(); _acc-=LOOP_MS; guard++; }
|
|
render();
|
|
try{ window.requestAnimationFrame(frame); }catch(e){}
|
|
}
|
|
|
|
// ===== Boot =====
|
|
buildDungeon(1); updateFog();
|
|
G.render=render; window.GAME=G;
|
|
try{
|
|
window.addEventListener('keydown',function(ev){
|
|
var k=(ev&&ev.key)?ev.key:'';
|
|
if(k==='ArrowUp'||k==='ArrowDown'||k==='ArrowLeft'||k==='ArrowRight'||k===' '){ try{ev.preventDefault();}catch(e){} }
|
|
if(k.length===1) k=k.toLowerCase();
|
|
G.input(k);
|
|
},false);
|
|
}catch(e){}
|
|
try{ window.requestAnimationFrame(frame); }catch(e){}
|
|
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|