Files
agentic-labs-challenge-subm…/body-snatcher/build/index.phase1.bak
T
2026-08-13 15:23:35 +02:00

518 lines
25 KiB
Plaintext

<!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>
*{box-sizing:border-box}
html,body{margin:0;padding:0;height:100%;background:#05060a;overflow:hidden;font-family:"Trebuchet MS","Segoe UI",sans-serif}
#wrap{display:flex;align-items:center;justify-content:center;height:100%;width:100%}
#c{background:#05060a;border-radius:6px;box-shadow:0 0 60px rgba(60,90,170,0.22) inset,0 12px 60px rgba(0,0,0,0.85);cursor:default}
</style>
</head>
<body>
<div id="wrap"><canvas id="c" width="800" height="576"></canvas></div>
<script>
(function(){
'use strict';
/* ===== Body Snatcher — Phase 1: engine, grid, ghost, fog, HUD, title, bestiary ===== */
var COLS=25, ROWS=18, TILE=32, VIEW=6.5;
var TURN_MS=600, MOVE_MS=85, TICK_SEC=0.6;
/* ---- enemy data (drives bestiary + spawn; AI arrives in later phases) ---- */
var ENEMIES = {
brute: {name:'Brute', color:'#c0392b', hp:8, damage:3, speed:2, essence:3,
enemy:'Slow A* chase. 2-dmg melee every 2 turns.',
kit:'HP 8, DMG 3, slow heavy hitter.'},
skitterer: {name:'Skitterer', color:'#e67e22', hp:2, damage:1, speed:2, essence:1,
enemy:'Swarm-flanks. Weak 1-dmg melee, moves every 2 turns.',
kit:'HP 2, DMG 1, fast rapid hits.'},
sentinel: {name:'Sentinel', color:'#9b59b6', hp:4, damage:1, speed:1, essence:3,
enemy:'Keeps range. 3-shot burst then overheats 3 turns.',
kit:'HP 4, fires facing, burst/overheat, no melee.'},
shielder: {name:'Shielder', color:'#3498db', hp:5, damage:2, speed:2, essence:4,
enemy:'Slow chase. Frontal shield blocks that facing.',
kit:'HP 5, shield active, Q/E to face threats.'},
bomber: {name:'Bomber', color:'#7d1f1f', hp:3, damage:4, speed:1, essence:2,
enemy:'Chases. Explodes 3x3 (3 dmg) adjacent or on death.',
kit:'HP 3, SPACE detonates 3x3 (4 dmg), suicide.'},
healer: {name:'Healer', color:'#27ae60', hp:3, damage:1, speed:2, essence:5,
enemy:'Heals nearby foes +1/turn. Flees the player.',
kit:'HP 3, SPACE AoE heal self +2, allies +1.'},
phaser: {name:'Phaser', color:'#1abc9c', hp:4, damage:2, speed:1, essence:4,
enemy:'Teleports toward you every 3 turns, hits, fades.',
kit:'HP 4, SPACE blink 5 tiles through walls, 2-turn cd.'},
turret: {name:'Turret', color:'#7f8c8d', hp:4, damage:1, speed:0, essence:5,
enemy:'Stationary 360-degree 3-shot burst, then overheats 3 turns.',
kit:'HP 4, rooted, SPACE fire facing, Q/E rotate.'},
charger: {name:'Charger', color:'#f1c40f', hp:5, damage:3, speed:1, essence:4,
enemy:'Telegraphs 1 turn, charges straight (3 dmg), stuns on wall.',
kit:'HP 5, SPACE dash 4 tiles, 2 dmg per foe hit.'},
siren: {name:'Siren', color:'#e84393', hp:3, damage:1, speed:3, essence:3,
enemy:'Pulls you 1 tile/turn within 5. Weak melee.',
kit:'HP 3, SPACE pull all foes within 4 one tile.'},
mimic: {name:'Mimic', color:'#b9772e', hp:4, damage:2, speed:1, essence:4,
enemy:'Disguised chest. Bursts 2-dmg cross when adjacent.',
kit:"HP 4, foes won't aggro disguised, SPACE 2-dmg cross."},
leech: {name:'Leech', color:'#6c3483', hp:4, damage:1, speed:2, essence:4,
enemy:'Slow chase. Drains 1 HP and heals self.',
kit:'HP 4, each hit 1 dmg + heal self 1.'}
};
/* ---- dungeon: hand-authored connected rooms for phase 1 ---- */
function buildDungeon(){
var g=[]; for(var y=0;y<ROWS;y++){var r=[];for(var x=0;x<COLS;x++)r.push(1);g.push(r);}
function carve(x1,y1,x2,y2){for(var y=y1;y<=y2;y++)for(var x=x1;x<=x2;x++) if(y>0&&y<ROWS-1&&x>0&&x<COLS-1)g[y][x]=0;}
function hcorr(x1,x2,y){var a=Math.min(x1,x2),b=Math.max(x1,x2);for(var x=a;x<=b;x++) if(y>0&&y<ROWS-1&&x>0&&x<COLS-1)g[y][x]=0;}
function vcorr(y1,y2,x){var a=Math.min(y1,y2),b=Math.max(y1,y2);for(var y=a;y<=b;y++) if(y>0&&y<ROWS-1&&x>0&&x<COLS-1)g[y][x]=0;}
carve(2,2,7,5); carve(11,2,16,6); carve(19,2,23,6);
carve(2,9,6,13); carve(10,10,15,14); carve(19,9,23,15);
hcorr(7,11,3); hcorr(16,19,4); vcorr(5,9,4);
hcorr(6,10,12); hcorr(15,19,12); vcorr(6,10,13);
g[3][22]=3; // exit stairs in far room
g[3][9]=2; g[4][18]=2; g[12][8]=2; // doors (flavour, walkable)
return g;
}
var SPAWN={x:4,y:3};
function rebuildExplored(){var e=[];for(var y=0;y<ROWS;y++){var r=[];for(var x=0;x<COLS;x++)r.push(false);e.push(r);}return e;}
function isSolid(t){ return t===1; }
function ghostCap(){ return (GAME.upgrades && GAME.upgrades.ghostT1) ? 20 : 15; }
function placePlayer(){
GAME.player={x:SPAWN.x,y:SPAWN.y,rx:SPAWN.x,ry:SPAWN.y,ghost:true,timer:ghostCap(),
host:null,facing:{x:0,y:1},fromX:SPAWN.x,fromY:SPAWN.y,moveT:1};
}
function updateFog(){
var p=GAME.player; if(!p||!GAME.explored) return;
for(var y=0;y<ROWS;y++)for(var x=0;x<COLS;x++){
if(Math.hypot(x-p.x,y-p.y)<=VIEW) GAME.explored[y][x]=true;
}
}
/* ============================ GAME API ================================= */
var GAME={
state:'title', grid:null, explored:null, player:null,
enemies:[], projectiles:[], upgrades:{}, achievements:{}, log:[],
floor:1, essence:0, showLegend:false, choices:[],
start:function(){
this.state='title'; this.grid=buildDungeon(); this.explored=rebuildExplored();
this.enemies=[]; this.projectiles=[]; this.upgrades={}; this.achievements={}; this.log=[];
this.floor=1; this.essence=0; this.showLegend=false;
placePlayer(); updateFog();
return this.state;
},
newRun:function(){
this.grid=buildDungeon(); this.explored=rebuildExplored();
this.enemies=[]; this.projectiles=[]; this.floor=1; this.showLegend=false;
placePlayer(); this.state='playing'; updateFog();
return this.state;
},
setFloor:function(n){
this.floor=n||1; this.grid=buildDungeon(); this.explored=rebuildExplored();
this.enemies=[]; this.projectiles=[]; this.showLegend=false;
placePlayer(); this.state='playing'; updateFog();
return this.state;
},
spawn:function(type,x,y){
var d=ENEMIES[type]; if(!d) return null;
if(x==null){x=SPAWN.x;} if(y==null){y=SPAWN.y;}
var e={type:type,name:d.name,color:d.color,hp:d.hp,maxHp:d.hp,damage:d.damage,
speed:d.speed,essence:d.essence,x:x,y:y,rx:x,ry:y,facing:{x:0,y:1},
aggroed:false,overheatLeft:0,shotsFired:0,burst:0,lockout:0,pacified:0,hitFlash:0,
fromX:x,fromY:y,moveT:1};
this.enemies.push(e); return e;
},
input:function(key){
if(key==='' ) key='Space';
if(key==='l'||key==='L'){ if(this.state==='title'||this.state==='paused'){ this.showLegend=!this.showLegend; } return; }
if(this.showLegend){ if(key==='Escape'){ this.showLegend=false; } return; }
if(key==='Enter'){
if(this.state==='title'){ this.newRun(); }
else if(this.state==='won'||this.state==='lost'){ this.start(); }
return;
}
if(key==='Escape'){
if(this.state==='playing'||this.state==='possessed'){ this.state='paused'; }
else if(this.state==='paused'){ this.state='playing'; }
return;
}
if(this.state!=='playing'&&this.state!=='possessed') return;
var dir=keyDir(key);
if(dir){ tryMove(dir.dx,dir.dy); return; }
if(key===' '||key==='Space'){ return; } // possess/attack: phase 2
if(key==='x'||key==='X'){ return; } // abandon: phase 2
if(key==='q'||key==='Q'){ rotate(-1); return; }
if(key==='e'||key==='E'){ rotate(1); return; }
},
tick:function(n){
n=n||1;
for(var i=0;i<n;i++){
if(this.state==='playing'){
this.player.timer-=TICK_SEC;
if(this.player.timer<=0){ this.player.timer=0; this.state='lost'; break; }
}
// enemy AI + possessed-host logic arrive in later phases
}
return this.state;
},
save:function(){
try{
var p=this.player;
localStorage.setItem('bodysnatcher_save', JSON.stringify({
floor:this.floor, essence:this.essence, upgrades:this.upgrades, state:this.state,
grid:this.grid, player:{x:p.x,y:p.y,timer:p.timer,ghost:p.ghost,facing:p.facing}
}));
}catch(e){}
return true;
},
load:function(){
try{
var raw=localStorage.getItem('bodysnatcher_save'); if(!raw) return false;
var d=JSON.parse(raw);
this.floor=d.floor||1; this.essence=d.essence||0; this.upgrades=d.upgrades||{};
this.grid = d.grid ? d.grid : buildDungeon();
this.explored=rebuildExplored();
var dp=d.player||{};
var px=dp.x!=null?dp.x:SPAWN.x, py=dp.y!=null?dp.y:SPAWN.y;
this.player={x:px,y:py,rx:px,ry:py,ghost:true,timer:dp.timer!=null?dp.timer:ghostCap(),
host:null,facing:dp.facing||{x:0,y:1},fromX:px,fromY:py,moveT:1};
this.enemies=[]; this.projectiles=[];
this.state = (d.state==='playing'||d.state==='possessed') ? d.state : 'playing';
updateFog();
}catch(e){}
return true;
}
};
function keyDir(k){
if(k==='ArrowUp'||k==='w'||k==='W') return {dx:0,dy:-1};
if(k==='ArrowDown'||k==='s'||k==='S') return {dx:0,dy:1};
if(k==='ArrowLeft'||k==='a'||k==='A') return {dx:-1,dy:0};
if(k==='ArrowRight'||k==='d'||k==='D') return {dx:1,dy:0};
return null;
}
function tryMove(dx,dy){
var p=GAME.player; p.facing={x:dx,y:dy};
var nx=p.x+dx, ny=p.y+dy;
if(nx<0||ny<0||nx>=COLS||ny>=ROWS) return;
if(isSolid(GAME.grid[ny][nx])) return;
p.fromX=p.x; p.fromY=p.y; p.x=nx; p.y=ny; p.moveT=0;
updateFog();
}
function rotate(s){ var f=GAME.player.facing; if(s>0){var nx=-f.y,ny=f.x;f.x=nx;f.y=ny;}else{var a=f.y,b=-f.x;f.x=a;f.y=b;} }
/* ============================ RENDERING ================================ */
function smoothstep(e0,e1,x){ var t=Math.max(0,Math.min(1,(x-e0)/(e1-e0))); return t*t*(3-2*t); }
function ease(t){ t=Math.max(0,Math.min(1,t)); return 1-(1-t)*(1-t); }
function panel(x,y,w,h){
ctx.fillStyle='rgba(10,14,26,0.88)'; ctx.fillRect(x,y,w,h);
ctx.lineWidth=2; ctx.strokeStyle='rgba(130,160,220,0.55)'; ctx.strokeRect(x+1,y+1,w-2,h-2);
ctx.lineWidth=1; ctx.strokeStyle='rgba(80,110,170,0.25)'; ctx.strokeRect(x+5,y+5,w-10,h-10);
}
function drawFloor(px,py,x,y){
var g=ctx.createLinearGradient(px,py,px+TILE,py+TILE);
g.addColorStop(0,'#26222e'); g.addColorStop(0.5,'#1c1923'); g.addColorStop(1,'#15131b');
ctx.fillStyle=g; ctx.fillRect(px,py,TILE,TILE);
ctx.strokeStyle='rgba(0,0,0,0.4)'; ctx.lineWidth=1; ctx.strokeRect(px+0.5,py+0.5,TILE-1,TILE-1);
ctx.strokeStyle='rgba(255,240,210,0.04)'; ctx.strokeRect(px+1.5,py+1.5,TILE-3,TILE-3);
ctx.strokeStyle='rgba(0,0,0,0.16)';
ctx.beginPath(); ctx.moveTo(px+TILE/2,py+4); ctx.lineTo(px+TILE/2,py+TILE-4);
ctx.moveTo(px+4,py+TILE/2); ctx.lineTo(px+TILE-4,py+TILE/2); ctx.stroke();
}
function drawWall(px,py,x,y){
var g=ctx.createLinearGradient(px,py,px,py+TILE);
g.addColorStop(0,'#4c4759'); g.addColorStop(0.18,'#3c3747'); g.addColorStop(1,'#201d2a');
ctx.fillStyle=g; ctx.fillRect(px,py,TILE,TILE);
ctx.fillStyle='rgba(190,180,210,0.18)'; ctx.fillRect(px,py,TILE,3);
ctx.fillStyle='rgba(0,0,0,0.5)'; ctx.fillRect(px,py+TILE-4,TILE,4);
ctx.fillStyle='rgba(255,255,255,0.05)'; ctx.fillRect(px,py,2,TILE);
ctx.fillStyle='rgba(0,0,0,0.28)'; ctx.fillRect(px+TILE-2,py,2,TILE);
ctx.strokeStyle='rgba(0,0,0,0.45)'; ctx.lineWidth=1; ctx.strokeRect(px+0.5,py+0.5,TILE-1,TILE-1);
var s=(x*37+y*53)%3;
ctx.beginPath();
if(s===0){ ctx.moveTo(px+8,py+6); ctx.lineTo(px+14,py+16); ctx.lineTo(px+12,py+26); }
else if(s===1){ ctx.moveTo(px+23,py+6); ctx.lineTo(px+18,py+21); }
else { ctx.moveTo(px+6,py+23); ctx.lineTo(px+21,py+25); }
ctx.strokeStyle='rgba(0,0,0,0.3)'; ctx.stroke();
}
function drawExit(px,py){
var cx=px+TILE/2, cy=py+TILE/2, pulse=0.5+0.5*Math.sin(time*0.004);
var rg=ctx.createRadialGradient(cx,cy,2,cx,cy,TILE);
rg.addColorStop(0,'rgba(130,235,255,'+(0.55+0.3*pulse)+')');
rg.addColorStop(0.5,'rgba(60,150,225,'+(0.25+0.15*pulse)+')');
rg.addColorStop(1,'rgba(20,40,90,0)');
ctx.fillStyle=rg; ctx.fillRect(px-TILE,py-TILE,TILE*3,TILE*3);
ctx.fillStyle='#0c1424'; ctx.fillRect(px+4,py+4,TILE-8,TILE-8);
for(var i=0;i<4;i++){ ctx.fillStyle='rgba(150,215,255,'+(0.3+0.1*i)+')'; ctx.fillRect(px+6+i*2,py+8+i*4,TILE-12-i*4,3); }
ctx.strokeStyle='rgba(185,240,255,'+(0.6+0.3*pulse)+')'; ctx.lineWidth=1.5;
ctx.strokeRect(px+10,py+10,TILE-20,TILE-20);
ctx.beginPath(); ctx.moveTo(px+12,cy); ctx.lineTo(px+TILE-12,cy);
ctx.moveTo(cx,py+12); ctx.lineTo(cx,py+TILE-12); ctx.stroke();
}
function drawDoor(px,py){
drawFloor(px,py,0,0);
ctx.fillStyle='#5a3a22'; ctx.fillRect(px+6,py+8,4,TILE-16); ctx.fillRect(px+TILE-10,py+8,4,TILE-16);
ctx.fillStyle='#7a5230'; ctx.fillRect(px+10,py+9,TILE-20,TILE-18);
ctx.fillStyle='#3a2414'; ctx.fillRect(px+TILE/2-1,py+9,2,TILE-18);
ctx.fillStyle='#d9b048'; ctx.beginPath(); ctx.arc(px+TILE/2+4,py+TILE/2,1.6,0,Math.PI*2); ctx.fill();
}
function drawDungeon(){
var p=GAME.player;
for(var y=0;y<ROWS;y++) for(var x=0;x<COLS;x++){
if(!GAME.explored[y][x]) continue;
var d=Math.hypot(x-p.x,y-p.y), visible=d<=VIEW;
var t=GAME.grid[y][x], px=x*TILE, py=y*TILE;
if(t===1) drawWall(px,py,x,y);
else if(t===3) drawExit(px,py);
else if(t===2) drawDoor(px,py);
else drawFloor(px,py,x,y);
if(visible){
var edge=smoothstep(VIEW-1.8,VIEW,d);
if(edge>0){ ctx.fillStyle='rgba(2,3,8,'+(edge*0.55)+')'; ctx.fillRect(px,py,TILE,TILE); }
} else {
ctx.fillStyle='rgba(18,26,46,0.62)'; ctx.fillRect(px,py,TILE,TILE);
}
}
}
function drawEnemy(e){
var px=e.rx*TILE+TILE/2, py=e.ry*TILE+TILE/2, bob=Math.sin(time*0.005+e.x)*1.2;
ctx.fillStyle='rgba(0,0,0,0.32)'; ctx.beginPath(); ctx.ellipse(px,py+12,10,4,0,0,Math.PI*2); ctx.fill();
var bg=ctx.createRadialGradient(px,py+bob-3,2,px,py+bob,13);
bg.addColorStop(0,shade(e.color,1.25)); bg.addColorStop(0.6,e.color); bg.addColorStop(1,shade(e.color,0.5));
ctx.fillStyle=bg; ctx.beginPath(); ctx.arc(px,py+bob,11,0,Math.PI*2); ctx.fill();
ctx.fillStyle='rgba(255,255,255,0.18)'; ctx.beginPath(); ctx.arc(px-3,py+bob-4,3.5,0,Math.PI*2); ctx.fill();
ctx.fillStyle='#0c1018'; ctx.beginPath(); ctx.arc(px-3.5,py+bob-1,1.5,0,Math.PI*2); ctx.arc(px+3.5,py+bob-1,1.5,0,Math.PI*2); ctx.fill();
}
function drawEnemies(){ for(var i=0;i<GAME.enemies.length;i++) drawEnemy(GAME.enemies[i]); }
function drawProjectiles(){ for(var i=0;i<GAME.projectiles.length;i++){ var p=GAME.projectiles[i]; ctx.fillStyle='#ffd36b'; ctx.beginPath(); ctx.arc(p.rx*TILE+TILE/2,p.ry*TILE+TILE/2,3,0,Math.PI*2); ctx.fill(); } }
function drawPlayer(){
var p=GAME.player; if(!p) return;
var px=p.rx*TILE+TILE/2, py=p.ry*TILE+TILE/2, bob=Math.sin(time*0.005)*1.6;
ctx.fillStyle='rgba(0,0,0,0.3)'; ctx.beginPath(); ctx.ellipse(px,py+12,10,4,0,0,Math.PI*2); ctx.fill();
var ag=ctx.createRadialGradient(px,py+bob,2,px,py+bob,22);
ag.addColorStop(0,'rgba(180,230,255,0.38)'); ag.addColorStop(1,'rgba(120,200,255,0)');
ctx.fillStyle=ag; ctx.fillRect(px-24,py+bob-24,48,48);
ctx.save(); ctx.globalAlpha=0.88;
var bg=ctx.createRadialGradient(px,py+bob-2,2,px,py+bob,13);
bg.addColorStop(0,'rgba(238,247,255,0.96)'); bg.addColorStop(0.6,'rgba(172,212,246,0.6)'); bg.addColorStop(1,'rgba(120,170,230,0.04)');
ctx.fillStyle=bg;
ctx.beginPath();
ctx.moveTo(px-9,py+bob-2);
ctx.quadraticCurveTo(px-11,py+bob-14,px,py+bob-15);
ctx.quadraticCurveTo(px+11,py+bob-14,px+9,py+bob-2);
ctx.quadraticCurveTo(px+7,py+bob+9,px+3,py+bob+5);
ctx.quadraticCurveTo(px,py+bob+13,px-3,py+bob+5);
ctx.quadraticCurveTo(px-7,py+bob+9,px-9,py+bob-2);
ctx.closePath(); ctx.fill();
ctx.restore();
var fx=p.facing.x, fy=p.facing.y;
ctx.fillStyle='#cfeeff';
ctx.beginPath(); ctx.arc(px-3.6,py+bob-6,1.9,0,Math.PI*2); ctx.arc(px+3.6,py+bob-6,1.9,0,Math.PI*2); ctx.fill();
ctx.fillStyle='#16242f';
ctx.beginPath(); ctx.arc(px-3.6+fx*1.2,py+bob-6+fy*1.2,0.95,0,Math.PI*2); ctx.arc(px+3.6+fx*1.2,py+bob-6+fy*1.2,0.95,0,Math.PI*2); ctx.fill();
}
function drawLighting(){
var W=canvas.width,H=canvas.height, p=GAME.player;
var cx=p.rx*TILE+TILE/2, cy=p.ry*TILE+TILE/2;
var tg=ctx.createRadialGradient(cx,cy,40,cx,cy,380);
tg.addColorStop(0,'rgba(255,200,120,0.10)'); tg.addColorStop(1,'rgba(255,180,90,0)');
ctx.fillStyle=tg; ctx.fillRect(0,0,W,H);
var vg=ctx.createRadialGradient(W/2,H/2,H*0.32,W/2,H/2,H*0.82);
vg.addColorStop(0,'rgba(0,0,0,0)'); vg.addColorStop(1,'rgba(0,0,0,0.62)');
ctx.fillStyle=vg; ctx.fillRect(0,0,W,H);
}
function drawHUD(){
var W=canvas.width, p=GAME.player;
ctx.textBaseline='middle'; ctx.textAlign='left';
ctx.fillStyle='rgba(8,10,18,0.85)'; ctx.fillRect(0,0,W,34);
ctx.fillStyle='rgba(120,150,210,0.5)'; ctx.fillRect(0,34,W,2);
ctx.font='bold 12px "Trebuchet MS",sans-serif';
ctx.fillStyle='#8294b0'; ctx.fillText('BODY',12,17);
ctx.fillStyle = p.ghost?'#6f7e96':'#e74c3c';
ctx.fillText(p.ghost?'N/A (Ghost)':('HP '+Math.max(0,Math.ceil(p.host&&p.host.hp))+'/'+(p.host&&p.host.maxHp)),50,17);
ctx.fillStyle='#8294b0'; ctx.fillText('TIMER',150,17);
var low=p.timer<=5;
ctx.fillStyle=low?'#e67e22':'#7fd8ff'; ctx.font='bold 15px "Trebuchet MS",sans-serif';
ctx.fillText(p.timer.toFixed(1)+'s',198,17);
ctx.font='bold 12px "Trebuchet MS",sans-serif';
ctx.fillStyle='#8294b0'; ctx.fillText('ESSENCE',272,17);
ctx.fillStyle='#f1c40f'; ctx.fillText(String(GAME.essence),340,17);
ctx.fillStyle='#8294b0'; ctx.fillText('FLOOR',390,17);
ctx.fillStyle='#bcaefs'; ctx.fillText(GAME.floor+'/10',432,17);
ctx.fillStyle='#8fe0b0'; ctx.fillText(p.ghost?'SPACE: Possess':'SPACE: Attack',498,17);
ctx.fillStyle='#6c7a93'; ctx.font='10px "Trebuchet MS",sans-serif';
ctx.fillText('Q/E face X abandon ESC pause L bestiary',640,17);
}
function drawTitleGhost(cx,cy){
var bob=Math.sin(time*0.004)*3;
var ag=ctx.createRadialGradient(cx,cy+bob,4,cx,cy+bob,60);
ag.addColorStop(0,'rgba(180,230,255,0.35)'); ag.addColorStop(1,'rgba(120,200,255,0)');
ctx.fillStyle=ag; ctx.fillRect(cx-70,cy+bob-70,140,140);
ctx.save(); ctx.globalAlpha=0.92;
var bg=ctx.createRadialGradient(cx,cy+bob-4,3,cx,cy+bob,34);
bg.addColorStop(0,'rgba(240,248,255,0.97)'); bg.addColorStop(0.6,'rgba(170,212,246,0.6)'); bg.addColorStop(1,'rgba(120,170,230,0.03)');
ctx.fillStyle=bg;
ctx.beginPath();
ctx.moveTo(cx-24,cy+bob-4);
ctx.quadraticCurveTo(cx-30,cy+bob-38,cx,cy+bob-40);
ctx.quadraticCurveTo(cx+30,cy+bob-38,cx+24,cy+bob-4);
ctx.quadraticCurveTo(cx+18,cy+bob+22,cx+8,cy+bob+12);
ctx.quadraticCurveTo(cx,cy+bob+32,cx-8,cy+bob+12);
ctx.quadraticCurveTo(cx-18,cy+bob+22,cx-24,cy+bob-4);
ctx.closePath(); ctx.fill();
ctx.restore();
ctx.fillStyle='#dff4ff';
ctx.beginPath(); ctx.arc(cx-9,cy+bob-14,5,0,Math.PI*2); ctx.arc(cx+9,cy+bob-14,5,0,Math.PI*2); ctx.fill();
ctx.fillStyle='#16242f';
ctx.beginPath(); ctx.arc(cx-9,cy+bob-13,2.4,0,Math.PI*2); ctx.arc(cx+9,cy+bob-13,2.4,0,Math.PI*2); ctx.fill();
}
function drawTitle(){
var W=canvas.width,H=canvas.height;
var bg=ctx.createLinearGradient(0,0,0,H); bg.addColorStop(0,'#0a0c16'); bg.addColorStop(1,'#15131f');
ctx.fillStyle=bg; ctx.fillRect(0,0,W,H);
var ag=ctx.createRadialGradient(W/2,H*0.30,20,W/2,H*0.30,380);
ag.addColorStop(0,'rgba(120,170,240,0.18)'); ag.addColorStop(1,'rgba(0,0,0,0)');
ctx.fillStyle=ag; ctx.fillRect(0,0,W,H);
ctx.textAlign='center'; ctx.textBaseline='alphabetic';
ctx.font='bold 52px "Trebuchet MS",sans-serif'; ctx.fillStyle='#d2e2ff';
ctx.fillText('BODY SNATCHER', W/2, 104);
ctx.font='italic 15px "Trebuchet MS",sans-serif'; ctx.fillStyle='#7fa8d8';
ctx.fillText('Possess. Survive. Descend.', W/2, 130);
drawTitleGhost(W/2,182);
var pw=440, ph=204, px=(W-pw)/2, py=238;
panel(px,py,pw,ph);
ctx.textAlign='left'; ctx.textBaseline='middle';
var rows=[
['WASD / Arrows','Move (hold to keep moving)'],
['SPACE','Possess (ghost) / Attack (host)'],
['Q / E','Rotate facing for Shielder / Turret'],
['X','Abandon host, eject as ghost'],
['ENTER','Start run / confirm choice'],
['ESC','Pause / resume']
];
for(var i=0;i<rows.length;i++){
var ry=py+38+i*24;
ctx.fillStyle='#f5d97a'; ctx.font='bold 13px "Trebuchet MS",sans-serif'; ctx.fillText(rows[i][0], px+24, ry);
ctx.fillStyle='#9fb2cc'; ctx.font='13px "Trebuchet MS",sans-serif'; ctx.fillText(rows[i][1], px+150, ry);
}
ctx.textAlign='center';
ctx.fillStyle='#8fe0b0'; ctx.font='13px "Trebuchet MS",sans-serif';
ctx.fillText('Press L for the Enemy Bestiary', W/2, py+ph+24);
var pulse=0.5+0.5*Math.sin(time*0.005);
ctx.fillStyle='rgba(185,222,255,'+(0.6+0.4*pulse)+')'; ctx.font='bold 16px "Trebuchet MS",sans-serif';
ctx.fillText('Press ENTER to begin', W/2, py+ph+52);
}
function drawLegend(){
var W=canvas.width,H=canvas.height;
ctx.fillStyle='rgba(4,6,12,0.94)'; ctx.fillRect(0,0,W,H);
ctx.textAlign='center'; ctx.textBaseline='alphabetic';
ctx.fillStyle='#d2e2ff'; ctx.font='bold 26px "Trebuchet MS",sans-serif'; ctx.fillText('ENEMY BESTIARY', W/2, 38);
ctx.fillStyle='#7fa8d8'; ctx.font='12px "Trebuchet MS",sans-serif';
ctx.fillText('How each foe behaves, and what you gain by possessing it', W/2, 58);
var keys=Object.keys(ENEMIES), colW=W/2, rowH=50;
ctx.textAlign='left'; ctx.textBaseline='top';
for(var i=0;i<keys.length;i++){
var col=i%2, row=Math.floor(i/2);
var ex=24+col*colW, ey=76+row*rowH, e=ENEMIES[keys[i]];
ctx.fillStyle=e.color; ctx.fillRect(ex,ey,14,14);
ctx.strokeStyle='rgba(255,255,255,0.25)'; ctx.lineWidth=1; ctx.strokeRect(ex+0.5,ey+0.5,13,13);
ctx.fillStyle='#f5d97a'; ctx.font='bold 13px "Trebuchet MS",sans-serif'; ctx.fillText(e.name, ex+22, ey-1);
ctx.fillStyle='#d99b9b'; ctx.font='11px "Trebuchet MS",sans-serif'; ctx.fillText('Enemy: '+e.enemy, ex+22, ey+15);
ctx.fillStyle='#a6e0a4'; ctx.fillText('Host: '+e.kit, ex+22, ey+29);
}
ctx.textAlign='center'; ctx.fillStyle='#8fb0d8'; ctx.font='12px "Trebuchet MS",sans-serif';
ctx.fillText('Press L or ESC to return', W/2, H-18);
}
function drawPause(){
var W=canvas.width,H=canvas.height;
ctx.fillStyle='rgba(4,6,12,0.8)'; ctx.fillRect(0,0,W,H);
ctx.textAlign='center'; ctx.textBaseline='alphabetic';
ctx.fillStyle='#d2e2ff'; ctx.font='bold 36px "Trebuchet MS",sans-serif'; ctx.fillText('PAUSED', W/2, H/2-26);
ctx.fillStyle='#9fb2cc'; ctx.font='14px "Trebuchet MS",sans-serif'; ctx.fillText('ESC: Resume L: Bestiary', W/2, H/2+8);
ctx.fillStyle='#7fa8d8'; ctx.font='12px "Trebuchet MS",sans-serif';
ctx.fillText('WASD/Arrows move SPACE act Q/E face X abandon', W/2, H/2+32);
}
function drawEnd(title,color,sub){
var W=canvas.width,H=canvas.height;
ctx.fillStyle='rgba(4,6,12,0.92)'; ctx.fillRect(0,0,W,H);
ctx.textAlign='center'; ctx.textBaseline='alphabetic';
ctx.fillStyle=color; ctx.font='bold 42px "Trebuchet MS",sans-serif'; ctx.fillText(title, W/2, H/2-6);
ctx.fillStyle='#9fb2cc'; ctx.font='15px "Trebuchet MS",sans-serif'; ctx.fillText(sub, W/2, H/2+30);
ctx.fillStyle='#7fa8d8'; ctx.font='13px "Trebuchet MS",sans-serif'; ctx.fillText('Press ENTER', W/2, H/2+58);
}
function render(){
if(!ctx) return;
var W=canvas.width,H=canvas.height;
ctx.fillStyle='#05060a'; ctx.fillRect(0,0,W,H);
if(GAME.state==='title'){
if(GAME.showLegend) drawLegend(); else drawTitle();
return;
}
drawDungeon(); drawEnemies(); drawPlayer(); drawProjectiles(); drawLighting(); drawHUD();
if(GAME.showLegend){ drawLegend(); return; }
if(GAME.state==='paused') drawPause();
else if(GAME.state==='won') drawEnd('YOU ESCAPED','#8fe0b0','Floor '+GAME.floor+' cleared.');
else if(GAME.state==='lost') drawEnd('YOUR SOUL FADES','#e74c3c','The dungeon keeps your ghost.');
}
/* ---- color helper (hex lighten/darken) ---- */
function shade(hex,f){
var c=hex.replace('#',''); if(c.length===3) c=c[0]+c[0]+c[1]+c[1]+c[2]+c[2];
var r=parseInt(c.substr(0,2),16), g=parseInt(c.substr(2,2),16), b=parseInt(c.substr(4,2),16);
r=Math.max(0,Math.min(255,Math.round(r*f))); g=Math.max(0,Math.min(255,Math.round(g*f))); b=Math.max(0,Math.min(255,Math.round(b*f)));
return 'rgb('+r+','+g+','+b+')';
}
/* ============================ LOOP / BOOT ============================== */
var time=0, last=0, lastTurn=0, held={};
function processHeld(){
var order=['ArrowUp','ArrowDown','ArrowLeft','ArrowRight','w','a','s','d'];
for(var i=0;i<order.length;i++){ if(held[order[i]]){ GAME.input(order[i]); break; } }
}
function frame(ts){
if(typeof ts==='number'){ time=ts; var p=GAME.player;
if(p){ p.moveT=Math.min(1,(p.moveT||1)+(ts-last)/MOVE_MS); var e=ease(p.moveT);
p.rx=p.fromX+(p.x-p.fromX)*e; p.ry=p.fromY+(p.y-p.fromY)*e;
if(p.moveT>=1){ p.rx=p.x; p.ry=p.y; p.fromX=p.x; p.fromY=p.y; } }
if(GAME.state==='playing'||GAME.state==='possessed'){
if(ts-lastTurn>=TURN_MS){ lastTurn=ts; processHeld(); GAME.tick(1); }
}
}
last=ts;
try{ render(); }catch(e){}
try{ requestAnimationFrame(frame); }catch(e){}
}
var canvas=null, ctx=null;
try{
canvas=document.getElementById('c');
if(!canvas && document.createElement){ canvas=document.createElement('canvas'); canvas.width=800; canvas.height=576; }
if(canvas && canvas.getContext){ ctx=canvas.getContext('2d'); }
}catch(e){ ctx=null; }
window.GAME=GAME;
GAME.start();
try{ requestAnimationFrame(frame); }catch(e){}
try{
if(window && window.addEventListener){
window.addEventListener('keydown',function(ev){
var k=ev.key; if(k==null) return;
held[k]=true; GAME.input(k);
if(k===' '||k==='ArrowUp'||k==='ArrowDown'||k==='ArrowLeft'||k==='ArrowRight') ev.preventDefault();
});
window.addEventListener('keyup',function(ev){ held[ev.key]=false; });
}
}catch(e){}
})();
</script>
</body>
</html>