🎮ArcadeLab

族群之争 · 终极乱斗

by EpicCoder88
857 lines47.7 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">
<title>族群之争 · 终极乱斗</title>
<style>
  * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
  body {
    margin: 0; min-height: 100vh; background: #15171a;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    font-family: system-ui, -apple-system, sans-serif; color: #fff; overflow: hidden;
  }
  .game-wrapper { position: relative; width: 100vw; max-width: 368px; aspect-ratio: 1 / 1; box-shadow: 0 12px 48px rgba(0,0,0,.65); background: #2b2119; }
  canvas { display: block; width: 100%; height: 100%; touch-action: none; }
  .overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(21, 23, 26, 0.95); display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 10; transition: opacity 0.3s; }
  .overlay.hidden { opacity: 0; pointer-events: none; }
  h1 { font-size: 24px; letter-spacing: 2px; margin-bottom: 5px; color: #e8d39a; }
  .subtitle { font-size: 13px; color: #9fb3c8; margin-bottom: 10px; }
  
  .char-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 15px; max-height: 360px; overflow-y: auto; padding: 5px; }
  .char-card { 
    width: 95px; height: 110px; background: #232830; border: 2px solid #39414d; border-radius: 10px; 
    display: flex; flex-direction: column; align-items: center; justify-content: center; 
    cursor: pointer; transition: all 0.2s; user-select: none; position: relative;
  }
  .char-card:active { transform: scale(0.95); }
  .char-card.selected { border-color: #e08b3a; background: #3a2e21; box-shadow: 0 0 15px rgba(224, 139, 58, 0.4); }
  .char-card.guardian { border-color: #4ade80; }
  .char-card.boss { border-color: #ef4444; }
  .char-card.new-hero { border-color: #a855f7; }

  .char-icon { width: 45px; height: 45px; border-radius: 50%; margin-bottom: 5px; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 16px; color: #111; }
  .char-name { font-size: 13px; font-weight: bold; margin-bottom: 2px; }
  
  .char-tag { font-size: 9px; font-weight: bold; padding: 1px 4px; border-radius: 4px; }
  .tag-guardian { color: #4ade80; background: rgba(74, 222, 128, 0.15); }
  .tag-boss { color: #ef4444; background: rgba(239, 68, 68, 0.15); }
  .tag-new { color: #a855f7; background: rgba(168, 85, 247, 0.15); }

  button { background: #4ade80; color: #000; border: none; border-radius: 8px; padding: 12px 40px; font-size: 16px; font-weight: bold; cursor: pointer; transition: all 0.2s; opacity: 0.5; pointer-events: none; }
  button.enabled { opacity: 1; pointer-events: auto; box-shadow: 0 4px 15px rgba(74, 222, 128, 0.4); }
  
  #game-hud { position: absolute; top: 5px; left: 0; width: 100%; text-align: center; pointer-events: none; font-size: 14px; font-weight: bold; color: #fff; text-shadow: 0 2px 4px #000; opacity: 0; transition: 0.3s; line-height: 1.2; }
  #game-hud.show { opacity: 1; }
  #result-layer { background: rgba(21, 23, 26, 0.85); backdrop-filter: blur(3px); }
  #winner-text { font-size: 26px; color: #fbbf24; margin-bottom: 30px; text-shadow: 0 0 10px rgba(251, 191, 36, 0.5); }
</style>
</head>
<body>
<div class="game-wrapper">
  <canvas id="game"></canvas>
  <div id="game-hud"></div>
  <div id="ui-layer" class="overlay">
    <h1>族群之争</h1>
    <div class="subtitle">最多选 5 名武士大乱斗</div>
    <div class="char-grid">
      <div class="char-card guardian" data-char="火星"><div class="char-icon" style="background:#f97316;">🔥</div><div class="char-name">火星</div><div class="char-tag tag-guardian">基础</div></div>
      <div class="char-card boss" data-char="长鞭"><div class="char-icon" style="background:#1a1a1a; color:#ff0000;">🩸</div><div class="char-name">长鞭</div><div class="char-tag tag-boss">质检</div></div>
      <div class="char-card" data-char="根泉"><div class="char-icon" style="background:#4b5563;">🌿</div><div class="char-name">根泉</div><div class="char-tag" style="opacity:0">-</div></div>
      <div class="char-card" data-char="鹰霜"><div class="char-icon" style="background:#e08b3a;">🦅</div><div class="char-name">鹰霜</div><div class="char-tag" style="opacity:0">-</div></div>
      <div class="char-card" data-char="灰翅"><div class="char-icon" style="background:#9ca3af;">🕊️</div><div class="char-name">灰翅</div><div class="char-tag" style="opacity:0">-</div></div>
      <div class="char-card" data-char="黄牙"><div class="char-icon" style="background:#e8d39a;">🌼</div><div class="char-name">黄牙</div><div class="char-tag" style="opacity:0">-</div></div>
      <div class="char-card new-hero" data-char="狮焰"><div class="char-icon" style="background:#fbbf24;">🦁</div><div class="char-name">狮焰</div><div class="char-tag tag-new">叹息</div></div>
      <div class="char-card new-hero" data-char="石毛"><div class="char-icon" style="background:#6b7280;">🪨</div><div class="char-name">石毛</div><div class="char-tag tag-new">不屈</div></div>
      <div class="char-card new-hero" data-char="羽尾"><div class="char-icon" style="background:#93c5fd;">🕊️</div><div class="char-name">羽尾</div><div class="char-tag tag-new">钟乳</div></div>
      <div class="char-card new-hero" data-char="蜡毛"><div class="char-icon" style="background:#374151; color:#a855f7;">🌫️</div><div class="char-name">蜡毛</div><div class="char-tag tag-new">三阶段</div></div>
    </div>
    <button id="btn-start">开始乱斗</button>
  </div>
  <div id="result-layer" class="overlay hidden">
    <div id="winner-text">胜者:火星</div>
    <button id="btn-restart" class="enabled">重新选择</button>
  </div>
</div>
<script>
(() => {
  const CELL = 32, COLS = 10, ROWS = 10, WALL = 24;
  const ARENA_W = CELL * COLS, ARENA_H = CELL * ROWS;
  const CANVAS_W = ARENA_W + WALL * 2, CANVAS_H = ARENA_H + WALL * 2;

  const canvas = document.getElementById('game');
  const ctx = canvas.getContext('2d');
  const dpr = Math.min(window.devicePixelRatio || 1, 2);
  canvas.width = CANVAS_W * dpr; canvas.height = CANVAS_H * dpr;
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

  let entities = [], traps = [], projectiles = [], groundEffects = [], floatingTexts = [], effects = [], stalactites = [];
  let paused = true, gameStarted = false, selectedChars = [];

  class Cat {
    constructor(name, team, x, y, color, hp, isRanged, isMinion = false) {
      this.name = name; this.team = team;
      this.x = x; this.y = y; this.color = color;
      this.maxHp = hp; this.hp = hp; this.r = isMinion ? 8 : 14;
      this.isRanged = isRanged; this.isMinion = isMinion; this.alive = true;
      
      this.baseSpeed = 75;
      if (isRanged) this.baseSpeed = 65;
      if (name === '火星') this.baseSpeed = 85;
      if (name === '长鞭') this.baseSpeed = 90;
      if (name === '灰翅') this.baseSpeed = 60;
      if (name === '根泉') this.baseSpeed = 85;
      if (name === '狮焰') this.baseSpeed = 70;
      if (name === '石毛') this.baseSpeed = 70;
      this.speed = this.baseSpeed;
      
      this.atkCd = 0; this.skillCd = 0;
      this.atkRange = isRanged ? 200 : 32;
      if (name === '长鞭') this.atkRange = 16;
      if (name === '灰翅') this.atkRange = 240;
      if (name === '狮焰') this.atkRange = 10; 
      if (name === '羽尾') this.atkRange = 8;
      
      this.atkDamage = 30;
      if (name === '火星') this.atkDamage = 28;
      if (name === '长鞭') this.atkDamage = 150;
      if (name === '灰翅') this.atkDamage = 20;
      if (name === '根泉') this.atkDamage = 35;
      if (name === '狮焰') this.atkDamage = 48;
      if (name === '石毛') this.atkDamage = 37;
      if (name === '羽尾') this.atkDamage = 17;
      if (name === '蜡毛') this.atkDamage = 54;
      if (isMinion) this.atkDamage = 45;
      
      this.scoutRange = 140;
      if (name === '火星') this.scoutRange = 180;
      if (name === '长鞭') this.scoutRange = 150;
      if (name === '灰翅') this.scoutRange = 280;
      if (name === '根泉') this.scoutRange = 160;
      if (name === '狮焰') this.scoutRange = 200;
      if (name === '羽尾') this.scoutRange = 0; 
      if (isMinion) this.scoutRange = 120;

      this.wanderAngle = Math.random() * Math.PI * 2;
      this.wanderDistRemaining = 0;
      this.slowPercent = 0; this.slowTimer = 0;
      this.poisonTimer = 0; this.poisonTickTimer = 0;
      this.trapDebuffs = [];
      this.knockbackVx = 0; this.knockbackVy = 0;
      this.hitFlashTimer = 0;
      this.lockOnTimer = 0; this.lockOnTarget = null;

      this.critRate = 0.1; this.critMult = 1.5;
      if (name === '长鞭') { this.critRate = 0.05; this.critMult = 2.0; }
      if (name === '根泉') { this.critRate = 0.2; this.critMult = 1.8; }
      if (name === '灰翅') { this.critRate = 0.15; this.critMult = 2.0; }
      if (name === '狮焰') { this.critRate = 0.3; this.critMult = 61/48; }
      if (name === '蜡毛') { this.critRate = 0; this.critMult = 1.2; }

      if (name === '火星') { this.hasRevived = false; this.invincibleTimer = 0; this.dashTimer = 0; this.damageDealt = 0; this.leaderBuff = 0; }
      if (name === '长鞭') { this.invincibleTimer = 0; this.damageTakenTracker = 0; }
      if (name === '灰翅') { this.asthmaTimer = 10; this.isAsthma = false; }
      if (name === '根泉') { this.damageTakenTracker = 0; this.possessedTimer = 0; this.nextHitGuaranteedCrit = false; this.markedTarget = null; this.markTimer = 0; }
      
      if (name === '狮焰') { this.damageReduction = 0.95; this.hitCount = 0; }
      if (name === '石毛') { this.isBerserk = false; this.unyieldingBuff = false; }
      if (name === '羽尾') { this.meteorTimer = 10; this.healBuffTimer = 0; }
      if (name === '蜡毛') { this.stage = 1; this.fireTracker = 0; this.isCamouflaged = false; this.disguiseTimer = 0; this.suspicion = 0; this.controlledMinion = null; }
    }

    takeDamage(amount, isPoison = false, attacker = null) {
      if (!this.alive || this.invincibleTimer > 0) return;

      // 狮焰递减公式
      if (this.name === '狮焰') {
        amount = amount * this.damageReduction;
        this.hitCount++;
        let decrease = (0.01 * this.hitCount) / 100;
        this.damageReduction = Math.max(0, this.damageReduction - decrease);
        
        if (attacker && attacker !== this) {
           let nearby = entities.filter(e => e !== this && e.team !== this.team && Math.hypot(e.x - this.x, e.y - this.y) < 60 && e.alive);
           if (nearby.length > 0) nearby[0].takeDamage(amount * 0.4, true); 
        }
      }

      // 羽尾替死机制
      if (this.name === '羽尾' && !isPoison && amount >= 210 && this.healBuffTimer <= 0 && Math.random() < 0.2) {
        let otherCats = entities.filter(e => e.alive && !e.isMinion && e !== this);
        let target = otherCats.length > 0 ? otherCats[Math.floor(Math.random() * otherCats.length)] : this;
        
        let transferAmount = amount * 0.4;
        if (target === this) amount = amount * 0.6; 
        else target.takeDamage(transferAmount, true); 
        
        this.healBuffTimer = 20.0; 
        floatingTexts.push({ x: this.x, y: this.y - 30, text: "星族庇佑!", timer: 1.0, maxTimer: 1.0, color: '#93c5fd', isBig: true });
      }

      // 蜡毛伪装免伤与怀疑条
      if (this.name === '蜡毛' && this.isCamouflaged && !isPoison) {
        this.suspicion += 10; 
        if (this.suspicion >= 100) {
          this.isCamouflaged = false;
          floatingTexts.push({ x: this.x, y: this.y - 30, text: "伪装识破!", timer: 1.0, maxTimer: 1.0, color: '#a855f7', isBig: true });
        }
        return; 
      }

      if (this.name === '长鞭' && !isPoison && Math.random() < 0.3) {
        let angle = attacker ? Math.atan2(this.y - attacker.y, this.x - attacker.x) : Math.random() * Math.PI * 2;
        this.knockbackVx = Math.cos(angle) * 800; this.knockbackVy = Math.sin(angle) * 800;
        this.invincibleTimer = 0.3;
        floatingTexts.push({ x: this.x, y: this.y - 30, text: "闪避!", timer: 0.8, maxTimer: 0.8, color: '#a855f7' });
        effects.push({ type: 'trail', x: this.x, y: this.y, timer: 0.4, maxTimer: 0.4, color: 'rgba(26, 26, 26, 0.8)' });
        return;
      }
      
      if (this.name === '石毛' && this.slowTimer > 0 && Math.random() < 0.5) {
        this.slowTimer = 0; this.slowPercent = 0; this.unyieldingBuff = true;
        floatingTexts.push({ x: this.x, y: this.y - 30, text: "免控!", timer: 0.8, maxTimer: 0.8, color: '#a855f7' });
      }

      this.hp -= amount;
      
      if (!isPoison) {
        this.hitFlashTimer = 0.15;
        if (!this.isRanged && attacker && attacker.isRanged && !this.isMinion) {
          this.lockOnTimer = 3.0; this.lockOnTarget = attacker;
          floatingTexts.push({ x: this.x, y: this.y - 30, text: "锁定!", timer: 0.8, maxTimer: 0.8, color: '#ff4d4d' });
        }
        if (this.isRanged && attacker && Math.random() < 0.3) {
          let angle = Math.atan2(this.y - attacker.y, this.x - attacker.x);
          this.knockbackVx = Math.cos(angle) * 450; this.knockbackVy = Math.sin(angle) * 450;
          floatingTexts.push({ x: this.x, y: this.y - 30, text: "拉开距离!", timer: 0.8, maxTimer: 0.8, color: '#9fb3c8' });
        } else {
          let angle = Math.random() * Math.PI * 2;
          this.knockbackVx = Math.cos(angle) * 300; this.knockbackVy = Math.sin(angle) * 300;
        }
        effects.push({ type: 'ring', x: this.x, y: this.y, timer: 0.2, maxTimer: 0.2, color: '#fff' });
      }

      if (amount > 0) {
        floatingTexts.push({ x: this.x + (Math.random() * 20 - 10), y: this.y - 20 + (Math.random() * 10 - 5), text: Math.round(amount), timer: 0.8, maxTimer: 0.8, color: isPoison ? '#d946ef' : (amount > 200 ? '#ff0000' : '#ff4d4d'), isBig: amount > 200 });
      }

      // 触发被动机制
      if (this.name === '长鞭') { this.damageTakenTracker += amount; while (this.damageTakenTracker >= 100) { this.damageTakenTracker -= 100; this.summonMinions(); } }
      if (this.name === '根泉') {
        this.damageTakenTracker += amount;
        if (this.damageTakenTracker >= 150 && this.possessedTimer <= 0) {
          this.damageTakenTracker -= 150; this.possessedTimer = 2.0; this.nextHitGuaranteedCrit = true;
          floatingTexts.push({ x: this.x, y: this.y - 40, text: "火星附身!", timer: 1.2, maxTimer: 1.2, color: '#f97316', isBig: true });
          effects.push({ type: 'revive', x: this.x, y: this.y, timer: 0.8, maxTimer: 0.8 });
        }
      }
      if (this.name === '石毛' && this.hp > 0 && this.hp <= this.maxHp * 0.3 && !this.isBerserk) {
        this.isBerserk = true; this.atkDamage = 100; this.slowPercent = 0.2; this.slowTimer = 999;
        floatingTexts.push({ x: this.x, y: this.y - 40, text: "不屈之魂!", timer: 1.2, maxTimer: 1.2, color: '#a855f7', isBig: true });
      }
      if (this.name === '蜡毛') {
        this.fireTracker += amount;
        if (this.stage === 1 && this.fireTracker >= 300) {
          this.fireTracker = -9999;
          for(let i=0; i<3; i++) groundEffects.push({ x: this.x + (Math.random()-0.5)*40, y: this.y + (Math.random()-0.5)*40, r: 25, timer: 999, owner: this, seed: Math.random(), isFire: true });
          floatingTexts.push({ x: this.x, y: this.y - 40, text: "烈焰燎原!", timer: 1.5, maxTimer: 1.5, color: '#ef4444', isBig: true });
        }
        if (this.stage === 1 && this.hp <= 750) {
          this.stage = 2; this.atkDamage = 73; this.critRate = 0.3; this.critMult = 1.2;
          this.isCamouflaged = true; this.disguiseTimer = 15.0; this.suspicion = 0;
          groundEffects = []; 
          floatingTexts.push({ x: this.x, y: this.y - 40, text: "伪装·黑莓星", timer: 1.5, maxTimer: 1.5, color: '#a855f7', isBig: true });
        }
      }

      if (this.name === '蜡毛' && this.controlledMinion && this.controlledMinion.alive) {
         this.controlledMinion.team = 'neutral';
         this.controlledMinion.loseControlTimer = 1.0;
      }

      if (this.hp <= 0) {
        if (this.name === '火星' && !this.hasRevived) {
          this.hasRevived = true; this.hp = 1; this.invincibleTimer = 2.0;
          this.slowTimer = 0; this.slowPercent = 0; this.poisonTimer = 0; this.trapDebuffs = [];
          effects.push({ type: 'revive', x: this.x, y: this.y, timer: 1.0, maxTimer: 1.0 });
          floatingTexts.push({ x: this.x, y: this.y - 40, text: "九命之韧!", timer: 1.5, maxTimer: 1.5, color: '#fbbf24', isBig: true });
        } else {
          this.hp = 0; this.alive = false; 
          if (this.name === '蜡毛') {
            groundEffects.push({ x: ARENA_W/2, y: ARENA_H/2, r: 300, timer: 5.0, owner: this, seed: Math.random(), isBlackWater: true });
            floatingTexts.push({ x: this.x, y: this.y - 40, text: "黑水灭世!", timer: 2.0, maxTimer: 2.0, color: '#a855f7', isBig: true });
          }
        }
      }
    }

    summonMinions() {
      let currentMinions = entities.filter(e => e.isMinion && e.team === this.team && e.alive).length;
      if (currentMinions >= 8) return;
      for (let i = 0; i < 2; i++) {
        let angle = Math.random() * Math.PI * 2;
        let dist = 30 + Math.random() * 20;
        let mx = this.x + Math.cos(angle) * dist, my = this.y + Math.sin(angle) * dist;
        mx = Math.max(10, Math.min(ARENA_W - 10, mx)); my = Math.max(10, Math.min(ARENA_H - 10, my));
        entities.push(new Cat('血族', this.team, mx, my, '#1a1a1a', 100, false, true));
        effects.push({ type: 'ring', x: mx, y: my, timer: 0.4, maxTimer: 0.4, color: '#ff0000' });
      }
    }

    update(dt) {
      if (!this.alive) return;

      if (this.hitFlashTimer > 0) this.hitFlashTimer -= dt;
      if (this.invincibleTimer > 0) this.invincibleTimer -= dt;
      if (this.slowTimer > 0 && this.slowTimer < 900) this.slowTimer -= dt; else if (this.slowTimer < 900) this.slowPercent = 0;
      
      if (this.poisonTimer > 0) {
        this.poisonTimer -= dt; this.poisonTickTimer -= dt;
        if (this.poisonTickTimer <= 0) { this.poisonTickTimer = 1.0; this.takeDamage(1, true); }
      }
      for (let i = this.trapDebuffs.length - 1; i >= 0; i--) {
        let debuff = this.trapDebuffs[i]; debuff.timeLeft -= dt;
        if (debuff.timeLeft > 0) {
          debuff.tickTimer -= dt;
          if (debuff.tickTimer <= 0) { debuff.tickTimer = 1.0; debuff.tickCount++; this.takeDamage(Math.pow(2, debuff.tickCount), true); }
        } else { this.trapDebuffs.splice(i, 1); }
      }
      if (this.lockOnTimer > 0) { this.lockOnTimer -= dt; if (this.lockOnTimer <= 0 || !this.lockOnTarget || !this.lockOnTarget.alive) { this.lockOnTimer = 0; this.lockOnTarget = null; } }

      if (this.name === '羽尾') {
        if (this.healBuffTimer > 0) {
           this.healBuffTimer -= dt;
           this.hp = Math.min(this.maxHp, this.hp + 2.5 * dt);
        }
        this.meteorTimer -= dt;
        if (this.meteorTimer <= 0) {
          this.meteorTimer = 10.0;
          let cols = [0,1,2,3,4,5,6,7,8,9].sort(() => Math.random() - 0.5).slice(0, 8);
          cols.forEach(c => { 
             // 钟乳石宽一格,高两格
             stalactites.push({ x: c * CELL + CELL/2, y: -64, vy: 150, active: true, r: 16, h: 64, ownerTeam: this.team, hit: false }); 
          });
          floatingTexts.push({ x: this.x, y: this.y - 40, text: "钟乳石雨!", timer: 1.0, maxTimer: 1.0, color: '#78716c', isBig: true });
        }
      }

      if (this.name === '蜡毛' && this.stage === 1 && Math.random() < 0.02 && groundEffects.filter(g => g.isFire).length < 30) {
        let fires = groundEffects.filter(g => g.isFire);
        if(fires.length > 0 && fires.length < 20) {
           let f = fires[Math.floor(Math.random()*fires.length)];
           groundEffects.push({ x: f.x + (Math.random()-0.5)*50, y: f.y + (Math.random()-0.5)*50, r: 25, timer: 999, owner: this, seed: Math.random(), isFire: true });
        }
      }
      
      if (this.name === '蜡毛') {
         if (this.isCamouflaged) {
            this.disguiseTimer -= dt;
            if (this.disguiseTimer <= 0) {
               this.isCamouflaged = false;
               floatingTexts.push({ x: this.x, y: this.y - 30, text: "伪装解除", timer: 1.0, maxTimer: 1.0, color: '#a855f7' });
            }
         } else if (this.stage === 2 && !this.controlledMinion && entities.filter(e=>e.team===this.team && e!==this).length === 0) {
            let pool = ['火星', '长鞭', '根泉', '鹰霜', '灰翅', '黄牙', '狮焰', '石毛', '羽尾'];
            let pick = pool[Math.floor(Math.random() * pool.length)];
            let minion = new Cat(pick, this.team, this.x + 40, this.y, '#a855f7', 200, pick === '黄牙' || pick === '灰翅');
            entities.push(minion);
            this.controlledMinion = minion;
            floatingTexts.push({ x: this.x, y: this.y - 40, text: `召唤:${pick}!`, timer: 1.5, maxTimer: 1.5, color: '#a855f7', isBig: true });
         }
         
         if (this.controlledMinion && !this.controlledMinion.alive) this.controlledMinion = null;
      }

      let currentSpeed = this.speed * (1 - this.slowPercent);
      if (this.name === '蜡毛' && this.controlledMinion && this.controlledMinion.alive) currentSpeed *= 0.5;
      if (this.invincibleTimer > 0 && this.name === '火星') currentSpeed *= 1.5;
      if (this.dashTimer > 0 && this.name === '火星') currentSpeed *= 3.0;
      if (this.possessedTimer > 0 && this.name === '根泉') currentSpeed *= 2.0;
      if (this.isAsthma && this.name === '灰翅') currentSpeed *= 0.5;

      if (Math.abs(this.knockbackVx) > 1 || Math.abs(this.knockbackVy) > 1) {
        this.x += this.knockbackVx * dt; this.y += this.knockbackVy * dt;
        this.knockbackVx *= Math.pow(0.001, dt); this.knockbackVy *= Math.pow(0.001, dt);
      } else { this.knockbackVx = 0; this.knockbackVy = 0; }

      let nearestEnemy = null, minDist = Infinity;
      for (let e of entities) { 
        if (e.team !== this.team && e.alive) { 
          let d = Math.hypot(e.x - this.x, e.y - this.y); 
          if (d < minDist) { minDist = d; nearestEnemy = e; } 
        } 
      }

      let edgeDist = nearestEnemy ? (minDist - this.r - nearestEnemy.r) : 9999;
      let inSight = nearestEnemy && edgeDist < this.scoutRange;
      
      let moveTarget = this.lockOnTimer > 0 && this.lockOnTarget ? this.lockOnTarget : (!this.isRanged && inSight && nearestEnemy ? nearestEnemy : null);

      if (this.name === '羽尾' || (this.name === '蜡毛' && this.isCamouflaged)) {
         moveTarget = null;
      }

      if (this.loseControlTimer > 0) {
         this.loseControlTimer -= dt;
         if (this.loseControlTimer <= 0 && this.name !== '血族') { this.team = 'team_' + (Math.floor(Math.random()*10)); } 
      }

      if (moveTarget) {
        let dx = moveTarget.x - this.x, dy = moveTarget.y - this.y, dist = Math.hypot(dx, dy);
        if (this.name === '火星' && this.dashTimer <= 0 && this.skillCd <= 0 && dist > this.atkRange) {
          this.dashTimer = 0.4; this.skillCd = 8.0;
          floatingTexts.push({ x: this.x, y: this.y - 30, text: "猛虎扑击!", timer: 0.8, maxTimer: 0.8, color: '#f97316' });
        }
        if (this.dashTimer > 0) {
          this.dashTimer -= dt;
          if (Math.random() < 0.6) effects.push({ type: 'trail', x: this.x, y: this.y, timer: 0.3, maxTimer: 0.3, color: 'rgba(249, 115, 22, 0.6)' }); 
          this.x += (dx / dist) * currentSpeed * dt; this.y += (dy / dist) * currentSpeed * dt;
        } else if (dist > this.atkRange * 0.8) {
          this.x += (dx / dist) * currentSpeed * dt; this.y += (dy / dist) * currentSpeed * dt;
        }
      } else {
        if (this.wanderDistRemaining <= 0) { this.wanderAngle = Math.random() * Math.PI * 2; this.wanderDistRemaining = 10 + Math.random() * 40; }
        let step = currentSpeed * 0.6 * dt; if (step > this.wanderDistRemaining) step = this.wanderDistRemaining;
        let nextX = this.x + Math.cos(this.wanderAngle) * step, nextY = this.y + Math.sin(this.wanderAngle) * step;
        if (nextX < this.r || nextX > ARENA_W - this.r || nextY < this.r || nextY > ARENA_H - this.r) this.wanderDistRemaining = 0;
        else { this.x = nextX; this.y = nextY; this.wanderDistRemaining -= step; }
      }

      if (this.name === '蜡毛' && this.stage === 1) {
         for (let g of groundEffects) {
            if (g.isFire && Math.hypot(this.x - g.x, this.y - g.y) < this.r + g.r + 20) {
               let angle = Math.atan2(this.y - g.y, this.x - g.x);
               this.x += Math.cos(angle) * currentSpeed * dt;
               this.y += Math.sin(angle) * currentSpeed * dt;
            }
         }
      }

      for (let s of stalactites) {
        if (s.y > 0) {
          let dx = this.x - s.x, dy = this.y - (s.y + s.h/2);
          let dist = Math.hypot(dx, dy);
          if (dist < this.r + s.r) {
            let angle = Math.atan2(dy, dx);
            this.x = s.x + Math.cos(angle) * (this.r + s.r);
            this.y = (s.y + s.h/2) + Math.sin(angle) * (this.r + s.r);
          }
        }
      }

      this.x = Math.max(this.r, Math.min(ARENA_W - this.r, this.x));
      this.y = Math.max(this.r, Math.min(ARENA_H - this.r, this.y));

      this.atkCd -= dt; this.skillCd -= dt;

      if (nearestEnemy && edgeDist <= this.atkRange && this.atkCd <= 0 && !(this.name === '灰翅' && this.isAsthma)) {
        let slashAngle = Math.atan2(nearestEnemy.y - this.y, nearestEnemy.x - this.x);
        let slashColor = '#e08b3a';
        if (this.name === '火星') slashColor = '#f97316';
        if (this.name === '长鞭') slashColor = '#ff0000';
        if (this.name === '根泉') slashColor = '#4b5563';
        if (this.name === '狮焰') slashColor = '#fbbf24';
        if (this.name === '石毛') slashColor = '#6b7280';
        if (this.name === '羽尾') slashColor = '#93c5fd';
        if (this.name === '蜡毛') slashColor = this.stage === 1 ? '#ef4444' : '#a855f7';
        if (this.isMinion) slashColor = '#333';

        if (this.name === '灰翅') {
          this.atkCd = 1.5;
          let isCrit = Math.random() < (this.critRate + (nearestEnemy.markedTarget === this ? 0.15 : 0));
          nearestEnemy.takeDamage(this.atkDamage * (isCrit ? this.critMult : 1), false, this);
          nearestEnemy.markedTarget = this; nearestEnemy.markTimer = 3.0;
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: isCrit ? '#fbbf24' : '#9ca3af' });
        }
        else if (this.name === '根泉') {
          this.atkCd = 1.0;
          let isCrit = this.nextHitGuaranteedCrit || Math.random() < this.critRate;
          let finalDmg = isCrit ? this.atkDamage * (this.nextHitGuaranteedCrit ? 3.0 : this.critMult) : this.atkDamage;
          if (this.nextHitGuaranteedCrit) this.nextHitGuaranteedCrit = false;
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.25, maxTimer: 0.25, color: isCrit ? '#fbbf24' : '#4b5563' });
          if (isCrit) {
            floatingTexts.push({ x: nearestEnemy.x, y: nearestEnemy.y - 30, text: "亡魂暴击!", timer: 0.8, maxTimer: 0.8, color: '#fbbf24' });
            nearestEnemy.takeDamage(30, true); 
          }
        }
        else if (this.name === '长鞭') {
          this.atkCd = 1.2;
          let hpRatio = this.hp / this.maxHp;
          let currentCrit = this.critRate + (1 - hpRatio) * 0.45;
          let isCrit = Math.random() < currentCrit;
          let finalDmg = isCrit ? 340 : 150;
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.3, maxTimer: 0.3, color: isCrit ? '#ff0000' : slashColor });
          if (isCrit) effects.push({ type: 'ring', x: nearestEnemy.x, y: nearestEnemy.y, timer: 0.3, maxTimer: 0.3, color: '#ff0000' });
        }
        else if (this.name === '火星') {
          this.atkCd = 1.0;
          let isCrit = Math.random() < this.critRate;
          let baseDmg = 28 + 28 * 0.05 * this.leaderBuff;
          let finalDmg = Math.round(baseDmg * (isCrit ? this.critMult : 1));
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: isCrit ? '#fbbf24' : slashColor });
          this.damageDealt += finalDmg;
          let newBuff = Math.floor(this.damageDealt / 100);
          if (newBuff > this.leaderBuff) { this.leaderBuff = newBuff; floatingTexts.push({ x: this.x, y: this.y - 20, text: `领袖+${this.leaderBuff}`, timer: 1.0, maxTimer: 1.0, color: '#fbbf24' }); }
        }
        else if (this.name === '石毛') {
          this.atkCd = 1.0;
          let finalDmg = this.atkDamage;
          if (this.unyieldingBuff) { finalDmg = 270; this.unyieldingBuff = false; effects.push({ type: 'ring', x: nearestEnemy.x, y: nearestEnemy.y, timer: 0.4, maxTimer: 0.4, color: '#a855f7' }); }
          if (this.isBerserk && Math.random() < 0.3) { finalDmg = 130; } 
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
        }
        else if (this.name === '狮焰') {
          this.atkCd = 1.2;
          let isCrit = Math.random() < this.critRate;
          let finalDmg = isCrit ? 61 : 48;
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
        }
        else if (this.name === '羽尾') {
          this.atkCd = 1.2;
          nearestEnemy.takeDamage(17, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
        }
        else if (this.name === '蜡毛') {
          this.atkCd = 1.2;
          // ★ 修正:伪装期间正常造成伤害,同时积攒对方怀疑条
          let isCrit = Math.random() < this.critRate;
          let finalDmg = isCrit ? 89 : 73;
          if (this.stage === 1) finalDmg = 54;
          nearestEnemy.takeDamage(finalDmg, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
          
          if (this.isCamouflaged) {
             nearestEnemy.suspicion = (nearestEnemy.suspicion || 0) + 15;
             if(nearestEnemy.suspicion >= 100) {
                nearestEnemy.suspicion = 0; 
                this.isCamouflaged = false;
                floatingTexts.push({ x: this.x, y: this.y - 30, text: "伪装识破!", timer: 1.0, maxTimer: 1.0, color: '#a855f7', isBig: true });
             }
          }
        }
        else if (this.name === '鹰霜') {
          this.atkCd = 1.0;
          nearestEnemy.takeDamage(this.atkDamage, false, this);
          effects.push({ type: 'slash', x: nearestEnemy.x, y: nearestEnemy.y, angle: slashAngle, timer: 0.2, maxTimer: 0.2, color: slashColor });
        }
        else if (this.isRanged) {
          this.atkCd = 1.5;
          let dx = nearestEnemy.x - this.x, dy = nearestEnemy.y - this.y, dist = Math.hypot(dx, dy);
          let bulletSpeed = this.name === '灰翅' ? 200 : 250;
          projectiles.push({ x: this.x, y: this.y, vx: (dx / dist) * bulletSpeed, vy: (dy / dist) * bulletSpeed, damage: this.atkDamage, type: 'berry', owner: this, life: 3.0 });
        }
        else if (this.isMinion) {
          this.atkCd = 1.0;
          nearestEnemy.takeDamage(45, false, this);
        }
      }

      if (this.name === '鹰霜' && this.skillCd <= 0) {
        this.skillCd = 3.0;
        if (Math.random() < 0.8 && traps.length < 6) traps.push({ x: this.x, y: this.y, r: 25, armed: true, owner: this });
      }
    }

    draw() {
      if (!this.alive) return;
      let sx = WALL + this.x, sy = WALL + this.y;
      if (this.trapDebuffs.length > 0 && this.invincibleTimer <= 0 && this.dashTimer <= 0) { sx += (Math.random() - 0.5) * 4; sy += (Math.random() - 0.5) * 4; }

      if (this.lockOnTimer > 0 && this.lockOnTarget) {
        ctx.strokeStyle = `rgba(255, 50, 50, 0.5)`; ctx.lineWidth = 2; ctx.setLineDash([5, 5]);
        ctx.beginPath(); ctx.moveTo(sx, sy); ctx.lineTo(WALL + this.lockOnTarget.x, WALL + this.lockOnTarget.y); ctx.stroke(); ctx.setLineDash([]);
      }

      if (!this.isMinion) {
        ctx.fillStyle = 'white'; ctx.font = 'bold 11px sans-serif'; ctx.textAlign = 'center';
        ctx.fillText(`${this.name} (${Math.ceil(this.hp)})`, sx, sy - this.r - 15);
        ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(sx - 18, sy - this.r - 10, 36, 4);
        ctx.fillStyle = this.hp > 300 ? '#4ade80' : '#ef4444'; ctx.fillRect(sx - 18, sy - this.r - 10, 36 * (this.hp / this.maxHp), 4);
        
        if (this.suspicion > 0) {
          ctx.fillStyle = '#a855f7'; ctx.font = 'bold 10px sans-serif';
          ctx.fillText(`怀疑: ${Math.min(100, Math.ceil(this.suspicion))}%`, sx, sy - this.r - 25);
        }
      } else {
        ctx.fillStyle = 'rgba(0,0,0,0.6)'; ctx.fillRect(sx - 10, sy - this.r - 6, 20, 3);
        ctx.fillStyle = '#ef4444'; ctx.fillRect(sx - 10, sy - this.r - 6, 20 * (this.hp / this.maxHp), 3);
      }

      ctx.beginPath(); ctx.arc(sx, sy, this.r, 0, Math.PI * 2);
      ctx.fillStyle = this.hitFlashTimer > 0 ? '#ffffff' : this.color; ctx.fill();
      ctx.strokeStyle = this.isMinion ? '#ff0000' : '#000'; ctx.lineWidth = this.isMinion ? 1 : 2; ctx.stroke();

      if (this.invincibleTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 4, 0, Math.PI * 2); ctx.strokeStyle = `rgba(255, 255, 255, ${0.5 + 0.5 * Math.sin(Date.now() / 100)})`; ctx.lineWidth = 3; ctx.stroke(); }
      else if (this.dashTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 6, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(249, 115, 22, 0.8)'; ctx.lineWidth = 4; ctx.stroke(); }
      if (this.possessedTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 5, 0, Math.PI * 2); ctx.strokeStyle = `rgba(249, 115, 22, ${0.6 + 0.4 * Math.sin(Date.now() / 80)})`; ctx.lineWidth = 3; ctx.stroke(); }
      if (this.isAsthma) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 3, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(156, 163, 175, 0.7)'; ctx.lineWidth = 2; ctx.setLineDash([3, 3]); ctx.stroke(); ctx.setLineDash([]); }
      if (this.slowTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 2, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(168, 85, 247, 0.8)'; ctx.lineWidth = 2; ctx.stroke(); }
      if (this.name === '蜡毛' && this.isCamouflaged) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 3, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(168, 85, 247, 0.5)'; ctx.setLineDash([3,3]); ctx.stroke(); ctx.setLineDash([]); }
      if (this.healBuffTimer > 0) { ctx.beginPath(); ctx.arc(sx, sy, this.r + 6, 0, Math.PI * 2); ctx.strokeStyle = 'rgba(147, 197, 253, 0.6)'; ctx.lineWidth = 2; ctx.stroke(); }

      for (let debuff of this.trapDebuffs) {
        ctx.beginPath(); ctx.arc(WALL + debuff.center.x, WALL + debuff.center.y, 128, 0, Math.PI * 2);
        let alpha = 0.15 + 0.15 * Math.abs(Math.sin(debuff.timeLeft * 5));
        ctx.strokeStyle = `rgba(255, 50, 50, ${alpha})`; ctx.lineWidth = 1.5; ctx.setLineDash([5, 5]); ctx.stroke(); ctx.setLineDash([]);
      }
    }
  }

  function startBattle() {
    if (selectedChars.length < 2) return;
    gameStarted = true;
    document.getElementById('ui-layer').classList.add('hidden');
    document.getElementById('result-layer').classList.add('hidden');
    document.getElementById('game-hud').classList.add('show');
    document.getElementById('game-hud').innerText = '大乱斗进行中...';
    
    entities = [];
    let centerX = ARENA_W / 2, centerY = ARENA_H / 2;
    let radius = 120;
    
    selectedChars.forEach((name, index) => {
      let angle = (index / selectedChars.length) * Math.PI * 2;
      let sx = centerX + Math.cos(angle) * radius;
      let sy = centerY + Math.sin(angle) * radius;
      
      let teamId = 'team_' + index;
      let cat;
      if (name === '鹰霜') cat = new Cat('鹰霜', teamId, sx, sy, '#e08b3a', 1030, false);
      if (name === '黄牙') cat = new Cat('黄牙', teamId, sx, sy, '#e8d39a', 1000, true);
      if (name === '火星') cat = new Cat('火星', teamId, sx, sy, '#f97316', 1050, false);
      if (name === '长鞭') cat = new Cat('长鞭', teamId, sx, sy, '#1a1a1a', 550, false);
      if (name === '灰翅') cat = new Cat('灰翅', teamId, sx, sy, '#9ca3af', 1000, true);
      if (name === '根泉') cat = new Cat('根泉', teamId, sx, sy, '#4b5563', 1000, false);
      if (name === '狮焰') cat = new Cat('狮焰', teamId, sx, sy, '#fbbf24', 2000, false);
      if (name === '石毛') cat = new Cat('石毛', teamId, sx, sy, '#6b7280', 1000, false);
      if (name === '羽尾') cat = new Cat('羽尾', teamId, sx, sy, '#93c5fd', 990, false);
      if (name === '蜡毛') cat = new Cat('蜡毛', teamId, sx, sy, '#374151', 1600, false);
      
      if (cat) entities.push(cat);
    });

    traps = []; projectiles = []; groundEffects = []; floatingTexts = []; effects = []; stalactites = [];
    paused = false;
  }

  function update(dt) {
    if (paused || !gameStarted) return;

    for (let i = stalactites.length - 1; i >= 0; i--) {
      let s = stalactites[i];
      s.y += s.vy * dt;
      
      for (let e of entities) {
        if (e.alive && s.y > -20) {
           let rectX = s.x - s.r, rectY = s.y, rectW = s.r * 2, rectH = s.h;
           if (e.x > rectX - e.r && e.x < rectX + rectW + e.r && e.y > rectY - e.r && e.y < rectY + rectH + e.r) {
              if (e.team !== s.ownerTeam && !s.hit) { s.hit = true; e.takeDamage(210, false, null); e.knockbackVx = 0; e.knockbackVy = 600; }
              if (e.team !== s.ownerTeam) e.y += 250 * dt; // 推人
           }
        }
      }
      
      if (s.y > ARENA_H - s.h) {
        s.y = ARENA_H - s.h; s.vy = 0; 
        if (s.active) { s.active = false; s.timer = 3.0; effects.push({type:'ring', x: s.x, y: s.y + s.h, timer: 0.5, maxTimer: 0.5, color: '#78716c'}); }
      }
      
      if (!s.active) {
        s.timer -= dt;
        if (s.timer <= 0) stalactites.splice(i, 1);
      }
    }

    for (let entity of entities) { if (entity.alive) entity.update(dt); }

    for (let e of entities) { if (e.markTimer > 0) { e.markTimer -= dt; if (e.markTimer <= 0) e.markedTarget = null; } }

    for (let i = traps.length - 1; i >= 0; i--) {
      let t = traps[i]; if (!t.armed) continue;
      for (let entity of entities) {
        if (!entity.alive || entity.team === t.owner.team || entity.invincibleTimer > 0) continue;
        if (Math.hypot(entity.x - t.x, entity.y - t.y) < entity.r + t.r) {
          t.armed = false; traps.splice(i, 1);
          entity.trapDebuffs.push({ center: { x: t.x, y: t.y }, timeLeft: 5.0, tickTimer: 1.0, tickCount: 0 });
          entity.slowPercent = 0.3; entity.slowTimer = 5.0;
          effects.push({ type: 'ring', x: t.x, y: t.y, timer: 0.3, maxTimer: 0.3, color: '#ef4444' });
        }
      }
    }

    for (let i = projectiles.length - 1; i >= 0; i--) {
      let p = projectiles[i]; p.life -= dt;
      p.x += p.vx * dt; p.y += p.vy * dt;
      
      let blocked = false;
      for (let s of stalactites) { if (s.y > 0 && Math.hypot(p.x - s.x, p.y - (s.y + s.h/2)) < s.r) { blocked = true; break; } }
      if (blocked) { projectiles.splice(i, 1); continue; }

      if (p.x < 0 || p.x > ARENA_W || p.y < 0 || p.y > ARENA_H || p.life <= 0) { projectiles.splice(i, 1); continue; }
      let hit = false;
      for (let entity of entities) {
        if (entity.alive && entity.team !== p.owner.team && Math.hypot(entity.x - p.x, entity.y - p.y) < entity.r + 6) {
          entity.takeDamage(p.damage, false, p.owner);
          entity.poisonTimer = 5.0; entity.poisonTickTimer = 1.0;
          groundEffects.push({ x: entity.x, y: entity.y, r: 30, timer: 20, owner: p.owner, seed: Math.random() });
          effects.push({ type: 'ring', x: entity.x, y: entity.y, timer: 0.3, maxTimer: 0.3, color: '#d946ef' });
          hit = true; break;
        }
      }
      if (hit) projectiles.splice(i, 1);
    }

    for (let i = groundEffects.length - 1; i >= 0; i--) {
      let g = groundEffects[i]; 
      if (!g.isFire && !g.isBlackWater) g.timer -= dt;
      if (g.timer <= 0) { groundEffects.splice(i, 1); continue; }
      
      if (Math.random() < 0.05 && !g.isBlackWater) {
        effects.push({ type: 'bubble', x: g.x + (Math.random() - 0.5) * g.r * 1.5, y: g.y + (Math.random() - 0.5) * g.r * 1.5, timer: 0.6, maxTimer: 0.6 }); 
      }
      
      for (let entity of entities) {
        if (!entity.alive || entity.team === g.owner.team || entity.invincibleTimer > 0) continue;
        if (Math.hypot(entity.x - g.x, entity.y - g.y) < entity.r + g.r) {
          if (g.isBlackWater) { entity.takeDamage(5000, true); } 
          else if (g.isFire) { 
             if (Math.random() < 0.1) { entity.takeDamage(5, true); }
          } 
          else {
             if (entity.slowPercent < 0.2) { entity.slowPercent = 0.2; entity.slowTimer = 0.5; }
             if (!entity.juiceTick) entity.juiceTick = 0;
             entity.juiceTick -= dt;
             if (entity.juiceTick <= 0) { entity.juiceTick = 1.0; entity.takeDamage(1, true); }
          }
        }
      }
    }

    for (let i = entities.length - 1; i >= 0; i--) { if (!entities[i].alive) entities.splice(i, 1); }
    
    let aliveNonMinions = entities.filter(e => e.alive && !e.isMinion);
    if (aliveNonMinions.length <= 1 && gameStarted) {
       paused = true; gameStarted = false;
       let winner = aliveNonMinions.length === 1 ? aliveNonMinions[0].name : '平局';
       document.getElementById('game-hud').classList.remove('show');
       document.getElementById('winner-text').innerText = `胜者:${winner}`;
       document.getElementById('result-layer').classList.remove('hidden');
    }

    for (let i = effects.length - 1; i >= 0; i--) { effects[i].timer -= dt; if (effects[i].timer <= 0) effects.splice(i, 1); }
    for (let i = floatingTexts.length - 1; i >= 0; i--) { let ft = floatingTexts[i]; ft.y -= 30 * dt; ft.timer -= dt; if (ft.timer <= 0) floatingTexts.splice(i, 1); }
  }

  function draw() {
    ctx.fillStyle = '#2b2119'; ctx.fillRect(0, 0, CANVAS_W, CANVAS_H);
    ctx.fillStyle = '#55734a'; ctx.fillRect(WALL, WALL, ARENA_W, ARENA_H);
    ctx.strokeStyle = 'rgba(255,255,255,0.075)'; ctx.beginPath();
    for (let i = 1; i < COLS; i++) { ctx.moveTo(WALL + i * CELL, WALL); ctx.lineTo(WALL + i * CELL, WALL + ARENA_H); }
    for (let j = 1; j < ROWS; j++) { ctx.moveTo(WALL, WALL + j * CELL); ctx.lineTo(WALL + ARENA_W, WALL + j * CELL); }
    ctx.stroke();

    for (let g of groundEffects) {
      if (g.isBlackWater) ctx.fillStyle = 'rgba(0, 0, 0, 0.85)';
      else if (g.isFire) ctx.fillStyle = 'rgba(239, 68, 68, 0.4)';
      else ctx.fillStyle = 'rgba(126, 34, 206, 0.5)';
      
      ctx.beginPath();
      for (let a = 0; a < Math.PI * 2; a += Math.PI / 4) { let r = g.r * (0.8 + Math.sin(a * 3 + g.seed) * 0.2); let px = WALL + g.x + Math.cos(a) * r, py = WALL + g.y + Math.sin(a) * r; if (a === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); }
      ctx.closePath(); ctx.fill();
    }

    for (let s of stalactites) {
      // 灰褐色岩石
      ctx.fillStyle = s.active ? '#78716c' : '#57534e';
      ctx.beginPath();
      // 宽 1 格 (32),长 2 格 (64)
      ctx.moveTo(WALL + s.x - s.r, WALL + s.y);
      ctx.lineTo(WALL + s.x + s.r, WALL + s.y);
      // 尖端
      ctx.lineTo(WALL + s.x + s.r * 0.6, WALL + s.y + s.h - 10);
      ctx.lineTo(WALL + s.x, WALL + s.y + s.h);
      ctx.lineTo(WALL + s.x - s.r * 0.6, WALL + s.y + s.h - 10);
      ctx.closePath(); 
      ctx.fill();
      ctx.strokeStyle = '#292524'; ctx.lineWidth = 1; ctx.stroke();
    }

    for (let t of traps) {
      ctx.beginPath(); ctx.arc(WALL + t.x, WALL + t.y, t.r, 0, Math.PI * 2); ctx.fillStyle = 'rgba(239, 68, 68, 0.3)'; ctx.fill();
      let pulse = 1 + Math.sin(Date.now() / 150) * 0.1; ctx.beginPath();
      for (let i = 0; i < 6; i++) { let ang = i * Math.PI / 3; let px = WALL + t.x + Math.cos(ang) * t.r * 0.6 * pulse, py = WALL + t.y + Math.sin(ang) * t.r * 0.6 * pulse; if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); }
      ctx.closePath(); ctx.strokeStyle = '#ef4444'; ctx.lineWidth = 2; ctx.stroke();
    }
    for (let p of projectiles) {
      ctx.beginPath(); ctx.arc(WALL + p.x, WALL + p.y, 6, 0, Math.PI * 2); ctx.fillStyle = '#d946ef'; ctx.fill(); ctx.strokeStyle = '#4a044e'; ctx.lineWidth = 1; ctx.stroke();
      let speed = Math.hypot(p.vx, p.vy); let trailX = WALL + p.x - (p.vx / speed) * 8, trailY = WALL + p.y - (p.vy / speed) * 8;
      ctx.beginPath(); ctx.arc(trailX, trailY, 3, 0, Math.PI * 2); ctx.fillStyle = 'rgba(217, 70, 239, 0.4)'; ctx.fill(); 
    }
    
    if (gameStarted) for (let entity of entities) entity.draw();

    for (let e of effects) {
      let progress = 1 - (e.timer / e.maxTimer);
      if (e.type === 'slash') {
        ctx.save(); ctx.translate(WALL + e.x, WALL + e.y); ctx.rotate(e.angle); ctx.strokeStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.lineWidth = 3; ctx.lineCap = 'round';
        for (let i = -1; i <= 1; i++) { ctx.beginPath(); ctx.moveTo(0, i * 8 - 5); ctx.quadraticCurveTo(15, i * 8 + 5, 30, i * 8 - 5); ctx.stroke(); }
        ctx.restore();
      } 
      else if (e.type === 'trail') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 14, 0, Math.PI * 2); ctx.fillStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.fill(); }
      else if (e.type === 'revive') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 14 + progress * 60, 0, Math.PI * 2); ctx.strokeStyle = `rgba(255, 215, 0, ${1 - progress})`; ctx.lineWidth = 4; ctx.stroke(); }
      else if (e.type === 'ring') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 10 + progress * 20, 0, Math.PI * 2); ctx.strokeStyle = e.color; ctx.globalAlpha = 1 - progress; ctx.lineWidth = 2; ctx.stroke(); }
      else if (e.type === 'bubble') { ctx.beginPath(); ctx.arc(WALL + e.x, WALL + e.y, 2 + progress * 3, 0, Math.PI * 2); ctx.fillStyle = `rgba(217, 70, 239, ${1 - progress})`; ctx.fill(); }
    }
    ctx.globalAlpha = 1;

    for (let ft of floatingTexts) {
      let progress = 1 - (ft.timer / ft.maxTimer); 
      let scale = progress < 0.2 ? (progress / 0.2) : (1 - (progress - 0.2) / 0.8 * 0.5); 
      let fontSize = ft.isBig ? 22 : 16;
      ctx.save(); ctx.translate(WALL + ft.x, WALL + ft.y); ctx.scale(scale, scale);
      ctx.font = `bold ${fontSize}px sans-serif`; ctx.textAlign = 'center'; ctx.globalAlpha = Math.min(1, ft.timer * 2);
      ctx.lineWidth = 3; ctx.strokeStyle = '#000'; ctx.strokeText(ft.text, 0, 0);
      ctx.fillStyle = ft.color; ctx.fillText(ft.text, 0, 0); ctx.restore();
    }
  }

  const cards = document.querySelectorAll('.char-card');
  const btnStart = document.getElementById('btn-start');
  const btnRestart = document.getElementById('btn-restart');

  cards.forEach(card => {
    card.addEventListener('click', () => {
      const charName = card.dataset.char;
      if (card.classList.contains('selected')) { card.classList.remove('selected'); selectedChars = selectedChars.filter(c => c !== charName); }
      else {
        if (selectedChars.length >= 5) return;
        card.classList.add('selected'); selectedChars.push(charName);
      }
      btnStart.classList.toggle('enabled', selectedChars.length >= 2);
    });
  });

  btnStart.addEventListener('click', startBattle);
  btnRestart.addEventListener('click', () => {
    document.getElementById('result-layer').classList.add('hidden');
    document.getElementById('ui-layer').classList.remove('hidden');
    selectedChars = []; cards.forEach(c => c.classList.remove('selected')); btnStart.classList.remove('enabled');
    gameStarted = false; entities = []; traps = []; projectiles = []; groundEffects = []; floatingTexts = []; effects = []; stalactites = [];
    draw();
  });

  let last = performance.now();
  function loop(now) {
    let dt = Math.min((now - last) / 1000, 0.1); last = now;
    update(dt); draw(); requestAnimationFrame(loop);
  }
  requestAnimationFrame(loop);
})();
</script>
</body>
</html>

Game Source: 族群之争 · 终极乱斗

Creator: EpicCoder88

Libraries: none

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