🎮ArcadeLab

电子斗蛐蛐 - 精简完整版

by EpicCoder88
944 lines46.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">
<title>电子斗蛐蛐 - 精简完整版</title>
<style>
    body { margin: 0; padding: 0; background-color: #1a1a1a; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; font-family: sans-serif; color: white; }
    .hidden { display: none !important; }
    #menu { text-align: center; }
    #startBtn { padding: 15px 40px; font-size: 24px; background-color: #4CAF50; color: white; border: none; border-radius: 8px; cursor: pointer; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
    #startBtn:hover { background-color: #45a049; }
    #selectScreen { display: flex; flex-direction: column; align-items: center; gap: 20px; }
    #selectScreen h2 { font-weight: normal; color: #aaa; font-size: 18px; letter-spacing: 2px; }
    #heroList { display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; max-width: 90vw; }
    .hero-card { padding: 15px 30px; background-color: #2a2a2a; border: 2px solid #444; border-radius: 8px; font-size: 18px; cursor: pointer; transition: all 0.2s; user-select: none; }
    .hero-card:hover { background-color: #3a3a3a; }
    .hero-card.selected { border-color: #00ffcc; background-color: rgba(0, 255, 204, 0.1); color: #00ffcc; box-shadow: 0 0 15px rgba(0, 255, 204, 0.4); }
    #gameCanvas { background-color: #2a2a2a; box-shadow: 0 0 20px rgba(0,0,0,0.5); max-width: 100vw; max-height: 100vh; }
    #gameOverScreen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 30px; z-index: 10; }
    #gameOverText { font-size: 36px; font-weight: bold; }
    #restartBtn { padding: 15px 40px; font-size: 20px; background-color: #00ffcc; color: #111; border: none; border-radius: 8px; cursor: pointer; font-weight: bold; }
</style>
</head>
<body>

<div id="menu"><button id="startBtn">开始游戏</button></div>
<div id="selectScreen" class="hidden"><h2>选择两名英雄进行对决</h2><div id="heroList"></div></div>
<canvas id="gameCanvas" class="hidden" width="800" height="600"></canvas>
<div id="gameOverScreen" class="hidden">
    <div id="gameOverText">胜利!</div>
    <button id="restartBtn">重新选择英雄</button>
</div>

<script>
(function () {
'use strict';

// ==========================================
// ⚙️ 常量
// ==========================================
const CONFIG = { GRID_SIZE: 40, GRID_COLS: 24, GRID_ROWS: 18, PADDING: 60 };
const ARENA_WIDTH  = CONFIG.GRID_COLS * CONFIG.GRID_SIZE;
const ARENA_HEIGHT = CONFIG.GRID_ROWS * CONFIG.GRID_SIZE;
const MAP_WIDTH    = ARENA_WIDTH  + CONFIG.PADDING * 2;
const MAP_HEIGHT   = ARENA_HEIGHT + CONFIG.PADDING * 2;
const ARENA_X = CONFIG.PADDING;
const ARENA_Y = CONFIG.PADDING;
const MAX_DT = 0.05;

// ==========================================
// 📖 英雄图鉴(5个英雄)
// ==========================================
const HERO_LIST = [
    { id: 'unknown', name: '未知数', color: '#00ffcc', maxHp: 1000, speed: 130 },
    { id: 'hanshou', name: '寒守',   color: '#ff4444', maxHp: 1000, speed: 250 },
    { id: 'werewolf', name: '狼人',  color: '#aa00aa', maxHp: 1200, speed: 150 },
    { id: 'newton',  name: '牛顿',   color: '#dddddd', maxHp: 1100, speed: 120 },
    { id: 'pla',     name: 'PLA',    color: '#4a5d23', maxHp: 1000, speed: 120 }
];
const HERO_MAP = {};
HERO_LIST.forEach(h => { HERO_MAP[h.id] = h; });

// ==========================================
// 🎮 全局状态
// ==========================================
let gameState = 'menu';
let selectedHeroes = [];
let heroEntities = [];
let projectiles = [];
let damageTexts = [];
let slashEffects = [];
let impactEffects = [];
let muzzleFlashes = [];
let bleedEffects = [];
let newtonPrisms = [];
let apples = [];
let civilians = [];
let grenades = [];
let lastTime = 0;
let rafId = 0;
const camera = { x: 0, y: 0 };

const menu = document.getElementById('menu');
const startBtn = document.getElementById('startBtn');
const selectScreen = document.getElementById('selectScreen');
const heroList = document.getElementById('heroList');
const canvas = document.getElementById('gameCanvas');
const gameOverScreen = document.getElementById('gameOverScreen');
const gameOverText = document.getElementById('gameOverText');
const restartBtn = document.getElementById('restartBtn');
const ctx = canvas.getContext('2d');

// ==========================================
// 🎬 事件
// ==========================================
startBtn.addEventListener('click', () => {
    if (gameState !== 'menu') return;
    menu.classList.add('hidden');
    selectScreen.classList.remove('hidden');
    gameState = 'select';
    renderHeroSelection();
});

restartBtn.addEventListener('click', () => {
    cancelAnimationFrame(rafId);
    gameOverScreen.classList.add('hidden');
    canvas.classList.add('hidden');
    selectScreen.classList.remove('hidden');
    gameState = 'select';
    resetBattleState();
    renderHeroSelection();
});

// ==========================================
// 🎴 选人
// ==========================================
function renderHeroSelection() {
    heroList.innerHTML = '';
    selectedHeroes = [];
    HERO_LIST.forEach(hero => {
        const card = document.createElement('div');
        card.className = 'hero-card';
        card.textContent = hero.name;
        card.dataset.id = hero.id;
        card.addEventListener('click', () => toggleHeroSelection(hero.id, card));
        heroList.appendChild(card);
    });
}

function toggleHeroSelection(id, card) {
    if (gameState !== 'select') return;
    if (selectedHeroes.includes(id)) {
        selectedHeroes = selectedHeroes.filter(x => x !== id);
        card.classList.remove('selected');
        return;
    }
    if (selectedHeroes.length >= 2) return;
    selectedHeroes.push(id);
    card.classList.add('selected');
    if (selectedHeroes.length === 2) {
        gameState = 'starting';
        setTimeout(() => { if (gameState === 'starting') startBattle(); }, 400);
    }
}

function resetEffects() {
    projectiles = []; damageTexts = []; slashEffects = []; impactEffects = [];
    muzzleFlashes = []; bleedEffects = []; newtonPrisms = []; apples = [];
    civilians = []; grenades = []; window._plaCivTimer = 0;
}
function resetBattleState() { heroEntities = []; resetEffects(); }

// ==========================================
// ⚔️ 战斗开始
// ==========================================
function startBattle() {
    gameState = 'playing';
    selectScreen.classList.add('hidden');
    canvas.classList.remove('hidden');
    camera.x = MAP_WIDTH / 2 - canvas.width / 2;
    camera.y = MAP_HEIGHT / 2 - canvas.height / 2;
    heroEntities = selectedHeroes.map((id, i) => createHero(id, i));
    resetEffects();
    lastTime = performance.now();
    cancelAnimationFrame(rafId);
    rafId = requestAnimationFrame(gameLoop);
}

function createHero(id, index) {
    const db = HERO_MAP[id];
    const startX = index === 0 ? ARENA_X + 150 : ARENA_X + ARENA_WIDTH - 150;
    const startY = ARENA_Y + ARENA_HEIGHT / 2;
    const hero = {
        id: db.id, name: db.name, color: db.color,
        x: startX, y: startY, radius: 15,
        hp: db.maxHp, maxHp: db.maxHp, speed: db.speed,
        attackCooldown: 1000, attackRangeGrid: 1.5, attackTimer: 0, attackAnimTimer: 0,
        facingRight: index === 0, dr: 0, stunTimer: 0, pinnedBy: null,
        wallTimer: 0, bleedTickTimer: 0,
        wanderTimer: Math.random() * 2, wanderAngle: Math.random() * Math.PI * 2,
        wanderX: 0, wanderY: 0,
        // 通用状态
        weaponState: null, shotsFired: 0, shotgunShots: 0,
        hanshouState: 0, pinnedTarget: null, exhaustTimer: 0, chargeTimer: 0, hasSword: true,
        battleTimer: 0, enraged: false, dashCooldown: 0, isDashing: false, dashTimer: 0,
        dashDirX: 0, dashDirY: 0,
        prismCooldown: 0, appleTimer: 30000, satellites: [], satelliteTimer: 5000,
        clipAmmo: 0, maxClipAmmo: 0, isReloading: false, reloadTimer: 0,
        baseDamage: 0, damageMult: 1, damageBuffAdd: 0, damageBuffTimer: 0, reactionCooldown: 0
    };
    if (id === 'unknown') {
        hero.weaponState = 'pistol'; hero.shotsFired = 0; hero.shotgunShots = 0;
        hero.attackCooldown = 500; hero.attackRangeGrid = 10;
    }
    if (id === 'hanshou') {
        hero.attackCooldown = 1500; hero.attackRangeGrid = 1.125;
    }
    if (id === 'werewolf') {
        hero.attackCooldown = 1000; hero.attackRangeGrid = 1.5;
    }
    if (id === 'newton') {
        hero.prismCooldown = 5000; hero.appleTimer = 30000;
        hero.attackCooldown = 99999; hero.attackRangeGrid = 999;
        hero.satellites = []; hero.satelliteTimer = 5000;
    }
    if (id === 'pla') {
        hero.clipAmmo = 30; hero.maxClipAmmo = 30;
        hero.attackCooldown = 120; hero.attackRangeGrid = 8;
        hero.baseDamage = 10; hero.damageMult = 1;
        hero.damageBuffAdd = 0; hero.damageBuffTimer = 0; hero.reactionCooldown = 0;
    }
    return hero;
}

// ==========================================
// 🔄 主循环
// ==========================================
function gameLoop(timestamp) {
    if (gameState !== 'playing') return;
    const dt = Math.min((timestamp - lastTime) / 1000, MAX_DT);
    lastTime = timestamp;
    update(dt);
    if (gameState === 'playing') {
        updateCamera(dt);
        render();
        rafId = requestAnimationFrame(gameLoop);
    }
}

function updateCamera(dt) {
    if (heroEntities.length < 2) return;
    const a = heroEntities[0], b = heroEntities[1];
    let cx = (a.x + b.x) / 2, cy = (a.y + b.y) / 2;
    if (!isFinite(cx)) cx = MAP_WIDTH / 2;
    if (!isFinite(cy)) cy = MAP_HEIGHT / 2;
    const targetX = Math.max(0, Math.min(MAP_WIDTH - canvas.width, cx - canvas.width / 2));
    const targetY = Math.max(0, Math.min(MAP_HEIGHT - canvas.height, cy - canvas.height / 2));
    const k = Math.min(1, 8 * dt);
    camera.x += (targetX - camera.x) * k;
    camera.y += (targetY - camera.y) * k;
}

// ==========================================
// 🏃 逻辑更新
// ==========================================
function update(dt) {
    const alive = heroEntities.filter(h => h.hp > 0);
    if (alive.length <= 1) {
        gameState = 'gameover';
        showGameOver(alive);
        return;
    }
    for (const h of heroEntities) {
        if (h.attackTimer > 0) h.attackTimer -= dt * 1000;
        if (h.attackAnimTimer > 0) h.attackAnimTimer -= dt * 1000;
        if (h.stunTimer > 0) h.stunTimer -= dt * 1000;
        if (h.exhaustTimer > 0) h.exhaustTimer -= dt * 1000;
        if (h.dashCooldown > 0) h.dashCooldown -= dt * 1000;
        if (h.prismCooldown > 0) h.prismCooldown -= dt * 1000;
        if (h.appleTimer > 0) h.appleTimer -= dt * 1000;
        if (h.satelliteTimer > 0) h.satelliteTimer -= dt * 1000;
        if (h.reactionCooldown > 0) h.reactionCooldown -= dt * 1000;
        if (h.id === 'werewolf' && !h.enraged) h.battleTimer += dt;
        if (h.id === 'pla') {
            if (h.isReloading) { h.reloadTimer -= dt * 1000; if (h.reloadTimer <= 0) { h.isReloading = false; h.clipAmmo = h.maxClipAmmo; } }
            if (h.damageBuffTimer > 0) { h.damageBuffTimer -= dt * 1000; if (h.damageBuffTimer <= 0) h.damageBuffAdd = 0; }
        }
    }
    // 寒守重新拔剑
    for (const h of heroEntities) {
        if (h.id === 'hanshou' && h.exhaustTimer <= 0 && !h.hasSword && h.hp > 0) {
            h.hasSword = true;
            addDamageText(h.x, h.y - 30, '拔剑!', '#ffffff');
        }
    }
    // 随机游走
    for (const h of heroEntities) {
        if (h.wanderTimer > 0) h.wanderTimer -= dt;
        else { h.wanderTimer = 1.2 + Math.random() * 1.5; h.wanderAngle += (Math.random() - 0.5) * Math.PI * 1.8; }
        h.wanderX = Math.cos(h.wanderAngle) * h.speed * 0.5;
        h.wanderY = Math.sin(h.wanderAngle) * h.speed * 0.5;
    }
    // 逐英雄AI
    for (const hero of heroEntities) {
        if (hero.hp <= 0 || hero.pinnedBy) continue;
        const isImmune = hero.id === 'hanshou' && (hero.hanshouState === 1 || hero.hanshouState === 2);
        if (hero.stunTimer > 0 && !isImmune) continue;
        if (hero.id === 'unknown')       updateUnknown(hero, dt);
        else if (hero.id === 'hanshou')  updateHanshou(hero, dt);
        else if (hero.id === 'werewolf') updateWerewolf(hero, dt);
        else if (hero.id === 'newton')   updateNewton(hero, dt);
        else if (hero.id === 'pla')      updatePla(hero, dt);
    }
    // 钉墙流血
    for (const h of heroEntities) {
        if (h.pinnedBy === 'wall') {
            h.wallTimer -= dt * 1000; h.bleedTickTimer -= dt * 1000;
            if (h.bleedTickTimer <= 0) { h.bleedTickTimer = 500; applyDamage(h, 20, null, null); bleedEffects.push({ x: h.x, y: h.y, life: 0.5 }); }
            if (h.wallTimer <= 0) { h.pinnedBy = null; h.x += (h.x < ARENA_X + ARENA_WIDTH / 2) ? 30 : -30; constrainToArena(h); }
        }
    }
    resolveCollisions();
    updateProjectiles(dt);
    updateEffects(dt);
}

// ==========================================
// 🐾 英雄AI
// ==========================================
function updateUnknown(hero, dt) {
    const enemies = heroEntities.filter(h => h.id !== hero.id && h.hp > 0);
    if (!enemies.length) return;
    let target = enemies[0], best = Infinity;
    for (const e of enemies) { const d = Math.hypot(e.x - hero.x, e.y - hero.y); if (d < best) { best = d; target = e; } }
    const dx = target.x - hero.x, dy = target.y - hero.y;
    const distPx = Math.hypot(dx, dy) || 1, distGrid = distPx / CONFIG.GRID_SIZE;
    const dirX = dx / distPx, dirY = dy / distPx;
    hero.facingRight = dirX > 0;
    let moveX = 0, moveY = 0;
    if (distGrid < 3) {
        moveX = -dirX * hero.speed; moveY = -dirY * hero.speed;
        if (hero.x <= ARENA_X + hero.radius + 60 || hero.x >= ARENA_X + ARENA_WIDTH - hero.radius - 60) { moveX = 0; moveY = (hero.y > ARENA_Y + ARENA_HEIGHT / 2 ? -1 : 1) * hero.speed; }
        else if (hero.y <= ARENA_Y + hero.radius + 60 || hero.y >= ARENA_Y + ARENA_HEIGHT - hero.radius - 60) { moveX = (hero.x > ARENA_X + ARENA_WIDTH / 2 ? -1 : 1) * hero.speed; moveY = 0; }
    } else if (distGrid > 6) { moveX = dirX * hero.speed * 0.6; moveY = dirY * hero.speed * 0.6; }
    else { const tx = -dirY, ty = dirX; const slide = Math.sin(performance.now() / 1500) > 0 ? 1 : -1; moveX = tx * hero.speed * 0.6 * slide; moveY = ty * hero.speed * 0.6 * slide; }
    hero.x += (moveX + hero.wanderX * 0.4) * dt;
    hero.y += (moveY + hero.wanderY * 0.4) * dt;
    constrainToArena(hero);
    if (hero.attackTimer > 0) return;
    if (hero.weaponState === 'pistol') {
        if (hero.shotsFired < 8) {
            hero.shotsFired++;
            const dmg = Math.max(0, Math.round(-0.5 * distGrid * distGrid + 50));
            spawnProjectile({ x: hero.x, y: hero.y, startX: hero.x, startY: hero.y, target, speed: 800, damage: dmg, type: 'bullet', ownerId: hero.id, color: hero.color });
            muzzleFlashes.push({ x: hero.x + dirX * 18, y: hero.y + dirY * 18, life: 0.1, maxLife: 0.1 });
            hero.attackTimer = 500;
        } else {
            hero.weaponState = 'shotgun'; hero.shotgunShots = 0;
            spawnProjectile({ x: hero.x, y: hero.y, startX: hero.x, startY: hero.y, target, speed: 700, damage: 67, type: 'thrownPistol', ownerId: hero.id });
            hero.attackTimer = 800;
        }
    } else if (hero.weaponState === 'shotgun') {
        if (hero.shotgunShots < 2) {
            hero.shotgunShots++;
            const baseAngle = Math.atan2(dy, dx);
            for (let i = 0; i < 6; i++) {
                const angle = baseAngle + (i - 2.5) * 0.15;
                spawnProjectile({ x: hero.x, y: hero.y, startX: hero.x, startY: hero.y, vx: Math.cos(angle) * 600, vy: Math.sin(angle) * 600, damage: 25, type: 'shotgunPellet', ownerId: hero.id, lifetime: 0.6, color: '#ffaa00' });
            }
            hero.attackTimer = 1000;
            if (hero.shotgunShots >= 2) { hero.weaponState = 'pistol'; hero.shotsFired = 0; hero.shotgunShots = 0; }
        }
    }
}

function updateHanshou(hero, dt) {
    if (hero.exhaustTimer > 0) { hero.x += hero.wanderX * 0.3 * dt; hero.y += hero.wanderY * 0.3 * dt; constrainToArena(hero); return; }
    const enemies = heroEntities.filter(h => h.id !== hero.id && h.hp > 0);
    if (!enemies.length) return;
    const target = enemies[0];
    const dx = target.x - hero.x, dy = target.y - hero.y;
    const distPx = Math.hypot(dx, dy) || 1, distGrid = distPx / CONFIG.GRID_SIZE;
    const dirX = dx / distPx, dirY = dy / distPx;
    hero.facingRight = dirX > 0;
    if (hero.hanshouState === 0) {
        if (distGrid > hero.attackRangeGrid) {
            hero.x += dirX * hero.speed * dt; hero.y += dirY * hero.speed * dt; constrainToArena(hero);
        } else {
            if (hero.attackTimer <= 0 && hero.hasSword) {
                hero.attackTimer = hero.attackCooldown; hero.attackAnimTimer = 300;
                applyDamage(target, 58, hero, null);
                slashEffects.push({ x: hero.x, y: hero.y, angle: Math.atan2(dy, dx), life: 0.3, maxLife: 0.3, color: '#ff4444' });
                hero.dr = Math.min(76, hero.dr + 10);
                if (hero.dr >= 76) { hero.dr = 0; hero.hanshouState = 1; hero.chargeTimer = 2.0; addDamageText(hero.x, hero.y - 30, '泥头车!', '#ff00ff'); }
            }
        }
        return;
    }
    if (hero.hanshouState === 1) {
        hero.chargeTimer -= dt;
        hero.x += dirX * 200 * dt; hero.y += dirY * 200 * dt; constrainToArena(hero);
        const nd = Math.hypot(target.x - hero.x, target.y - hero.y);
        if (nd <= hero.radius + target.radius + 10) {
            applyDamage(target, 220, hero, null);
            hero.hanshouState = 2; hero.pinnedTarget = target; hero.chargeTimer = 4.0;
            addImpact(hero.x, hero.y, '#ff0000', 0.5);
        } else if (hero.chargeTimer <= 0) { hero.hanshouState = 0; hero.exhaustTimer = 1500; hero.dr = 0; }
        return;
    }
    if (hero.hanshouState === 2) {
        hero.chargeTimer -= dt;
        const pt = hero.pinnedTarget;
        if (!pt || pt.hp <= 0) { hero.hanshouState = 0; hero.exhaustTimer = 2000; hero.pinnedTarget = null; return; }
        const dL = hero.x - ARENA_X, dR = ARENA_X + ARENA_WIDTH - hero.x;
        const dT = hero.y - ARENA_Y, dB = ARENA_Y + ARENA_HEIGHT - hero.y;
        const m = Math.min(dL, dR, dT, dB);
        let px = 0, py = 0;
        if (m === dL) px = -1; else if (m === dR) px = 1; else if (m === dT) py = -1; else py = 1;
        hero.x += px * 400 * dt; hero.y += py * 400 * dt; constrainToArena(hero);
        pt.x = hero.x + px * (hero.radius + pt.radius + 2);
        pt.y = hero.y + py * (hero.radius + pt.radius + 2); constrainToArena(pt);
        const hitWall = pt.x <= ARENA_X + pt.radius + 1 || pt.x >= ARENA_X + ARENA_WIDTH - pt.radius - 1 || pt.y <= ARENA_Y + pt.radius + 1 || pt.y >= ARENA_Y + ARENA_HEIGHT - pt.radius - 1;
        if (hitWall || hero.chargeTimer <= 0) {
            pt.pinnedBy = 'wall'; pt.wallTimer = 4000; pt.bleedTickTimer = 0; pt.hasSwordInBack = true;
            hero.hanshouState = 0; hero.exhaustTimer = 6000; hero.pinnedTarget = null; hero.dr = 0; hero.hasSword = false;
            addImpact(hero.x, hero.y, '#ff0000', 0.6);
        }
    }
}

function updateWerewolf(hero, dt) {
    const enemies = heroEntities.filter(h => h.id !== hero.id && h.hp > 0);
    if (!enemies.length) return;
    const target = enemies[0];
    const dx = target.x - hero.x, dy = target.y - hero.y;
    const distPx = Math.hypot(dx, dy) || 1, distGrid = distPx / CONFIG.GRID_SIZE;
    const dirX = dx / distPx, dirY = dy / distPx;
    hero.facingRight = dirX > 0;
    if (!hero.enraged && hero.battleTimer >= 20) { hero.enraged = true; hero.speed *= 1.5; addDamageText(hero.x, hero.y - 30, '狂怒!', '#ff00ff'); addImpact(hero.x, hero.y, '#ff00ff', 0.5); }
    if (hero.isDashing) {
        hero.dashTimer -= dt;
        hero.x += hero.dashDirX * 200 * dt; hero.y += hero.dashDirY * 200 * dt; constrainToArena(hero);
        const nd = Math.hypot(target.x - hero.x, target.y - hero.y);
        if (nd <= hero.radius + target.radius + 10) {
            applyDamage(target, 100, hero, null);
            slashEffects.push({ x: hero.x, y: hero.y, angle: Math.atan2(dy, dx), life: 0.3, maxLife: 0.3, color: '#aa00aa' });
            hero.isDashing = false; hero.dashCooldown = 5000;
        } else if (hero.dashTimer <= 0) { hero.isDashing = false; hero.dashCooldown = 5000; }
        return;
    }
    if (hero.dashCooldown <= 0 && distGrid > 1.5 && distGrid < 2.5) {
        hero.isDashing = true; hero.dashTimer = 0.5;
        hero.dashDirX = dirX; hero.dashDirY = dirY;
        addDamageText(hero.x, hero.y - 30, '突进!', '#ff4444');
        return;
    }
    if (distGrid <= 1.5) {
        if (hero.attackTimer <= 0) {
            hero.attackTimer = hero.attackCooldown; hero.attackAnimTimer = 300;
            let dmg = hero.enraged ? 45 : 30;
            if (target.hp > 0 && target.hp / target.maxHp < 0.1) { dmg = target.hp; addDamageText(target.x, target.y - 30, '终结!', '#ff0000'); }
            applyDamage(target, dmg, hero, null);
            slashEffects.push({ x: hero.x, y: hero.y, angle: Math.atan2(dy, dx), life: 0.3, maxLife: 0.3, color: '#ff4444' });
        }
    } else {
        hero.x += (dirX * hero.speed + hero.wanderX * 0.5) * dt;
        hero.y += (dirY * hero.speed + hero.wanderY * 0.5) * dt;
        constrainToArena(hero);
    }
}

function updateNewton(hero, dt) {
    const enemies = heroEntities.filter(h => h.id !== hero.id && h.hp > 0);
    if (!enemies.length) return;
    const target = enemies[0];
    const dx = target.x - hero.x, dy = target.y - hero.y;
    const distPx = Math.hypot(dx, dy) || 1, distGrid = distPx / CONFIG.GRID_SIZE;
    const dirX = dx / distPx, dirY = dy / distPx;
    hero.facingRight = dirX > 0;
    let moveX = 0, moveY = 0;
    if (distGrid < 3) { moveX = -dirX * hero.speed; moveY = -dirY * hero.speed; }
    else if (distGrid > 6) { moveX = dirX * hero.speed * 0.5; moveY = dirY * hero.speed * 0.5; }
    else { let t = (Math.sin(performance.now() / 2000) > 0) ? 1 : -1; moveX = -dirY * t * hero.speed * 0.8; moveY = dirX * t * hero.speed * 0.8; }
    hero.x += (moveX + hero.wanderX * 0.4) * dt;
    hero.y += (moveY + hero.wanderY * 0.4) * dt;
    constrainToArena(hero);
    // 三棱镜
    if (hero.prismCooldown <= 0) {
        hero.prismCooldown = 5000;
        let px = hero.x + (target.x - hero.x) * 0.5, py = hero.y + (target.y - hero.y) * 0.5;
        let ba = Math.atan2(target.y - py, target.x - px);
        newtonPrisms.push({ x: px, y: py, timer: 10000, baseAngle: ba, angle: 0, sweepDir: 1 });
        addDamageText(hero.x, hero.y - 40, '🔺 三棱镜!', '#00ffff');
    }
    for (let i = newtonPrisms.length - 1; i >= 0; i--) {
        let p = newtonPrisms[i];
        p.timer -= dt * 1000;
        if (p.timer <= 0) { newtonPrisms.splice(i, 1); continue; }
        p.angle += p.sweepDir * 2.5 * dt;
        if (p.angle > Math.PI / 3) p.sweepDir = -1;
        if (p.angle < -Math.PI / 3) p.sweepDir = 1;
        for (let h of heroEntities) {
            if (h.id === 'newton' || h.hp <= 0) continue;
            let ddx = h.x - p.x, ddy = h.y - p.y, dd = Math.hypot(ddx, ddy);
            if (dd < 240) {
                let at = Math.atan2(ddy, ddx);
                let diff = Math.abs(at - p.baseAngle);
                while (diff > Math.PI) diff = Math.abs(diff - 2 * Math.PI);
                if (diff < p.angle + Math.PI / 6) applyDamage(h, 1, hero, null, true, true);
            }
        }
    }
    // 苹果雨
    if (hero.appleTimer <= 0) {
        hero.appleTimer = 30000;
        for (let i = 0; i < 10; i++) {
            if (apples.length >= 20) break;
            let ax = ARENA_X + Math.random() * ARENA_WIDTH, ay = ARENA_Y + Math.random() * ARENA_HEIGHT;
            let hit = false;
            for (let h of heroEntities) {
                if (h.id !== 'newton' && h.hp > 0 && Math.hypot(h.x - ax, h.y - ay) < h.radius + 10) { applyDamage(h, 40, hero, null); hit = true; break; }
            }
            if (!hit && apples.length < 20) apples.push({ x: ax, y: ay, radius: 8 });
        }
        addDamageText(hero.x, hero.y - 40, '🍎 苹果雨!', '#ff4444');
    }
    for (let i = apples.length - 1; i >= 0; i--) {
        let a = apples[i];
        for (let h of heroEntities) {
            if (h.hp <= 0 || h.pinnedBy) continue;
            if (Math.hypot(h.x - a.x, h.y - a.y) < h.radius + a.radius) {
                if (h.id === 'newton') { h.hp = Math.min(h.maxHp, h.hp + 50); addDamageText(h.x, h.y - 20, '+50', '#00ff00'); }
                else { h.hp = Math.min(h.maxHp, h.hp + 10); addDamageText(h.x, h.y - 20, '+10', '#00ff00'); }
                apples.splice(i, 1); break;
            }
        }
    }
    // 卫星
    if (hero.satellites) {
        hero.satelliteTimer -= dt * 1000;
        if (hero.satelliteTimer <= 0) {
            hero.satelliteTimer = 5000;
            if (Math.random() < 0.3 && hero.satellites.length < 5) hero.satellites.push({ angle: Math.random() * Math.PI * 2 });
        }
        const orbR = 2 * CONFIG.GRID_SIZE;
        for (let i = hero.satellites.length - 1; i >= 0; i--) {
            let s = hero.satellites[i]; s.angle += 2.5 * dt;
            let sx = hero.x + Math.cos(s.angle) * orbR, sy = hero.y + Math.sin(s.angle) * orbR;
            let hit = false;
            for (let h of heroEntities) {
                if (h.id === 'newton' || h.hp <= 0) continue;
                if (Math.hypot(h.x - sx, h.y - sy) < h.radius + 10) {
                    applyDamage(h, 40, hero, null); h.stunTimer = 1000;
                    impactEffects.push({ x: sx, y: sy, color: '#00ffff', life: 0.5, maxLife: 0.5 }); hit = true; break;
                }
            }
            if (hit) hero.satellites.splice(i, 1);
        }
    }
}

function updatePla(hero, dt) {
    const enemies = heroEntities.filter(h => h.id !== hero.id && h.hp > 0);
    if (!enemies.length) return;
    let target = enemies[0], best = Infinity;
    for (const e of enemies) { const d = Math.hypot(e.x - hero.x, e.y - hero.y); if (d < best) { best = d; target = e; } }
    const dx = target.x - hero.x, dy = target.y - hero.y;
    const distPx = Math.hypot(dx, dy) || 1, distGrid = distPx / CONFIG.GRID_SIZE;
    const dirX = dx / distPx, dirY = dy / distPx;
    hero.facingRight = dirX > 0;
    let moveX = 0, moveY = 0;
    if (distGrid < 4) { moveX = -dirX * hero.speed; moveY = -dirY * hero.speed; }
    else if (distGrid > 7) { moveX = dirX * hero.speed * 0.6; moveY = dirY * hero.speed * 0.6; }
    else { let t = (Math.sin(performance.now() / 1500) > 0) ? 1 : -1; moveX = -dirY * t * hero.speed * 0.6; moveY = dirX * t * hero.speed * 0.6; }
    hero.x += (moveX + hero.wanderX * 0.4) * dt;
    hero.y += (moveY + hero.wanderY * 0.4) * dt;
    constrainToArena(hero);
    // 人民刷新
    if (!window._plaCivTimer) window._plaCivTimer = 0;
    window._plaCivTimer -= dt * 1000;
    if (window._plaCivTimer <= 0) {
        window._plaCivTimer = 10000;
        if (civilians.length < 5) {
            let cx = ARENA_X + Math.random() * ARENA_WIDTH, cy = ARENA_Y + Math.random() * ARENA_HEIGHT;
            civilians.push({ x: cx, y: cy, radius: 10, hp: 300, maxHp: 300, wanderTimer: Math.random() * 2, wanderAngle: Math.random() * Math.PI * 2, wanderX: 0, wanderY: 0, isFleeing: false, fleeTimer: 0 });
        }
    }
    for (let i = civilians.length - 1; i >= 0; i--) {
        let c = civilians[i];
        if (c.hp <= 0) { civilians.splice(i, 1); continue; }
        if (c.wanderTimer > 0) c.wanderTimer -= dt;
        else { c.wanderTimer = 1.5 + Math.random() * 2; c.wanderAngle += (Math.random() - 0.5) * Math.PI * 2; }
        if (c.isFleeing) { c.fleeTimer -= dt; if (c.fleeTimer <= 0) c.isFleeing = false; let pla = heroEntities.find(h => h.id === 'pla' && h.hp > 0); if (pla) { let ddx = c.x - pla.x, ddy = c.y - pla.y, dd = Math.hypot(ddx, ddy) || 1; c.x += (ddx / dd) * 200 * dt; c.y += (ddy / dd) * 200 * dt; } }
        else { c.x += Math.cos(c.wanderAngle) * 80 * dt; c.y += Math.sin(c.wanderAngle) * 80 * dt; }
        constrainToArena(c);
    }
    if (!hero.isReloading && hero.attackTimer <= 0 && distGrid <= hero.attackRangeGrid) {
        if (hero.clipAmmo > 0) {
            hero.clipAmmo--; hero.attackTimer = hero.attackCooldown;
            let dmg = Math.round((hero.baseDamage + hero.damageBuffAdd) * hero.damageMult);
            spawnProjectile({ x: hero.x, y: hero.y, startX: hero.x, startY: hero.y, target, speed: 1000, damage: dmg, type: 'bullet', ownerId: hero.id, color: '#ffff00' });
            muzzleFlashes.push({ x: hero.x + dirX * 18, y: hero.y + dirY * 18, life: 0.05, maxLife: 0.05 });
            if (Math.random() < 0.1) grenades.push({ x: hero.x + dirX * 20, y: hero.y + dirY * 20, vx: dirX * 300, vy: dirY * 300, timer: 0.8 });
            if (hero.clipAmmo <= 0) { hero.isReloading = true; hero.reloadTimer = 500; }
        }
    }
    for (let i = grenades.length - 1; i >= 0; i--) {
        let g = grenades[i];
        g.timer -= dt; g.x += g.vx * dt; g.y += g.vy * dt; g.vx *= 0.95; g.vy *= 0.95;
        if (g.timer <= 0) {
            let r = 2 * CONFIG.GRID_SIZE;
            impactEffects.push({ x: g.x, y: g.y, color: '#ff8800', life: 0.5, maxLife: 0.5 });
            for (let h of heroEntities) if (h.hp > 0 && h.id !== 'pla' && Math.hypot(h.x - g.x, h.y - g.y) < r + h.radius) applyDamage(h, 50, hero, null);
            grenades.splice(i, 1);
        }
    }
}

// ==========================================
// 🧱 物理引擎
// ==========================================
function resolveCollisions() {
    for (let i = 0; i < heroEntities.length; i++) {
        for (let j = i + 1; j < heroEntities.length; j++) {
            const h1 = heroEntities[i], h2 = heroEntities[j];
            if (h1.hp <= 0 || h2.hp <= 0) continue;
            if (h1.isDashing || h2.isDashing) continue;
            if (h1.id === 'hanshou' && (h1.hanshouState === 1 || h1.hanshouState === 2)) continue;
            if (h2.id === 'hanshou' && (h2.hanshouState === 1 || h2.hanshouState === 2)) continue;
            let dx = h2.x - h1.x, dy = h2.y - h1.y;
            let dist = Math.hypot(dx, dy);
            const minDist = h1.radius + h2.radius;
            if (dist < 0.01) { dx = 0.01; dy = 0; dist = 0.01; }
            if (dist < minDist) {
                const overlap = minDist - dist;
                const nx = dx / dist, ny = dy / dist;
                h1.x -= nx * overlap * 0.5; h1.y -= ny * overlap * 0.5;
                h2.x += nx * overlap * 0.5; h2.y += ny * overlap * 0.5;
            }
        }
    }
    for (const h of heroEntities) constrainToArena(h);
}

function constrainToArena(h) {
    if (!isFinite(h.x)) h.x = ARENA_X + ARENA_WIDTH / 2;
    if (!isFinite(h.y)) h.y = ARENA_Y + ARENA_HEIGHT / 2;
    if (h.x < ARENA_X + h.radius) { h.x = ARENA_X + h.radius; h.wanderAngle += Math.PI; }
    if (h.x > ARENA_X + ARENA_WIDTH - h.radius) { h.x = ARENA_X + ARENA_WIDTH - h.radius; h.wanderAngle += Math.PI; }
    if (h.y < ARENA_Y + h.radius) { h.y = ARENA_Y + h.radius; h.wanderAngle += Math.PI; }
    if (h.y > ARENA_Y + ARENA_HEIGHT - h.radius) { h.y = ARENA_Y + ARENA_HEIGHT - h.radius; h.wanderAngle += Math.PI; }
}

// ==========================================
// 💥 伤害
// ==========================================
function applyDamage(target, amount, attacker, projectile, isTrueDamage, silent) {
    if (!target || target.hp <= 0) return;
    if (!isFinite(amount)) amount = 0;
    amount = Math.max(0, amount);
    if (!isTrueDamage && target.dr > 0) amount = amount * (1 - target.dr / 100);
    amount = Math.round(amount);
    target.hp -= amount;
    if (!silent) addDamageText(target.x, target.y, amount, isTrueDamage ? '#ff00ff' : '#ff6666');
    if (target.id === 'hanshou' && target.exhaustTimer <= 0 && target.hanshouState === 0) target.dr = Math.max(0, target.dr - 5);
}

// ==========================================
// 🚀 弹道
// ==========================================
function spawnProjectile(opts) {
    projectiles.push(Object.assign({ x: 0, y: 0, startX: 0, startY: 0, target: null, speed: 600, damage: 0, type: 'bullet', ownerId: null, vx: undefined, vy: undefined, lifetime: undefined, knockback: 0, color: null, text: null }, opts));
}

function updateProjectiles(dt) {
    for (let i = projectiles.length - 1; i >= 0; i--) {
        const p = projectiles[i];
        if (p.target && p.target.hp <= 0) { projectiles.splice(i, 1); continue; }
        let hit = null;
        if (p.vx !== undefined && p.vy !== undefined) {
            p.x += p.vx * dt; p.y += p.vy * dt; p.lifetime -= dt;
            if (p.lifetime <= 0) { projectiles.splice(i, 1); continue; }
            if (p.x < -50 || p.x > MAP_WIDTH + 50 || p.y < -50 || p.y > MAP_HEIGHT + 50) { projectiles.splice(i, 1); continue; }
            for (const h of heroEntities) { if (h.hp > 0 && h.id !== p.ownerId && Math.hypot(h.x - p.x, h.y - p.y) < h.radius + 8) { hit = h; break; } }
        } else if (p.target) {
            const t = p.target;
            const dx = t.x - p.x, dy = t.y - p.y, d = Math.hypot(dx, dy) || 1;
            if (d < t.radius + 8) hit = t;
            else { p.x += (dx / d) * p.speed * dt; p.y += (dy / d) * p.speed * dt; }
        }
        if (hit) {
            const attacker = p.ownerId ? heroEntities.find(h => h.id === p.ownerId) || null : null;
            applyDamage(hit, p.damage, attacker, p);
            if (p.knockback > 0) {
                const dx = hit.x - p.x, dy = hit.y - p.y, d = Math.hypot(dx, dy) || 1;
                hit.x += (dx / d) * p.knockback * CONFIG.GRID_SIZE;
                hit.y += (dy / d) * p.knockback * CONFIG.GRID_SIZE;
                constrainToArena(hit);
            }
            if (p.type === 'thrownPistol' && attacker && attacker.id === 'unknown') { attacker.weaponState = 'shotgun'; attacker.shotgunShots = 0; attacker.shotsFired = 0; }
            addImpact(p.x, p.y, '#ffffff', 0.2);
            projectiles.splice(i, 1);
        }
    }
    // 非PLA子弹伤害人民
    for (let i = projectiles.length - 1; i >= 0; i--) {
        let p = projectiles[i];
        if (p.ownerId === 'pla') continue;
        for (let c of civilians) {
            if (c.hp <= 0) continue;
            if (Math.hypot(c.x - p.x, c.y - p.y) < c.radius + 8) {
                c.hp -= p.damage; c.isFleeing = true; c.fleeTimer = 3;
                let pla = heroEntities.find(h => h.id === 'pla' && h.hp > 0);
                if (pla) {
                    pla.damageBuffAdd += 12; pla.damageBuffTimer = 30000;
                    if (c.hp <= 0) pla.damageMult *= 2;
                }
                projectiles.splice(i, 1); break;
            }
        }
    }
}

// ==========================================
// ✨ 特效
// ==========================================
function updateEffects(dt) {
    for (let i = muzzleFlashes.length - 1; i >= 0; i--) { muzzleFlashes[i].life -= dt; if (muzzleFlashes[i].life <= 0) muzzleFlashes.splice(i, 1); }
    for (let i = slashEffects.length - 1; i >= 0; i--) { slashEffects[i].life -= dt; if (slashEffects[i].life <= 0) slashEffects.splice(i, 1); }
    for (let i = impactEffects.length - 1; i >= 0; i--) { impactEffects[i].life -= dt; if (impactEffects[i].life <= 0) impactEffects.splice(i, 1); }
    for (let i = damageTexts.length - 1; i >= 0; i--) { damageTexts[i].y -= 30 * dt; damageTexts[i].life -= dt; if (damageTexts[i].life <= 0) damageTexts.splice(i, 1); }
    for (let i = bleedEffects.length - 1; i >= 0; i--) { bleedEffects[i].life -= dt; if (bleedEffects[i].life <= 0) bleedEffects.splice(i, 1); }
}

function addDamageText(x, y, amount, color) { damageTexts.push({ x, y: y - 20, text: String(amount), color: color || '#fff', life: 1.0 }); }
function addImpact(x, y, color, maxLife) { maxLife = maxLife || 0.3; impactEffects.push({ x, y, color, life: maxLife, maxLife }); }

function showGameOver(survivors) {
    gameOverScreen.classList.remove('hidden');
    canvas.classList.add('hidden');
    if (survivors.length === 1) { gameOverText.innerText = survivors[0].name + ' 胜利!'; gameOverText.style.color = survivors[0].color; }
    else { gameOverText.innerText = '同归于尽!'; gameOverText.style.color = '#fff'; }
}

// ==========================================
// 🎨 渲染
// ==========================================
function render() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();
    ctx.translate(-camera.x, -camera.y);
    drawArena();
    drawApples();
    drawNewtonPrisms();
    drawEffects();
    drawGrenades();
    drawProjectiles();
    drawEntities();
    drawCivilians();
    drawDamageTexts();
    ctx.restore();
}

function drawArena() {
    ctx.fillStyle = '#2a2a2a'; ctx.fillRect(0, 0, MAP_WIDTH, MAP_HEIGHT);
    ctx.fillStyle = '#3a3a3a'; ctx.fillRect(ARENA_X, ARENA_Y, ARENA_WIDTH, ARENA_HEIGHT);
    ctx.strokeStyle = '#00ffcc'; ctx.lineWidth = 6; ctx.strokeRect(ARENA_X, ARENA_Y, ARENA_WIDTH, ARENA_HEIGHT);
    ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)'; ctx.lineWidth = 1; ctx.beginPath();
    for (let i = 1; i < CONFIG.GRID_COLS; i++) { const x = ARENA_X + i * CONFIG.GRID_SIZE; ctx.moveTo(x, ARENA_Y); ctx.lineTo(x, ARENA_Y + ARENA_HEIGHT); }
    for (let i = 1; i < CONFIG.GRID_ROWS; i++) { const y = ARENA_Y + i * CONFIG.GRID_SIZE; ctx.moveTo(ARENA_X, y); ctx.lineTo(ARENA_X + ARENA_WIDTH, y); }
    ctx.stroke();
}

function drawApples() {
    for (let a of apples) {
        ctx.beginPath(); ctx.arc(a.x, a.y, a.radius, 0, Math.PI * 2);
        ctx.fillStyle = '#ff4444'; ctx.fill();
        ctx.strokeStyle = '#ff0000'; ctx.lineWidth = 2; ctx.stroke();
        ctx.fillStyle = '#00ff00';
        ctx.beginPath(); ctx.ellipse(a.x, a.y - a.radius - 2, 4, 2, Math.PI / 4, 0, Math.PI * 2); ctx.fill();
    }
}

function drawNewtonPrisms() {
    for (let p of newtonPrisms) {
        ctx.save(); ctx.translate(p.x, p.y); ctx.rotate(p.baseAngle);
        ctx.beginPath(); ctx.moveTo(0, 0);
        ctx.arc(0, 0, 240, -Math.PI / 6 + p.angle, Math.PI / 6 + p.angle);
        ctx.closePath(); ctx.fillStyle = 'rgba(255, 255, 255, 0.08)'; ctx.fill();
        for (let i = 0; i < 7; i++) {
            let a = -Math.PI / 6 + p.angle + (i / 6) * (Math.PI / 3);
            ctx.beginPath(); ctx.moveTo(0, 0);
            ctx.lineTo(Math.cos(a) * 240, Math.sin(a) * 240);
            ctx.strokeStyle = `hsl(${i * 51}, 100%, 50%)`; ctx.lineWidth = 2; ctx.stroke();
        }
        ctx.beginPath(); ctx.moveTo(0, -12); ctx.lineTo(10, 8); ctx.lineTo(-10, 8); ctx.closePath();
        ctx.fillStyle = '#fff'; ctx.fill(); ctx.strokeStyle = '#aaa'; ctx.lineWidth = 1; ctx.stroke();
        ctx.restore();
    }
}

function drawGrenades() {
    for (let g of grenades) {
        ctx.beginPath(); ctx.arc(g.x, g.y, 8, 0, Math.PI * 2);
        ctx.fillStyle = '#333'; ctx.fill(); ctx.strokeStyle = '#ff8800'; ctx.lineWidth = 3; ctx.stroke();
        ctx.beginPath(); ctx.arc(g.x, g.y - 10, 4, 0, Math.PI * 2); ctx.fillStyle = '#ffff00'; ctx.fill();
    }
}

function drawCivilians() {
    for (let c of civilians) {
        if (c.hp <= 0) continue;
        ctx.beginPath(); ctx.arc(c.x, c.y, c.radius, 0, Math.PI * 2);
        ctx.fillStyle = '#fff'; ctx.fill(); ctx.strokeStyle = '#aaa'; ctx.lineWidth = 2; ctx.stroke();
        const bw = 30, bh = 4, bx = c.x - bw / 2, by = c.y - c.radius - 10;
        const hp = c.hp / c.maxHp;
        ctx.fillStyle = '#333'; ctx.fillRect(bx, by, bw, bh);
        ctx.fillStyle = hp > 0.3 ? '#00ff00' : '#ff0000'; ctx.fillRect(bx, by, bw * hp, bh);
    }
}

function drawEffects() {
    muzzleFlashes.forEach(e => {
        const a = Math.max(0, Math.min(1, e.life / (e.maxLife || 0.1)));
        ctx.beginPath(); ctx.arc(e.x, e.y, 8, 0, Math.PI * 2); ctx.fillStyle = `rgba(255, 255, 255, ${a})`; ctx.fill();
    });
    slashEffects.forEach(e => {
        ctx.save(); ctx.translate(e.x, e.y); ctx.rotate(e.angle);
        const a = Math.max(0, Math.min(1, e.life / (e.maxLife || 0.3)));
        ctx.beginPath(); ctx.arc(0, 0, 35, -Math.PI / 4, Math.PI / 4);
        if (e.color) {
            const r = parseInt(e.color.slice(1, 3), 16), g = parseInt(e.color.slice(3, 5), 16), b = parseInt(e.color.slice(5, 7), 16);
            ctx.strokeStyle = `rgba(${r},${g},${b},${a})`;
        } else ctx.strokeStyle = `rgba(255, 68, 68, ${a})`;
        ctx.lineWidth = 6; ctx.stroke(); ctx.restore();
    });
    impactEffects.forEach(e => {
        const t = 1 - Math.max(0, Math.min(1, e.life / (e.maxLife || 0.3)));
        ctx.beginPath(); ctx.arc(e.x, e.y, Math.max(0.5, t * 20), 0, Math.PI * 2);
        ctx.strokeStyle = e.color || '#fff'; ctx.lineWidth = 3; ctx.stroke();
    });
    bleedEffects.forEach(e => {
        const a = Math.max(0, Math.min(1, e.life / 0.5));
        ctx.beginPath(); ctx.arc(e.x, e.y, 6, 0, Math.PI * 2); ctx.fillStyle = `rgba(255, 0, 0, ${a})`; ctx.fill();
    });
}

function drawProjectiles() {
    for (const p of projectiles) {
        if (p.type === 'thrownPistol') {
            ctx.save(); ctx.translate(p.x, p.y);
            if (p.target) ctx.rotate(Math.atan2(p.target.y - p.startY, p.target.x - p.startX));
            ctx.fillStyle = '#888'; ctx.fillRect(-10, -4, 20, 8); ctx.restore();
        } else if (p.type === 'shotgunPellet') {
            ctx.beginPath(); ctx.arc(p.x, p.y, 4, 0, Math.PI * 2); ctx.fillStyle = '#ffaa00'; ctx.fill();
        } else {
            ctx.beginPath(); ctx.moveTo(p.startX, p.startY); ctx.lineTo(p.x, p.y);
            ctx.strokeStyle = p.color || 'rgba(0, 255, 204, 0.8)'; ctx.lineWidth = 5; ctx.stroke();
            ctx.beginPath(); ctx.arc(p.x, p.y, 6, 0, Math.PI * 2);
            ctx.fillStyle = '#fff'; ctx.fill(); ctx.strokeStyle = p.color || '#00ffcc'; ctx.lineWidth = 2; ctx.stroke();
        }
    }
}

function drawEntities() {
    for (const e of heroEntities) {
        ctx.globalAlpha = e.hp > 0 ? 1.0 : 0.2;
        let scale = 1.0;
        if (e.attackAnimTimer > 0) scale = 1.0 + (e.attackAnimTimer / 300) * 0.3;
        let fillColor = e.color;
        if (e.id === 'hanshou') { if (e.exhaustTimer > 0) fillColor = '#555'; else if (e.hanshouState === 1) fillColor = '#ff00ff'; }
        if (e.stunTimer > 0) fillColor = '#ffff00';
        if (e.id === 'werewolf' && e.enraged) fillColor = '#cc0000';
        ctx.save(); ctx.translate(e.x, e.y);
        if (!e.facingRight) ctx.scale(-1, 1);
        ctx.scale(scale, scale);
        if (e.id === 'pla') {
            ctx.beginPath(); ctx.arc(0, 0, e.radius, 0, Math.PI * 2);
            ctx.fillStyle = '#4a5d23'; ctx.fill();
            ctx.strokeStyle = '#ffcc00'; ctx.lineWidth = 3; ctx.stroke();
            ctx.fillStyle = '#ffcc00'; ctx.font = 'bold 12px sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
            ctx.fillText('PLA', 0, 1);
        } else {
            ctx.beginPath(); ctx.arc(0, 0, e.radius, 0, Math.PI * 2);
            ctx.fillStyle = fillColor; ctx.fill();
            ctx.strokeStyle = 'rgba(255,255,255,0.8)'; ctx.lineWidth = 2; ctx.stroke();
        }
        if (e.id === 'werewolf') {
            ctx.beginPath(); ctx.moveTo(-8, -e.radius); ctx.lineTo(-14, -e.radius - 12); ctx.lineTo(-2, -e.radius); ctx.fill();
            ctx.beginPath(); ctx.moveTo(8, -e.radius); ctx.lineTo(14, -e.radius - 12); ctx.lineTo(2, -e.radius); ctx.fill();
        }
        if (e.id === 'unknown') {
            ctx.fillStyle = '#666';
            if (e.weaponState === 'pistol') { ctx.fillRect(e.radius - 2, -5, 12, 10); ctx.fillStyle = '#333'; ctx.fillRect(e.radius - 2, 5, 6, 6); }
            else if (e.weaponState === 'shotgun') { ctx.fillRect(e.radius - 2, -7, 22, 14); ctx.fillStyle = '#333'; ctx.fillRect(e.radius - 2, 7, 8, 8); }
        }
        if (e.id === 'newton') { ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.moveTo(0, -6); ctx.lineTo(6, 6); ctx.lineTo(-6, 6); ctx.closePath(); ctx.fill(); }
        if (e.id === 'hanshou' && e.hasSword) {
            ctx.fillStyle = e.hanshouState === 1 ? '#ffcc00' : '#dddddd';
            ctx.fillRect(e.radius - 2, -3, 20, 6);
            ctx.fillStyle = '#666'; ctx.fillRect(e.radius - 6, -2, 6, 4);
            ctx.beginPath(); ctx.moveTo(e.radius + 18, -3); ctx.lineTo(e.radius + 26, 0); ctx.lineTo(e.radius + 18, 3); ctx.closePath();
            ctx.fillStyle = e.hanshouState === 1 ? '#ffcc00' : '#dddddd'; ctx.fill();
        }
        ctx.restore();
        if (e.hasSwordInBack && e.pinnedBy === 'wall') {
            ctx.save(); ctx.translate(e.x, e.y); ctx.rotate(Math.PI / 4);
            ctx.fillStyle = '#dddddd'; ctx.fillRect(-3, -20, 6, 30);
            ctx.fillStyle = '#666'; ctx.fillRect(-6, -26, 12, 6); ctx.restore();
        }
        const bw = 50, bh = 5, bx = e.x - bw / 2, by = e.y - e.radius - 15;
        const hp = Math.max(0, e.hp) / e.maxHp;
        ctx.fillStyle = '#333'; ctx.fillRect(bx, by, bw, bh);
        ctx.fillStyle = hp > 0.3 ? '#00ff00' : '#ff0000'; ctx.fillRect(bx, by, bw * hp, bh);
        if (e.id === 'hanshou') {
            const drY = by + bh + 2;
            ctx.fillStyle = '#333'; ctx.fillRect(bx, drY, bw, 3);
            ctx.fillStyle = '#ffcc00'; ctx.fillRect(bx, drY, bw * (e.dr / 76), 3);
        }
        if (e.id === 'werewolf' && !e.enraged) {
            const tY = by + bh + 2;
            ctx.fillStyle = '#333'; ctx.fillRect(bx, tY, bw, 3);
            ctx.fillStyle = '#ff00aa'; ctx.fillRect(bx, tY, bw * Math.min(1, e.battleTimer / 20), 3);
        }
        if (e.id === 'newton') {
            const tY = by + bh + 2;
            ctx.fillStyle = '#333'; ctx.fillRect(bx, tY, bw, 3);
            ctx.fillStyle = '#00ffff'; ctx.fillRect(bx, tY, bw * (1 - e.prismCooldown / 5000), 3);
            ctx.fillStyle = '#ff4444'; ctx.fillRect(bx, tY + 4, bw * (1 - e.appleTimer / 30000), 3);
        }
        if (e.id === 'pla') {
            const tY = by + bh + 2;
            ctx.fillStyle = '#333'; ctx.fillRect(bx, tY, bw, 3);
            ctx.fillStyle = '#ffff00'; ctx.fillRect(bx, tY, bw * (e.clipAmmo / e.maxClipAmmo), 3);
        }
        ctx.fillStyle = '#fff'; ctx.font = '11px sans-serif'; ctx.textAlign = 'center';
        ctx.fillText(e.name, e.x, by - 5);
        if (e.id === 'newton' && e.satellites) {
            const orbR = 2 * CONFIG.GRID_SIZE;
            for (let s of e.satellites) {
                let sx = e.x + Math.cos(s.angle) * orbR, sy = e.y + Math.sin(s.angle) * orbR;
                ctx.beginPath(); ctx.arc(sx, sy, 8, 0, Math.PI * 2);
                ctx.fillStyle = '#00ffff'; ctx.fill();
                ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; ctx.stroke();
            }
        }
        ctx.globalAlpha = 1.0;
    }
}

function drawDamageTexts() {
    for (const t of damageTexts) {
        ctx.fillStyle = t.color; ctx.font = 'bold 14px sans-serif'; ctx.textAlign = 'center';
        ctx.globalAlpha = Math.max(0, Math.min(1, t.life));
        ctx.fillText(t.text, t.x, t.y);
    }
    ctx.globalAlpha = 1.0;
}

})();
</script>
</body>
</html>

Game Source: 电子斗蛐蛐 - 精简完整版

Creator: EpicCoder88

Libraries: none

Complexity: complex (944 lines, 46.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: game-epiccoder88-muhzxbno" to link back to the original. Then publish at arcadelab.ai/publish.