'use client' import { useEffect, useRef, useState, useCallback } from 'react' const TOWER_META = [ { type: 'outsource', name: '外包程序员', cost: 30, desc: '近战 8伤 0.7/s', color: '#94A3B8', img: '/game-assets/tower-outsource.png', tip: '廉价但30%丢空,5%自伤' }, { type: 'intern', name: '00后实习生', cost: 50, desc: '近战 15伤 1.5/s', color: '#22C55E', img: '/game-assets/tower-intern.png', tip: '整顿职场:5%概率秒杀' }, { type: 'hrbp', name: 'HRBP', cost: 80, desc: '辅助 +20%攻速', color: '#EC4899', img: '/game-assets/tower-hrbp.png', tip: '打鸡血:周围塔攻速+20%' }, { type: 'ops', name: '运营专员', cost: 90, desc: '远程 18伤 范围溅射', color: '#8B5CF6', img: '/game-assets/tower-ops.png', tip: '增长黑客:20%双倍HC' }, { type: 'ppt', name: 'PPT大师', cost: 100, desc: 'AOE减速 5伤', color: '#F59E0B', img: '/game-assets/tower-ppt.png', tip: '黑话领域:减速40%' }, { type: 'senior', name: 'P6资深开发', cost: 120, desc: '远程 30伤 5格射程', color: '#3B82F6', img: '/game-assets/tower-senior.png', tip: '代码屎山:附带持续伤害' }, { type: 'pm', name: '产品经理', cost: 160, desc: '远程 20伤 需求变更', color: '#06B6D4', img: '/game-assets/tower-pm.png', tip: '需求变更:每4次把怪打回去' }, ] as const type TowerType = 'outsource' | 'intern' | 'hrbp' | 'ops' | 'ppt' | 'senior' | 'pm' export default function GamePage() { const gameRef = useRef<{ destroy: (removeCanvas: boolean) => void } | null>(null) const [hc, setHc] = useState(200) const [selectedTower, setSelectedTower] = useState(null) const [gameReady, setGameReady] = useState(false) const selectedTowerRef = useRef(null) const handleSelectTower = useCallback((type: TowerType) => { const next = selectedTowerRef.current === type ? null : type selectedTowerRef.current = next setSelectedTower(next) if (typeof window !== 'undefined') { (window as any).__gameSelectTower?.(next) } }, []) useEffect(() => { let mounted = true const initGame = async () => { const Phaser = (await import('phaser')).default const { createGameConfig } = await import('@/game/config') const { createGameScene } = await import('@/game/GameScene') if (!mounted) return // 从 localStorage 读取难度,通过 window 变量传给场景 if (typeof window !== 'undefined') { const storedDifficulty = localStorage.getItem('game-difficulty') if (storedDifficulty === 'easy' || storedDifficulty === 'normal' || storedDifficulty === 'hard') { ;(window as any).__gameDifficulty = storedDifficulty } else { ;(window as any).__gameDifficulty = 'normal' } } const GameScene = createGameScene(Phaser) const config = createGameConfig('game-canvas-container') config.scene = [GameScene] if (config.scale) { config.scale.mode = Phaser.Scale.FIT config.scale.autoCenter = Phaser.Scale.CENTER_BOTH } config.type = Phaser.AUTO if (typeof window !== 'undefined') { (window as any).__gameOnHCChange = (val: number) => { if (mounted) setHc(val) } (window as any).__gameOnTowerDeselect = () => { if (mounted) { selectedTowerRef.current = null setSelectedTower(null) } } ;(window as any).__gameReady = () => { if (mounted) setGameReady(true) } } gameRef.current = new Phaser.Game(config) } initGame().catch(console.error) return () => { mounted = false gameRef.current?.destroy(true) gameRef.current = null if (typeof window !== 'undefined') { delete (window as any).__gameOnHCChange delete (window as any).__gameOnTowerDeselect delete (window as any).__gameSelectTower delete (window as any).__gameReady delete (window as any).__gameDifficulty } } }, []) return (
{/* Phaser canvas 区域 */}
{/* 底部塔选择面板 */}
{/* 选塔提示 */}
{selectedTower ? '点击格子建造' : '选择塔 ▼'}
{TOWER_META.map((meta) => { const canAfford = hc >= meta.cost const isSelected = selectedTower === meta.type return ( ) })}
) }