圣安托万门外 · 血肉增殖
by EpicCoder881606 lines70.9 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; }
#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-wrapper {
position: fixed; left: 12px; bottom: 20px;
z-index: 10; display: flex; flex-direction: column;
}
#ui-toggle {
width: 60px; height: 60px;
background: #5b3b22; color: #f0c674;
border: 2px solid #a57843; border-radius: 8px;
font-family: inherit; font-size: 24px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
box-shadow: 0 4px 6px rgba(0,0,0,0.5);
transition: background 0.2s;
}
#ui-toggle:hover { background: #714b2b; }
#ui-panel {
display: none;
background: rgba(20,16,12,.95);
border: 1px solid #5a4529; border-radius: 6px;
padding: 10px; width: 220px;
margin-bottom: 10px;
}
#ui-panel.active { display: block; }
#ui-panel .gold { color: #f0c674; font-size: 15px; margin-bottom: 8px; text-align: center; }
#ui-panel .gold span { font-size: 20px; font-weight: bold; }
#ui-panel 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;
}
#ui-panel button:hover { background: #714b2b; }
#ui-panel button:disabled { background: #3b2a18; color: #7a6a50; border-color: #4a3420; cursor: not-allowed; }
#ui-panel button span.price { color: #ffaa55; font-size: 12px; }
#btnReset {
position: fixed; right: 12px; top: 12px;
width: auto; padding: 8px 12px;
background: #5b3b22; color: #f4dfb4; border: 1px solid #a57843; border-radius: 4px;
font-family: inherit; cursor: pointer; 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-wrapper">
<div id="ui-panel">
<div class="gold">国库:<span id="goldTxt">0</span></div>
<button id="btnMilitia" onclick="recruit('militia')">招募民兵 <span class="price">10</span></button>
<button id="btnPrisoner" onclick="recruit('prisoner')" style="border-color: #8c2a2a; color: #ff8888;">招募英军俘虏 <span class="price" style="color:#ff8888;">20</span></button>
<button id="btnLine" onclick="recruit('line')">招募线列步兵 <span class="price">30</span></button>
<button id="btnMerchant" onclick="recruit('merchant')" style="border-color: #4a9aba; color: #aaffaa;">招募商人 <span class="price" style="color:#aaffaa;">40</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="ui-toggle">📜</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 selectedUnit = null;
let clickedOfficer = null;
let uiBtns = [];
/* ========== UI 折叠控制 ========== */
const uiPanel = document.getElementById('ui-panel');
const uiToggle = document.getElementById('ui-toggle');
uiToggle.addEventListener('click', () => {
uiPanel.classList.toggle('active');
});
/* ========== 游戏逻辑 ========== */
function initGame() {
game = {
gold: 100, goldTimer: 0,
units: [], zombies: [], bandits: [], corpses: [], floatTexts: [], bullets: [], meats: [],
zombieTimer: 0, spawnZombieInterval: 10,
banditTimer: 15, spawnBanditInterval: 20,
running: true
};
GATE = { x: 0, y: 40, w: 6, h: 20, hp: 1000, maxHp: 1000, broken: false };
resetDoors();
selectedUnit = null;
document.getElementById('game-over').style.display = 'none';
updateUI();
}
function recruit(type) {
initAudio();
if (!game.running) return;
let cost = 10;
if (type === 'prisoner') cost = 20;
if (type === 'line') cost = 30;
if (type === 'merchant') cost = 40;
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 === 'prisoner') { hp = 90; atk = 35; range = 15; speed = 1.8; interval = 14.0; }
if (type === 'line') { hp = 80; atk = 40; range = 15; speed = 1.0; interval = 14.0; }
if (type === 'merchant') { hp = 80; atk = 12; range = 2; speed = 2.0; interval = 1.2; }
if (type === 'officer') { hp = 150; atk = 30; range = 10; speed = 2.5; interval = 6.0; }
let unit = {
faction: 'human', type: type,
x: 12, y: 50 + (Math.random() * 8 - 4),
hp: hp, maxHp: hp, showHpTimer: 0,
atk: atk, scoutRange: 20,
attackRange: range, meleeRange: 2.5,
speed: speed, cooldown: 0, attackInterval: interval,
angle: 0, isAttacking: false, isDead: false, deathTimer: 0,
isReloading: false, reloadTimer: 0,
moveTarget: null,
squad: [], tactic: 'free',
protectors: [],
hasAmmo: true,
virusTimer: 0 // 血肉感染倒计时
};
if (type === 'officer') unit.chargeTimer = 0;
game.units.push(unit);
updateUI();
}
function spawnZombie() {
for (let i = 0; i < 5; i++) {
const rand = Math.random();
let zType = 'zombie';
let hp = 60, atk = 8, speed = 1.2, range = 1.5, interval = 1.2;
if (rand < 0.15) { // 15% 血尸
zType = 'blood';
hp = 65; atk = 5; speed = 0.7; range = 1.5; interval = 1.5;
} else if (rand < 0.40) { // 25% 跑尸
zType = 'runner';
speed = 3.2; interval = 0.5;
}
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({
faction: 'zombie', type: zType,
x: x, y: y, hp: hp, maxHp: hp, showHpTimer: 0,
atk: atk, scoutRange: 20, attackRange: range,
speed: speed,
cooldown: 0, attackInterval: interval,
angle: Math.PI, isAttacking: false, isDead: false, deathTimer: 0
});
}
}
function spawnBandit() {
const count = 1 + Math.floor(Math.random() * 5);
for (let i = 0; i < count; i++) {
const rand = Math.random();
let bType = 'knife';
let interval = 1.5;
let range = 3;
if (rand > 0.7) { bType = 'rifle'; interval = 14.0; range = 15; }
else if (rand > 0.4) { bType = 'pistol'; interval = 6.0; range = 10; }
let x, y, safe, tries = 0;
do {
x = 30 + Math.random() * 60; y = 10 + Math.random() * 80;
safe = true;
if (x < 6) safe = false;
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.bandits.push({
faction: 'bandit', type: bType,
x: x, y: y, hp: 60, maxHp: 60, showHpTimer: 0,
atk: bType === 'knife' ? 15 : 30,
scoutRange: 20, attackRange: range, meleeRange: 2.5,
speed: 1.6, cooldown: 0, attackInterval: interval,
angle: Math.PI, isAttacking: false, isDead: false, deathTimer: 0,
isReloading: false, reloadTimer: 0, virusTimer: 0
});
}
}
function addFloatText(x, y, text, color) {
game.floatTexts.push({ x, y: y - 1.5, text, life: 1.0, color });
}
function createCorpse(x, y, faction, 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, faction, type, timer: 0, maxTime: 12,
isTurning: faction === 'human' || faction === 'bandit', 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.faction !== 'human') {
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 findClosestEnemy(u, possibleTargets) {
let target = null, minDist = Infinity;
for (let t of possibleTargets) {
if (t.isDead) continue;
let d = Math.hypot(t.x - u.x, t.y - u.y);
if (d < u.scoutRange && d < minDist) {
if (t.type === 'officer' || t.type === 'merchant') d *= 0.4;
minDist = d;
target = t;
}
}
return { target, minDist };
}
function updateGame(dt, time) {
if (!game.running) return;
let merchantCount = game.units.filter(u => u.type === 'merchant' && !u.isDead).length;
game.goldTimer += dt;
if (game.goldTimer >= 1) {
game.gold += 3 + (merchantCount * 4);
game.goldTimer -= 1;
updateUI();
}
game.zombieTimer += dt;
if (game.zombieTimer >= game.spawnZombieInterval) { game.zombieTimer = 0; spawnZombie(); }
game.banditTimer += dt;
if (game.banditTimer >= game.spawnBanditInterval) { game.banditTimer = 0; spawnBandit(); }
// --- 感染倒计时 ---
for (let u of [...game.units, ...game.bandits]) {
if (u.isDead) continue;
if (u.virusTimer > 0) {
u.virusTimer -= dt;
if (u.virusTimer <= 0) {
u.hp = 0; // 3秒后直接死亡,触发接下来的尸变逻辑
addFloatText(u.x, u.y, "感染尸变!", "#ff3333");
}
}
}
// --- 血肉实体更新 ---
for (let i = game.meats.length - 1; i >= 0; i--) {
let meat = game.meats[i];
meat.life -= dt;
// 命中检测(在存活期间一直检测)
if (!meat.hit) {
// 检测人类/匪徒
let hitHuman = false;
for (let u of [...game.units, ...game.bandits]) {
if (!u.isDead && Math.hypot(u.x - meat.x, u.y - meat.y) < 0.8) {
u.virusTimer = 3.0; // 感染3秒
u.showHpTimer = 3.0;
hitHuman = true;
break;
}
}
// 检测丧尸
let hitZombie = false;
for (let z of game.zombies) {
if (!z.isDead && Math.hypot(z.x - meat.x, z.y - meat.y) < 0.8) {
if (z.type === 'blood') {
// 击中血尸:增殖
z.hp = Math.min(z.maxHp, z.hp + 20);
addFloatText(z.x, z.y, "+20", "#55ff55");
// 半径2格召唤普通僵尸
for(let k=0; k<1; k++) {
let px = z.x + (Math.random()-0.5)*4;
let py = z.y + (Math.random()-0.5)*4;
game.zombies.push({
faction: 'zombie', type: 'zombie',
x: px, y: py, hp: 60, maxHp: 60, showHpTimer: 0,
atk: 8, scoutRange: 20, attackRange: 1.5, speed: 1.2, cooldown: 0, attackInterval: 1.2,
angle: Math.PI, isAttacking: false, isDead: false, deathTimer: 0
});
addFloatText(px, py, "增殖!", "#ff5555");
}
} else {
// 击中普通丧尸:回血
z.hp = Math.min(z.maxHp, z.hp + 20);
addFloatText(z.x, z.y, "+20", "#55ff55");
}
hitZombie = true;
break;
}
}
if (hitHuman || hitZombie) meat.hit = true; // 击中后不再重复判定
}
if (meat.life <= 0) {
game.meats.splice(i, 1);
}
}
// 编队与保镖逻辑
for (let u of game.units) {
if (u.isDead) continue;
if (u.type === 'officer') {
if (u.squad.length < 6) {
for (let other of game.units) {
if ((other.type === 'line' || other.type === 'prisoner') && !other.isDead && !other.commander) {
let d = Math.hypot(other.x - u.x, other.y - u.y);
if (d < 15) {
other.commander = u;
u.squad.push(other);
if (u.squad.length >= 6) break;
}
}
}
}
u.squad = u.squad.filter(s => !s.isDead);
if (u.chargeTimer > 0) {
u.chargeTimer -= dt;
if (u.chargeTimer <= 0) u.tactic = 'free';
}
}
if (u.type === 'merchant') {
if (u.protectors.length < 4) {
for (let other of game.units) {
if (other.type === 'militia' && !other.isDead && !other.merchantOwner) {
let d = Math.hypot(other.x - u.x, other.y - u.y);
if (d < 15 && Math.random() < 0.4) {
other.merchantOwner = u;
u.protectors.push(other);
}
}
}
}
u.protectors = u.protectors.filter(s => !s.isDead);
}
}
const allUnits = [...game.units, ...game.zombies, ...game.bandits];
for (let u of allUnits) {
if (u.isDead) continue;
if (u.showHpTimer > 0) u.showHpTimer -= dt;
u.cooldown = Math.max(0, u.cooldown - dt);
u.isAttacking = false;
// 感染状态下不能攻击,只能等死
if (u.virusTimer > 0) {
resolveCollision(u);
continue;
}
if (u.isReloading) {
let stopReload = false;
if (u.faction === 'human' && (u.type === 'line' || u.type === 'prisoner') && u.commander && u.commander.tactic === 'charge') stopReload = true;
if (!stopReload) {
u.reloadTimer -= dt;
if (u.reloadTimer <= 0) {
u.reloadTimer = 0;
u.isReloading = false;
if (u.type === 'prisoner') u.hasAmmo = true;
}
}
}
if (u.faction === 'human' && (u.type === 'officer' || u.type === 'merchant') && u.moveTarget) {
let dx = u.moveTarget.x - u.x, dy = u.moveTarget.y - u.y, dist = Math.hypot(dx, dy);
if (dist > 0.5) {
let spd = u.speed * (u.tactic === 'charge' ? 1.5 : 1.0);
u.x += (dx / dist) * spd * dt; u.y += (dy / dist) * spd * dt;
u.angle = Math.atan2(dy, dx);
} else u.moveTarget = null;
}
// 编队阵列
if (u.faction === 'human' && (u.type === 'line' || u.type === 'prisoner') && u.commander && !u.commander.isDead) {
let idx = u.commander.squad.indexOf(u);
if (idx !== -1) {
let targetX = u.commander.x;
let targetY = u.commander.y + (u.commander.tactic === 'charge' ? 1.5 + idx * 1.2 : 2.5 + idx * 1.5);
let dx = targetX - u.x, dy = targetY - u.y, dist = Math.hypot(dx, dy);
if (dist > 0.2) {
let spd = dist > 3 ? 4.0 : 2.0;
u.x += (dx / dist) * spd * dt; u.y += (dy / dist) * spd * dt;
}
u.angle = u.commander.angle;
}
} else if (u.faction === 'human' && u.type === 'militia' && u.merchantOwner && !u.merchantOwner.isDead) {
let idx = u.merchantOwner.protectors.indexOf(u);
if (idx !== -1) {
let offsetX = (idx % 2 === 0 ? -1 : 1) * 1.5;
let offsetY = (idx < 2 ? -1 : 1) * 1.5;
let targetX = u.merchantOwner.x + offsetX;
let targetY = u.merchantOwner.y + offsetY;
let dx = targetX - u.x, dy = targetY - u.y, dist = Math.hypot(dx, dy);
let hasEnemyNear = false;
for (let z of [...game.zombies, ...game.bandits]) {
if (!z.isDead && Math.hypot(z.x - u.x, z.y - u.y) < 10) { hasEnemyNear = true; break; }
}
if (dist > 0.5 && !hasEnemyNear) {
let spd = dist > 3 ? 3.0 : 1.5;
u.x += (dx / dist) * spd * dt; u.y += (dy / dist) * spd * dt;
}
}
}
// 索敌
let possibleTargets = [];
if (u.faction === 'human') possibleTargets = [...game.zombies, ...game.bandits];
else if (u.faction === 'zombie') possibleTargets = [...game.units, ...game.bandits];
else if (u.faction === 'bandit') possibleTargets = [...game.units, ...game.zombies];
let { target, minDist } = findClosestEnemy(u, possibleTargets);
if (target) {
if (u.faction === 'human') {
if (!u.moveTarget && (!u.commander || u.type === 'officer')) {
u.angle = Math.atan2(target.y - u.y, target.x - u.x);
}
} else {
u.angle = Math.atan2(target.y - u.y, target.x - u.x);
}
// 攻击逻辑
if (u.faction === 'human') {
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.merchantOwner || minDist < 8)) {
u.x += (target.x - u.x) / minDist * u.speed * dt; u.y += (target.y - u.y) / minDist * u.speed * dt;
}
}
else if (u.type === 'prisoner') {
if (u.hasAmmo) {
if (minDist <= u.attackRange && minDist > u.meleeRange) {
u.isAttacking = true;
if (u.cooldown <= 0) {
target.hp -= u.atk; target.showHpTimer = 3.0; u.cooldown = 1.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, color: '#ffccaa' });
u.hasAmmo = false;
}
} else if (minDist <= u.meleeRange) {
u.isAttacking = true;
if (u.cooldown <= 0) {
target.hp -= u.atk; target.showHpTimer = 3.0; u.cooldown = 1.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, color: '#ffccaa' });
u.hasAmmo = false;
}
}
} else {
if (minDist <= u.meleeRange + 0.5) {
u.isAttacking = true;
if (u.cooldown <= 0) {
let dmg = 25;
target.hp -= dmg;
target.showHpTimer = 3.0;
u.cooldown = 1.2;
addFloatText(target.x, target.y, `-${dmg}`, '#ff5555');
playMeleeSound();
if (target.hp <= 0) {
u.isReloading = true;
u.reloadTimer = 14.0;
}
}
} else {
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') {
let tactic = u.commander ? u.commander.tactic : 'free';
if (tactic === 'charge') {
if (minDist <= u.meleeRange + 0.5 && u.cooldown <= 0) {
u.isAttacking = true; target.hp -= 40; target.showHpTimer = 3.0; u.cooldown = 1.0;
addFloatText(target.x, target.y, `-40`, '#ffaa55'); playMeleeSound();
}
} else {
if (minDist <= u.meleeRange) {
if (u.isReloading && !u.commander) { u.isReloading = false; u.reloadTimer = 0; }
if (u.cooldown <= 0 && !u.isReloading) {
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 && !u.isReloading && u.cooldown <= 0) {
let canFire = true;
if (tactic === 'volley' && u.commander) {
canFire = u.commander.squad.every(s => !s.isReloading && s.cooldown <= 0 && !s.isDead);
}
if (canFire) {
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, color: '#ffccaa' });
}
}
}
}
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 && !u.isReloading && u.cooldown <= 0 && u.tactic !== 'charge') {
u.isAttacking = true; target.hp -= u.atk; target.showHpTimer = 3.0; u.cooldown = 0.5;
u.isReloading = true; u.reloadTimer = 6.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, color: '#ffccaa' });
}
}
else if (u.type === 'merchant') {
if (minDist <= u.attackRange) {
if (u.cooldown <= 0) {
u.isAttacking = true; target.hp -= u.atk; target.showHpTimer = 3.0; u.cooldown = u.attackInterval;
addFloatText(target.x, target.y, `-${u.atk}`, '#ff5555'); playMeleeSound();
}
}
}
}
else if (u.faction === 'zombie') {
let isTargetNearDoor = false;
for (let d of DOORS) {
if (d.broken) continue;
if (Math.hypot((d.x + d.w/2) - u.x, (d.y + d.h/2) - u.y) < 2.5) isTargetNearDoor = true;
}
if (isTargetNearDoor) {
u.isAttacking = true;
if (u.cooldown <= 0) {
for (let d of DOORS) {
if (!d.broken && Math.hypot((d.x + d.w/2) - u.x, (d.y + d.h/2) - u.y) < 2.5) {
d.hp -= u.atk; addFloatText(d.x + d.w/2, d.y, `-${u.atk}`, '#ffaa55');
if (d.hp <= 0) d.broken = true;
}
}
u.cooldown = u.attackInterval; playZombieSound();
}
} else {
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}`, '#ffaa55'); playZombieSound();
}
} else {
u.x += (target.x - u.x) / minDist * u.speed * dt; u.y += (target.y - u.y) / minDist * u.speed * dt;
}
}
}
else if (u.faction === 'bandit') {
if (u.type === 'knife') {
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}`, '#ffaa55'); playMeleeSound();
}
} else {
u.x += (target.x - u.x) / minDist * u.speed * dt; u.y += (target.y - u.y) / minDist * u.speed * dt;
}
} else {
if (minDist <= u.meleeRange && !u.isReloading) {
if (u.cooldown <= 0) {
u.isAttacking = true; target.hp -= 15; target.showHpTimer = 3.0; u.cooldown = 1.5;
addFloatText(target.x, target.y, `-15`, '#ff5555'); playMeleeSound();
}
} else if (minDist <= u.attackRange && !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 = u.attackInterval;
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, color: '#ff9966' });
}
}
}
} else {
if (u.faction === 'zombie') {
u.angle = Math.atan2(50 - u.y, 6 - u.x);
if (!GATE.broken && u.x - UNIT_RADIUS < GATE.w && u.y > GATE.y && u.y < GATE.y + GATE.h) {
u.isAttacking = true;
if (u.cooldown <= 0) {
GATE.hp -= u.atk; u.cooldown = u.attackInterval;
addFloatText(u.x, u.y, `-${u.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 - u.x, dy = 50 - u.y; let dist = Math.hypot(dx, dy);
if (dist > 1) { u.x += (dx / dist) * u.speed * dt * 0.5; u.y += (dy / dist) * u.speed * dt * 0.5; }
}
} else if (u.faction === 'bandit') {
if (!u.wanderTarget || Math.hypot(u.x - u.wanderTarget.x, u.y - u.wanderTarget.y) < 1) {
u.wanderTarget = { x: 20 + Math.random() * 60, y: 20 + Math.random() * 60 };
}
let dx = u.wanderTarget.x - u.x, dy = u.wanderTarget.y - u.y, dist = Math.hypot(dx, dy);
u.angle = Math.atan2(dy, dx);
u.x += (dx / dist) * u.speed * 0.5 * dt; u.y += (dy / dist) * u.speed * 0.5 * dt;
}
}
resolveCollision(u);
}
for (let b of game.bullets) b.life -= dt;
game.bullets = game.bullets.filter(b => b.life > 0);
const cleanupFaction = (arr, faction) => {
for (let u of arr) {
if (u.hp <= 0 && !u.isDead) {
u.isDead = true; u.deathTimer = 0; u.hp = 0; createCorpse(u.x, u.y, faction, u.type);
// 血尸死亡掉落血肉
if (u.type === 'blood') {
let dropCount = 3 + Math.floor(Math.random() * 5); // 3-7块
for (let i = 0; i < dropCount; i++) {
let angle = Math.random() * Math.PI * 2;
let dist = 1 + Math.random() * 2; // 半径3格内
game.meats.push({
x: u.x + Math.cos(angle) * dist,
y: u.y + Math.sin(angle) * dist,
life: 3.0,
hit: false
});
}
}
if (u.type === 'officer') {
if (selectedUnit === u) selectedUnit = null;
if (clickedOfficer === u) clickedOfficer = null;
u.squad.forEach(s => s.commander = null);
}
if (u.type === 'merchant') {
if (selectedUnit === u) selectedUnit = null;
u.protectors.forEach(s => s.merchantOwner = null);
}
if ((u.type === 'line' || u.type === 'prisoner') && u.commander) u.commander.squad = u.commander.squad.filter(s => s !== u);
if (u.type === 'militia' && u.merchantOwner) u.merchantOwner.protectors = u.merchantOwner.protectors.filter(s => s !== u);
}
if (u.isDead) u.deathTimer += dt;
}
return arr.filter(u => !(u.isDead && u.deathTimer > 12));
};
game.units = cleanupFaction(game.units, 'human');
game.zombies = cleanupFaction(game.zombies, 'zombie');
game.bandits = cleanupFaction(game.bandits, 'bandit');
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({
faction: 'zombie', type: Math.random() < 0.5 ? 'runner' : 'zombie',
x: c.x, y: c.y, hp: 60, maxHp: 60, showHpTimer: 0,
atk: 8, scoutRange: 20, attackRange: 1.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('btnPrisoner').disabled = game.gold < 20 || !game.running;
document.getElementById('btnLine').disabled = game.gold < 30 || !game.running;
document.getElementById('btnMerchant').disabled = game.gold < 40 || !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 drawPrisonerBody() {
ctx.fillStyle = '#8c2a2a';
ctx.strokeStyle = '#3a0a0a'; 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 = '#0d0a07';
ctx.beginPath(); ctx.moveTo(-0.75, 0.9); ctx.lineTo(-0.5, 0.8); ctx.lineTo(-0.2, 0.9); ctx.lineTo(0.1, 0.75); ctx.lineTo(0.4, 0.9); ctx.lineTo(0.75, 0.9); ctx.lineTo(0.75, 1.1); ctx.lineTo(-0.75, 1.1); ctx.fill();
ctx.fillStyle = '#1a2d52'; 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 = '#b8b8a0'; 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 = 'rgba(60, 40, 20, 0.6)'; ctx.beginPath(); ctx.ellipse(-0.3, 0.2, 0.15, 0.1, 0.4, 0, Math.PI*2); ctx.fill();
ctx.fillStyle = 'rgba(80, 15, 15, 0.6)'; ctx.beginPath(); ctx.ellipse(0.2, 0.4, 0.12, 0.08, -0.3, 0, Math.PI*2); ctx.fill();
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(0, -1.0, 0.32, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = '#3a0a0a'; ctx.lineWidth = 0.05; ctx.stroke();
ctx.fillStyle = '#4a3420'; ctx.beginPath(); ctx.ellipse(-0.2, -0.9, 0.1, 0.15, 0.5, 0, Math.PI*2); ctx.fill(); ctx.beginPath(); ctx.ellipse(0.2, -0.9, 0.1, 0.15, -0.5, 0, Math.PI*2); ctx.fill();
ctx.save(); ctx.translate(0, -1.15); ctx.rotate(-0.1);
ctx.fillStyle = '#1a1a1a'; ctx.strokeStyle = '#000'; ctx.lineWidth = 0.05; ctx.beginPath(); ctx.ellipse(0, 0, 0.6, 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.beginPath(); ctx.moveTo(-0.6, 0); ctx.quadraticCurveTo(-0.3, -0.15, 0, -0.1); ctx.quadraticCurveTo(0.4, -0.3, 0.6, 0); ctx.strokeStyle = '#333'; ctx.lineWidth = 0.06; ctx.stroke();
ctx.fillStyle = '#2a2a2a'; ctx.beginPath(); ctx.ellipse(0, -0.05, 0.3, 0.1, 0, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#0d0a07'; ctx.beginPath(); ctx.arc(-0.2, -0.05, 0.05, 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 drawMerchantBody() {
ctx.fillStyle = '#2b4a2b'; ctx.strokeStyle = '#0f1a0f'; ctx.lineWidth = 0.1;
ctx.beginPath(); ctx.moveTo(-0.5, -0.9); ctx.lineTo(0.5, -0.9); ctx.lineTo(0.75, 0.95); ctx.lineTo(-0.75, 0.95); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#6b4a2a'; ctx.beginPath(); ctx.ellipse(-0.45, 0.2, 0.25, 0.35, 0.2, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#c9a882'; ctx.beginPath(); ctx.arc(0, -1.1, 0.32, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#4a3420'; ctx.beginPath(); ctx.ellipse(0, -1.2, 0.5, 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#3a2410'; ctx.beginPath(); ctx.ellipse(0, -1.3, 0.3, 0.15, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
}
function drawBanditBody() {
ctx.fillStyle = '#4a4035'; ctx.strokeStyle = '#1a1510'; ctx.lineWidth = 0.1;
ctx.beginPath(); ctx.moveTo(-0.5, -0.9); ctx.lineTo(0.5, -0.9); ctx.lineTo(0.7, 0.95); ctx.lineTo(-0.7, 0.95); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.strokeStyle = '#2a2015'; ctx.lineWidth = 0.1; ctx.beginPath(); ctx.moveTo(-0.4, -0.5); ctx.lineTo(0.4, 0.5); ctx.stroke();
ctx.fillStyle = '#c9a882'; ctx.beginPath(); ctx.arc(0, -1.1, 0.32, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#111'; ctx.beginPath(); ctx.ellipse(0, -1.2, 0.45, 0.25, 0, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = '#c9a882'; ctx.fillRect(-0.15, -1.2, 0.3, 0.1);
}
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 drawBanditKnife() {
ctx.fillStyle = '#3a2a18'; ctx.fillRect(-0.2, -0.05, 0.2, 0.1);
ctx.fillStyle = '#999'; ctx.strokeStyle = '#444'; ctx.lineWidth = 0.02;
ctx.beginPath(); ctx.moveTo(0, -0.08); ctx.lineTo(0.8, -0.12); ctx.lineTo(1.1, 0); ctx.lineTo(0.8, 0.06); ctx.lineTo(0, 0.05); 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 drawLongMuzzleFlash() {
ctx.save(); ctx.translate(2.8, 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, isBlood) {
ctx.fillStyle = isBlood ? '#4a1a1a' : (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 = isBlood ? '#a07a70' : (isRunner ? '#9a6a60' : '#7a8a70');
ctx.beginPath(); ctx.arc(0.1, -1.3, 0.5, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
// 红眼
ctx.fillStyle = isBlood ? '#ff00ff' : (isRunner ? '#ff0000' : '#ff3a3a');
ctx.beginPath(); ctx.arc(0.4, -1.4, 0.08, 0, Math.PI * 2); ctx.fill();
// 血尸身上的血泡
if (isBlood) {
ctx.fillStyle = '#d93030';
for(let i=0; i<4; i++) {
ctx.beginPath(); ctx.arc(-0.2 + Math.random()*0.4, -0.5 + Math.random()*0.8, 0.1, 0, Math.PI*2); ctx.fill();
}
}
}
function drawZombieArm(isRunner, isBlood) {
ctx.fillStyle = isBlood ? '#a07a70' : (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.faction === 'human' && (c.type === 'line' || c.type === 'officer') ? 1.3 : 1.0);
ctx.scale(scale, scale);
if (c.faction === 'human') {
if (c.type === 'line') drawLineBody();
else if (c.type === 'officer') drawOfficerBody();
else if (c.type === 'merchant') drawMerchantBody();
else if (c.type === 'prisoner') drawPrisonerBody();
else drawMilitiaBody();
} else if (c.faction === 'bandit') {
drawBanditBody();
} else {
drawZombieBody(c.type === 'runner', c.type === 'blood');
}
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.strokeStyle = b.color || '#ffccaa';
ctx.beginPath(); ctx.moveTo(b.x, b.y); ctx.lineTo(b.targetX, b.targetY); ctx.stroke();
}
// 渲染血肉
for (let meat of game.meats) {
ctx.fillStyle = meat.hit ? 'rgba(100, 20, 20, 0.5)' : '#b02020';
ctx.beginPath(); ctx.arc(meat.x, meat.y, 0.35, 0, Math.PI * 2); ctx.fill();
ctx.strokeStyle = '#500'; ctx.lineWidth = 0.05; ctx.stroke();
// 血肉飞溅轨迹
if (!meat.hit) {
ctx.fillStyle = 'rgba(180, 30, 30, 0.4)';
ctx.beginPath(); ctx.arc(meat.x - 0.1, meat.y - 0.1, 0.5, 0, Math.PI * 2); ctx.fill();
}
}
const drawEntity = (u) => {
if (u.isDead) return;
const isOfficer = u.type === 'officer';
const isMerchant = u.type === 'merchant';
const isPrisoner = u.type === 'prisoner';
const isBandit = u.faction === 'bandit';
const isBlood = u.type === 'blood';
const s = UNIT_SCALE * ((u.type === 'line' || isOfficer || isPrisoner) ? 1.3 : 1.0);
if ((isOfficer || isMerchant) && selectedUnit === u) {
ctx.strokeStyle = isOfficer ? '#d4af37' : '#aaffaa'; 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.virusTimer > 0 && Math.floor(time * 10) % 2 === 0) {
ctx.globalAlpha = 0.5;
}
if (u.faction === 'human') {
if (u.type === 'line') drawLineBody();
else if (isOfficer) drawOfficerBody();
else if (isMerchant) drawMerchantBody();
else if (isPrisoner) drawPrisonerBody();
else drawMilitiaBody();
} else if (isBandit) {
drawBanditBody();
} else {
const isRunner = u.type === 'runner';
const sway = Math.sin(time * (isRunner ? 12 : 4) + u.x) * (isRunner ? 0.25 : 0.15);
ctx.translate(sway, 0);
if (Math.cos(u.angle) < 0) ctx.scale(-1, 1);
drawZombieBody(isRunner, isBlood);
}
ctx.globalAlpha = 1.0;
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)) && u.tactic !== 'charge';
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 if (isMerchant) {
ctx.rotate(u.angle); if (u.isAttacking) ctx.translate(strike*0.8, 0); ctx.scale(s, s);
drawBanditKnife();
} else if (isBandit) {
if (u.type === 'knife') {
ctx.rotate(u.angle); if (u.isAttacking) ctx.translate(strike*0.8, 0); ctx.scale(s, s);
drawBanditKnife();
} else if (u.type === 'pistol') {
ctx.rotate(u.angle); ctx.scale(s, s);
drawCompactPistol();
if (u.isReloading) drawPistolReload(u.reloadTimer / 6.0);
if (u.isAttacking && u.cooldown > 0.2) drawMuzzleFlash();
} else if (u.type === 'rifle') {
ctx.rotate(u.angle); ctx.scale(s, s);
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);
if (u.isAttacking && u.cooldown > 0.2) drawLongMuzzleFlash();
}
} else if (u.faction === 'human') {
let tactic = u.commander ? u.commander.tactic : 'free';
if (isPrisoner) {
if (u.hasAmmo) {
ctx.rotate(u.angle); if (u.isAttacking) ctx.translate(strike*0.8*s, 0); ctx.scale(s, s);
drawLineWeaponAndReload(0, false);
if (u.isAttacking && u.cooldown > 0.2) drawLongMuzzleFlash();
} else {
ctx.rotate(u.angle); if (u.isAttacking) ctx.translate(strike*0.8*s, 0); ctx.scale(s, s);
const isActivelyReloading = u.isReloading;
drawLineWeaponAndReload(u.reloadTimer / 14.0, isActivelyReloading);
}
} else if (u.type === 'line' && u.commander && tactic === 'charge') {
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 && tactic !== 'charge') {
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 && tactic !== 'charge');
if (u.isAttacking && u.cooldown > 0.2) drawLongMuzzleFlash();
} else {
drawMilitiaWeapon();
}
}
} else if (u.faction === 'zombie') {
const isRunner = u.type === 'runner';
const leftStrike = u.isAttacking ? Math.max(0, Math.sin(time * (isRunner?20:5) + u.x)) * 0.7 : 0.1;
const rightStrike = u.isAttacking ? Math.max(0, Math.sin(time * (isRunner?20:5) + Math.PI + u.x)) * 0.7 : 0.1;
ctx.save(); ctx.translate(-0.3 * s, -0.6 * s); ctx.rotate(u.angle); ctx.translate(leftStrike * s, 0); ctx.scale(s, s); drawZombieArm(isRunner, isBlood); ctx.restore();
ctx.save(); ctx.translate(0.3 * s, -0.6 * s); ctx.rotate(u.angle); ctx.translate(rightStrike * s, 0); ctx.scale(s, s); drawZombieArm(isRunner, isBlood); ctx.restore();
}
ctx.restore();
if (u.showHpTimer > 0 || u.virusTimer > 0) {
let color = '#4a7a9a';
if (isOfficer) color = '#d4af37';
else if (isMerchant) color = '#aaffaa';
else if (u.type === 'line') color = '#4a9aba';
else if (isPrisoner) color = '#ff5555';
else if (isBandit) color = '#c9a882';
else if (u.faction === 'zombie') color = isBlood ? '#a02020' : '#7a3a3a';
if (u.virusTimer <= 0) {
drawHealthBar(u.x, u.y - 2.2 * s, u.hp / u.maxHp, color);
}
}
};
game.units.forEach(drawEntity);
game.bandits.forEach(drawEntity);
game.zombies.forEach(drawEntity);
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);
}
/* ========== UI 绘制与点击 ========== */
function drawTacticalUI() {
if (!clickedOfficer || clickedOfficer.isDead) return;
const screenX = (clickedOfficer.x - cam.x) * cam.scale + W / 2;
const screenY = (clickedOfficer.y - cam.y) * cam.scale + H / 2;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const uiW = 180, uiH = 60;
const startX = screenX - uiW / 2;
const startY = screenY - 120;
ctx.fillStyle = 'rgba(20,16,12,0.95)'; ctx.strokeStyle = '#d4af37'; ctx.lineWidth = 2;
ctx.beginPath(); ctx.roundRect(startX, startY, uiW, uiH, 8); ctx.fill(); ctx.stroke();
const btnW = 50, btnH = 30, gap = 10;
const btnY = startY + 15;
const btnStartX = startX + gap;
uiBtns = [
{ id: 'volley', x: btnStartX, y: btnY, w: btnW, h: btnH, label: '齐射' },
{ id: 'free', x: btnStartX + btnW + gap, y: btnY, w: btnW, h: btnH, label: '自由' },
{ id: 'charge', x: btnStartX + (btnW + gap)*2, y: btnY, w: btnW, h: btnH, label: '冲锋' }
];
uiBtns.forEach(btn => {
const isActive = clickedOfficer.tactic === btn.id;
ctx.fillStyle = isActive ? '#a57843' : '#3a2a18';
ctx.strokeStyle = isActive ? '#f4dfb4' : '#5a4529'; ctx.lineWidth = 1;
ctx.beginPath(); ctx.roundRect(btn.x, btn.y, btn.w, btn.h, 4); ctx.fill(); ctx.stroke();
ctx.fillStyle = isActive ? '#fff' : '#c9b489';
ctx.font = 'bold 14px "Noto Serif SC", serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(btn.label, btn.x + btn.w/2, btn.y + btn.h/2 + 2);
});
}
/* ========== 主循环与地图 ========== */
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();
drawTacticalUI();
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) {
for (let btn of uiBtns) {
if (p.x >= btn.x && p.x <= btn.x + btn.w && p.y >= btn.y && p.y <= btn.y + btn.h) {
if (clickedOfficer) {
clickedOfficer.tactic = btn.id;
if (btn.id === 'charge') clickedOfficer.chargeTimer = 20;
}
return;
}
}
const w = screenToWorld(p.x, p.y);
let hitUnit = null;
for (let u of game.units) {
if ((u.type === 'officer' || u.type === 'merchant') && !u.isDead) {
if (Math.hypot(u.x - w.x, u.y - w.y) < 2.5) { hitUnit = u; break; }
}
}
if (hitUnit) {
if (hitUnit.type === 'officer' && clickedOfficer === hitUnit && Date.now() - (hitUnit.lastClickTime || 0) < 400) {
selectedUnit = hitUnit;
} else {
selectedUnit = hitUnit;
clickedOfficer = null;
}
hitUnit.lastClickTime = Date.now();
uiBtns = [];
} else if (selectedUnit) {
selectedUnit.moveTarget = { x: w.x, y: w.y };
selectedUnit = null;
clickedOfficer = null;
uiBtns = [];
} else {
clickedOfficer = null;
uiBtns = [];
}
}
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 (1606 lines, 70.9 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-mwhfq0m4" to link back to the original. Then publish at arcadelab.ai/publish.