🎮ArcadeLab

圣安托万门外 · 1642

by EpicCoder88
630 lines16.2 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>圣安托万门外 · 1642</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;
  }
  #hud b { color: #f0c674; font-weight: normal; }

  #ui {
    position: fixed; left: 12px; bottom: 20px;
    background: rgba(20,16,12,.9);
    border: 1px solid #5a4529; border-radius: 6px;
    padding: 12px; width: 200px;
  }
  #ui .gold { color: #f0c674; font-size: 18px; margin-bottom: 10px; }
  #ui .gold span { font-size: 24px; 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: 8px;
  }
  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 style="color: #8f7a55; font-size: 11px;">双指缩放 / 拖动平移</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 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 game = {
  gold: 20,
  goldTimer: 0,
  units: [],
  zombies: [],
  zombieTimer: 0,
  spawnZombieInterval: 8, // 每8秒刷一个僵尸
  running: true
};

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

/* ========== 游戏逻辑 ========== */
function recruitMilitia() {
  if (game.gold < 10) return;
  game.gold -= 10;
  
  // 生成在城门外(吊桥附近)
  game.units.push({
    type: 'militia',
    x: 12,
    y: 50 + (Math.random() * 4 - 2),
    hp: 100,
    maxHp: 100,
    atk: 15,
    range: 4,     // 攻击范围
    speed: 1.5,
    cooldown: 0
  });
  updateUI();
}

function spawnZombie() {
  // 生成在官道上(距离城门不太远,x=75)
  game.zombies.push({
    type: 'zombie',
    x: 75 + Math.random() * 5,
    y: 50 + (Math.random() * 8 - 4),
    hp: 60,
    maxHp: 60,
    atk: 8,
    range: 1.5,
    speed: 1.2,
    cooldown: 0
  });
}

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

  // 1. 金币产出 (每秒 2 枚)
  game.goldTimer += dt;
  if (game.goldTimer >= 1) {
    game.gold += 2;
    game.goldTimer -= 1;
    updateUI();
  }

  // 2. 僵尸生成
  game.zombieTimer += dt;
  if (game.zombieTimer >= game.spawnZombieInterval) {
    game.zombieTimer = 0;
    spawnZombie();
  }

  // 3. 单位更新
  // 民兵:如果没有敌人靠近,在城门附近巡逻/待命。有敌人则攻击
  for (let u of game.units) {
    u.cooldown = Math.max(0, u.cooldown - dt);
    
    // 寻找最近的僵尸
    let target = null;
    let minDist = Infinity;
    for (let z of game.zombies) {
      let d = Math.hypot(z.x - u.x, z.y - u.y);
      if (d < minDist) {
        minDist = d;
        target = z;
      }
    }

    if (target) {
      if (minDist <= u.range) {
        // 攻击
        if (u.cooldown <= 0) {
          target.hp -= u.atk;
          u.cooldown = 1.5; // 1.5秒攻击一次
        }
      } else {
        // 走向目标
        let dx = target.x - u.x;
        let dy = target.y - u.y;
        let dist = Math.hypot(dx, dy);
        u.x += (dx / dist) * u.speed * dt;
        u.y += (dy / dist) * u.speed * dt;
      }
    }
  }

  // 4. 僵尸更新
  for (let z of game.zombies) {
    z.cooldown = Math.max(0, z.cooldown - dt);

    // 寻找最近的民兵
    let target = null;
    let minDist = Infinity;
    for (let u of game.units) {
      let d = Math.hypot(u.x - z.x, u.y - z.y);
      if (d < minDist) {
        minDist = d;
        target = u;
      }
    }

    if (target) {
      if (minDist <= z.range) {
        // 攻击
        if (z.cooldown <= 0) {
          target.hp -= z.atk;
          z.cooldown = 1.2; // 1.2秒攻击一次
        }
      } else {
        // 走向目标
        let dx = target.x - z.x;
        let dy = target.y - z.y;
        let dist = Math.hypot(dx, dy);
        z.x += (dx / dist) * z.speed * dt;
        z.y += (dy / dist) * z.speed * dt;
      }
    } else {
      // 没有民兵,朝城门走 (x=5, y=50)
      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;
      }
    }
  }

  // 5. 清理死亡单位
  game.units = game.units.filter(u => u.hp > 0);
  game.zombies = game.zombies.filter(z => z.hp > 0);
}

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

/* ========== 绘制 ========== */
function draw() {
  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();
  drawUnits();

  ctx.restore();
}

// ... 保留之前的 drawWorld, drawFields, drawStructuredBuildings 等函数 ...
// (为了篇幅,这里在最终代码中包含,此处略过)

function drawUnits() {
  // 绘制民兵(蓝色)
  for (let u of game.units) {
    // 身体
    ctx.fillStyle = '#4a7a9a';
    ctx.beginPath();
    ctx.arc(u.x, u.y, 1.2, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = '#1a3a5a';
    ctx.lineWidth = 0.3;
    ctx.stroke();

    // 血条
    drawHealthBar(u.x, u.y - 2, u.hp / u.maxHp, '#4a7a9a');
  }

  // 绘制僵尸(绿色/暗红色)
  for (let z of game.zombies) {
    // 身体
    ctx.fillStyle = '#5a7a3a';
    ctx.beginPath();
    ctx.arc(z.x, z.y, 1.2, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = '#2a3a1a';
    ctx.lineWidth = 0.3;
    ctx.stroke();

    // 血条
    drawHealthBar(z.x, z.y - 2, z.hp / z.maxHp, '#5a7a3a');
  }
}

function drawHealthBar(x, y, pct, color) {
  if (cam.scale < 3) return; // 太小就不画血条了
  const w = 2.5;
  const h = 0.4;
  
  // 背景
  ctx.fillStyle = 'rgba(0,0,0,0.5)';
  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(time) {
  const dt = Math.min((time - lastTime) / 1000, 0.1); // 限制最大dt,防止切后台跳帧
  lastTime = time;

  updateGame(dt);
  draw();
  
  requestAnimationFrame(gameLoop);
}

/* ========== 缩放 / 平移 ========== */
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();
    draw();
  } 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();
    draw();
  }
});

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();
  draw();
}, { passive: false });

function handleClick(p) {
  // 暂时保留点选功能,后续可以用来选中单位
  const w = screenToWorld(p.x, p.y);
  const cx = Math.floor(w.x);
  const cy = Math.floor(w.y);
  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();
  draw();
}

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

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

/* ========== 完整地图绘制函数 ========== */
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);
}

function drawWorld() {
  ctx.fillStyle = '#c2b68c';
  ctx.fillRect(0, 0, WORLD_W, WORLD_H);
  drawFields();
  
  // 城墙
  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, 42);
  ctx.fillRect(5, 58, 5, 42);
  ctx.strokeStyle = '#7d9db0';
  ctx.strokeRect(5, 0, 5, 42);
  ctx.strokeRect(5, 58, 5, 42);
  ctx.fillStyle = '#9b8a6b';
  ctx.fillRect(5, 42, 5, 16); // 吊桥

  // 大门
  ctx.fillStyle = '#3b2a18';
  ctx.fillRect(0, 44, 6, 12);
  ctx.strokeStyle = '#d9b36c';
  ctx.lineWidth = 0.8;
  ctx.strokeRect(0, 44, 6, 12);
  ctx.fillStyle = '#6b4a2a';
  ctx.fillRect(0, 42, 6, 2);
  ctx.fillRect(0, 56, 6, 2);

  // 道路
  ctx.fillStyle = '#a89a7a';
  ctx.beginPath();
  ctx.moveTo(10, 46); ctx.lineTo(WORLD_W, 40);
  ctx.lineTo(WORLD_W, 60); ctx.lineTo(10, 54);
  ctx.closePath(); ctx.fill();
  ctx.strokeStyle = 'rgba(90, 70, 40, 0.5)';
  ctx.beginPath();
  ctx.moveTo(10, 46); ctx.lineTo(WORLD_W, 40);
  ctx.moveTo(10, 54); ctx.lineTo(WORLD_W, 60);
  ctx.stroke();

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

function drawFields() {
  ctx.save();
  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.restore();
}

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, 38);
    
    ctx.fillStyle = '#5a4529';
    ctx.font = '2px "Noto Serif SC", serif';
    ctx.save();
    ctx.translate(2.5, 20);
    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() {
  const s = cam.scale;
  let step = 1;
  if (s < 4) step = 10;
  else if (s < 10) step = 5;

  ctx.save();
  ctx.strokeStyle = 'rgba(70, 55, 35, 0.35)';
  ctx.lineWidth = 1 / s;
  ctx.setLineDash([1 / s, 1 / s]);
  ctx.beginPath();
  for (let x = 0; x <= WORLD_W; x += step) { ctx.moveTo(x, 0); ctx.lineTo(x, WORLD_H); }
  for (let y = 0; y <= WORLD_H; y += step) { ctx.moveTo(0, y); ctx.lineTo(WORLD_W, y); }
  ctx.stroke();
  ctx.restore();
}

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

Game Source: 圣安托万门外 · 1642

Creator: EpicCoder88

Libraries: none

Complexity: complex (630 lines, 16.2 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: 1642-epiccoder88-mwfz3278" to link back to the original. Then publish at arcadelab.ai/publish.