圣安托万门外 · 防御战 (军官与通条穿透)
by EpicCoder881066 lines46.4 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title>圣安托万门外 · 防御战 (军官与通条穿透)</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #0d0a07; font-family: Georgia, "Noto Serif SC", serif; }
canvas { display: block; touch-action: none; cursor: crosshair; }
#hud {
position: fixed; left: 12px; top: 12px;
color: #c9b489; font-size: 12px; line-height: 1.7;
background: rgba(20,16,12,.85);
border: 1px solid #5a4529; border-radius: 6px;
padding: 8px 11px; pointer-events: none;
z-index: 10;
width: 220px;
}
#hud b { color: #f0c674; font-weight: normal; }
#hud .tip { color: #8f7a55; font-size: 11px; }
#gate-hp-bar {
width: 100%; height: 8px; background: #3b2a18; border: 1px solid #5a4529;
margin-top: 5px; border-radius: 4px; overflow: hidden;
}
#gate-hp-fill {
height: 100%; width: 100%; background: #8c2a2a; transition: width 0.2s;
}
#ui {
position: fixed; left: 12px; bottom: 20px;
background: rgba(20,16,12,.9);
border: 1px solid #5a4529; border-radius: 6px;
padding: 10px; width: 240px;
z-index: 10;
}
#ui .gold { color: #f0c674; font-size: 15px; margin-bottom: 8px; text-align: center; }
#ui .gold span { font-size: 20px; font-weight: bold; }
button {
width: 100%; padding: 8px;
background: #5b3b22; color: #f4dfb4;
border: 1px solid #a57843; border-radius: 4px;
font-family: inherit; font-size: 13px; cursor: pointer;
margin-bottom: 6px;
display: flex; justify-content: space-between; align-items: center;
}
button:hover { background: #714b2b; }
button:disabled { background: #3b2a18; color: #7a6a50; border-color: #4a3420; cursor: not-allowed; }
button span.price { color: #ffaa55; font-size: 12px; }
#btnReset {
position: fixed; right: 12px; top: 12px;
width: auto; margin: 0; padding: 8px 12px;
z-index: 10;
}
#game-over {
display: none; position: fixed; inset: 0;
background: rgba(20, 5, 5, 0.85); z-index: 999;
flex-direction: column; align-items: center; justify-content: center;
color: #ff5555; font-size: 40px; letter-spacing: 5px; font-weight: bold;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="hud">
圣安托万门 · 防御战<br>
<div style="margin-top:5px; font-size:11px;">城门耐久: <span id="gateHpTxt">1000</span> / 1000</div>
<div id="gate-hp-bar"><div id="gate-hp-fill"></div></div>
</div>
<div id="ui">
<div class="gold">国库:<span id="goldTxt">0</span></div>
<button id="btnMilitia" onclick="recruit('militia')">招募民兵 <span class="price">10</span></button>
<button id="btnLine" onclick="recruit('line')">招募线列步兵 <span class="price">30</span></button>
<button id="btnOfficer" onclick="recruit('officer')" style="border-color: #d4af37; color: #f0c674;">招募军官 <span class="price" style="color:#d4af37;">50</span></button>
</div>
<button id="btnReset">重置视野</button>
<div id="game-over">巴黎沦陷</div>
<script>
/* ========== 音频引擎 ========== */
let audioCtx = null;
function initAudio() {
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
if (audioCtx.state === 'suspended') audioCtx.resume();
if (!audioCtx.unlocked) {
audioCtx.unlocked = true;
const buffer = audioCtx.createBuffer(1, 1, 22050);
const source = audioCtx.createBufferSource();
source.buffer = buffer; source.connect(audioCtx.destination); source.start(0);
}
}
window.addEventListener('touchstart', initAudio, { once: true });
window.addEventListener('mousedown', initAudio, { once: true });
function playFireSound() {
if (!audioCtx) return;
const t = audioCtx.currentTime;
const bufferSize = audioCtx.sampleRate * 0.3;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) data[i] = Math.random() * 2 - 1;
const noise = audioCtx.createBufferSource(); noise.buffer = buffer;
const filter = audioCtx.createBiquadFilter(); filter.type = 'lowpass'; filter.frequency.setValueAtTime(1000, t); filter.frequency.exponentialRampToValueAtTime(100, t + 0.3);
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.4, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.3);
noise.connect(filter).connect(gain).connect(audioCtx.destination); noise.start(t);
const osc = audioCtx.createOscillator(); osc.type = 'sine'; osc.frequency.setValueAtTime(150, t); osc.frequency.exponentialRampToValueAtTime(40, t + 0.2);
const oscGain = audioCtx.createGain(); oscGain.gain.setValueAtTime(0.5, t); oscGain.gain.exponentialRampToValueAtTime(0.01, t + 0.2);
osc.connect(oscGain).connect(audioCtx.destination); osc.start(t); osc.stop(t + 0.2);
}
function playMeleeSound() {
if (!audioCtx) return;
const t = audioCtx.currentTime;
const bufferSize = audioCtx.sampleRate * 0.15;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) data[i] = Math.random() * 2 - 1;
const noise = audioCtx.createBufferSource(); noise.buffer = buffer;
const filter = audioCtx.createBiquadFilter(); filter.type = 'bandpass'; filter.frequency.value = 1500;
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.2, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.15);
noise.connect(filter).connect(gain).connect(audioCtx.destination); noise.start(t);
}
function playZombieSound() {
if (!audioCtx) return;
const t = audioCtx.currentTime;
const osc = audioCtx.createOscillator(); osc.type = 'sawtooth'; osc.frequency.setValueAtTime(120, t); osc.frequency.exponentialRampToValueAtTime(40, t + 0.15);
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.08, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.15);
osc.connect(gain).connect(audioCtx.destination); osc.start(t); osc.stop(t + 0.15);
}
function playReloadStepSound(stepType) {
if (!audioCtx) return;
const t = audioCtx.currentTime;
if (stepType === 'paper') {
const bufferSize = audioCtx.sampleRate * 0.1;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) data[i] = Math.random() * 2 - 1;
const noise = audioCtx.createBufferSource(); noise.buffer = buffer;
const filter = audioCtx.createBiquadFilter(); filter.type = 'highpass'; filter.frequency.value = 3000;
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.1, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.1);
noise.connect(filter).connect(gain).connect(audioCtx.destination); noise.start(t);
} else if (stepType === 'powder') {
const osc = audioCtx.createOscillator(); osc.type = 'sine'; osc.frequency.setValueAtTime(800, t); osc.frequency.exponentialRampToValueAtTime(400, t + 0.4);
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.05, t); gain.gain.linearRampToValueAtTime(0.01, t + 0.4);
osc.connect(gain).connect(audioCtx.destination); osc.start(t); osc.stop(t + 0.4);
} else if (stepType === 'ramrod') {
[0, 0.3].forEach(delay => {
const osc = audioCtx.createOscillator(); osc.type = 'triangle'; osc.frequency.setValueAtTime(600, t + delay); osc.frequency.exponentialRampToValueAtTime(1500, t + delay + 0.08);
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.05, t + delay); gain.gain.exponentialRampToValueAtTime(0.01, t + delay + 0.08);
osc.connect(gain).connect(audioCtx.destination); osc.start(t + delay); osc.stop(t + delay + 0.1);
});
} else if (stepType === 'click') {
const osc = audioCtx.createOscillator(); osc.type = 'square'; osc.frequency.setValueAtTime(1800, t);
const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.05, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.02);
osc.connect(gain).connect(audioCtx.destination); osc.start(t); osc.stop(t + 0.02);
}
}
/* ========== 基础数据 ========== */
const WORLD_W = 100;
const WORLD_H = 100;
const UNIT_SCALE = 1.0;
const UNIT_RADIUS = 0.8;
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
let W = 0, H = 0, dpr = 1;
const cam = { x: WORLD_W / 2, y: WORLD_H / 2, scale: 6 };
let minScale = 1, maxScale = 40;
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
const BUILDINGS = [
{ x: 22, y: 15, w: 10, h: 8 }, { x: 60, y: 15, w: 14, h: 10 },
{ x: 45, y: 68, w: 12, h: 10 }, { x: 75, y: 75, w: 8, h: 7 }
];
const WALLS = [
{x: 22, y: 15, w: 10, h: 0.5}, {x: 22, y: 22.5, w: 5, h: 0.5}, {x: 29, y: 22.5, w: 3, h: 0.5}, {x: 22, y: 15, w: 0.5, h: 8}, {x: 31.5, y: 15, w: 0.5, h: 8},
{x: 60, y: 15, w: 14, h: 0.5}, {x: 60, y: 24.5, w: 5, h: 0.5}, {x: 69, y: 24.5, w: 5, h: 0.5}, {x: 60, y: 15, w: 0.5, h: 10}, {x: 73.5, y: 15, w: 0.5, h: 10},
{x: 45, y: 68, w: 12, h: 0.5}, {x: 45, y: 77.5, w: 3, h: 0.5}, {x: 50, y: 77.5, w: 7, h: 0.5}, {x: 45, y: 68, w: 0.5, h: 10}, {x: 56.5, y: 68, w: 0.5, h: 10},
{x: 75, y: 75, w: 8, h: 0.5}, {x: 75, y: 81.5, w: 4, h: 0.5}, {x: 81, y: 81.5, w: 2, h: 0.5}, {x: 75, y: 75, w: 0.5, h: 7}, {x: 82.5, y: 75, w: 0.5, h: 7}
];
let DOORS = [];
function resetDoors() {
DOORS = [
{ x: 27, y: 22.5, w: 2, h: 0.5, hp: 100, maxHp: 100, broken: false },
{ x: 65, y: 24.5, w: 4, h: 0.5, hp: 150, maxHp: 150, broken: false },
{ x: 48, y: 77.5, w: 2, h: 0.5, hp: 100, maxHp: 100, broken: false },
{ x: 79, y: 81.5, w: 2, h: 0.5, hp: 50, maxHp: 50, broken: false }
];
}
let GATE = { x: 0, y: 40, w: 6, h: 20, hp: 1000, maxHp: 1000, broken: false };
let game = {};
let selectedOfficer = null; // 当前被选中的军官
/* ========== 游戏逻辑 ========== */
function initGame() {
game = {
gold: 80, goldTimer: 0,
units: [], zombies: [], corpses: [], floatTexts: [], bullets: [],
zombieTimer: 0, spawnZombieInterval: 8, running: true
};
GATE = { x: 0, y: 40, w: 6, h: 20, hp: 1000, maxHp: 1000, broken: false };
resetDoors();
selectedOfficer = null;
document.getElementById('game-over').style.display = 'none';
updateUI();
}
function recruit(type) {
initAudio();
if (!game.running) return;
let cost = 10;
if (type === 'line') cost = 30;
if (type === 'officer') cost = 50;
if (game.gold < cost) return;
game.gold -= cost;
let hp = 100, atk = 15, range = 3, speed = 1.5, interval = 1.5;
if (type === 'line') { hp = 80; atk = 40; range = 15; speed = 1.0; interval = 14.0; }
if (type === 'officer') { hp = 150; atk = 30; range = 10; speed = 2.0; interval = 6.0; } // 手枪装填6s
game.units.push({
type: type,
x: 12, y: 50 + (Math.random() * 8 - 4),
hp: hp, maxHp: hp, showHpTimer: 0,
atk: atk, scoutRange: 20,
attackRange: range, meleeRange: 3,
speed: speed, cooldown: 0, attackInterval: interval,
angle: 0, isAttacking: false, isDead: false, deathTimer: 0,
isReloading: false, reloadTimer: 0,
moveTarget: null // 仅军官使用
});
updateUI();
}
function spawnZombie() {
for (let i = 0; i < 5; i++) {
const isRunner = Math.random() < 0.3;
let x, y, safe, tries = 0;
do {
x = 60 + Math.random() * 35; y = 10 + Math.random() * 80;
safe = true;
for (let b of BUILDINGS) {
if (x > b.x && x < b.x + b.w && y > b.y && y < b.y + b.h) { safe = false; break; }
}
tries++;
} while (!safe && tries < 50);
game.zombies.push({
type: isRunner ? 'runner' : 'zombie',
x: x, y: y, hp: 60, maxHp: 60, showHpTimer: 0,
atk: 8, scoutRange: 20, attackRange: 2.5,
speed: isRunner ? 3.2 : 1.2,
cooldown: 0, attackInterval: isRunner ? 0.5 : 1.2,
angle: Math.PI, isAttacking: false, isDead: false, deathTimer: 0
});
}
}
function addFloatText(x, y, text, color) {
game.floatTexts.push({ x, y: y - 1.5, text, life: 1.0, color });
}
function createCorpse(x, y, type) {
const drops = [];
for (let i = 0; i < 4; i++) drops.push({ dx: (Math.random()-0.5)*1.5, dy: (Math.random()-0.5)*1.5, r: 0.2 + Math.random()*0.4 });
game.corpses.push({
x, y, type, timer: 0, maxTime: 12,
isTurning: type === 'militia' || type === 'line' || type === 'officer', turnTimer: 10,
rotation: (Math.random() - 0.5) * 0.5, drops
});
}
function resolveCollision(u) {
const r = UNIT_RADIUS;
if (u.y < 40 || u.y > 60) { if (u.x - r < 5) u.x = 5 + r; }
if (!GATE.broken) { if (u.y >= 40 && u.y <= 60) { if (u.x - r < GATE.w) u.x = GATE.w + r; } }
let obstacles = [...WALLS];
if (u.type === 'zombie' || u.type === 'runner') {
for (let d of DOORS) { if (!d.broken) obstacles.push(d); }
}
for (let rect of obstacles) {
const cx = Math.max(rect.x, Math.min(u.x, rect.x + rect.w));
const cy = Math.max(rect.y, Math.min(u.y, rect.y + rect.h));
const dx = u.x - cx, dy = u.y - cy;
const dist = Math.hypot(dx, dy);
if (dist < r) {
if (dist > 0.001) { u.x += (dx / dist) * (r - dist); u.y += (dy / dist) * (r - dist); }
else {
const left = Math.abs(u.x - rect.x), right = Math.abs(u.x - (rect.x + rect.w));
const top = Math.abs(u.y - rect.y), bottom = Math.abs(u.y - (rect.y + rect.h));
const min = Math.min(left, right, top, bottom);
if (min === left) u.x = rect.x - r; else if (min === right) u.x = rect.x + rect.w + r;
else if (min === top) u.y = rect.y - r; else u.y = rect.y + rect.h + r;
}
}
}
}
function updateGame(dt, time) {
if (!game.running) return;
game.goldTimer += dt;
if (game.goldTimer >= 1) { game.gold += 3; game.goldTimer -= 1; updateUI(); }
game.zombieTimer += dt;
if (game.zombieTimer >= game.spawnZombieInterval) { game.zombieTimer = 0; spawnZombie(); }
for (let u of game.units) {
if (u.isDead) continue;
if (u.showHpTimer > 0) u.showHpTimer -= dt;
u.cooldown = Math.max(0, u.cooldown - dt);
u.isAttacking = false;
if ((u.type === 'line' || u.type === 'officer') && u.isReloading) {
u.reloadTimer -= dt;
if (u.reloadTimer <= 0) { u.reloadTimer = 0; u.isReloading = false; }
}
// --- 军官专属移动逻辑 ---
if (u.type === 'officer' && u.moveTarget) {
let dx = u.moveTarget.x - u.x;
let dy = u.moveTarget.y - u.y;
let dist = Math.hypot(dx, dy);
if (dist > 0.5) {
u.x += (dx / dist) * u.speed * dt;
u.y += (dy / dist) * u.speed * dt;
u.angle = Math.atan2(dy, dx);
} else {
u.moveTarget = null; // 到达目的地
}
}
// 索敌
let target = null, minDist = Infinity;
for (let z of game.zombies) {
if (z.isDead) continue;
let d = Math.hypot(z.x - u.x, z.y - u.y);
if (d < u.scoutRange && d < minDist) { minDist = d; target = z; }
}
if (target) {
// 如果军官在移动,依然可以攻击(边走边打)
if (!u.moveTarget) {
u.angle = Math.atan2(target.y - u.y, target.x - u.x);
}
if (u.type === 'militia') {
if (minDist <= u.attackRange) {
u.isAttacking = true;
if (u.cooldown <= 0) {
target.hp -= u.atk; target.showHpTimer = 3.0;
u.cooldown = u.attackInterval;
addFloatText(target.x, target.y, `-${u.atk}`, '#ff5555');
playMeleeSound();
}
} else if (!u.moveTarget) {
u.x += (target.x - u.x) / minDist * u.speed * dt;
u.y += (target.y - u.y) / minDist * u.speed * dt;
}
}
else if (u.type === 'line') {
if (minDist <= u.meleeRange) {
if (u.isReloading) { u.isReloading = false; u.reloadTimer = 0; }
if (u.cooldown <= 0) {
u.isAttacking = true;
target.hp -= 20; target.showHpTimer = 3.0;
u.cooldown = 1.5;
addFloatText(target.x, target.y, `-20`, '#ff5555');
playMeleeSound();
}
} else if (minDist <= u.attackRange) {
if (!u.isReloading && u.cooldown <= 0) {
u.isAttacking = true;
target.hp -= u.atk; target.showHpTimer = 3.0;
u.cooldown = 1.0;
u.isReloading = true; u.reloadTimer = 14.0;
addFloatText(target.x, target.y, `-${u.atk}`, '#ffaa55');
playFireSound();
game.bullets.push({ x: u.x, y: u.y, targetX: target.x, targetY: target.y, life: 0.15 });
}
}
}
else if (u.type === 'officer') {
// 军官自卫逻辑:远近皆可
if (minDist <= u.meleeRange) {
// 近战军刀
if (u.isReloading) { u.isReloading = false; u.reloadTimer = 0; } // 打断装填
if (u.cooldown <= 0) {
u.isAttacking = true;
target.hp -= 30; target.showHpTimer = 3.0;
u.cooldown = 1.2;
addFloatText(target.x, target.y, `-30`, '#ff5555');
playMeleeSound();
}
} else if (minDist <= u.attackRange) {
// 远程手枪
if (!u.isReloading && u.cooldown <= 0) {
u.isAttacking = true;
target.hp -= u.atk; target.showHpTimer = 3.0;
u.cooldown = 0.5;
u.isReloading = true; u.reloadTimer = 6.0; // 6秒装弹
addFloatText(target.x, target.y, `-${u.atk}`, '#ffaa55');
playFireSound();
game.bullets.push({ x: u.x, y: u.y, targetX: target.x, targetY: target.y, life: 0.15 });
}
}
}
}
resolveCollision(u);
}
// 丧尸逻辑
for (let z of game.zombies) {
if (z.isDead) continue;
if (z.showHpTimer > 0) z.showHpTimer -= dt;
z.cooldown = Math.max(0, z.cooldown - dt);
z.isAttacking = false;
let target = null, minDist = Infinity;
for (let u of game.units) {
if (u.isDead) continue;
let d = Math.hypot(u.x - z.x, u.y - z.y);
if (d < z.scoutRange && d < minDist) { minDist = d; target = u; }
}
if (target) {
z.angle = Math.atan2(target.y - z.y, target.x - z.x);
let nearDoor = null;
for (let d of DOORS) {
if (d.broken) continue;
if (Math.hypot((d.x + d.w/2) - z.x, (d.y + d.h/2) - z.y) < 2.5) nearDoor = d;
}
if (nearDoor) {
z.isAttacking = true;
if (z.cooldown <= 0) {
nearDoor.hp -= z.atk; z.cooldown = z.attackInterval;
addFloatText(nearDoor.x + nearDoor.w/2, nearDoor.y, `-${z.atk}`, '#ffaa55');
if (nearDoor.hp <= 0) nearDoor.broken = true;
playZombieSound();
}
} else {
if (minDist <= z.attackRange) {
z.isAttacking = true;
if (z.cooldown <= 0) {
target.hp -= z.atk; target.showHpTimer = 3.0; z.cooldown = z.attackInterval;
addFloatText(target.x, target.y, `-${z.atk}`, '#ffaa55');
playZombieSound();
}
} else {
z.x += (target.x - z.x) / minDist * z.speed * dt;
z.y += (target.y - z.y) / minDist * z.speed * dt;
}
}
} else {
z.angle = Math.atan2(50 - z.y, 6 - z.x);
if (!GATE.broken && z.x - UNIT_RADIUS < GATE.w && z.y > GATE.y && z.y < GATE.y + GATE.h) {
z.isAttacking = true;
if (z.cooldown <= 0) {
GATE.hp -= z.atk; z.cooldown = z.attackInterval;
addFloatText(z.x, z.y, `-${z.atk}`, '#ff5555');
playZombieSound();
if (GATE.hp <= 0) { GATE.hp = 0; GATE.broken = true; game.running = false; document.getElementById('game-over').style.display = 'flex'; }
updateUI();
}
} else {
let dx = 6 - z.x, dy = 50 - z.y;
let dist = Math.hypot(dx, dy);
if (dist > 1) { z.x += (dx / dist) * z.speed * dt * 0.5; z.y += (dy / dist) * z.speed * dt * 0.5; }
}
}
resolveCollision(z);
}
for (let b of game.bullets) b.life -= dt;
game.bullets = game.bullets.filter(b => b.life > 0);
for (let u of game.units) {
if (u.hp <= 0 && !u.isDead) {
u.isDead = true; u.deathTimer = 0; u.hp = 0; createCorpse(u.x, u.y, u.type);
if (selectedOfficer === u) selectedOfficer = null; // 如果军官死了,取消选中
}
if (u.isDead) u.deathTimer += dt;
}
game.units = game.units.filter(u => !(u.isDead && u.deathTimer > 15));
for (let z of game.zombies) {
if (z.hp <= 0 && !z.isDead) { z.isDead = true; z.deathTimer = 0; z.hp = 0; createCorpse(z.x, z.y, z.type); }
if (z.isDead) z.deathTimer += dt;
}
game.zombies = game.zombies.filter(z => !(z.isDead && z.deathTimer > 12));
for (let c of game.corpses) {
c.timer += dt;
if (c.isTurning) {
c.turnTimer -= dt;
if (c.turnTimer <= 0) {
c.isTurning = false; c.maxTime = 0;
game.zombies.push({
type: Math.random() < 0.5 ? 'runner' : 'zombie',
x: c.x, y: c.y, hp: 60, maxHp: 60, showHpTimer: 0,
atk: 8, scoutRange: 20, attackRange: 2.5, speed: 1.5, cooldown: 1.0, attackInterval: 1.0,
angle: Math.PI, isAttacking: false, isDead: false, deathTimer: 0
});
addFloatText(c.x, c.y, "尸变!", "#ff0000");
}
}
}
game.corpses = game.corpses.filter(c => c.timer < c.maxTime);
for (let ft of game.floatTexts) { ft.y -= dt * 2; ft.life -= dt; }
game.floatTexts = game.floatTexts.filter(ft => ft.life > 0);
}
function updateUI() {
document.getElementById('goldTxt').textContent = Math.floor(game.gold);
document.getElementById('btnMilitia').disabled = game.gold < 10 || !game.running;
document.getElementById('btnLine').disabled = game.gold < 30 || !game.running;
document.getElementById('btnOfficer').disabled = game.gold < 50 || !game.running;
document.getElementById('gateHpTxt').textContent = Math.max(0, Math.floor(GATE.hp));
document.getElementById('gate-hp-fill').style.width = Math.max(0, GATE.hp / GATE.maxHp * 100) + '%';
}
/* ========== 绘制:通用模型 ========== */
function drawMilitiaBody() {
ctx.fillStyle = '#8c7a5c'; ctx.strokeStyle = '#3a2a18'; ctx.lineWidth = 0.15;
ctx.beginPath(); ctx.moveTo(-0.5, -1); ctx.lineTo(0.5, -1); ctx.lineTo(0.8, 1); ctx.lineTo(-0.8, 1); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#c9a882'; ctx.beginPath(); ctx.arc(0, -1.4, 0.5, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#4a3420'; ctx.beginPath(); ctx.ellipse(0, -1.5, 0.7, 0.3, 0, 0, Math.PI * 2); ctx.fill();
}
function drawMilitiaWeapon() {
ctx.strokeStyle = '#6b4a2a'; ctx.lineWidth = 0.15; ctx.beginPath(); ctx.moveTo(-0.5, 0); ctx.lineTo(1.8, 0); ctx.stroke();
ctx.fillStyle = '#7a8a8a'; ctx.beginPath(); ctx.moveTo(1.8, -0.15); ctx.lineTo(2.4, 0); ctx.lineTo(1.8, 0.15); ctx.closePath(); ctx.fill();
}
function drawLineBody() {
ctx.fillStyle = '#162744'; ctx.strokeStyle = '#07101a'; ctx.lineWidth = 0.08;
ctx.beginPath(); ctx.moveTo(-0.5, -0.8); ctx.lineTo(0.5, -0.8); ctx.lineTo(0.75, 0.9); ctx.lineTo(-0.75, 0.9); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#9e2424'; ctx.beginPath(); ctx.moveTo(-0.3, -0.8); ctx.lineTo(0.3, -0.8); ctx.lineTo(0, -0.1); ctx.closePath(); ctx.fill();
ctx.strokeStyle = '#e8e8e8'; ctx.lineWidth = 0.08; ctx.beginPath(); ctx.moveTo(-0.35, -0.7); ctx.lineTo(0.35, 0.4); ctx.moveTo(0.35, -0.7); ctx.lineTo(-0.35, 0.4); ctx.stroke();
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0, -1.0, 0.32, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = '#07101a'; ctx.lineWidth = 0.05; ctx.stroke();
ctx.save(); ctx.translate(0, -1.15);
ctx.fillStyle = '#1a1a1a'; ctx.beginPath(); ctx.ellipse(0, 0, 0.6, 0.15, 0, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#2a2a2a'; ctx.beginPath(); ctx.ellipse(0, -0.1, 0.35, 0.12, 0, 0, Math.PI * 2); ctx.fill();
ctx.restore();
}
function drawOfficerBody() {
ctx.fillStyle = '#1a2d52'; ctx.strokeStyle = '#0a1428'; ctx.lineWidth = 0.1;
ctx.beginPath(); ctx.moveTo(-0.55, -0.9); ctx.lineTo(0.55, -0.9); ctx.lineTo(0.8, 0.95); ctx.lineTo(-0.8, 0.95); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.strokeStyle = '#d4af37'; ctx.lineWidth = 0.06;
ctx.beginPath(); ctx.moveTo(-0.55, -0.9); ctx.lineTo(-0.8, 0.95); ctx.moveTo(0.55, -0.9); ctx.lineTo(0.8, 0.95); ctx.stroke();
ctx.fillStyle = '#a32a2a'; ctx.beginPath(); ctx.moveTo(-0.4, -0.9); ctx.lineTo(0.4, -0.9); ctx.lineTo(0.15, -0.2); ctx.lineTo(-0.15, -0.2); ctx.closePath(); ctx.fill();
ctx.fillStyle = '#e8c766'; for (let i = 0; i < 4; i++) { ctx.beginPath(); ctx.arc(0, -0.1 + i * 0.25, 0.06, 0, Math.PI * 2); ctx.fill(); }
ctx.strokeStyle = '#d4af37'; ctx.lineWidth = 0.15; ctx.beginPath(); ctx.moveTo(0.4, -0.8); ctx.lineTo(-0.6, 0.6); ctx.stroke();
ctx.fillStyle = '#e3be9b'; ctx.beginPath(); ctx.arc(0, -1.15, 0.35, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = '#0a1428'; ctx.lineWidth = 0.06; ctx.stroke();
ctx.save(); ctx.translate(0, -1.3);
ctx.fillStyle = '#111'; ctx.strokeStyle = '#000'; ctx.lineWidth = 0.05;
ctx.beginPath(); ctx.ellipse(0, 0, 0.8, 0.18, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.beginPath(); ctx.moveTo(-0.7, 0); ctx.quadraticCurveTo(-0.4, -0.3, 0, -0.2); ctx.quadraticCurveTo(0.4, -0.3, 0.7, 0); ctx.strokeStyle = '#333'; ctx.lineWidth = 0.08; ctx.stroke();
ctx.fillStyle = '#222'; ctx.beginPath(); ctx.ellipse(0, -0.1, 0.5, 0.15, 0, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#f0f0f0'; ctx.beginPath(); ctx.moveTo(0.5, -0.1); ctx.quadraticCurveTo(0.8, -0.5, 1.0, -0.7); ctx.quadraticCurveTo(0.7, -0.4, 0.4, -0.1); ctx.closePath(); ctx.fill();
ctx.fillStyle = '#d4af37'; ctx.beginPath(); ctx.arc(0, 0, 0.1, 0, Math.PI * 2); ctx.fill();
ctx.restore();
}
/* ========== 装填动画(带裁剪,全部生效) ========== */
function drawLineWeaponAndReload(reloadProgress, isReloading) {
const muzzleX = 2.0, breechX = -0.1, buttX = -1.25;
ctx.fillStyle = '#4a4a4a'; ctx.strokeStyle = '#1a1a1a'; ctx.lineWidth = 0.03;
ctx.beginPath(); ctx.rect(0.6, -0.045, 1.4, 0.09); ctx.fill(); ctx.stroke();
ctx.beginPath(); ctx.moveTo(0.6, -0.06); ctx.lineTo(breechX, -0.065); ctx.lineTo(breechX, 0.065); ctx.lineTo(0.6, 0.06); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#6b4226'; ctx.beginPath(); ctx.moveTo(muzzleX, -0.04); ctx.lineTo(muzzleX + 0.3, -0.02); ctx.lineTo(muzzleX + 0.3, 0.02); ctx.lineTo(muzzleX, 0.04); ctx.closePath(); ctx.fill();
ctx.fillStyle = '#d0d0d0'; ctx.beginPath(); ctx.moveTo(muzzleX + 0.3, -0.02); ctx.lineTo(muzzleX + 0.8, 0); ctx.lineTo(muzzleX + 0.3, 0.02); ctx.closePath(); ctx.fill();
ctx.fillStyle = '#583a22'; ctx.strokeStyle = '#221208'; ctx.lineWidth = 0.04;
ctx.beginPath(); ctx.moveTo(0.6, -0.07); ctx.lineTo(breechX, -0.085); ctx.lineTo(-0.35, -0.07); ctx.lineTo(-0.7, -0.09);
ctx.quadraticCurveTo(-1.0, -0.16, buttX, -0.22); ctx.lineTo(buttX - 0.05, -0.06); ctx.quadraticCurveTo(buttX + 0.05, 0.12, -0.9, 0.16);
ctx.lineTo(-0.45, 0.09); ctx.lineTo(breechX, 0.08); ctx.lineTo(0.6, 0.07); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#3a3a3a'; ctx.fillRect(-0.45, 0.02, 0.3, 0.08);
ctx.strokeStyle = '#222'; ctx.lineWidth = 0.04; ctx.beginPath(); ctx.moveTo(-0.25, 0.06); ctx.quadraticCurveTo(-0.2, -0.06, -0.28, -0.09); ctx.stroke();
ctx.fillStyle = '#8a6a30'; ctx.fillRect(0.8, 0.06, 0.1, 0.03); ctx.fillRect(1.5, 0.06, 0.1, 0.03);
if (isReloading) {
const p = reloadProgress;
if (p > 0.85) {
ctx.save(); ctx.translate(muzzleX - 0.2, -0.3);
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0, 0, 0.08, 0, Math.PI*2); ctx.fill();
ctx.fillStyle = '#f0e0c0'; ctx.fillRect(-0.05, -0.15, 0.1, 0.3);
ctx.fillStyle = '#c0a070'; ctx.fillRect(-0.05, -0.15, 0.1, 0.05);
ctx.restore();
}
else if (p > 0.60) {
ctx.save(); ctx.translate(muzzleX, -0.1); ctx.rotate(-Math.PI / 3);
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0, 0.2, 0.08, 0, Math.PI*2); ctx.fill();
ctx.fillStyle = '#f0e0c0'; ctx.fillRect(-0.05, -0.2, 0.1, 0.2);
ctx.fillStyle = '#333';
for(let i=0; i<6; i++) { ctx.fillRect((Math.random()-0.5)*0.1, 0.1 + Math.random()*0.2, 0.03, 0.03); }
ctx.restore();
}
// 通条阶段(使用裁剪)
else if (p > 0.20) {
const isExtracted = p > 0.45;
if (!isExtracted) {
const outProgress = (0.60 - p) / 0.15;
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.035;
ctx.beginPath(); ctx.moveTo(1.8, 0.085 + outProgress * 0.2); ctx.lineTo(0.1, 0.085 + outProgress * 0.2); ctx.stroke();
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(1.8, 0.085 + outProgress * 0.2, 0.07, 0, Math.PI*2); ctx.fill();
} else {
const cycle = (0.45 - p) / 0.25;
// 让冲程看起来更深
const insertDepth = Math.abs(Math.sin(cycle * Math.PI * 3)) * 1.2;
const rodFront = muzzleX - insertDepth;
const rodBack = rodFront + 2.0;
// 裁剪:枪管左侧的东西看不见
ctx.save();
ctx.beginPath();
ctx.rect(muzzleX, -0.2, 3.0, 0.4);
ctx.clip();
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.035;
ctx.beginPath(); ctx.moveTo(rodFront, 0); ctx.lineTo(rodBack, 0); ctx.stroke();
ctx.fillStyle = '#b08a40'; ctx.fillRect(rodFront - 0.04, -0.02, 0.05, 0.04);
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(rodBack - 0.1, 0.05, 0.07, 0, Math.PI * 2); ctx.fill();
ctx.restore();
}
}
else {
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(-0.2, 0.1, 0.08, 0, Math.PI*2); ctx.fill();
}
}
}
function drawCompactPistol() {
const muzzleX = 0.7;
ctx.fillStyle = '#4a2c16'; ctx.strokeStyle = '#1a0d05'; ctx.lineWidth = 0.04;
ctx.beginPath();
ctx.moveTo(0.15, -0.08); ctx.lineTo(-0.1, -0.1);
ctx.quadraticCurveTo(-0.3, -0.1, -0.35, 0.18);
ctx.quadraticCurveTo(-0.35, 0.32, -0.2, 0.35);
ctx.quadraticCurveTo(-0.1, 0.2, 0.0, 0.1);
ctx.lineTo(0.15, 0.08);
ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#505050'; ctx.strokeStyle = '#1a1a1a'; ctx.lineWidth = 0.03;
ctx.beginPath(); ctx.moveTo(0.15, -0.08); ctx.lineTo(muzzleX, -0.06); ctx.lineTo(muzzleX, 0.06); ctx.lineTo(0.15, 0.08); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#2a2a2a'; ctx.fillRect(0.3, -0.08, 0.05, 0.16); ctx.fillRect(0.5, -0.07, 0.05, 0.14);
ctx.fillStyle = '#3a3a3a'; ctx.fillRect(-0.05, 0.04, 0.2, 0.08);
ctx.strokeStyle = '#222'; ctx.lineWidth = 0.05; ctx.beginPath(); ctx.moveTo(0.1, 0.08); ctx.quadraticCurveTo(0.15, 0.22, 0.05, 0.22); ctx.stroke();
ctx.fillStyle = '#111'; ctx.fillRect(0.02, 0.22, 0.06, 0.05); ctx.fillStyle = '#555'; ctx.fillRect(0.15, 0.06, 0.06, 0.04);
ctx.strokeStyle = '#111'; ctx.lineWidth = 0.035; ctx.beginPath(); ctx.arc(-0.08, 0.12, 0.06, 0, Math.PI); ctx.stroke();
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.02; ctx.beginPath(); ctx.moveTo(0.2, 0.085); ctx.lineTo(0.65, 0.085); ctx.stroke();
}
function drawPistolReload(progress) {
const muzzleX = 0.7;
const p = progress;
if (p > 0.8) {
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0.05, 0.22, 0.08, 0, Math.PI*2); ctx.fill();
} else if (p > 0.6) {
const outP = (0.8 - p) / 0.2;
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.03;
ctx.beginPath(); ctx.moveTo(0.2, 0.1); ctx.lineTo(0.6 + outP * 0.3, 0.1); ctx.stroke();
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0.6 + outP * 0.3, 0.1, 0.06, 0, Math.PI*2); ctx.fill();
} else if (p > 0.3) {
const cycle = (0.6 - p) / 0.3;
const insertDepth = Math.abs(Math.sin(cycle * Math.PI * 3)) * 0.35;
const rodFront = muzzleX - insertDepth;
const rodBack = rodFront + 0.7;
ctx.save(); ctx.beginPath(); ctx.rect(muzzleX, -0.2, 2.0, 0.4); ctx.clip();
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.03;
ctx.beginPath(); ctx.moveTo(rodFront, 0); ctx.lineTo(rodBack, 0); ctx.stroke();
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(rodBack - 0.08, 0, 0.07, 0, Math.PI*2); ctx.fill();
ctx.restore();
} else {
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(muzzleX - 0.1, -0.15, 0.06, 0, Math.PI*2); ctx.fill();
ctx.fillStyle = '#f0e0c0'; ctx.fillRect(muzzleX - 0.15, -0.1, 0.08, 0.15);
}
}
function drawSaber() {
ctx.fillStyle = '#5c3a1e'; ctx.fillRect(-0.4, -0.06, 0.4, 0.12);
ctx.strokeStyle = '#d4af37'; ctx.lineWidth = 0.06;
ctx.beginPath(); ctx.arc(0.1, 0, 0.15, -Math.PI / 2, Math.PI / 2); ctx.stroke();
ctx.fillStyle = 'rgba(212, 175, 55, 0.4)';
ctx.beginPath(); ctx.ellipse(0.1, 0, 0.12, 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#e0e0e0'; ctx.strokeStyle = '#777'; ctx.lineWidth = 0.02;
ctx.beginPath(); ctx.moveTo(0.25, -0.04); ctx.lineTo(1.9, -0.015); ctx.lineTo(2.0, 0); ctx.lineTo(1.9, 0.015); ctx.lineTo(0.25, 0.04); ctx.closePath(); ctx.fill(); ctx.stroke();
}
function drawMuzzleFlash() {
ctx.save(); ctx.translate(0.7, 0); ctx.rotate(Math.random() * Math.PI);
ctx.fillStyle = 'rgba(255, 220, 100, 0.9)';
ctx.beginPath(); ctx.moveTo(0, -0.12); ctx.lineTo(-0.35, 0); ctx.lineTo(0, 0.12); ctx.closePath(); ctx.fill();
ctx.fillStyle = 'rgba(220, 220, 220, 0.7)';
ctx.beginPath(); ctx.arc(-0.15, 0, 0.15, 0, Math.PI * 2); ctx.fill();
ctx.restore();
}
function drawSlashArc(progress) {
ctx.save(); ctx.beginPath();
const startAngle = -Math.PI / 3;
const curAngle = startAngle + (Math.PI / 3 + Math.PI / 4) * progress;
ctx.arc(0, 0, 2.0, startAngle, curAngle);
ctx.strokeStyle = `rgba(255, 255, 255, ${1 - progress})`; ctx.lineWidth = 0.15; ctx.stroke();
ctx.strokeStyle = `rgba(200, 230, 255, ${0.5 * (1 - progress)})`; ctx.lineWidth = 0.3; ctx.stroke();
ctx.restore();
}
function drawZombieBody(isRunner) {
ctx.fillStyle = isRunner ? '#6a3030' : '#5a5a50'; ctx.strokeStyle = '#1a1a15'; ctx.lineWidth = 0.15;
ctx.beginPath(); ctx.moveTo(-0.45, -1); ctx.lineTo(0.55, -0.9); ctx.lineTo(0.8, 1); ctx.lineTo(-0.7, 1); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = isRunner ? '#9a6a60' : '#7a8a70'; ctx.beginPath(); ctx.arc(0.1, -1.3, 0.5, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = isRunner ? '#ff0000' : '#ff3a3a'; ctx.beginPath(); ctx.arc(0.4, -1.4, 0.08, 0, Math.PI * 2); ctx.fill();
}
function drawZombieArm(isRunner) {
ctx.fillStyle = isRunner ? '#9a6a60' : '#7a8a70'; ctx.strokeStyle = '#1a1a15'; ctx.lineWidth = 0.15;
ctx.beginPath(); ctx.moveTo(0, -0.15); ctx.lineTo(0.9, -0.2); ctx.lineTo(0.9, 0.15); ctx.lineTo(0, 0.15); ctx.closePath(); ctx.fill(); ctx.stroke();
}
function renderCorpses() {
for (let c of game.corpses) {
const alpha = Math.max(0, 1 - c.timer / c.maxTime);
ctx.fillStyle = `rgba(100, 15, 15, ${alpha * 0.8})`;
for (let d of c.drops) { ctx.beginPath(); ctx.arc(c.x + d.dx, c.y + d.dy, d.r, 0, Math.PI * 2); ctx.fill(); }
const bodyAlpha = Math.max(0, Math.min(1, (c.maxTime - c.timer) / 3));
ctx.globalAlpha = bodyAlpha;
if (c.isTurning && c.turnTimer < 3 && Math.floor(c.timer * 10) % 2 === 0) ctx.globalAlpha = 0.3;
ctx.save(); ctx.translate(c.x, c.y); ctx.rotate(Math.PI / 2 * Math.min(1, c.timer / 0.3) + c.rotation);
const scale = UNIT_SCALE * (c.type === 'line' || c.type === 'officer' ? 1.3 : 1.0);
ctx.scale(scale, scale);
if (c.type === 'line') drawLineBody();
else if (c.type === 'officer') drawOfficerBody();
else if (c.type === 'militia') drawMilitiaBody();
else { drawZombieBody(c.type === 'runner'); }
ctx.restore();
ctx.globalAlpha = 1.0;
}
}
let lastReloadSteps = new Map();
function renderUnits(time) {
ctx.strokeStyle = '#ffccaa'; ctx.lineWidth = 0.2;
for (let b of game.bullets) { ctx.beginPath(); ctx.moveTo(b.x, b.y); ctx.lineTo(b.targetX, b.targetY); ctx.stroke(); }
for (let u of game.units) {
if (u.isDead) continue;
const isOfficer = u.type === 'officer';
const s = UNIT_SCALE * ((u.type === 'line' || isOfficer) ? 1.3 : 1.0);
// 军官选中高亮
if (isOfficer && selectedOfficer === u) {
ctx.strokeStyle = '#d4af37'; ctx.lineWidth = 0.15;
ctx.beginPath(); ctx.ellipse(u.x, u.y + 0.5, 1.2, 0.6, 0, 0, Math.PI*2); ctx.stroke();
}
// 身体
ctx.save(); ctx.translate(u.x, u.y); ctx.scale(s, s);
if (u.type === 'line') drawLineBody();
else if (isOfficer) drawOfficerBody();
else drawMilitiaBody();
ctx.restore();
// 武器与动作
ctx.save();
ctx.translate(u.x, u.y - 0.4 * s);
const strike = u.isAttacking ? Math.max(0, Math.sin(time * 8)) : 0;
if (isOfficer) {
const reloadProgress = u.isReloading ? (u.reloadTimer / 6.0) : 0;
const usePistol = (u.isReloading || (u.isAttacking && u.cooldown < 0.4));
if (usePistol) {
ctx.rotate(u.angle);
ctx.scale(s, s);
drawCompactPistol();
if (u.isReloading) drawPistolReload(reloadProgress);
if (u.isAttacking && u.cooldown > 0.2) drawMuzzleFlash();
} else {
// 军刀挥砍
if (u.isAttacking) {
const slashProg = 1 - (u.cooldown / 1.2);
ctx.rotate(u.angle - 0.5 + slashProg * 1.0);
drawSlashArc(slashProg);
} else {
ctx.rotate(u.angle);
}
ctx.scale(s, s);
drawSaber();
}
} else {
ctx.rotate(u.angle);
if (u.isAttacking) ctx.translate(strike * 0.8 * s, 0);
ctx.scale(s, s);
if (u.type === 'line') {
const reloadProgress = u.isReloading ? (u.reloadTimer / 14.0) : 0;
if (u.isReloading) {
ctx.rotate(0.18 * reloadProgress);
let step = '';
if (reloadProgress > 0.85) step = 'paper';
else if (reloadProgress > 0.60) step = 'powder';
else if (reloadProgress > 0.20) step = 'ramrod';
else if (reloadProgress > 0.05) step = 'click';
if (step && lastReloadSteps.get(u) !== step) { lastReloadSteps.set(u, step); playReloadStepSound(step); }
} else { lastReloadSteps.delete(u); }
drawLineWeaponAndReload(reloadProgress, u.isReloading);
} else {
drawMilitiaWeapon();
}
}
ctx.restore();
if (u.showHpTimer > 0) drawHealthBar(u.x, u.y - 2.2 * s, u.hp / u.maxHp, isOfficer ? '#d4af37' : (u.type==='line' ? '#4a9aba' : '#4a7a9a'));
}
for (let z of game.zombies) {
if (z.isDead) continue;
const isRunner = z.type === 'runner';
const sway = Math.sin(time * (isRunner ? 12 : 4) + z.x) * (isRunner ? 0.25 : 0.15);
const leftStrike = z.isAttacking ? Math.max(0, Math.sin(time * (isRunner?20:5) + z.x)) * 0.7 : 0.1;
const rightStrike = z.isAttacking ? Math.max(0, Math.sin(time * (isRunner?20:5) + Math.PI + z.x)) * 0.7 : 0.1;
ctx.save(); ctx.translate(z.x + sway, z.y); if (Math.cos(z.angle) < 0) ctx.scale(-1, 1); ctx.scale(UNIT_SCALE, UNIT_SCALE); drawZombieBody(isRunner); ctx.restore();
ctx.save(); ctx.translate(z.x - 0.3 * UNIT_SCALE, z.y - 0.6 * UNIT_SCALE); ctx.rotate(z.angle); ctx.translate(leftStrike * UNIT_SCALE, 0); ctx.scale(UNIT_SCALE, UNIT_SCALE); drawZombieArm(isRunner); ctx.restore();
ctx.save(); ctx.translate(z.x + 0.3 * UNIT_SCALE, z.y - 0.6 * UNIT_SCALE); ctx.rotate(z.angle); ctx.translate(rightStrike * UNIT_SCALE, 0); ctx.scale(UNIT_SCALE, UNIT_SCALE); drawZombieArm(isRunner); ctx.restore();
if (z.showHpTimer > 0) drawHealthBar(z.x, z.y - 2.2 * UNIT_SCALE, z.hp / z.maxHp, '#7a3a3a');
}
for (let ft of game.floatTexts) {
ctx.fillStyle = ft.color; ctx.font = 'bold 2.5px "Noto Serif SC", serif'; ctx.textAlign = 'center';
ctx.globalAlpha = ft.life; ctx.fillText(ft.text, ft.x, ft.y); ctx.globalAlpha = 1.0;
}
}
function drawHealthBar(x, y, pct, color) {
if (cam.scale < 3) return;
const w = 1.8, h = 0.3;
ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(x - w/2, y, w, h);
ctx.fillStyle = pct > 0.5 ? color : (pct > 0.25 ? '#d1a13a' : '#c13a3a');
ctx.fillRect(x - w/2, y, w * pct, h);
}
/* ========== 主循环与地图 ========== */
let lastTime = performance.now();
function gameLoop(now) {
const dt = Math.min((now - lastTime) / 1000, 0.1);
const time = now / 1000;
lastTime = now;
updateGame(dt, time);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = '#0d0a07'; ctx.fillRect(0, 0, W, H);
ctx.save();
ctx.translate(W / 2, H / 2); ctx.scale(cam.scale, cam.scale); ctx.translate(-cam.x, -cam.y);
drawWorld();
renderCorpses();
renderUnits(time);
ctx.restore();
requestAnimationFrame(gameLoop);
}
function drawWorld() {
ctx.fillStyle = '#c2b68c'; ctx.fillRect(0, 0, WORLD_W, WORLD_H);
ctx.strokeStyle = 'rgba(140, 128, 80, 0.25)'; ctx.lineWidth = 0.3;
for (let y = 5; y < WORLD_H; y += 6) {
for (let x = 15; x < WORLD_W; x += 8) {
const off = (x + y) % 3;
ctx.beginPath(); ctx.moveTo(x + off, y); ctx.lineTo(x + off + 5, y); ctx.stroke();
}
}
ctx.fillStyle = '#8c7a5c'; ctx.fillRect(0, 0, 5, WORLD_H);
ctx.strokeStyle = '#5a4529'; ctx.lineWidth = 0.5; ctx.beginPath(); ctx.moveTo(5, 0); ctx.lineTo(5, WORLD_H); ctx.stroke();
ctx.fillStyle = 'rgba(90, 110, 110, 0.4)'; ctx.fillRect(5, 0, 5, 40); ctx.fillRect(5, 60, 5, 40);
ctx.strokeStyle = '#7d9db0'; ctx.strokeRect(5, 0, 5, 40); ctx.strokeRect(5, 60, 5, 40);
ctx.fillStyle = '#9b8a6b'; ctx.fillRect(5, 40, 5, 20);
if (!GATE.broken) {
ctx.fillStyle = '#3b2a18'; ctx.fillRect(GATE.x, GATE.y, GATE.w, GATE.h);
ctx.strokeStyle = '#d9b36c'; ctx.lineWidth = 0.8; ctx.strokeRect(GATE.x, GATE.y, GATE.w, GATE.h);
} else {
ctx.fillStyle = '#2a1a10'; ctx.fillRect(GATE.x, GATE.y, GATE.w, GATE.h);
}
ctx.fillStyle = '#6b4a2a'; ctx.fillRect(0, 38, 6, 2); ctx.fillRect(0, 60, 6, 2);
ctx.fillStyle = '#a89a7a';
ctx.beginPath(); ctx.moveTo(10, 44); ctx.lineTo(WORLD_W, 40); ctx.lineTo(WORLD_W, 60); ctx.lineTo(10, 56); ctx.closePath(); ctx.fill();
ctx.strokeStyle = 'rgba(90, 70, 40, 0.5)'; ctx.beginPath(); ctx.moveTo(10, 44); ctx.lineTo(WORLD_W, 40); ctx.moveTo(10, 56); ctx.lineTo(WORLD_W, 60); ctx.stroke();
BUILDINGS.forEach(b => { ctx.fillStyle = '#b8a889'; ctx.fillRect(b.x, b.y, b.w, b.h); });
ctx.fillStyle = '#4a3420';
WALLS.forEach(w => { ctx.fillRect(w.x, w.y, w.w, w.h); });
DOORS.forEach(d => {
if (d.broken) { ctx.fillStyle = '#3a2a18'; ctx.fillRect(d.x, d.y, d.w, d.h); }
else { ctx.fillStyle = '#8a5a1e'; ctx.fillRect(d.x, d.y, d.w, d.h); }
});
drawInteriors();
drawGrid();
}
function drawInteriors() {
ctx.strokeStyle = '#4a3420'; ctx.lineWidth = 0.6;
ctx.beginPath(); ctx.moveTo(26, 15); ctx.lineTo(26, 19); ctx.moveTo(22, 19); ctx.lineTo(26, 19); ctx.stroke();
ctx.beginPath(); ctx.moveTo(51, 68); ctx.lineTo(51, 78); ctx.moveTo(51, 73); ctx.lineTo(57, 73); ctx.stroke();
ctx.beginPath(); ctx.moveTo(79, 75); ctx.lineTo(79, 78); ctx.moveTo(79, 80); ctx.lineTo(79, 82); ctx.stroke();
}
function drawGrid() {
ctx.save(); ctx.strokeStyle = 'rgba(70, 55, 35, 0.25)'; ctx.lineWidth = 1 / cam.scale; ctx.setLineDash([1 / cam.scale, 1 / cam.scale]);
ctx.beginPath();
for (let x = 0; x <= WORLD_W; x++) { ctx.moveTo(x, 0); ctx.lineTo(x, WORLD_H); }
for (let y = 0; y <= WORLD_H; y++) { ctx.moveTo(0, y); ctx.lineTo(WORLD_W, y); }
ctx.stroke(); ctx.restore();
}
/* ========== 交互控制 ========== */
const pointers = new Map();
let dragStart = null, pinch = null;
let downPos = null;
function getPos(e) { const r = canvas.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; }
canvas.addEventListener('pointerdown', e => {
initAudio();
canvas.setPointerCapture(e.pointerId);
const p = getPos(e); pointers.set(e.pointerId, p);
downPos = { x: p.x, y: p.y, t: Date.now(), camX: cam.x, camY: cam.y };
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinch = { dist: Math.hypot(b.x - a.x, b.y - a.y), scale: cam.scale, worldMid: screenToWorld((a.x+b.x)/2, (a.y+b.y)/2) };
dragStart = null;
}
});
canvas.addEventListener('pointermove', e => {
if (!pointers.has(e.pointerId)) return;
const p = getPos(e); pointers.set(e.pointerId, p);
if (pointers.size === 1 && downPos) {
const dx = p.x - downPos.x;
const dy = p.y - downPos.y;
if (Math.hypot(dx, dy) > 5) {
// 拖拽视野
cam.x = downPos.camX - dx / cam.scale;
cam.y = downPos.camY - dy / cam.scale;
clampCam();
downPos.isDragging = true;
}
} else if (pointers.size === 2 && pinch) {
const [a, b] = [...pointers.values()];
cam.scale = clamp(pinch.scale * (Math.hypot(b.x - a.x, b.y - a.y) / pinch.dist), minScale, maxScale);
cam.x = pinch.worldMid.x - ((a.x+b.x)/2 - W / 2) / cam.scale;
cam.y = pinch.worldMid.y - ((a.y+b.y)/2 - H / 2) / cam.scale;
clampCam();
}
});
function endPointer(e) {
if (!pointers.has(e.pointerId)) return;
pointers.delete(e.pointerId);
if (pointers.size === 0) {
if (downPos && !downPos.isDragging && Date.now() - downPos.t < 400) {
handleClick(getPos(e));
}
downPos = null; pinch = null;
} else if (pointers.size === 1) {
const p = [...pointers.values()][0];
downPos = { x: p.x, y: p.y, t: Date.now(), camX: cam.x, camY: cam.y };
pinch = null;
}
}
canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);
canvas.addEventListener('wheel', e => {
e.preventDefault();
const p = getPos(e);
const w = screenToWorld(p.x, p.y);
const factor = e.deltaY < 0 ? 1.15 : 1 / 1.15;
cam.scale = clamp(cam.scale * factor, minScale, maxScale);
cam.x = w.x - (p.x - W / 2) / cam.scale;
cam.y = w.y - (p.y - H / 2) / cam.scale;
clampCam();
}, { passive: false });
function handleClick(p) {
const w = screenToWorld(p.x, p.y);
// 1. 先判断是否点到了军官
let clickedOfficer = null;
for (let u of game.units) {
if (u.type === 'officer' && !u.isDead) {
if (Math.hypot(u.x - w.x, u.y - w.y) < 2.0) {
clickedOfficer = u;
break;
}
}
}
if (clickedOfficer) {
selectedOfficer = clickedOfficer;
} else if (selectedOfficer) {
// 如果已经选中了军官,且点在其他地方 -> 命令移动
selectedOfficer.moveTarget = { x: w.x, y: w.y };
selectedOfficer = null; // 下达命令后取消选中
}
}
function screenToWorld(sx, sy) { return { x: (sx - W / 2) / cam.scale + cam.x, y: (sy - H / 2) / cam.scale + cam.y }; }
function clampCam() {
const hw = W / 2 / cam.scale, hh = H / 2 / cam.scale;
cam.x = WORLD_W <= hw * 2 ? WORLD_W / 2 : clamp(cam.x, hw, WORLD_W - hw);
cam.y = WORLD_H <= hh * 2 ? WORLD_H / 2 : clamp(cam.y, hh, WORLD_H - hh);
}
function resize() {
dpr = window.devicePixelRatio || 1;
W = window.innerWidth; H = window.innerHeight;
canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr);
canvas.style.width = W + 'px'; canvas.style.height = H + 'px';
}
function fitView() {
const s = Math.min(W / WORLD_W, H / WORLD_H) * 0.9;
minScale = Math.min(W, H) / Math.max(WORLD_W, WORLD_H) * 0.4;
maxScale = 40;
cam.scale = s; cam.x = WORLD_W / 2; cam.y = WORLD_H / 2;
}
document.getElementById('btnReset').addEventListener('click', fitView);
window.addEventListener('resize', () => { resize(); clampCam(); });
/* ========== 启动 ========== */
resize(); fitView();
initGame();
requestAnimationFrame(gameLoop);
</script>
</body>
</html>Game Source: 圣安托万门外 · 防御战 (军官与通条穿透)
Creator: EpicCoder88
Libraries: none
Complexity: complex (1066 lines, 46.4 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: game-epiccoder88-mwg7y87q" to link back to the original. Then publish at arcadelab.ai/publish.