1076 lines
60 KiB
HTML
1076 lines
60 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>
|
|
*{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:2, 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:2, 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:3, 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:2, 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:0, 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:3, essence:4,
|
|
enemy:'Slow chase. Drains 1 HP and heals self.',
|
|
kit:'HP 4, each hit 1 dmg + heal self 1.'}
|
|
};
|
|
|
|
/* ---- procedural dungeon: random rooms + L-corridors; exit in farthest room ---- */
|
|
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){var ax=Math.min(x1,x2),bx=Math.max(x1,x2),ay=Math.min(y1,y2),by=Math.max(y1,y2);for(var y=ay;y<=by;y++)for(var x=ax;x<=bx;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;}
|
|
var rooms=[], target=5+Math.floor(Math.random()*4), tries=0;
|
|
while(rooms.length<target && tries<400){
|
|
tries++;
|
|
var w=4+Math.floor(Math.random()*5), h=4+Math.floor(Math.random()*3);
|
|
var rx=1+Math.floor(Math.random()*(COLS-w-2)), ry=1+Math.floor(Math.random()*(ROWS-h-2));
|
|
rooms.push({x:rx,y:ry,w:w,h:h,cx:Math.floor(rx+w/2),cy:Math.floor(ry+h/2)});
|
|
carve(rx,ry,rx+w-1,ry+h-1);
|
|
}
|
|
for(var i=1;i<rooms.length;i++){ var a=rooms[i-1],b=rooms[i]; if(Math.random()<0.5){hcorr(a.cx,b.cx,a.cy);vcorr(a.cy,b.cy,b.cx);}else{vcorr(a.cy,b.cy,a.cx);hcorr(a.cx,b.cx,b.cy);} }
|
|
SPAWN={x:rooms[0].cx,y:rooms[0].cy};
|
|
var bi=1,bd=-1;
|
|
for(var j=1;j<rooms.length;j++){var dd=Math.abs(rooms[j].cx-SPAWN.x)+Math.abs(rooms[j].cy-SPAWN.y); if(dd>bd){bd=dd;bi=j;}}
|
|
var er=rooms[bi]||rooms[0]; g[er.cy][er.cx]=3;
|
|
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; this._possessedTypes={}; this._hostDamage=false;
|
|
placePlayer(); updateFog();
|
|
return this.state;
|
|
},
|
|
newRun:function(){
|
|
this.grid=buildDungeon(); this.explored=rebuildExplored();
|
|
this.enemies=[]; this.projectiles=[]; this.floor=1; this.showLegend=false; this._possessedTypes={}; this._hostDamage=false;
|
|
placePlayer(); populateFloor(); this.state='playing'; updateFog(); achCheck();
|
|
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,burstCount:0,moveCd: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(this.state==='shop'){
|
|
if(key==='ArrowUp'||key==='w'||key==='W') this.shopCursor=((this.shopCursor||0)+2)%3;
|
|
else if(key==='ArrowDown'||key==='s'||key==='S') this.shopCursor=((this.shopCursor||0)+1)%3;
|
|
else if(key==='Enter'||key===' '||key==='Space') buy(this.shopCursor||0);
|
|
else if(key==='Escape') skipShop();
|
|
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'){ if(this.state==='playing'){ tryPossess(); } else if(this.state==='possessed'){ hostAttack(); } return; }
|
|
if(key==='x'||key==='X'){ if(this.state==='possessed'){ abandonHost(); } return; }
|
|
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++){
|
|
var st=this.state;
|
|
if(st==='playing'){
|
|
if(this.player.possessing){ completePossession(this.player.possessing); this.player.possessing=null; }
|
|
else { this.player.timer-=TICK_SEC; if(this.player.timer<=0){ this.player.timer=0; this.state='lost'; return this.state; } }
|
|
}
|
|
if(this.state==='playing'||this.state==='possessed'){
|
|
achCheck();
|
|
if(this.state==='possessed'&&this.player.host&&(this.player.host.overheatLeft||0)>0) this.player.host.overheatLeft--;
|
|
advanceEnemies();
|
|
advanceProjectiles();
|
|
}
|
|
}
|
|
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, achievements:this.achievements,
|
|
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.achievements=d.achievements||{};
|
|
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=[];
|
|
// host/enemies aren't serialised, so always resume as a ghost on a live
|
|
// floor — never a possessed-but-hostless inconsistent state.
|
|
this.state='playing';
|
|
updateFog();
|
|
}catch(e){}
|
|
return true;
|
|
},
|
|
addEssence:function(n){ addEssence(n); return this.essence; },
|
|
playSound:function(t){ sfx(t); },
|
|
openShop:function(){ openShop(); return this.state; },
|
|
buy:function(i){ buy(i); return this.state; }
|
|
};
|
|
|
|
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; if(p.host){ p.host.x=nx; p.host.y=ny; p.host.fromX=p.fromX; p.host.fromY=p.fromY; p.host.moveT=0; }
|
|
updateFog(); onStepTile();
|
|
}
|
|
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;} }
|
|
|
|
/* ===== Phase 2: possession, enemy AI, pathfinding, combat, audio ===== */
|
|
var AGGRO=6, GHOST_HIT_PENALTY=1.0;
|
|
var audioCtx=null;
|
|
function ensureAudio(){ if(audioCtx) return audioCtx; try{ var AC=window.AudioContext||window.webkitAudioContext; if(AC) audioCtx=new AC(); }catch(e){ audioCtx=null; } return audioCtx; }
|
|
function sfx(type){
|
|
try{
|
|
var ac=ensureAudio(); if(!ac) return;
|
|
var now=ac.currentTime||0, o=ac.createOscillator(), g=ac.createGain();
|
|
o.connect(g); g.connect(ac.destination);
|
|
var f=440,d=0.08,w='square',v=0.05;
|
|
if(type==='hit'){f=170;d=0.07;v=0.07;}
|
|
else if(type==='possess'){f=520;d=0.18;w='sine';v=0.07;}
|
|
else if(type==='eject'){f=260;d=0.16;w='sawtooth';v=0.06;}
|
|
else if(type==='shot'){f=700;d=0.05;v=0.05;}
|
|
else if(type==='death'){f=110;d=0.22;w='sawtooth';v=0.08;}
|
|
else if(type==='pickup'){f=880;d=0.06;w='triangle';v=0.06;}
|
|
else if(type==='blip'){f=300;d=0.03;v=0.03;}
|
|
o.type=w; try{o.frequency.setValueAtTime(f,now);}catch(e){}
|
|
try{g.gain.setValueAtTime(v,now); g.gain.exponentialRampToValueAtTime(0.0001,now+d);}catch(e){}
|
|
try{o.start(now); o.stop(now+d);}catch(e){}
|
|
}catch(e){}
|
|
}
|
|
function unlockAch(n){ if(!GAME.achievements[n]){ GAME.achievements[n]=true; sfx('pickup'); } }
|
|
function achCheck(){ if(GAME.essence>=30) unlockAch('Essence Hoarder'); if(GAME.floor>=7) unlockAch('Deep Diver'); }
|
|
|
|
function lineOfSight(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;
|
|
var err=dx-dy, cx=x0, cy=y0;
|
|
while(!(cx===x1&&cy===y1)){
|
|
var e2=2*err;
|
|
if(e2>-dy){err-=dy;cx+=sx;}
|
|
if(e2<dx){err+=dx;cy+=sy;}
|
|
if(cx===x1&&cy===y1) break;
|
|
if(cx<0||cy<0||cx>=COLS||cy>=ROWS) return false;
|
|
if(isSolid(GAME.grid[cy][cx])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
function bfsFirstStep(sx,sy,tx,ty){
|
|
if(sx===tx&&sy===ty) return null;
|
|
var W=COLS,H=ROWS,y;
|
|
var seen=[],prevX=[],prevY=[];
|
|
for(y=0;y<H;y++){ seen.push(new Array(W).fill(false)); prevX.push(new Array(W).fill(-1)); prevY.push(new Array(W).fill(-1)); }
|
|
var qx=[sx],qy=[sy]; seen[sy][sx]=true; var found=false;
|
|
while(qx.length){
|
|
var cx=qx.shift(),cy=qy.shift();
|
|
if(cx===tx&&cy===ty){found=true;break;}
|
|
var nbs=[[1,0],[-1,0],[0,1],[0,-1]];
|
|
for(var k=0;k<4;k++){
|
|
var nx=cx+nbs[k][0],ny=cy+nbs[k][1];
|
|
if(nx<0||ny<0||nx>=W||ny>=H) continue;
|
|
if(seen[ny][nx]) continue;
|
|
if(isSolid(GAME.grid[ny][nx])) continue;
|
|
seen[ny][nx]=true; prevX[ny][nx]=cx; prevY[ny][nx]=cy; qx.push(nx); qy.push(ny);
|
|
}
|
|
}
|
|
if(!found) return null;
|
|
var px2=tx, py2=ty;
|
|
while(!(prevX[py2][px2]===sx&&prevY[py2][px2]===sy)){
|
|
var pa=prevX[py2][px2], pb=prevY[py2][px2];
|
|
if(pa<0) return null;
|
|
if(pa===sx&&pb===sy) break;
|
|
px2=pa; py2=pb;
|
|
}
|
|
return {x:px2,y:py2};
|
|
}
|
|
// greedy Manhattan descent straight at the player when a reducing floor tile exists;
|
|
// falls back to BFS when walled in. Ignores enemy occupancy so chasers never jam behind a blocker.
|
|
function pursueStep(e,p){
|
|
var bestD=Math.abs(e.x-p.x)+Math.abs(e.y-p.y), best=null, dirs=[[1,0],[-1,0],[0,1],[0,-1]];
|
|
for(var k=0;k<4;k++){ var nx=e.x+dirs[k][0], ny=e.y+dirs[k][1];
|
|
if(nx<0||ny<0||nx>=COLS||ny>=ROWS) continue; if(nx===p.x&&ny===p.y) continue; if(isSolid(GAME.grid[ny][nx])) continue;
|
|
var d=Math.abs(nx-p.x)+Math.abs(ny-p.y); if(d<bestD){ bestD=d; best=[nx,ny]; } }
|
|
if(best){ moveEnemyTo(e,best[0],best[1]); return; }
|
|
var st=bfsFirstStep(e.x,e.y,p.x,p.y); if(st&&!(st.x===p.x&&st.y===p.y)) moveEnemyTo(e,st.x,st.y);
|
|
}
|
|
function enemyAt(x,y){ for(var i=0;i<GAME.enemies.length;i++){var e=GAME.enemies[i]; if(e.x===x&&e.y===y) return e;} return null; }
|
|
function addEssence(n){ GAME.essence+=n; achCheck(); }
|
|
function dropEssence(e){ var d=ENEMIES[e.type]; if(d) addEssence(d.essence); sfx('pickup'); }
|
|
|
|
function damageEnemy(e,n){
|
|
if(!e||e===GAME.player.host) return;
|
|
if(e.type==='shielder'){ var pp=GAME.player; var fdx=Math.sign(pp.x-e.x), fdy=Math.sign(pp.y-e.y); if(e.facing&&e.facing.x===fdx&&e.facing.y===fdy){ sfx('blip'); return; } }
|
|
e.hp-=n; e.hitFlash=1; sfx('hit');
|
|
if(e.hp<=0){ e.hp=0; if(e.type==='bomber'){ bomberExplode(e); } dropEssence(e); var i=GAME.enemies.indexOf(e); if(i>=0) GAME.enemies.splice(i,1); }
|
|
}
|
|
function damagePlayer(n){
|
|
var p=GAME.player;
|
|
if(p.ghost){ p.timer-=GHOST_HIT_PENALTY; sfx('hit'); if(p.timer<=0){ p.timer=0; GAME.state='lost'; } }
|
|
else if(p.host){ GAME._hostDamage=true; p.host.hp-=n; p.host.hitFlash=1; sfx('hit'); if(p.host.hp<=0){ p.host.hp=0; ejectOnDeath(); } }
|
|
}
|
|
function moveEnemyTo(e,x,y){ e.fromX=e.x; e.fromY=e.y; e.x=x; e.y=y; e.moveT=0; }
|
|
|
|
function tryPossess(){
|
|
var p=GAME.player; if(!p.ghost) return;
|
|
var range=(GAME.upgrades&&GAME.upgrades.ghostT2)?2:1;
|
|
for(var dy=-range;dy<=range;dy++) for(var dx=-range;dx<=range;dx++){
|
|
if(dx===0&&dy===0) continue;
|
|
if(Math.abs(dx)+Math.abs(dy)>range) continue;
|
|
var e=enemyAt(p.x+dx,p.y+dy);
|
|
if(e&&e!==p.host&&(e.lockout||0)<=0){ p.possessing=e; sfx('possess'); return; }
|
|
}
|
|
}
|
|
function completePossession(e){
|
|
var p=GAME.player;
|
|
p.host=e; p.ghost=false; p.possessing=null;
|
|
e.aggroed=false; e.pacified=0;
|
|
p.x=e.x; p.y=e.y; p.fromX=e.x; p.fromY=e.y; p.rx=e.x; p.ry=e.y; p.moveT=1;
|
|
GAME.state='possessed'; updateFog();
|
|
if(!GAME._possessedTypes) GAME._possessedTypes={};
|
|
GAME._possessedTypes[e.type]=1;
|
|
var cnt=0; for(var kk in GAME._possessedTypes) cnt++;
|
|
unlockAch('First Possession');
|
|
if(cnt>=5) unlockAch('Body Hopper');
|
|
sfx('possess'); try{GAME.save();}catch(e){}
|
|
}
|
|
function abandonHost(){
|
|
var p=GAME.player; if(!p.host) return;
|
|
var h=p.host; h.pacified=2; h.lockout=2; h.aggroed=false;
|
|
p.host=null; p.ghost=true; p.possessing=null;
|
|
GAME.state='playing'; sfx('eject'); try{GAME.save();}catch(e){}
|
|
}
|
|
function ejectOnDeath(){
|
|
var p=GAME.player;
|
|
if(p.host){ var idx=GAME.enemies.indexOf(p.host); if(idx>=0) GAME.enemies.splice(idx,1); p.host=null; }
|
|
p.ghost=true; p.timer=ghostCap(); p.possessing=null;
|
|
GAME.state='playing'; sfx('eject');
|
|
}
|
|
function hostAttack(){
|
|
var p=GAME.player; if(!p.host) return; var h=p.host, t=h.type;
|
|
if(t==='sentinel'||t==='turret'){
|
|
if((h.overheatLeft||0)>0){ sfx('blip'); return; }
|
|
GAME.projectiles.push({x:p.x,y:p.y,rx:p.rx,ry:p.ry,vx:p.facing.x,vy:p.facing.y,damage:h.damage,from:'player',life:14});
|
|
h.burstCount=(h.burstCount||0)+1; h.shotsFired=(h.shotsFired||0)+1;
|
|
if(h.burstCount>=3){ h.burstCount=0; h.overheatLeft=3; }
|
|
sfx('shot'); return;
|
|
}
|
|
if(t==='bomber'){ bomberDetonate(h); ejectOnDeath(); return; }
|
|
if(t==='healer'){
|
|
h.hp=Math.min(h.maxHp,h.hp+2);
|
|
for(var i=0;i<GAME.enemies.length;i++){ var o=GAME.enemies[i]; if(o===h) continue; if(Math.abs(o.x-p.x)<=3&&Math.abs(o.y-p.y)<=3) o.hp=Math.min(o.maxHp,o.hp+1); }
|
|
sfx('pickup'); return;
|
|
}
|
|
if(t==='phaser'){
|
|
if((h.phaseCd||0)>0){ sfx('blip'); return; }
|
|
var ptx=Math.max(0,Math.min(COLS-1,p.x+p.facing.x*5)), pty=Math.max(0,Math.min(ROWS-1,p.y+p.facing.y*5));
|
|
p.fromX=p.x; p.fromY=p.y; p.x=ptx; p.y=pty; p.moveT=0; p.rx=ptx; p.ry=pty;
|
|
h.x=ptx; h.y=pty; h.fromX=p.fromX; h.fromY=p.fromY; h.moveT=0; h.phaseCd=2; updateFog(); sfx('possess'); return;
|
|
}
|
|
if(t==='charger'){
|
|
var cx=p.x, cy=p.y;
|
|
for(var s=0;s<4;s++){ var nx=cx+p.facing.x, ny=cy+p.facing.y; if(nx<0||ny<0||nx>=COLS||ny>=ROWS||isSolid(GAME.grid[ny][nx])) break; cx=nx; cy=ny; var en=enemyAt(cx,cy); if(en&&en!==h) damageEnemy(en,2); }
|
|
if(cx!==p.x||cy!==p.y){ p.fromX=p.x; p.fromY=p.y; p.x=cx; p.y=cy; p.moveT=0; p.rx=cx; p.ry=cy; h.x=cx; h.y=cy; h.fromX=p.fromX; h.fromY=p.fromY; h.moveT=0; updateFog(); }
|
|
sfx('hit'); return;
|
|
}
|
|
if(t==='siren'){
|
|
for(var j=0;j<GAME.enemies.length;j++){ var en2=GAME.enemies[j]; if(en2===h) continue; if(Math.abs(en2.x-p.x)>4||Math.abs(en2.y-p.y)>4) continue; var ddx=Math.sign(p.x-en2.x), ddy=Math.sign(p.y-en2.y), mx=en2.x, my=en2.y; if(Math.abs(p.x-en2.x)>=Math.abs(p.y-en2.y)){ if(ddx!==0) mx=en2.x+ddx; } else { if(ddy!==0) my=en2.y+ddy; } if(mx>=0&&my>=0&&mx<COLS&&my<ROWS&&!isSolid(GAME.grid[my][mx])&&!enemyAt(mx,my)&&!(mx===p.x&&my===p.y)){ en2.fromX=en2.x; en2.fromY=en2.y; en2.x=mx; en2.y=my; en2.moveT=0; } }
|
|
sfx('possess'); return;
|
|
}
|
|
if(t==='mimic'){
|
|
for(var m=0;m<GAME.enemies.length;m++){ var em=GAME.enemies[m]; if(em===h) continue; var adx=Math.abs(em.x-p.x), ady=Math.abs(em.y-p.y); if((adx<=1&&ady===0)||(ady<=1&&adx===0)) damageEnemy(em,2); }
|
|
sfx('hit'); return;
|
|
}
|
|
var target=enemyAt(p.x+p.facing.x,p.y+p.facing.y);
|
|
if(target&&target!==h){ damageEnemy(target,h.damage); if(t==='leech') h.hp=Math.min(h.maxHp,h.hp+1); }
|
|
sfx('hit');
|
|
}
|
|
|
|
/* ---- enemy AI: calm baseline, aggro+LOS gated, no friendly fire ---- */
|
|
function advanceEnemies(){
|
|
var p=GAME.player;
|
|
for(var i=0;i<GAME.enemies.length;i++){
|
|
var e=GAME.enemies[i];
|
|
if(e===p.host) continue;
|
|
if(e.hitFlash>0) e.hitFlash=Math.max(0,e.hitFlash-0.25);
|
|
if(e.pacified>0){ e.pacified--; if(e.lockout>0) e.lockout--; continue; }
|
|
if(e.lockout>0) e.lockout--;
|
|
var dist=Math.abs(e.x-p.x)+Math.abs(e.y-p.y);
|
|
if(!e.aggroed){ if(Math.max(Math.abs(e.x-p.x),Math.abs(e.y-p.y))<=AGGRO) e.aggroed=true; }
|
|
if(e.moveCd>0) e.moveCd--;
|
|
var t=e.type;
|
|
if(t==='sentinel') sentinelAI(e,p,dist);
|
|
else if(t==='turret') turretAI(e,p,dist);
|
|
else if(t==='bomber') bomberAI(e,p,dist);
|
|
else if(t==='healer') healerAI(e,p,dist);
|
|
else if(t==='phaser') phaserAI(e,p,dist);
|
|
else if(t==='charger') chargerAI(e,p,dist);
|
|
else if(t==='siren') sirenAI(e,p,dist);
|
|
else if(t==='mimic') mimicAI(e,p,dist);
|
|
else if(t==='shielder') shielderAI(e,p,dist);
|
|
else if(t==='leech') leechAI(e,p,dist);
|
|
else meleeChase(e,p,dist);
|
|
}
|
|
for(var k=GAME.enemies.length-1;k>=0;k--){ if(GAME.enemies[k].dead){ dropEssence(GAME.enemies[k]); GAME.enemies.splice(k,1); } }
|
|
}
|
|
function meleeChase(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(e.moveCd>0) return;
|
|
if(dist<=1){ damagePlayer(e.damage); e.moveCd=e.speed; return; }
|
|
if(e.speed>0){ pursueStep(e,p); e.moveCd=e.speed; }
|
|
}
|
|
function sentinelAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(e.overheatLeft>0){ e.overheatLeft--; return; }
|
|
var dx=Math.sign(p.x-e.x), dy=Math.sign(p.y-e.y);
|
|
if(dx===0&&dy===0) dx=1;
|
|
GAME.projectiles.push({x:e.x,y:e.y,rx:(e.rx!=null?e.rx:e.x),ry:(e.ry!=null?e.ry:e.y),vx:dx,vy:dy,damage:e.damage,from:'enemy',life:14});
|
|
e.burstCount=(e.burstCount||0)+1; e.shotsFired=(e.shotsFired||0)+1;
|
|
if(e.burstCount>=3){ e.burstCount=0; e.overheatLeft=3; }
|
|
sfx('shot');
|
|
}
|
|
function turretAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(e.overheatLeft>0){ e.overheatLeft--; return; }
|
|
if((e.fireCd||0)>0){ e.fireCd--; return; }
|
|
var dx=Math.sign(p.x-e.x), dy=Math.sign(p.y-e.y); if(dx===0&&dy===0) dx=1;
|
|
GAME.projectiles.push({x:e.x,y:e.y,rx:(e.rx!=null?e.rx:e.x),ry:(e.ry!=null?e.ry:e.y),vx:dx,vy:dy,damage:e.damage,from:'enemy',life:14});
|
|
e.burstCount=(e.burstCount||0)+1; e.shotsFired=(e.shotsFired||0)+1; sfx('shot');
|
|
if(e.burstCount>=3){ e.burstCount=0; e.overheatLeft=3; } else { e.fireCd=2; }
|
|
}
|
|
function bomberExplode(e){
|
|
sfx('death'); var p=GAME.player;
|
|
if(Math.abs(p.x-e.x)<=1&&Math.abs(p.y-e.y)<=1){ damagePlayer(3); }
|
|
}
|
|
function bomberAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(dist<=1){ bomberExplode(e); e.hp=0; e.dead=true; return; }
|
|
if(e.moveCd>0) return;
|
|
if(e.speed>0){ var st=bfsFirstStep(e.x,e.y,p.x,p.y); if(st&&!(st.x===p.x&&st.y===p.y)&&!enemyAt(st.x,st.y)) moveEnemyTo(e,st.x,st.y); e.moveCd=e.speed; }
|
|
}
|
|
function healerAI(e,p,dist){
|
|
for(var i=0;i<GAME.enemies.length;i++){ var o=GAME.enemies[i]; if(o===e||o===GAME.player.host) continue; if(Math.abs(o.x-e.x)<=3&&Math.abs(o.y-e.y)<=3&&o.hp<o.maxHp) o.hp=Math.min(o.maxHp,o.hp+1); }
|
|
if(!e.aggroed) return;
|
|
if(e.moveCd>0) return;
|
|
if(dist<=4){ var aw=bfsFirstStep(e.x,e.y,2*e.x-p.x,2*e.y-p.y); if(aw&&!enemyAt(aw.x,aw.y)) moveEnemyTo(e,aw.x,aw.y); e.moveCd=e.speed; }
|
|
}
|
|
function phaserAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
e.phaseCd=(e.phaseCd||0)+1;
|
|
if(e.phaseCd>=3){
|
|
e.phaseCd=0; var spots=[];
|
|
for(var dx=-1;dx<=1;dx++) for(var dy=-1;dy<=1;dy++){ if(dx===0&&dy===0) continue; var tx=p.x+dx,ty=p.y+dy; if(tx>=0&&ty>=0&&tx<COLS&&ty<ROWS&&!isSolid(GAME.grid[ty][tx])&&!enemyAt(tx,ty)&&!(tx===p.x&&ty===p.y)) spots.push([tx,ty]); }
|
|
if(spots.length){ var s=spots[0]; e.fromX=e.x; e.fromY=e.y; e.x=s[0]; e.y=s[1]; e.moveT=0; if(Math.abs(e.x-p.x)+Math.abs(e.y-p.y)<=1) damagePlayer(e.damage); }
|
|
}
|
|
}
|
|
function chargerAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if((e.stun||0)>0){ e.stun--; return; }
|
|
if(e.telegraph){
|
|
e.telegraph=0; var dx=e.cdir.x, dy=e.cdir.y, px=e.x, py=e.y;
|
|
for(var k=0;k<4;k++){ var nx=px+dx, ny=py+dy; if(nx<0||ny<0||nx>=COLS||ny>=ROWS||isSolid(GAME.grid[ny][nx])){ e.stun=2; break; } px=nx; py=ny; if(px===p.x&&py===p.y){ damagePlayer(3); break; } }
|
|
if(px!==e.x||py!==e.y){ e.fromX=e.x; e.fromY=e.y; e.x=px; e.y=py; e.moveT=0; }
|
|
e.chargeCd=2; return;
|
|
}
|
|
if((e.chargeCd||0)>0) e.chargeCd--;
|
|
if((e.chargeCd||0)===0){ var cdx=Math.sign(p.x-e.x), cdy=Math.sign(p.y-e.y); if(cdx!==0) e.cdir={x:cdx,y:0}; else if(cdy!==0) e.cdir={x:0,y:cdy}; else e.cdir={x:1,y:0}; e.telegraph=1; }
|
|
}
|
|
function sirenAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(dist>1&&dist<=5){
|
|
var dx=Math.sign(e.x-p.x), dy=Math.sign(e.y-p.y), sx=0, sy=0;
|
|
if(Math.abs(e.x-p.x)>=Math.abs(e.y-p.y)){ sx=dx; } else { sy=dy; }
|
|
var nx=p.x+sx, ny=p.y+sy;
|
|
if(nx>=0&&ny>=0&&nx<COLS&&ny<ROWS&&!isSolid(GAME.grid[ny][nx])&&!enemyAt(nx,ny)){
|
|
p.fromX=p.x; p.fromY=p.y; p.x=nx; p.y=ny; p.moveT=0; p.rx=nx; p.ry=ny;
|
|
if(p.host){ p.host.x=nx; p.host.y=ny; p.host.fromX=p.fromX; p.host.fromY=p.fromY; p.host.moveT=0; }
|
|
updateFog();
|
|
}
|
|
}
|
|
if(dist<=1) damagePlayer(e.damage);
|
|
}
|
|
function mimicAI(e,p,dist){
|
|
if(e.moveCd>0) return;
|
|
if(dist<=1){ if(!e.revealed) e.revealed=true; damagePlayer(e.damage); e.moveCd=3; }
|
|
}
|
|
function shielderAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
var dx=Math.sign(p.x-e.x), dy=Math.sign(p.y-e.y); if(dx||dy) e.facing={x:dx,y:dy};
|
|
if(e.moveCd>0) return;
|
|
if(dist<=1){ damagePlayer(e.damage); e.moveCd=e.speed; return; }
|
|
if(e.speed>0){ var st=bfsFirstStep(e.x,e.y,p.x,p.y); if(st&&!(st.x===p.x&&st.y===p.y)&&!enemyAt(st.x,st.y)) moveEnemyTo(e,st.x,st.y); e.moveCd=e.speed; }
|
|
}
|
|
function leechAI(e,p,dist){
|
|
if(!e.aggroed) return;
|
|
if(e.moveCd>0) return;
|
|
if(dist<=1){ damagePlayer(e.damage); e.hp=Math.min(e.maxHp,e.hp+1); e.moveCd=e.speed; return; }
|
|
if(e.speed>0){ var st=bfsFirstStep(e.x,e.y,p.x,p.y); if(st&&!(st.x===p.x&&st.y===p.y)&&!enemyAt(st.x,st.y)) moveEnemyTo(e,st.x,st.y); e.moveCd=e.speed; }
|
|
}
|
|
function bomberDetonate(h){
|
|
sfx('death'); var p=GAME.player, killed=0;
|
|
for(var i=GAME.enemies.length-1;i>=0;i--){ var o=GAME.enemies[i]; if(o===h) continue; if(Math.abs(o.x-p.x)<=1&&Math.abs(o.y-p.y)<=1){ damageEnemy(o,4); if(o.hp<=0) killed++; } }
|
|
if(killed>=3) unlockAch('Bomberman');
|
|
}
|
|
function advanceProjectiles(){
|
|
var p=GAME.player;
|
|
for(var i=GAME.projectiles.length-1;i>=0;i--){
|
|
var pr=GAME.projectiles[i];
|
|
pr.age=(pr.age||0)+1; pr.x+=pr.vx; pr.y+=pr.vy; pr.life--; pr.rx=pr.x; pr.ry=pr.y;
|
|
if(pr.age>1&&(pr.x<0||pr.y<0||pr.x>=COLS||pr.y>=ROWS||isSolid(GAME.grid[pr.y][pr.x]))){ GAME.projectiles.splice(i,1); continue; }
|
|
if(pr.from==='player'){ var en=enemyAt(pr.x,pr.y); if(pr.age>1&&en&&en!==p.host){ damageEnemy(en,pr.damage); GAME.projectiles.splice(i,1); continue; } }
|
|
else { if(pr.age>1&&pr.x===p.x&&pr.y===p.y){ damagePlayer(pr.damage); GAME.projectiles.splice(i,1); continue; } }
|
|
if(pr.life<=0) GAME.projectiles.splice(i,1);
|
|
}
|
|
}
|
|
function floorPool(f){
|
|
if(f<=3) return ['brute','skitterer','sentinel'];
|
|
if(f<=6) return ['brute','skitterer','sentinel','shielder','bomber','charger'];
|
|
if(f<=9) return ['brute','skitterer','sentinel','shielder','bomber','charger','healer','phaser','siren','turret'];
|
|
return ['brute','skitterer','sentinel','shielder','bomber','healer','phaser','turret','charger','siren','mimic','leech'];
|
|
}
|
|
function populateFloor(){
|
|
var f=GAME.floor, p=GAME.player, pool=floorPool(f), floors=[];
|
|
for(var y=0;y<ROWS;y++) for(var x=0;x<COLS;x++){ if(GAME.grid[y][x]===0 && (Math.abs(x-p.x)+Math.abs(y-p.y))>=5) floors.push([x,y]); }
|
|
for(var i=floors.length-1;i>0;i--){ var j=Math.floor(Math.random()*(i+1)); var t=floors[i];floors[i]=floors[j];floors[j]=t; }
|
|
var count=Math.min(4+f, floors.length), k=0;
|
|
for(;k<count;k++) GAME.spawn(pool[Math.floor(Math.random()*pool.length)], floors[k][0], floors[k][1]);
|
|
var nch=1+Math.floor(Math.random()*2);
|
|
for(var c=0;c<nch && k<floors.length;c++,k++) GAME.grid[floors[k][1]][floors[k][0]]=5;
|
|
}
|
|
|
|
/* ===== Phase 4: exit handling, shop, upgrades, floor descent ===== */
|
|
function onStepTile(){
|
|
var p=GAME.player; if(!p) return;
|
|
if(GAME.state!=='playing'&&GAME.state!=='possessed') return;
|
|
var t=GAME.grid[p.y][p.x];
|
|
if(t===5){ addEssence(3+Math.floor(Math.random()*8)); GAME.grid[p.y][p.x]=0; sfx('pickup'); }
|
|
else if(t===3){ handleExit(); }
|
|
}
|
|
function handleExit(){
|
|
sfx('pickup');
|
|
var nf=GAME.floor+1;
|
|
if(nf>10){ GAME.state='won'; unlockAch('Escape Artist'); try{GAME.save();}catch(e){} return; }
|
|
if(nf===3||nf===5||nf===7||nf===9){ GAME._nextFloor=nf; openShop(); }
|
|
else descendTo(nf);
|
|
}
|
|
function descendTo(n){
|
|
GAME.floor=n; GAME.grid=buildDungeon(); GAME.explored=rebuildExplored();
|
|
GAME.enemies=[]; GAME.projectiles=[]; GAME._nextFloor=null;
|
|
if(n>=3 && !GAME._hostDamage) unlockAch('Pacifist Ghost');
|
|
placePlayer(); populateFloor(); updateFog(); GAME.state='playing';
|
|
if(GAME.upgrades&&GAME.upgrades.metaT1){
|
|
var pl=GAME.player, pool=floorPool(n), spot=null;
|
|
for(var d=0;d<4;d++){ var dx=[1,-1,0,0][d], dy=[0,0,1,-1][d], nx=pl.x+dx, ny=pl.y+dy; if(nx>=0&&ny>=0&&nx<COLS&&ny<ROWS&&GAME.grid[ny][nx]===0){ spot=[nx,ny]; break; } }
|
|
if(spot){ var e=GAME.spawn(pool[Math.floor(Math.random()*pool.length)], spot[0], spot[1]); if(e){ completePossession(e); } }
|
|
}
|
|
achCheck(); try{GAME.save();}catch(e){}
|
|
}
|
|
|
|
var UPGRADES=[
|
|
{id:'ghostT1',tree:'ghost',tier:1,cost:5,name:'Ghost Vigor',desc:'Ghost timer 15s -> 20s'},
|
|
{id:'ghostT2',tree:'ghost',tier:2,cost:8,name:'Far Reach',desc:'Possess from 2 tiles away'},
|
|
{id:'ghostT3',tree:'ghost',tier:3,cost:12,name:'Phase Walk',desc:'Burst through walls (1s / 5s cd)'},
|
|
{id:'ghostT4',tree:'ghost',tier:4,cost:15,name:'Ghost Dash',desc:'Dash 3 tiles as ghost (3s cd)'},
|
|
{id:'bodyT1',tree:'body',tier:1,cost:10,name:'Retain',desc:'Keep one ability from previous host'},
|
|
{id:'bodyT2',tree:'body',tier:2,cost:12,name:'Overclock',desc:'+50% stats on fresh possession'},
|
|
{id:'bodyT3',tree:'body',tier:3,cost:18,name:'Dual-wield',desc:'Carry two abilities at once'},
|
|
{id:'metaT1',tree:'meta',tier:1,cost:8,name:'Possession Seed',desc:'Start each floor already in a body'},
|
|
{id:'metaT2',tree:'meta',tier:2,cost:10,name:'Foresight',desc:'See enemy types through fog of war'},
|
|
{id:'metaT3',tree:'meta',tier:3,cost:6,name:'Haggler',desc:'30% off all shop prices'}
|
|
];
|
|
function shopCost(u){ return (GAME.upgrades&&GAME.upgrades.metaT3)?Math.ceil(u.cost*0.7):u.cost; }
|
|
function availableUpgrades(){
|
|
var out=[];
|
|
for(var i=0;i<UPGRADES.length;i++){ var u=UPGRADES[i];
|
|
if(GAME.upgrades[u.id]) continue;
|
|
if(u.tier>1 && !GAME.upgrades[u.tree+'T'+(u.tier-1)]) continue;
|
|
out.push(u);
|
|
}
|
|
return out;
|
|
}
|
|
function pickChoices(n){
|
|
var av=availableUpgrades().slice();
|
|
for(var i=av.length-1;i>0;i--){ var j=Math.floor(Math.random()*(i+1)); var t=av[i];av[i]=av[j];av[j]=t; }
|
|
return av.slice(0,n);
|
|
}
|
|
function openShop(){
|
|
GAME._nextFloor=GAME._nextFloor||(GAME.floor+1);
|
|
GAME.choices=pickChoices(3); GAME.shopCursor=0; GAME.state='shop'; sfx('blip');
|
|
try{GAME.save();}catch(e){}
|
|
}
|
|
function applyUpgrade(id){
|
|
GAME.upgrades[id]=true;
|
|
if(id==='ghostT1' && GAME.player) GAME.player.timer=ghostCap();
|
|
if(id==='bodyT3') unlockAch('Dual Wield');
|
|
}
|
|
function buy(index){
|
|
if(GAME.state!=='shop') return;
|
|
var c=GAME.choices[index]; if(!c) return;
|
|
var cost=shopCost(c);
|
|
if(GAME.essence>=cost) GAME.essence-=cost; else GAME.essence=0;
|
|
applyUpgrade(c.id); GAME.choices=[];
|
|
var nf=GAME._nextFloor||(GAME.floor+1); GAME._nextFloor=null;
|
|
if(nf>10){ GAME.state='won'; unlockAch('Escape Artist'); }
|
|
else descendTo(nf);
|
|
}
|
|
function skipShop(){
|
|
var nf=GAME._nextFloor||(GAME.floor+1); GAME._nextFloor=null; GAME.choices=[];
|
|
if(nf>10){ GAME.state='won'; unlockAch('Escape Artist'); }
|
|
else descendTo(nf);
|
|
}
|
|
|
|
/* ============================ 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+e.y)*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();
|
|
drawSprite(e,px,py+bob);
|
|
if(e.hitFlash>0){ ctx.fillStyle='rgba(255,255,255,0.5)'; ctx.beginPath(); ctx.arc(px,py+bob,11,0,Math.PI*2); ctx.fill(); }
|
|
}
|
|
function drawSprite(e,px,py){
|
|
var c=e.color, t=e.type, f=e.facing||{x:0,y:1}, fx=f.x, fy=f.y;
|
|
function eye(ex,ey,r){ ctx.fillStyle='#fff200'; ctx.beginPath(); ctx.arc(ex+fx*1.4,ey+fy*1.4,r,0,Math.PI*2); ctx.fill(); ctx.fillStyle='#1a1206'; ctx.beginPath(); ctx.arc(ex+fx*2.0,ey+fy*2.0,r*0.5,0,Math.PI*2); ctx.fill(); }
|
|
if(t==='brute'){
|
|
var g=ctx.createLinearGradient(px,py-12,px,py+12); g.addColorStop(0,'#e85648');g.addColorStop(0.5,c);g.addColorStop(1,'#5a1410');
|
|
ctx.fillStyle=g; ctx.beginPath(); ctx.moveTo(px-12,py+11);ctx.lineTo(px-9,py-11);ctx.lineTo(px+9,py-11);ctx.lineTo(px+12,py+11);ctx.closePath(); ctx.fill();
|
|
ctx.fillStyle='#7d1f17'; ctx.fillRect(px-13,py-5,5,13); ctx.fillRect(px+8,py-5,5,13);
|
|
ctx.fillStyle='#3a0e0a'; ctx.fillRect(px-9,py-9,18,4);
|
|
eye(px-4,py-3,2.1); eye(px+4,py-3,2.1);
|
|
ctx.fillStyle='#9a2a20'; ctx.beginPath(); ctx.arc(px-11,py+12,3,0,Math.PI*2); ctx.arc(px+11,py+12,3,0,Math.PI*2); ctx.fill();
|
|
} else if(t==='skitterer'){
|
|
var g2=ctx.createRadialGradient(px,py-2,2,px,py,12); g2.addColorStop(0,shade(c,1.3));g2.addColorStop(1,shade(c,0.55));
|
|
ctx.fillStyle=g2; ctx.beginPath(); ctx.ellipse(px,py,9,7,0,0,Math.PI*2); ctx.fill();
|
|
ctx.strokeStyle=shade(c,0.5); ctx.lineWidth=1.5; ctx.beginPath(); ctx.moveTo(px-7,py);ctx.lineTo(px+7,py); ctx.moveTo(px-3,py-4);ctx.lineTo(px-3,py+4); ctx.stroke();
|
|
ctx.strokeStyle=shade(c,0.7); ctx.lineWidth=1.5; ctx.beginPath(); ctx.moveTo(px-8,py+5);ctx.lineTo(px-12,py+9); ctx.moveTo(px+8,py+5);ctx.lineTo(px+12,py+9); ctx.moveTo(px,py+6);ctx.lineTo(px,py+11); ctx.stroke();
|
|
ctx.fillStyle=shade(c,0.4); ctx.beginPath(); ctx.moveTo(px-3,py-7);ctx.lineTo(px-6,py-10);ctx.lineTo(px-1,py-8);ctx.fill(); ctx.beginPath(); ctx.moveTo(px+3,py-7);ctx.lineTo(px+6,py-10);ctx.lineTo(px+1,py-8);ctx.fill();
|
|
eye(px-3,py-2,1.6); eye(px+3,py-2,1.6);
|
|
} else if(t==='sentinel'){
|
|
var g3=ctx.createRadialGradient(px,py-3,2,px,py,13); g3.addColorStop(0,shade(c,1.4));g3.addColorStop(0.7,c);g3.addColorStop(1,shade(c,0.4));
|
|
ctx.fillStyle=g3; ctx.beginPath(); for(var i=0;i<6;i++){ var a=Math.PI/2+i*Math.PI/3, r=11; var xx=px+Math.cos(a)*r, yy=py+Math.sin(a)*r; if(i===0)ctx.moveTo(xx,yy); else ctx.lineTo(xx,yy);} ctx.closePath(); ctx.fill();
|
|
ctx.strokeStyle='rgba(255,255,255,0.25)'; ctx.lineWidth=1; ctx.stroke();
|
|
ctx.fillStyle='rgba(180,120,255,0.5)'; ctx.beginPath(); ctx.arc(px,py+6,4,0,Math.PI*2); ctx.fill();
|
|
eye(px,py-1,2.2); ctx.fillStyle='rgba(200,160,255,0.8)'; ctx.fillRect(px-1,py-12,2,4);
|
|
} else if(t==='shielder'){
|
|
var gb=ctx.createRadialGradient(px,py-2,2,px,py,10); gb.addColorStop(0,shade(c,1.25));gb.addColorStop(1,shade(c,0.5));
|
|
ctx.fillStyle=gb; ctx.beginPath(); ctx.ellipse(px-fx*3,py-fy*3,7,9,0,0,Math.PI*2); ctx.fill();
|
|
ctx.save(); ctx.translate(px+fx*4,py+fy*4); ctx.rotate(Math.atan2(fy,fx)+Math.PI/2);
|
|
var gs=ctx.createLinearGradient(0,-13,0,13); gs.addColorStop(0,'#5aa0e0');gs.addColorStop(0.5,c);gs.addColorStop(1,'#1d3f66');
|
|
ctx.fillStyle=gs; ctx.beginPath(); ctx.moveTo(-7,-12);ctx.lineTo(7,-12);ctx.lineTo(8,11);ctx.lineTo(0,15);ctx.lineTo(-8,11);ctx.closePath(); ctx.fill();
|
|
ctx.strokeStyle='#0e2238'; ctx.lineWidth=1.5; ctx.stroke();
|
|
ctx.fillStyle='#dfe9f7'; ctx.fillRect(-1,-9,2,18); ctx.fillRect(-5,-3,10,2);
|
|
ctx.fillStyle='#9fc2ee'; ctx.beginPath(); ctx.arc(-4,-6,1,0,Math.PI*2); ctx.arc(4,-6,1,0,Math.PI*2); ctx.arc(-4,6,1,0,Math.PI*2); ctx.arc(4,6,1,0,Math.PI*2); ctx.fill();
|
|
ctx.restore();
|
|
eye(px-fx*3-2,py-fy*3-3,1.7);
|
|
} else if(t==='bomber'){
|
|
var gf=ctx.createRadialGradient(px-3,py-3,2,px,py,12); gf.addColorStop(0,shade(c,1.5));gf.addColorStop(0.6,c);gf.addColorStop(1,'#2a0a0a');
|
|
ctx.fillStyle=gf; ctx.beginPath(); ctx.arc(px,py+1,11,0,Math.PI*2); ctx.fill();
|
|
ctx.strokeStyle='#1a0606'; ctx.lineWidth=2; ctx.stroke();
|
|
ctx.strokeStyle='rgba(0,0,0,0.4)'; ctx.lineWidth=1; ctx.beginPath(); ctx.moveTo(px-8,py+1);ctx.lineTo(px+8,py+1); ctx.moveTo(px,py-7);ctx.lineTo(px,py+9); ctx.stroke();
|
|
ctx.strokeStyle='#5a4030'; ctx.lineWidth=2; ctx.beginPath(); ctx.moveTo(px+4,py-8);ctx.quadraticCurveTo(px+9,py-13,px+6,py-16); ctx.stroke();
|
|
var sp=0.5+0.5*Math.sin(time*0.03); ctx.fillStyle='rgba(255,200,40,'+(0.6+0.4*sp)+')'; ctx.beginPath(); ctx.arc(px+6,py-16,2+sp,0,Math.PI*2); ctx.fill();
|
|
eye(px-3.5,py-2,1.7); eye(px+3.5,py-2,1.7);
|
|
} else if(t==='healer'){
|
|
var gh=ctx.createLinearGradient(px,py-12,px,py+12); gh.addColorStop(0,shade(c,1.2));gh.addColorStop(1,shade(c,0.45));
|
|
ctx.fillStyle=gh; ctx.beginPath(); ctx.moveTo(px-9,py+12);ctx.lineTo(px-6,py-8);ctx.quadraticCurveTo(px,py-13,px+6,py-8);ctx.lineTo(px+9,py+12);ctx.closePath(); ctx.fill();
|
|
ctx.fillStyle=shade(c,0.35); ctx.beginPath(); ctx.moveTo(px-5,py-6);ctx.quadraticCurveTo(px,py-11,px+5,py-6);ctx.quadraticCurveTo(px,py-3,px-5,py-6);ctx.fill();
|
|
ctx.strokeStyle='#eafff0'; ctx.lineWidth=2; ctx.beginPath(); ctx.arc(px,py,2.5,0,Math.PI*2); ctx.moveTo(px,py+3);ctx.lineTo(px,py+9); ctx.moveTo(px-3,py+5);ctx.lineTo(px+3,py+5); ctx.stroke();
|
|
eye(px-3,py-3,1.5); eye(px+3,py-3,1.5);
|
|
} else if(t==='phaser'){
|
|
var gp=ctx.createLinearGradient(px,py-13,px,py+13); gp.addColorStop(0,shade(c,1.5));gp.addColorStop(0.5,c);gp.addColorStop(1,shade(c,0.4));
|
|
ctx.fillStyle=gp; ctx.beginPath(); ctx.moveTo(px,py-13);ctx.lineTo(px+9,py);ctx.lineTo(px,py+13);ctx.lineTo(px-9,py);ctx.closePath(); ctx.fill();
|
|
ctx.strokeStyle='rgba(255,255,255,0.5)'; ctx.lineWidth=1; ctx.beginPath(); ctx.moveTo(px,py-13);ctx.lineTo(px-4,py);ctx.lineTo(px,py+13);ctx.moveTo(px,py-13);ctx.lineTo(px+4,py);ctx.stroke();
|
|
ctx.fillStyle='rgba(180,255,250,0.7)'; ctx.beginPath(); ctx.arc(px,py,2.5,0,Math.PI*2); ctx.fill();
|
|
eye(px-3,py-2,1.5); eye(px+3,py-2,1.5);
|
|
} else if(t==='turret'){
|
|
var gt=ctx.createRadialGradient(px-3,py-3,2,px,py,12); gt.addColorStop(0,'#b9c0c8');gt.addColorStop(1,'#3a4048');
|
|
ctx.fillStyle=gt; ctx.beginPath(); for(var i=0;i<8;i++){ var a=Math.PI/8+i*Math.PI/4; var xx=px+Math.cos(a)*11, yy=py+Math.sin(a)*11; if(i===0)ctx.moveTo(xx,yy); else ctx.lineTo(xx,yy);} ctx.closePath(); ctx.fill();
|
|
ctx.strokeStyle='#22282e'; ctx.lineWidth=1.5; ctx.stroke();
|
|
ctx.fillStyle='#5a626a'; for(var b=0;b<8;b+=2){ var ab=b*Math.PI/4; ctx.beginPath(); ctx.arc(px+Math.cos(ab)*8,py+Math.sin(ab)*8,1.5,0,Math.PI*2); ctx.fill(); }
|
|
var ov=(e.overheatLeft||0)/3; ctx.fillStyle='rgba(255,'+Math.round(120-100*ov)+',40,'+(0.4+0.5*ov)+')'; ctx.fillRect(px-4,py+6,8,2);
|
|
ctx.save(); ctx.translate(px,py); ctx.rotate(Math.atan2(fy,fx)); ctx.fillStyle='#2a2e34'; ctx.fillRect(0,-3,13,6); ctx.fillStyle='#15181c'; ctx.fillRect(11,-2,4,4); ctx.restore();
|
|
eye(px-3,py-1,1.5); eye(px+3,py-1,1.5);
|
|
} else if(t==='charger'){
|
|
var gc=ctx.createRadialGradient(px-3,py-3,2,px,py,12); gc.addColorStop(0,'#ffe98a');gc.addColorStop(0.6,c);gc.addColorStop(1,'#7a5e08');
|
|
ctx.fillStyle=gc; ctx.beginPath(); ctx.ellipse(px,py,11,8,0,0,Math.PI*2); ctx.fill();
|
|
ctx.strokeStyle=shade(c,0.6); ctx.lineWidth=1.5; ctx.beginPath(); for(var cc=-7;cc<=7;cc+=4){ ctx.moveTo(px+cc,py-6);ctx.lineTo(px+cc+2,py+6);} ctx.stroke();
|
|
ctx.save(); ctx.translate(px,py); ctx.rotate(Math.atan2(fy,fx)); ctx.fillStyle='#fff3c0'; ctx.beginPath(); ctx.moveTo(8,0);ctx.lineTo(16,-3);ctx.lineTo(16,3);ctx.closePath(); ctx.fill(); ctx.restore();
|
|
eye(px-3,py-2,1.7); eye(px+3,py-2,1.7);
|
|
if(e.telegraph){ ctx.strokeStyle='rgba(255,80,80,'+(0.4+0.4*Math.sin(time*0.04))+')'; ctx.lineWidth=2; ctx.strokeRect(px-13,py-12,26,24); }
|
|
} else if(t==='siren'){
|
|
for(var ring=3;ring>=1;ring--){ ctx.strokeStyle='rgba(240,120,190,'+(0.12*ring)+')'; ctx.lineWidth=2; ctx.beginPath(); ctx.arc(px,py,6+ring*4+Math.sin(time*0.004+ring)*2,0,Math.PI*2); ctx.stroke(); }
|
|
var gsr=ctx.createRadialGradient(px,py,1,px,py,9); gsr.addColorStop(0,'#2a0a18');gsr.addColorStop(0.6,c);gsr.addColorStop(1,shade(c,0.5));
|
|
ctx.fillStyle=gsr; ctx.beginPath(); ctx.arc(px,py,8,0,Math.PI*2); ctx.fill();
|
|
ctx.fillStyle='rgba(255,200,230,0.8)'; ctx.beginPath(); ctx.arc(px,py,2.5,0,Math.PI*2); ctx.fill();
|
|
eye(px-3,py-2,1.4); eye(px+3,py-2,1.4);
|
|
} else if(t==='mimic'){
|
|
if(e.revealed){
|
|
var gm=ctx.createLinearGradient(px,py-10,px,py+12); gm.addColorStop(0,'#7a4a1a');gm.addColorStop(1,'#3a1f0a');
|
|
ctx.fillStyle=gm; ctx.beginPath(); ctx.moveTo(px-12,py+10);ctx.lineTo(px-10,py-8);ctx.lineTo(px+10,py-8);ctx.lineTo(px+12,py+10);ctx.closePath(); ctx.fill();
|
|
ctx.fillStyle='#3a0a0a'; ctx.beginPath(); ctx.moveTo(px-11,py+2);ctx.lineTo(px+11,py+2);ctx.lineTo(px+8,py+10);ctx.lineTo(px-8,py+10);ctx.closePath(); ctx.fill();
|
|
ctx.fillStyle='#fff'; for(var teeth=-8;teeth<=8;teeth+=4){ ctx.beginPath(); ctx.moveTo(px+teeth,py+2);ctx.lineTo(px+teeth+2,py+2);ctx.lineTo(px+teeth+1,py+6);ctx.fill(); }
|
|
ctx.fillStyle='#e85a8a'; ctx.beginPath(); ctx.arc(px,py+7,2.5,0,Math.PI*2); ctx.fill();
|
|
eye(px-4,py-4,1.6); eye(px+4,py-4,1.6);
|
|
} else {
|
|
ctx.fillStyle='#6b4423'; ctx.fillRect(px-11,py-3,22,13);
|
|
ctx.fillStyle='#8a5a2e'; ctx.beginPath(); ctx.moveTo(px-11,py-3);ctx.lineTo(px+11,py-3);ctx.lineTo(px+8,py-9);ctx.lineTo(px-8,py-9);ctx.closePath(); ctx.fill();
|
|
ctx.fillStyle='#d9b048'; ctx.fillRect(px-12,py-4,24,2); ctx.fillRect(px-12,py+8,24,2);
|
|
ctx.fillStyle='#b8902f'; ctx.fillRect(px-2,py-1,4,8); ctx.fillStyle='#3a2414'; ctx.fillRect(px-1,py+1,2,3);
|
|
ctx.strokeStyle='rgba(0,0,0,0.3)'; ctx.lineWidth=1; ctx.strokeRect(px-11,py-3,22,13);
|
|
}
|
|
} else if(t==='leech'){
|
|
var gl=ctx.createRadialGradient(px-3,py-4,2,px,py+3,12); gl.addColorStop(0,shade(c,1.4));gl.addColorStop(0.6,c);gl.addColorStop(1,'#2a1038');
|
|
ctx.fillStyle=gl; ctx.beginPath(); ctx.moveTo(px-9,py-6);ctx.quadraticCurveTo(px-12,py+8,px,py+12);ctx.quadraticCurveTo(px+12,py+8,px+9,py-6);ctx.quadraticCurveTo(px,py-10,px-9,py-6);ctx.closePath(); ctx.fill();
|
|
ctx.strokeStyle=shade(c,0.5); ctx.lineWidth=1; ctx.beginPath(); ctx.moveTo(px-7,py);ctx.lineTo(px+7,py); ctx.moveTo(px-5,py+4);ctx.lineTo(px+5,py+4); ctx.stroke();
|
|
ctx.fillStyle='#1a0820'; ctx.beginPath(); ctx.ellipse(px,py+9,4,3,0,0,Math.PI*2); ctx.fill();
|
|
ctx.fillStyle='rgba(200,80,160,0.6)'; ctx.beginPath(); ctx.arc(px,py+9,1.5,0,Math.PI*2); ctx.fill();
|
|
eye(px-3.5,py-3,1.5); eye(px+3.5,py-3,1.5);
|
|
} else {
|
|
var gf2=ctx.createRadialGradient(px,py-3,2,px,py,11); gf2.addColorStop(0,shade(c,1.25));gf2.addColorStop(1,shade(c,0.5));
|
|
ctx.fillStyle=gf2; ctx.beginPath(); ctx.arc(px,py,10,0,Math.PI*2); ctx.fill();
|
|
eye(px-3,py-2,1.6); eye(px+3,py-2,1.6);
|
|
}
|
|
}
|
|
function drawEnemies(){ for(var i=0;i<GAME.enemies.length;i++){ if(GAME.enemies[i]===GAME.player.host) continue; 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+p.x)*1.2;
|
|
if(!p.ghost&&p.host){ var h=p.host; h.rx=p.rx; h.ry=p.ry; h.facing=p.facing; var hx=p.rx*TILE+TILE/2, hy=p.ry*TILE+TILE/2; var au=ctx.createRadialGradient(hx,hy,4,hx,hy,26); au.addColorStop(0,'rgba(150,220,255,0.42)'); au.addColorStop(1,'rgba(120,200,255,0)'); ctx.fillStyle=au; ctx.fillRect(hx-28,hy-28,56,56); drawEnemy(h); return; }
|
|
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);
|
|
var act=p.ghost?'Possess':(p.host&&p.host.type==='sentinel'?'Fire burst':(p.host&&p.host.type==='skitterer'?'Rapid strike':'Heavy swing'));
|
|
ctx.fillStyle='#8fe0b0'; ctx.fillText(p.ghost?'SPACE: '+act:'SPACE: '+act,498,17);
|
|
if(!p.ghost){ ctx.fillStyle='#e2c04a'; ctx.fillText('X: Abandon',610,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 drawShop(){
|
|
var W=canvas.width,H=canvas.height;
|
|
ctx.fillStyle='rgba(4,6,12,0.93)'; ctx.fillRect(0,0,W,H);
|
|
ctx.textAlign='center'; ctx.textBaseline='alphabetic';
|
|
ctx.fillStyle='#f5d97a'; ctx.font='bold 30px "Trebuchet MS",sans-serif'; ctx.fillText('WANDERING SHOP', W/2, 60);
|
|
ctx.fillStyle='#9fb2cc'; ctx.font='13px "Trebuchet MS",sans-serif'; ctx.fillText('Essence: '+GAME.essence+' — spend before the next floor', W/2, 86);
|
|
var cw=226, ch=152, gap=18, total=3*cw+2*gap, sx=(W-total)/2, cy=116;
|
|
for(var i=0;i<3;i++){ var c=GAME.choices[i]; if(!c) continue;
|
|
var x=sx+i*(cw+gap), sel=i===(GAME.shopCursor||0), cost=shopCost(c), aff=GAME.essence>=cost;
|
|
panel(x,cy,cw,ch);
|
|
if(sel){ ctx.strokeStyle='rgba(245,217,122,0.95)'; ctx.lineWidth=2.5; ctx.strokeRect(x+2,cy+2,cw-4,ch-4); }
|
|
ctx.fillStyle='#d2e2ff'; ctx.font='bold 17px "Trebuchet MS",sans-serif'; ctx.fillText(c.name, x+cw/2, cy+38);
|
|
ctx.fillStyle='#7fa8d8'; ctx.font='bold 11px "Trebuchet MS",sans-serif'; ctx.fillText(c.tree.toUpperCase()+' · TIER '+c.tier, x+cw/2, cy+58);
|
|
ctx.fillStyle='#a6e0a4'; ctx.font='12px "Trebuchet MS",sans-serif'; ctx.fillText(c.desc, x+cw/2, cy+86);
|
|
ctx.fillStyle=aff?'#f1c40f':'#7a6a3a'; ctx.font='bold 19px "Trebuchet MS",sans-serif'; ctx.fillText(cost+' ess', x+cw/2, cy+ch-20);
|
|
}
|
|
ctx.fillStyle='#8fb0d8'; ctx.font='12px "Trebuchet MS",sans-serif';
|
|
ctx.fillText('Up / Down select ENTER buy ESC skip', W/2, H-22);
|
|
}
|
|
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;
|
|
}
|
|
if(GAME.state==='shop'){ drawDungeon(); drawShop(); 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; } }
|
|
var dt=(ts-last)/MOVE_MS;
|
|
for(var ei=0;ei<GAME.enemies.length;ei++){ var en=GAME.enemies[ei]; en.moveT=Math.min(1,(en.moveT||1)+dt); var ee=ease(en.moveT); en.rx=en.fromX+(en.x-en.fromX)*ee; en.ry=en.fromY+(en.y-en.fromY)*ee; if(en.moveT>=1){ en.rx=en.x; en.ry=en.y; en.fromX=en.x; en.fromY=en.y; } }
|
|
for(var pi=0;pi<GAME.projectiles.length;pi++){ var pr=GAME.projectiles[pi]; pr.rx=pr.x; pr.ry=pr.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;
|
|
// browsers fire keydown repeatedly while a key is held; only act on the
|
|
// initial press so continuous travel stays gated to the turn cadence
|
|
// (processHeld in the loop). Auto-repeat would otherwise move far too fast.
|
|
if(!ev.repeat) GAME.input(k);
|
|
try{ var ac=ensureAudio(); if(ac&&ac.state==='suspended') ac.resume(); }catch(e2){}
|
|
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>
|