🎮ArcadeLab

圣安托万门外 · 防御战

by EpicCoder88
838 lines23.6 KB
▶ Play
<!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;
  }
  #hud b { color: #f0c674; font-weight: normal; }
  #hud .tip { color: #8f7a55; font-size: 11px; }

  #ui {
    position: fixed; left: 12px; bottom: 20px;
    background: rgba(20,16,12,.9);
    border: 1px solid #5a4529; border-radius: 6px;
    padding: 12px; width: 220px;
    z-index: 10;
  }
  #ui .gold { color: #f0c674; font-size: 16px; margin-bottom: 10px; }
  #ui .gold span { font-size: 22px; font-weight: bold; }
  
  button {
    width: 100%; padding: 10px;
    background: #5b3b22; color: #f4dfb4;
    border: 1px solid #a57843; border-radius: 4px;
    font-family: inherit; font-size: 14px; cursor: pointer;
    margin-bottom: 5px;
  }
  button:hover { background: #714b2b; }
  button:active { transform: translateY(1px); }
  button:disabled { background: #3b2a18; color: #7a6a50; border-color: #4a3420; cursor: not-allowed; }

  #btnReset {
    position: fixed; right: 12px; top: 12px;
    width: auto; margin: 0; padding: 8px 12px;
    z-index: 10;
  }
</style>
</head>
<body>
<canvas id="c"></canvas>

<div id="hud">
  圣安托万门 · 防御战<br>
  缩放 <b id="zoomTxt">1.0×</b> · 防区 <b id="cellTxt">—</b><br>
  <span class="tip">双指缩放 / 拖动平移</span>
</div>

<div id="ui">
  <div class="gold">国库:<span id="goldTxt">0</span> 金币</div>
  <button id="btnRecruit" onclick="recruitMilitia()">招募民兵 (10 金币)</button>
</div>

<button id="btnReset">重置视野</button>

<script>
/* ========== 基础 ========== */
const WORLD_W = 100;
const WORLD_H = 100;
const UNIT_SCALE = 0.65; 

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;
let selectedCell = null;

const clamp = (v, a, b) => Math.max(a, Math.min(b, v));

// 游戏状态
const game = {
  gold: 20,
  goldTimer: 0,
  units: [],
  zombies: [],
  corpses: [],
  floatTexts: [],
  zombieTimer: 0,
  spawnZombieInterval: 8, 
  running: true
};

/* ========== 游戏逻辑 ========== */
function recruitMilitia() {
  if (game.gold < 10) return;
  game.gold -= 10;
  
  game.units.push({
    type: 'militia',
    x: 12,
    y: 50 + (Math.random() * 8 - 4),
    hp: 100, maxHp: 100,
    showHpTimer: 0,
    atk: 15, 
    scoutRange: 20, 
    attackRange: 3, 
    speed: 1.5,
    cooldown: 0,
    attackInterval: 1.5,
    angle: 0,
    isAttacking: false,
    isDead: false,
    deathTimer: 0
  });
  updateUI();
}

function spawnZombie() {
  for (let i = 0; i < 5; i++) {
    const isRunner = Math.random() < 0.3; // 30% 概率生成跑尸
    
    game.zombies.push({
      type: isRunner ? 'runner' : 'zombie',
      // 生成在右半区,不仅是官道,上下也会生成
      x: 60 + Math.random() * 35, 
      y: 10 + Math.random() * 80, 
      hp: 60, maxHp: 60,
      showHpTimer: 0,
      atk: 8, 
      scoutRange: 20, 
      attackRange: 2.5,
      speed: isRunner ? 3.2 : 1.2, 
      cooldown: 0,
      attackInterval: isRunner ? 0.5 : 1.2, // 跑尸攻击间隔极短,像疯狗一样
      angle: Math.PI,
      isAttacking: false,
      isDead: false,
      deathTimer: 0
    });
  }
}

function addFloatText(x, y, text, color) {
  game.floatTexts.push({ x, y: y - 1.5, text, life: 1.0, color });
}

function createCorpse(x, y, type) {
  const drops = [];
  const numDrops = 3 + Math.floor(Math.random() * 3);
  for (let i = 0; i < numDrops; i++) {
    drops.push({
      dx: (Math.random() - 0.5) * 1.5,
      dy: (Math.random() - 0.5) * 1.5,
      r: 0.2 + Math.random() * 0.4
    });
  }

  game.corpses.push({
    x, y, type,
    timer: 0,
    maxTime: 12,
    rotation: (Math.random() - 0.5) * 0.5,
    drops
  });
}

function updateGame(dt, time) {
  if (!game.running) return;

  game.goldTimer += dt;
  if (game.goldTimer >= 1) {
    game.gold += 2;
    game.goldTimer -= 1;
    updateUI();
  }

  game.zombieTimer += dt;
  if (game.zombieTimer >= game.spawnZombieInterval) {
    game.zombieTimer = 0;
    spawnZombie();
  }

  // --- 民兵更新 ---
  for (let u of game.units) {
    if (u.isDead) continue;
    if (u.showHpTimer > 0) u.showHpTimer -= dt;

    u.cooldown = Math.max(0, u.cooldown - dt);
    u.isAttacking = false;
    
    let target = null;
    let minDist = Infinity;
    for (let z of game.zombies) {
      if (z.isDead) continue;
      let d = Math.hypot(z.x - u.x, z.y - u.y);
      if (d < u.scoutRange && d < minDist) {
        minDist = d;
        target = z;
      }
    }

    if (target) {
      u.angle = Math.atan2(target.y - u.y, target.x - u.x);
      
      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');
        }
      } else {
        let dx = target.x - u.x;
        let dy = target.y - u.y;
        u.x += (dx / minDist) * u.speed * dt;
        u.y += (dy / minDist) * u.speed * dt;
      }
    } else {
      u.angle = 0;
    }
  }

  // --- 僵尸更新 ---
  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;
    let 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);
      if (minDist <= z.attackRange) {
        z.isAttacking = true;
        if (z.cooldown <= 0) {
          target.hp -= z.atk;
          target.showHpTimer = 3.0;
          z.cooldown = z.attackInterval; // 使用自己的攻击间隔
          addFloatText(target.x, target.y, `-${z.atk}`, '#ffaa55');
        }
      } else {
        let dx = target.x - z.x;
        let dy = target.y - z.y;
        z.x += (dx / minDist) * z.speed * dt;
        z.y += (dy / minDist) * z.speed * dt;
      }
    } else {
      // 朝大门移动
      z.angle = Math.atan2(50 - z.y, 5 - z.x);
      let dx = 5 - z.x;
      let dy = 50 - z.y;
      let dist = Math.hypot(dx, dy);
      if (dist > 1) {
        z.x += (dx / dist) * z.speed * dt * 0.5;
        z.y += (dy / dist) * z.speed * dt * 0.5;
      }
    }
  }

  // --- 处理死亡状态 ---
  for (let u of game.units) {
    if (u.hp <= 0 && !u.isDead) {
      u.isDead = true;
      u.deathTimer = 0;
      u.hp = 0;
      createCorpse(u.x, u.y, 'militia');
    }
    if (u.isDead) u.deathTimer += dt;
  }
  game.units = game.units.filter(u => !(u.isDead && u.deathTimer > 12));

  for (let z of game.zombies) {
    if (z.hp <= 0 && !z.isDead) {
      z.isDead = true;
      z.deathTimer = 0;
      z.hp = 0;
      createCorpse(z.x, z.y, z.type);
    }
    if (z.isDead) z.deathTimer += dt;
  }
  game.zombies = game.zombies.filter(z => !(z.isDead && z.deathTimer > 12));

  for (let ft of game.floatTexts) {
    ft.y -= dt * 2;
    ft.life -= dt;
  }
  game.floatTexts = game.floatTexts.filter(ft => ft.life > 0);

  for (let c of game.corpses) c.timer += dt;
  game.corpses = game.corpses.filter(c => c.timer < c.maxTime);
}

function updateUI() {
  document.getElementById('goldTxt').textContent = Math.floor(game.gold);
  document.getElementById('btnRecruit').disabled = game.gold < 10;
}

/* ========== 绘制模块 ========== */
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.strokeStyle = '#2a1a10'; ctx.lineWidth = 0.1;
  ctx.beginPath(); ctx.ellipse(0, -1.5, 0.7, 0.3, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
  ctx.fillStyle = '#3a2410';
  ctx.beginPath(); ctx.ellipse(0, -1.6, 0.4, 0.2, 0, 0, Math.PI * 2); ctx.fill(); ctx.stroke();
  ctx.strokeStyle = '#5a3a1e'; ctx.lineWidth = 0.15;
  ctx.beginPath(); ctx.moveTo(-0.4, -0.5); ctx.lineTo(0.5, 0.5); ctx.moveTo(0.4, -0.5); ctx.lineTo(-0.5, 0.5); ctx.stroke();
}

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.strokeStyle = '#2a3a3a'; ctx.lineWidth = 0.1;
  ctx.beginPath(); ctx.moveTo(1.8, -0.15); ctx.lineTo(2.4, 0); ctx.lineTo(1.8, 0.15); ctx.closePath(); ctx.fill(); ctx.stroke();
  ctx.beginPath(); ctx.moveTo(1.8, -0.1); ctx.lineTo(2.0, -0.5); ctx.lineTo(1.6, -0.2); 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();
  
  ctx.fillStyle = 'rgba(120, 20, 20, 0.7)';
  ctx.beginPath(); ctx.ellipse(-0.2, 0, 0.25, 0.15, 0.5, 0, Math.PI * 2); ctx.fill();
  ctx.beginPath(); ctx.ellipse(0.3, 0.5, 0.15, 0.2, -0.2, 0, Math.PI * 2); ctx.fill();
  
  ctx.strokeStyle = '#1a1a15'; ctx.lineWidth = 0.1;
  ctx.beginPath(); ctx.moveTo(-0.7, 1); ctx.lineTo(-0.4, 0.8); ctx.lineTo(-0.1, 1); ctx.lineTo(0.2, 0.85); ctx.lineTo(0.5, 1); ctx.lineTo(0.8, 0.9); ctx.stroke();
}

function drawZombieArm(isRunner) {
  ctx.fillStyle = isRunner ? '#9a6a60' : '#7a8a70'; 
  ctx.strokeStyle = '#1a1a15'; ctx.lineWidth = 0.15;
  ctx.beginPath();
  ctx.moveTo(0, -0.15); ctx.lineTo(0.9, -0.2); ctx.lineTo(0.9, 0.15); ctx.lineTo(0, 0.15); 
  ctx.closePath(); ctx.fill(); ctx.stroke();
}

/* ========== 渲染逻辑 ========== */
function renderCorpses() {
  for (let c of game.corpses) {
    const alpha = Math.max(0, 1 - c.timer / c.maxTime);
    ctx.fillStyle = `rgba(100, 15, 15, ${alpha * 0.8})`;
    
    for (let d of c.drops) {
      ctx.beginPath();
      ctx.arc(c.x + d.dx, c.y + d.dy, d.r, 0, Math.PI * 2);
      ctx.fill();
    }

    const bodyAlpha = Math.max(0, Math.min(1, (c.maxTime - c.timer) / 3));
    ctx.globalAlpha = bodyAlpha;
    
    ctx.save();
    ctx.translate(c.x, c.y);
    const fallProgress = Math.min(1, c.timer / 0.3);
    ctx.rotate(Math.PI / 2 * fallProgress + c.rotation); 
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    
    if (c.type === 'militia') {
      drawMilitiaBody();
      ctx.rotate(Math.PI / 4);
      ctx.translate(0.5, 0);
      drawMilitiaWeapon();
    } else {
      drawZombieBody(c.type === 'runner');
      ctx.save(); ctx.translate(-0.4, -0.6); ctx.rotate(-0.5); drawZombieArm(c.type === 'runner'); ctx.restore();
      ctx.save(); ctx.translate(0.4, -0.6); ctx.rotate(0.5); drawZombieArm(c.type === 'runner'); ctx.restore();
    }
    ctx.restore();
    
    ctx.globalAlpha = 1.0;
  }
}

function renderUnits(time) {
  for (let u of game.units) {
    if (u.isDead) continue;

    const strike = u.isAttacking ? Math.max(0, Math.sin(time * 8)) : 0;
    
    ctx.save(); 
    ctx.translate(u.x, u.y); 
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    drawMilitiaBody(); 
    ctx.restore();
    
    ctx.save();
    ctx.translate(u.x, u.y - 0.4 * UNIT_SCALE);
    ctx.rotate(u.angle);
    ctx.translate(strike * 0.8 * UNIT_SCALE, 0);
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    drawMilitiaWeapon();
    ctx.restore();

    if (u.showHpTimer > 0) {
      drawHealthBar(u.x, u.y - 2.2 * UNIT_SCALE, 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 attackSpeed = isRunner ? 20 : 5; // 跑尸挥动速度极快
    const leftStrike = z.isAttacking ? Math.max(0, Math.sin(time * attackSpeed + z.x)) * 0.7 : 0.1;
    const rightStrike = z.isAttacking ? Math.max(0, Math.sin(time * attackSpeed + Math.PI + z.x)) * 0.7 : 0.1;

    ctx.save(); 
    ctx.translate(z.x + sway, z.y); 
    if (Math.cos(z.angle) < 0) ctx.scale(-1, 1);
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    drawZombieBody(isRunner); 
    ctx.restore();

    ctx.save();
    ctx.translate(z.x - 0.3 * UNIT_SCALE, z.y - 0.6 * UNIT_SCALE);
    ctx.rotate(z.angle);
    ctx.translate(leftStrike * UNIT_SCALE, 0);
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    drawZombieArm(isRunner);
    ctx.restore();

    ctx.save();
    ctx.translate(z.x + 0.3 * UNIT_SCALE, z.y - 0.6 * UNIT_SCALE);
    ctx.rotate(z.angle);
    ctx.translate(rightStrike * UNIT_SCALE, 0);
    ctx.scale(UNIT_SCALE, UNIT_SCALE);
    drawZombieArm(isRunner);
    ctx.restore();

    if (z.showHpTimer > 0) {
      drawHealthBar(z.x, z.y - 2.2 * UNIT_SCALE, z.hp / z.maxHp, '#7a3a3a');
    }
  }

  for (let ft of game.floatTexts) {
    ctx.fillStyle = ft.color;
    ctx.font = 'bold 2.5px "Noto Serif SC", serif';
    ctx.textAlign = 'center';
    ctx.globalAlpha = ft.life;
    ctx.fillText(ft.text, ft.x, ft.y);
    ctx.globalAlpha = 1.0;
  }
}

function drawHealthBar(x, y, pct, color) {
  if (cam.scale < 3) return;
  const w = 1.8, h = 0.3;
  ctx.fillStyle = 'rgba(0,0,0,0.6)';
  ctx.fillRect(x - w/2, y, w, h);
  ctx.fillStyle = pct > 0.5 ? color : (pct > 0.25 ? '#d1a13a' : '#c13a3a');
  ctx.fillRect(x - w/2, y, w * pct, h);
}

/* ========== 主循环 ========== */
let lastTime = performance.now();

function gameLoop(now) {
  const dt = Math.min((now - lastTime) / 1000, 0.1);
  const time = now / 1000;
  lastTime = now;

  updateGame(dt, time);

  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  ctx.fillStyle = '#0d0a07';
  ctx.fillRect(0, 0, W, H);

  ctx.save();
  ctx.translate(W / 2, H / 2);
  ctx.scale(cam.scale, cam.scale);
  ctx.translate(-cam.x, -cam.y);

  drawWorld();
  renderCorpses();
  renderUnits(time);
  drawSelection();

  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); 

  // 大门
  ctx.fillStyle = '#3b2a18';
  ctx.fillRect(0, 40, 6, 20);
  ctx.strokeStyle = '#d9b36c';
  ctx.lineWidth = 0.8;
  ctx.strokeRect(0, 40, 6, 20);
  
  // 门楼塔楼
  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();

  drawStructuredBuildings();
  drawLabels();
  drawGrid();
}

function drawStructuredBuildings() {
  drawHouse(22, 15, 10, 8);
  drawBarn(60, 15, 14, 10);
  drawTavern(45, 68, 12, 10);
  drawHouse(75, 75, 8, 7);
}

function drawBaseHouse(x, y, w, h, floorColor) {
  ctx.fillStyle = floorColor || '#b8a889';
  ctx.fillRect(x, y, w, h);
  ctx.strokeStyle = '#4a3420';
  ctx.lineWidth = 0.8;
  ctx.strokeRect(x, y, w, h);
}

function drawWall(x1, y1, x2, y2) {
  ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2);
  ctx.strokeStyle = '#4a3420'; ctx.lineWidth = 0.6; ctx.stroke();
}

function drawDoor(x, y, isVertical) {
  ctx.fillStyle = '#8a5a1e';
  if (isVertical) ctx.fillRect(x - 0.3, y - 1, 0.6, 2);
  else ctx.fillRect(x - 1, y - 0.3, 2, 0.6);
}

function drawHouse(x, y, w, h) {
  drawBaseHouse(x, y, w, h);
  drawWall(x + 4, y, x + 4, y + h);
  drawWall(x, y + 4, x + 4, y + 4);
  drawDoor(x + 6, y + h, false);
  drawDoor(x + 4, y + 2, true);
  ctx.fillStyle = '#7a5a3a';
  ctx.fillRect(x + 1, y + 1, 2, 3);
  ctx.fillRect(x + 7, y + 2, 2, 2);
}

function drawBarn(x, y, w, h) {
  drawBaseHouse(x, y, w, h, '#9e8e6e');
  ctx.fillStyle = 'rgba(180, 150, 80, 0.4)';
  ctx.fillRect(x + 2, y + 2, 3, 3);
  ctx.fillRect(x + 9, y + 5, 3, 3);
  ctx.fillStyle = '#6b4a2a';
  ctx.fillRect(x + 5, y + h - 0.4, 4, 0.8);
  ctx.fillStyle = '#4a3420';
  ctx.fillRect(x + 6.9, y + h - 0.4, 0.2, 0.8);
}

function drawTavern(x, y, w, h) {
  drawBaseHouse(x, y, w, h, '#c0b090');
  drawWall(x + w/2, y, x + w/2, y + h);
  drawWall(x + w/2, y + h/2, x + w, y + h/2);
  drawDoor(x + w/4, y + h, false);
  drawDoor(x + w/2, y + h/4, true);
  drawDoor(x + w*0.75, y + h/2, false);
  ctx.fillStyle = '#7a5a3a';
  ctx.fillRect(x + 1, y + 4, 4, 1);
  ctx.fillRect(x + 8, y + 2, 2, 2);
  ctx.fillRect(x + 8, y + 6, 2, 2);
}

function drawLabels() {
  if (cam.scale > 3) {
    ctx.fillStyle = '#4a3420';
    ctx.font = '2px "Noto Serif SC", serif';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';

    ctx.fillStyle = '#6b3b1a';
    ctx.font = '3px "Noto Serif SC", serif';
    ctx.textBaseline = 'top';
    ctx.fillText('圣安托万门', 15, 36);
    
    ctx.fillStyle = '#5a4529';
    ctx.font = '2px "Noto Serif SC", serif';
    ctx.save();
    ctx.translate(2.5, 15);
    ctx.rotate(-Math.PI / 2);
    ctx.fillText('巴 黎 城 墙', 0, 0);
    ctx.restore();

    ctx.fillStyle = '#6b5a3a';
    ctx.font = '2.2px "Noto Serif SC", serif';
    ctx.textBaseline = 'top';
    ctx.fillText('通往外省的官道', 40, 56);
  }
}

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();
}

function drawSelection() {
  if (!selectedCell) return;
  ctx.save();
  ctx.fillStyle = 'rgba(180, 110, 30, 0.25)';
  ctx.fillRect(selectedCell.x, selectedCell.y, 1, 1);
  ctx.strokeStyle = '#8a3a10';
  ctx.lineWidth = 2 / cam.scale;
  ctx.strokeRect(selectedCell.x, selectedCell.y, 1, 1);
  ctx.restore();
}

/* ========== 交互控制 ========== */
const pointers = new Map();
let dragStart = null;
let 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 => {
  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 };
    downPos = { x: p.x, y: p.y, t: Date.now() };
  } else if (pointers.size === 2) {
    const [a, b] = [...pointers.values()];
    const dist = Math.hypot(b.x - a.x, b.y - a.y);
    const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
    pinch = { dist, scale: cam.scale, worldMid: screenToWorld(mid.x, mid.y) };
    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) {
    cam.x = dragStart.camX - (p.x - dragStart.x) / cam.scale;
    cam.y = dragStart.camY - (p.y - dragStart.y) / cam.scale;
    clampCam();
  } else if (pointers.size === 2 && pinch) {
    const [a, b] = [...pointers.values()];
    const dist = Math.hypot(b.x - a.x, b.y - a.y);
    const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };

    cam.scale = clamp(pinch.scale * (dist / pinch.dist), minScale, maxScale);
    cam.x = pinch.worldMid.x - (mid.x - W / 2) / cam.scale;
    cam.y = pinch.worldMid.y - (mid.y - H / 2) / cam.scale;
    clampCam();
    updateHud();
  }
});

function endPointer(e) {
  if (!pointers.has(e.pointerId)) return;
  pointers.delete(e.pointerId);

  if (pointers.size === 0) {
    if (downPos && e.type === 'pointerup') {
      const p = getPos(e);
      const moved = Math.hypot(p.x - downPos.x, p.y - downPos.y);
      if (moved < 8 && Date.now() - downPos.t < 400) handleClick(p);
    }
    dragStart = null;
    pinch = null;
    downPos = null;
  } else if (pointers.size === 1) {
    const p = [...pointers.values()][0];
    dragStart = { x: p.x, y: p.y, 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();
  updateHud();
}, { passive: false });

function handleClick(p) {
  const w = screenToWorld(p.x, p.y);
  const cx = Math.floor(w.x);
  const cy = Math.floor(w.y);

  if (cx < 0 || cx >= WORLD_W || cy < 0 || cy >= WORLD_H) {
    selectedCell = null;
    document.getElementById('cellTxt').textContent = '—';
    return;
  }

  selectedCell = { x: cx, y: cy };
  document.getElementById('cellTxt').textContent = `${cx + 1}, ${cy + 1}`;
}

/* ========== 视图控制 ========== */
function updateHud() {
  document.getElementById('zoomTxt').textContent = cam.scale.toFixed(1) + '×';
}

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;
  updateHud();
}

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;
  const 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);
}

document.getElementById('btnReset').addEventListener('click', fitView);

window.addEventListener('resize', () => {
  resize();
  clampCam();
});

/* ========== 启动 ========== */
resize();
fitView();
updateUI();
requestAnimationFrame(gameLoop);
</script>
</body>
</html>

Game Source: 圣安托万门外 · 防御战

Creator: EpicCoder88

Libraries: none

Complexity: complex (838 lines, 23.6 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-mwg0rk01" to link back to the original. Then publish at arcadelab.ai/publish.