-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgameloop.js
More file actions
198 lines (173 loc) · 6.7 KB
/
gameloop.js
File metadata and controls
198 lines (173 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
// ══════════════════════════════════════════════
// PLAYER UPDATE
// ══════════════════════════════════════════════
function updatePlayer(effectiveKeys) {
if (gameOverActive) return;
const k = effectiveKeys || keys;
const MS=0.06, TS=0.038;
const ca=Math.cos(pl.a), sa=Math.sin(pl.a);
const sr=Math.cos(pl.a+Math.PI/2), sp=Math.sin(pl.a+Math.PI/2);
const tryMove=(nx,ny)=>{
if (!collidesAt(nx,pl.y)) pl.x=nx;
if (!collidesAt(pl.x,ny)) pl.y=ny;
};
if (k['KeyW']) tryMove(pl.x+ca*MS, pl.y+sa*MS);
if (k['ArrowUp']) tryMove(pl.x+ca*MS, pl.y+sa*MS);
if (k['KeyS']) tryMove(pl.x-ca*MS, pl.y-sa*MS);
if (k['ArrowDown']) tryMove(pl.x-ca*MS, pl.y-sa*MS);
if (k['KeyA']) tryMove(pl.x-sr*MS, pl.y-sp*MS);
if (k['KeyD']) tryMove(pl.x+sr*MS, pl.y+sp*MS);
if (k['ArrowLeft']) pl.a-=TS;
if (k['ArrowRight'])pl.a+=TS;
if (k['Digit1'] && !pl.wepFiring) pl.weapon = 1;
if (k['Digit2'] && !pl.wepFiring) pl.weapon = 2;
if (k['Space'] && pl.rcd<=0) fireWeapon();
if (pl.rcd>0) pl.rcd--;
if (pl.invincible>0) pl.invincible--;
}
const dmgFlash = document.getElementById('dmg-flash');
let dmgFlashT = 0;
function updateProjectiles() {
for (const p of projs) {
if (!p.alive) continue;
p.life++;
if (p.life >= p.maxLife) { p.alive=false; continue; }
p.x += Math.cos(p.a)*p.spd;
p.y += Math.sin(p.a)*p.spd;
if (mapAt(p.x, p.y)) {
p.alive=false;
if (p.type !== 'bullet') exps.push({x:p.x, y:p.y, t:10, maxT:10, type:p.type});
continue;
}
if ((p.type==='bullet'||p.type==='gauss'||p.type==='rocket') && !en.dead) {
if (Math.hypot(p.x-en.x, p.y-en.y) < 0.65) {
p.alive=false;
const dmg = p.type==='bullet' ? WEP_DAMAGE[1] : p.type==='gauss' ? WEP_DAMAGE[2] : 5;
en.hp = Math.max(0, en.hp-dmg);
en.flashT = 9;
exps.push({x:p.x, y:p.y, t:16, maxT:16, type:'hit'});
if (en.hp<=0) { en.dead=true; en.respT=210; kills++; en.spr_seq=0;
en.spr_seq_time=performance.now()-TRO_DEATH_FRAME_MS;
en.gibDeath=(p.type==='gauss'||p.type==='rocket'); }
}
}
if (p.type==='fireball' && pl.invincible<=0) {
if (Math.hypot(p.x-pl.x, p.y-pl.y) < 0.6) {
p.alive=false;
pl.hp = Math.max(0, pl.hp-15);
pl.invincible = 65;
dmgFlashT = 12;
exps.push({x:p.x, y:p.y, t:10, maxT:10, type:'fire'});
if (pl.hp<=0) {
pl.hp = 0;
triggerGameOver();
}
}
}
}
for (let i=projs.length-1; i>=0; i--) if (!projs[i].alive) projs.splice(i,1);
for (let i=exps.length-1; i>=0; i--) { exps[i].t--; if (exps[i].t<=0) exps.splice(i,1); }
// Damage flash effect
if (dmgFlashT > 0) {
dmgFlashT--;
dmgFlash.className = 'hit';
} else {
dmgFlash.className = '';
}
}
const RESPAWN_POS=[[13,13],[13,2],[7,1],[1,7],[14,7],[7,14]];
function stepEnemy() {
if (gameOverActive) return;
if (en.dead) {
if (--en.respT<=0) {
const [rx,ry]=RESPAWN_POS[kills%RESPAWN_POS.length];
const maxHp = en.maxHp;
en = { x:rx+0.5, y:ry+0.5, a:Math.PI, hp:maxHp, maxHp, fcd:90, dead:false, spr_seq:0, spr_seq_time:0, respT:0, flashT:0,
walkFrame:0, walkFrameTime:0, atkFrame:0, atkFrameTime:0, isAttacking:false, walkDir:0, gibDeath:false };
enemies.push(en);
}
return;
}
if (en.flashT>0) en.flashT--;
const inp=getInput(), ideal=getIdeal();
let lsum=0;
for (let i=0; i<TRAINING_STEPS_PER_FRAME; i++) { lsum+=nnTrain(inp,ideal); steps++; }
curLoss=lsum/TRAINING_STEPS_PER_FRAME;
lossHist.push(curLoss);
if (lossHist.length>220) lossHist.shift();
const out=nnForward(inp);
lastOut=new Float32Array(out);
const ETURN=0.065, ESPD=0.022;
if (out[0]>0.5) en.a-=ETURN;
if (out[1]>0.5) en.a+=ETURN;
const ca=Math.cos(en.a), sa=Math.sin(en.a);
const sca=Math.cos(en.a+Math.PI/2), ssa=Math.sin(en.a+Math.PI/2);
let mvx=0, mvy=0;
if (out[2]>0.5) { mvx+=ca*ESPD; mvy+=sa*ESPD; }
if (out[3]>0.5) { mvx-=ca*ESPD*0.65; mvy-=sa*ESPD*0.65; }
if (out[4]>0.5) { mvx-=sca*ESPD*0.8; mvy-=ssa*ESPD*0.8; }
if (out[5]>0.5) { mvx+=sca*ESPD*0.8; mvy+=ssa*ESPD*0.8; }
const px=en.x, py=en.y;
const nx=en.x+mvx, ny=en.y+mvy;
if (!collidesAt(nx,en.y)) en.x=nx;
if (!collidesAt(en.x,ny)) en.y=ny;
// Drive walk animation from actual displacement projected onto facing direction
const adx=en.x-px, ady=en.y-py;
const fwd = adx*Math.cos(en.a) + ady*Math.sin(en.a);
en.walkDir = Math.abs(adx)+Math.abs(ady) < 1e-5 ? 0 : fwd < 0 ? -1 : 1;
// Advance attack animation; spawn fireball at last frame (h) using en.a
if (en.isAttacking) {
const now = performance.now();
if (now - en.atkFrameTime >= ATK_FRAME_MS) {
en.atkFrameTime = now;
en.atkFrame++;
if (en.atkFrame === ATK_FRAMES.length - 1) spawnFireball();
if (en.atkFrame >= ATK_FRAMES.length) { en.isAttacking=false; en.atkFrame=0; }
}
} else if (out[6]>0.5 && en.fcd<=0) {
en.fcd=80; en.isAttacking=true; en.atkFrame=0; en.atkFrameTime=performance.now();
}
if (en.fcd>0) en.fcd--;
}
function spawnFireball() {
const a = en.a + (Math.random()-0.5)*0.15;
projs.push({
x: en.x+Math.cos(a)*0.55, y: en.y+Math.sin(a)*0.55,
a, spd:0.10, type:'fireball', alive:true, life:0, maxLife:170
});
}
// ══════════════════════════════════════════════
// MAIN LOOP
// ══════════════════════════════════════════════
let _snnEffKeys = null;
// Persistent buffer for SNN frame capture (avoids getImageData on potentially tainted canvas)
const _snnCapture = document.createElement('canvas');
_snnCapture.width = CW; _snnCapture.height = CH;
const _snnCaptureCtx = _snnCapture.getContext('2d');
function frame() {
// Use SNN keys computed from previous frame's full render
const effectiveKeys = _snnEffKeys || keys;
updatePlayer(effectiveKeys);
updateWeapon();
updateProjectiles();
stepEnemy();
drawScene();
drawEnemy();
drawProjectiles();
drawExplosions();
drawWeapon();
drawMinimap();
// Player invincibility flicker overlay
if (pl.invincible>0 && (pl.invincible%8)<4) {
drawRedFlicker();
}
// SNN/GPT reads complete rendered frame (enemies, projectiles, weapon all visible)
if (typeof snnCtrlStep === 'function') {
const fullPx = ctx.getImageData(0, 0, CW, CH).data;
_snnEffKeys = snnCtrlStep(fullPx, pl, en, enemies, projs, keys);
}
drawLoss();
updateHUD();
requestAnimationFrame(frame);
}