圣安托万门外 · 军官指挥测试版
by EpicCoder88949 lines43.8 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; }
#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 {
position: fixed; left: 12px; bottom: 20px;
background: rgba(20,16,12,.9);
border: 1px solid #5a4529; border-radius: 6px;
padding: 10px; width: 220px;
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; }
/* 军官战术面板 (固定大小 10x5 比例,绝对定位) */
#officer-menu {
display: none; position: fixed;
background: rgba(26, 20, 14, 0.95);
border: 2px solid #d9b36c; border-radius: 6px;
padding: 5px; z-index: 100;
width: 150px; height: 80px; /* 约 10x5 的视觉比例 */
flex-direction: column; gap: 4px;
box-shadow: 0 4px 12px rgba(0,0,0,0.8);
transform: translate(-50%, -120%);
}
#officer-menu button {
padding: 4px; font-size: 12px; margin: 0; justify-content: center;
background: #3b2a18; border-color: #5a4529;
}
#officer-menu button:hover { background: #5b3b22; border-color: #d9b36c; }
#officer-menu button.active { background: #a57843; color: #111; font-weight: bold; }
#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')">招募军官 <span class="price">50 金币</span></button>
</div>
<button id="btnReset">重置视野</button>
<div id="game-over">巴黎沦陷</div>
<div id="officer-menu">
<button id="btnVolley" onclick="setOfficerMode('volley')">齐射 (Volley)</button>
<button id="btnFree" onclick="setOfficerMode('free')">自由开火</button>
<button id="btnCharge" onclick="setOfficerMode('charge')">冲锋 (Charge)</button>
</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 playSound(type) {
if (!audioCtx) return;
const t = audioCtx.currentTime;
if (type === 'fire') {
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.3, t); gain.gain.exponentialRampToValueAtTime(0.01, t + 0.3);
noise.connect(filter).connect(gain).connect(audioCtx.destination); noise.start(t);
} else if (type === 'melee') {
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);
} else if (type === 'zombie') {
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);
}
}
/* ========== 基础数据 ========== */
const WORLD_W = 100, WORLD_H = 100, UNIT_SCALE = 1.0, 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 = [], GATE = {}, game = {};
let selectedUnit = null; // 当前选中的单位
let lastOfficerClickTime = 0;
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 }
];
}
function initGame() {
game = { gold: 100, 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();
document.getElementById('game-over').style.display = 'none';
document.getElementById('officer-menu').style.display = 'none';
selectedUnit = null;
updateUI();
}
function recruit(type) {
initAudio();
if (!game.running) return;
const cost = type === 'militia' ? 10 : (type === 'line' ? 30 : 50);
if (game.gold < cost) return;
game.gold -= cost;
let u = {
type: type, x: 12, y: 50 + (Math.random() * 8 - 4),
hp: type === 'line' ? 80 : (type === 'officer' ? 120 : 100), maxHp: type === 'line' ? 80 : (type === 'officer' ? 120 : 100),
showHpTimer: 0, atk: type === 'militia' ? 15 : 40,
scoutRange: 20, attackRange: type === 'line' ? 15 : 3, meleeRange: 3,
speed: type === 'militia' ? 1.5 : 1.0, cooldown: 0,
attackInterval: type === 'militia' ? 1.5 : (type === 'line' ? 14.0 : 6.0),
angle: 0, isAttacking: false, isDead: false, deathTimer: 0,
isReloading: false, reloadTimer: 0
};
if (type === 'officer') {
u.squad = []; // 部下数组
u.mode = 'free'; // 'free', 'volley', 'charge'
u.chargeTimer = 0;
u.targetPos = null; // 移动指令目标点
}
game.units.push(u);
updateUI();
}
function setOfficerMode(mode) {
if (!selectedUnit || selectedUnit.type !== 'officer' || selectedUnit.isDead) return;
selectedUnit.mode = mode;
if (mode === 'volley') {
// 齐射:强制全员开火
selectedUnit.squad.forEach(s => {
if (!s.isDead) {
let target = findNearestZombie(s);
if (target) {
target.hp -= s.atk; target.showHpTimer = 3.0;
addFloatText(target.x, target.y, `-${s.atk}`, '#ffaa55');
playSound('fire');
game.bullets.push({ x: s.x, y: s.y, targetX: target.x, targetY: target.y, life: 0.15 });
}
s.isReloading = true; s.reloadTimer = 14.0; // 齐射完强制重新装填
}
});
selectedUnit.mode = 'free'; // 齐射只打一轮,立刻恢复自由开火
} else if (mode === 'charge') {
selectedUnit.chargeTimer = 20.0; // 20秒冲锋
}
updateOfficerMenu();
}
function findNearestZombie(u) {
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; }
}
return target;
}
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 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, 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.type !== 'officer') continue;
if (u.showHpTimer > 0) u.showHpTimer -= dt;
u.cooldown = Math.max(0, u.cooldown - dt);
u.isAttacking = false;
// 1. 移动指令处理
if (u.targetPos) {
let dx = u.targetPos.x - u.x, dy = u.targetPos.y - u.y;
let dist = Math.hypot(dx, dy);
if (dist > 0.2) {
let spd = u.mode === 'charge' ? u.speed * 2 : u.speed; // 冲锋加速
u.x += (dx / dist) * spd * dt;
u.y += (dy / dist) * spd * dt;
} else {
u.targetPos = null; // 到达
}
}
// 2. 冲锋计时器
if (u.mode === 'charge') {
u.chargeTimer -= dt;
if (u.chargeTimer <= 0) { u.mode = 'free'; updateOfficerMenu(); }
}
// 3. 自卫寻敌(手枪/军刀)
let target = findNearestZombie(u);
if (target) {
u.angle = Math.atan2(target.y - u.y, target.x - u.x);
let dist = Math.hypot(target.x - u.x, target.y - u.y);
if (u.cooldown <= 0 && !u.isReloading) {
u.isAttacking = true;
if (dist > 3 && dist <= u.attackRange) {
// 手枪射击
u.isReloading = true; u.reloadTimer = 6.0;
u.cooldown = 0.5;
target.hp -= 25; target.showHpTimer = 3;
addFloatText(target.x, target.y, '-25', '#ffaa55');
playSound('fire');
game.bullets.push({ x: u.x, y: u.y, targetX: target.x, targetY: target.y, life: 0.15 });
} else if (dist <= 3) {
// 军刀近战
u.cooldown = 1.2;
target.hp -= 20; target.showHpTimer = 3;
addFloatText(target.x, target.y, '-20', '#ff5555');
playSound('melee');
}
}
}
if (u.isReloading) {
u.reloadTimer -= dt;
if (u.reloadTimer <= 0) u.isReloading = false;
}
// 4. 集结阵型逻辑
// 寻找最多7个落单的线列步兵
for (let s of game.units) {
if (s.type === 'line' && !s.isDead && s.squadId === undefined && u.squad.length < 7) {
let d = Math.hypot(s.x - u.x, s.y - u.y);
if (d < 15) {
s.squadId = u.id || (u.id = Math.random()); // 分配小队ID
u.squad.push(s);
}
}
}
// 剔除已死亡的队员
u.squad = u.squad.filter(s => !s.isDead);
// 排阵 (军官在最右侧,即 x 最大,沿 y 轴展开)
for (let i = 0; i < u.squad.length; i++) {
let s = u.squad[i];
// 目标位置:军官左边(-x方向),依次排开
let targetX = u.x - 2.5;
let targetY = u.y + (i - Math.floor(u.squad.length / 2)) * 1.8;
// 平滑移动过去
let dx = targetX - s.x, dy = targetY - s.y;
let dist = Math.hypot(dx, dy);
if (dist > 0.2) {
s.x += (dx / dist) * s.speed * 1.5 * dt;
s.y += (dy / dist) * s.speed * 1.5 * dt;
}
resolveCollision(s);
}
resolveCollision(u);
}
// ================= 其他单位逻辑 =================
for (let u of game.units) {
if (u.isDead || u.type === 'officer') continue;
if (u.showHpTimer > 0) u.showHpTimer -= dt;
u.cooldown = Math.max(0, u.cooldown - dt);
u.isAttacking = false;
if (u.type === 'line' && u.isReloading) {
u.reloadTimer -= dt;
if (u.reloadTimer <= 0) { u.reloadTimer = 0; u.isReloading = false; }
}
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 (u.squadId !== undefined) {
if (target) u.angle = Math.atan2(target.y - u.y, target.x - u.x);
// 寻找对应的军官
let myOfficer = game.units.find(off => off.type === 'officer' && off.id === u.squadId && !off.isDead);
if (myOfficer) {
if (myOfficer.mode === 'charge') {
// 冲锋状态:不顾一切往前冲,近战伤害翻倍
if (target) {
if (minDist <= u.meleeRange) {
u.isAttacking = true;
if (u.cooldown <= 0) {
target.hp -= 40; target.showHpTimer = 3; u.cooldown = 1.0; // 快砍
addFloatText(target.x, target.y, `-40`, '#ff3333');
playSound('melee');
}
} else {
u.x += (target.x - u.x) / minDist * u.speed * 2 * dt;
u.y += (target.y - u.y) / minDist * u.speed * 2 * dt;
}
} else {
// 没敌人就朝军官面向的方向冲
u.x += Math.cos(u.angle) * u.speed * 2 * dt;
u.y += Math.sin(u.angle) * u.speed * 2 * dt;
}
} else {
// 正常状态 (齐射/自由开火),不主动移动,只射击
if (target) {
if (minDist <= u.meleeRange) {
// 近战
if (u.cooldown <= 0) {
u.isAttacking = true; u.isReloading = false; u.reloadTimer = 0; // 打断装填
target.hp -= 20; target.showHpTimer = 3; u.cooldown = 1.5;
addFloatText(target.x, target.y, `-20`, '#ff5555');
playSound('melee');
}
} else if (minDist <= u.attackRange) {
// 远程自由开火
if (!u.isReloading && u.cooldown <= 0) {
u.isAttacking = true;
target.hp -= u.atk; target.showHpTimer = 3;
u.cooldown = 1.0; u.isReloading = true; u.reloadTimer = 14.0;
addFloatText(target.x, target.y, `-${u.atk}`, '#ffaa55');
playSound('fire');
game.bullets.push({ x: u.x, y: u.y, targetX: target.x, targetY: target.y, life: 0.15 });
}
}
}
}
}
} else {
// 伞兵(未编队)状态,保持原有逻辑
if (target) {
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; u.cooldown = u.attackInterval;
addFloatText(target.x, target.y, `-${u.atk}`, '#ff5555'); playSound('melee');
}
} 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') {
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; u.cooldown = 1.5;
addFloatText(target.x, target.y, `-20`, '#ff5555'); playSound('melee');
}
} else if (minDist <= u.attackRange && !u.isReloading && u.cooldown <= 0) {
u.isAttacking = true; target.hp -= u.atk; target.showHpTimer = 3; u.cooldown = 1.0;
u.isReloading = true; u.reloadTimer = 14.0;
addFloatText(target.x, target.y, `-${u.atk}`, '#ffaa55'); playSound('fire');
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 && 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;
if (nearDoor.hp <= 0) nearDoor.broken = true;
playSound('zombie');
}
} else {
if (minDist <= z.attackRange) {
z.isAttacking = true;
if (z.cooldown <= 0) {
target.hp -= z.atk; target.showHpTimer = 3; z.cooldown = z.attackInterval;
addFloatText(target.x, target.y, `-${z.atk}`, '#ffaa55'); playSound('zombie');
}
} 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; playSound('zombie');
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, 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 ft of game.floatTexts) { ft.y -= dt * 2; ft.life -= dt; }
game.floatTexts = game.floatTexts.filter(ft => ft.life > 0);
for (let u of game.units) {
if (u.hp <= 0 && !u.isDead) {
u.isDead = true; u.hp = 0;
if (u.type === 'officer') {
// 军官阵亡,解除全队编队
u.squad.forEach(s => s.squadId = undefined);
if (selectedUnit === u) { selectedUnit = null; document.getElementById('officer-menu').style.display = 'none'; }
}
}
}
game.units = game.units.filter(u => !(u.isDead && u.deathTimer > 15));
for (let z of game.zombies) { if (z.hp <= 0) z.isDead = true; }
game.zombies = game.zombies.filter(z => !z.isDead);
// 更新UI面板位置
if (selectedUnit && !selectedUnit.isDead) {
let sx = (selectedUnit.x - cam.x) * cam.scale + W / 2;
let sy = (selectedUnit.y - cam.y) * cam.scale + H / 2;
let menu = document.getElementById('officer-menu');
menu.style.left = sx + 'px';
menu.style.top = sy + 'px';
}
}
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 updateOfficerMenu() {
if (!selectedUnit) return;
document.getElementById('btnVolley').classList.remove('active');
document.getElementById('btnFree').classList.remove('active');
document.getElementById('btnCharge').classList.remove('active');
if (selectedUnit.mode === 'charge') document.getElementById('btnCharge').classList.add('active');
else document.getElementById('btnFree').classList.add('active');
}
/* ========== 绘制模块 ========== */
// (保留之前的线列步兵、民兵、丧尸绘制函数)
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 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 rodProgress = Math.abs(Math.sin(cycle * Math.PI * 3)) * 1.4;
const rodFront = muzzleX + 0.3 - rodProgress; const rodBack = muzzleX - 2.0;
ctx.strokeStyle = '#8c6a4a'; ctx.lineWidth = 0.035; ctx.beginPath(); ctx.moveTo(rodBack, 0); ctx.lineTo(rodFront, 0); ctx.stroke();
ctx.fillStyle = '#b08a40'; ctx.fillRect(rodFront - 0.04, -0.02, 0.05, 0.04);
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(rodFront + 0.1, 0.05, 0.07, 0, Math.PI * 2); ctx.fill();
}
} else {
ctx.fillStyle = '#d4b494'; ctx.beginPath(); ctx.arc(-0.2, 0.1, 0.08, 0, Math.PI*2); ctx.fill();
}
}
}
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.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.restore();
}
function drawCompactPistol() {
const muzzleX = 0.75, buttX = -0.45;
ctx.fillStyle = '#583a22'; ctx.strokeStyle = '#221208'; ctx.lineWidth = 0.04;
ctx.beginPath(); ctx.moveTo(0.1, -0.06); ctx.quadraticCurveTo(buttX + 0.1, -0.12, buttX, -0.2); ctx.lineTo(buttX - 0.03, -0.05); ctx.quadraticCurveTo(buttX + 0.1, 0.08, 0.1, 0.08); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#5a5a5a'; ctx.strokeStyle = '#1a1a1a'; ctx.lineWidth = 0.03;
ctx.beginPath(); ctx.moveTo(0.1, -0.06); ctx.lineTo(muzzleX, -0.04); ctx.lineTo(muzzleX, 0.04); ctx.lineTo(0.1, 0.07); ctx.closePath(); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#2a2a2a'; ctx.fillRect(0.3, -0.07, 0.04, 0.14); ctx.fillRect(0.55, -0.05, 0.04, 0.1);
ctx.fillStyle = '#3a3a3a'; ctx.fillRect(-0.05, 0.02, 0.15, 0.06);
ctx.strokeStyle = '#222'; ctx.lineWidth = 0.04; ctx.beginPath(); ctx.moveTo(0.08, 0.06); ctx.quadraticCurveTo(0.12, 0.15, 0.05, 0.16); ctx.stroke();
ctx.fillStyle = '#444'; ctx.fillRect(0.1, 0.03, 0.04, 0.03);
ctx.strokeStyle = '#111'; ctx.lineWidth = 0.03; ctx.beginPath(); ctx.arc(-0.1, 0.1, 0.05, 0, Math.PI); ctx.stroke();
}
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 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 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 s = UNIT_SCALE * (u.type === 'line' || u.type === 'officer' ? 1.3 : 1.0);
// 选中圈
if (u === selectedUnit) {
ctx.strokeStyle = '#4aff4a'; ctx.lineWidth = 0.15;
ctx.beginPath(); ctx.arc(u.x, u.y, 1.2, 0, Math.PI*2); ctx.stroke();
}
ctx.save(); ctx.translate(u.x, u.y); ctx.scale(s, s);
u.type === 'line' ? drawLineBody() : (u.type === 'officer' ? drawOfficerBody() : drawMilitiaBody());
ctx.restore();
ctx.save();
ctx.translate(u.x, u.y - 0.4 * s); ctx.rotate(u.angle);
const strike = u.isAttacking ? Math.max(0, Math.sin(time * 15)) : 0;
if (u.isAttacking) ctx.translate(strike * 0.5 * s, 0);
ctx.scale(s, s);
if (u.type === 'line') {
drawLineWeaponAndReload(u.isReloading ? (u.reloadTimer / 14.0) : 0, u.isReloading);
} else if (u.type === 'officer') {
// 如果是冲锋状态,拿军刀,如果是装弹拿手枪,默认拿刀
if (u.isReloading) drawCompactPistol();
else drawSaber();
} else {
drawMilitiaWeapon();
}
ctx.restore();
if (u.showHpTimer > 0) drawHealthBar(u.x, u.y - 2.2 * s, u.hp / u.maxHp, '#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);
}
/* ========== 交互控制 ========== */
const pointers = new Map();
let dragStart = null, pinch = null;
function getPos(e) { const r = canvas.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; }
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);
}
canvas.addEventListener('pointerdown', e => {
initAudio();
canvas.setPointerCapture(e.pointerId);
const p = getPos(e); pointers.set(e.pointerId, p);
if (pointers.size === 1) {
dragStart = { x: p.x, y: p.y, camX: cam.x, camY: cam.y, moved: false };
// 点击逻辑
const worldPt = screenToWorld(p.x, p.y);
// 检查是否点中军官
let clickedOfficer = null;
for (let u of game.units) {
if (u.type === 'officer' && !u.isDead) {
if (Math.hypot(worldPt.x - u.x, worldPt.y - u.y) < 2.5) {
clickedOfficer = u; break;
}
}
}
if (clickedOfficer) {
// 双击检测
if (selectedUnit === clickedOfficer && Date.now() - lastOfficerClickTime < 300) {
document.getElementById('officer-menu').style.display = 'flex';
updateOfficerMenu();
} else {
document.getElementById('officer-menu').style.display = 'none';
selectedUnit = clickedOfficer;
}
lastOfficerClickTime = Date.now();
} else {
// 如果点击了空地,且有选中的军官,下达移动指令
if (selectedUnit && selectedUnit.type === 'officer') {
selectedUnit.targetPos = { x: worldPt.x, y: worldPt.y };
document.getElementById('officer-menu').style.display = 'none'; // 移动时关掉面板
} else {
selectedUnit = null;
document.getElementById('officer-menu').style.display = 'none';
}
}
} else 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 && dragStart) {
const dx = p.x - dragStart.x;
const dy = p.y - dragStart.y;
if (Math.hypot(dx, dy) > 5) dragStart.moved = true; // 标记移动
cam.x = dragStart.camX - dx / cam.scale;
cam.y = dragStart.camY - dy / cam.scale;
clampCam();
} 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) { dragStart = null; pinch = null; }
else if (pointers.size === 1) {
const p = [...pointers.values()][0];
dragStart = { x: p.x, y: p.y, camX: cam.x, camY: cam.y, moved: true }; // 抬起一根手指不触发点击
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 });
/* ========== 主循环 ========== */
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();
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); }
});
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();
}
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 (949 lines, 43.8 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-mwg7n49q" to link back to the original. Then publish at arcadelab.ai/publish.