Files
test1/game/GameScene.ts

252 lines
8.9 KiB
TypeScript

import type Phaser from 'phaser'
import { MAP_COLS, MAP_ROWS, HUD_HEIGHT, GAME_HEIGHT, GAME_WIDTH } from './constants'
import { GameManager } from './GameManager'
import {
PATH_TILES,
getCellSize,
drawAllTiles,
renderMapLabels,
renderHUD,
updateKPIBar,
BAR_X, BAR_Y, BAR_W, BAR_H,
} from './mapRenderer'
import { TowerManager, type TowerType } from './towers/TowerManager'
import { WaveManager } from './enemies/WaveManager'
import { HUD } from './ui/HUD'
import { WeeklyReportModal } from './ui/WeeklyReportModal'
// Suppress unused-variable warnings for exported constants
void MAP_ROWS; void GAME_HEIGHT; void GAME_WIDTH
void BAR_X; void BAR_Y; void BAR_W; void BAR_H
/** 所有游戏精灵的 key → public path 映射 */
const SPRITE_ASSETS: Record<string, string> = {
'tower-intern': '/game-assets/tower-intern.png',
'tower-senior': '/game-assets/tower-senior.png',
'tower-ppt': '/game-assets/tower-ppt.png',
'tower-hrbp': '/game-assets/tower-hrbp.png',
'enemy-fresh': '/game-assets/enemy-fresh.png',
'enemy-old': '/game-assets/enemy-old.png',
'enemy-trouble': '/game-assets/enemy-trouble.png',
'enemy-boss': '/game-assets/enemy-boss.png',
}
export function createGameScene(PhaserLib: typeof Phaser): typeof Phaser.Scene {
class GameScene extends PhaserLib.Scene {
private manager!: GameManager
private kpiBar!: Phaser.GameObjects.Graphics
private kpiText!: Phaser.GameObjects.Text
private hcText!: Phaser.GameObjects.Text
private hoveredTile: { col: number; row: number } | null = null
private tileGraphics!: Phaser.GameObjects.Graphics
private towerManager!: TowerManager
private waveManager!: WaveManager
private hud!: HUD
private weeklyModal!: WeeklyReportModal
private selectedTowerType: TowerType | null = null
private buildModeGraphics!: Phaser.GameObjects.Graphics
private isWaveRunning: boolean = false
constructor() { super({ key: 'GameScene' }) }
preload(): void {
// 加载所有 AI 生成的角色图片
for (const [key, path] of Object.entries(SPRITE_ASSETS)) {
this.load.image(key, path)
}
}
create(): void {
this.manager = GameManager.getInstance()
this.manager.reset()
this.manager.gameState = 'playing'
this.tileGraphics = this.add.graphics()
drawAllTiles(this.tileGraphics, null)
renderMapLabels(this)
const hudObjs = renderHUD(this)
this.kpiBar = hudObjs.kpiBar
this.kpiText = hudObjs.kpiText
this.hcText = hudObjs.hcText
this.buildModeGraphics = this.add.graphics().setDepth(5)
this.towerManager = new TowerManager(this)
this.weeklyModal = new WeeklyReportModal({
onBossInspection: () => this.freezeAllTowers(3000),
onFullStamina: () => this.refillAllStamina(),
})
this.waveManager = new WaveManager(
this,
() => this.onWeeklyReport(),
() => this.onAllWavesComplete(),
() => this.towerManager.removeRandomTower()
)
this.hud = new HUD(this)
this.hud.createWaveButton(() => this.onWaveButtonClick())
// 接收 React 层传来的选塔事件
if (typeof window !== 'undefined') {
;(window as any).__gameSelectTower = (type: TowerType | null) => {
this.selectedTowerType = type
if (!type) this.buildModeGraphics.clear()
}
}
this.setupInteraction()
// HC/KPI 变化时同步到 React HUD
this.manager.onHCChange.push((hc: number) => {
this.hcText.setText(`HC: ${hc}`)
// 通知 React 层更新塔面板可用状态
if (typeof window !== 'undefined') {
;(window as any).__gameOnHCChange?.(hc)
}
})
this.manager.onKPIChange.push((kpi: number) => {
updateKPIBar(this.kpiBar, kpi)
this.kpiText.setText(`${kpi}%`)
})
this.manager.onGameOver.push(() => {
this.hud.showGameOver()
})
this.manager.onVictory.push(() => {
this.hud.showVictory()
})
// 通知 React 层游戏已就绪
if (typeof window !== 'undefined') {
;(window as any).__gameReady?.()
}
}
update(_time: number, delta: number): void {
if (this.manager.gameState !== 'playing' && this.manager.gameState !== 'idle') return
this.towerManager.update(delta, this.waveManager.getAllActiveEnemies())
this.waveManager.update(delta)
if (
this.isWaveRunning &&
this.waveManager.getAllActiveEnemies().length === 0 &&
this.waveManager.hasMoreWaves()
) {
this.isWaveRunning = false
this.hud.enableWaveButton()
this.hud.setWaveButtonText('▶ 召唤下一波')
}
}
private onWaveButtonClick(): void {
if (!this.waveManager.hasMoreWaves() || this.isWaveRunning) return
this.isWaveRunning = true
this.hud.disableWaveButton()
this.hud.setWaveButtonText('波次进行中...')
this.waveManager.startNextWave()
const waveNum = this.waveManager.getCurrentWaveNumber()
this.hud.showWaveBanner(waveNum, this.waveManager.totalWaves)
}
private onWeeklyReport(): void {
this.hud.showWeeklyReportAlert()
this.time.delayedCall(600, () => { this.weeklyModal.show() })
}
/** 禁锢全场塔(老板视察效果) */
freezeAllTowers(duration: number = 3000): void {
this.towerManager.getAllTowers().forEach(tower => {
tower.isFrozen = true
setTimeout(() => { tower.isFrozen = false }, duration)
})
}
/** 补满全场塔精力 */
refillAllStamina(): void {
this.towerManager.getAllTowers().forEach(tower => {
tower.stamina = tower.maxStamina
tower['isActive'] = true
})
}
private onAllWavesComplete(): void {
this.manager.triggerVictory()
this.hud.disableWaveButton()
}
private setupInteraction(): void {
const { cellW, cellH } = getCellSize()
this.input.on('pointermove', (pointer: Phaser.Input.Pointer) => {
const col = Math.floor(pointer.x / cellW)
const row = Math.floor((pointer.y - HUD_HEIGHT) / cellH)
if (col >= 0 && col < MAP_COLS && row >= 0 && row < MAP_ROWS && !PATH_TILES.has(`${col},${row}`)) {
if (!this.hoveredTile || this.hoveredTile.col !== col || this.hoveredTile.row !== row) {
this.hoveredTile = { col, row }
drawAllTiles(this.tileGraphics, this.hoveredTile)
}
if (this.selectedTowerType) this.drawBuildPreview(col, row, cellW, cellH)
return
}
if (this.hoveredTile !== null) {
this.hoveredTile = null
drawAllTiles(this.tileGraphics, null)
this.buildModeGraphics.clear()
}
})
this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
const col = Math.floor(pointer.x / cellW)
const row = Math.floor((pointer.y - HUD_HEIGHT) / cellH)
if (col >= 0 && col < MAP_COLS && row >= 0 && row < MAP_ROWS) {
this.handleTileClick(col, row, cellW, cellH)
}
})
}
private drawBuildPreview(col: number, row: number, cellW: number, cellH: number): void {
this.buildModeGraphics.clear()
if (!this.towerManager.canPlace(col, row)) return
const x = col * cellW
const y = HUD_HEIGHT + row * cellH
this.buildModeGraphics.lineStyle(2, 0xa78bfa, 0.9)
this.buildModeGraphics.strokeRect(x + 2, y + 2, cellW - 4, cellH - 4)
// 填充半透明预览
this.buildModeGraphics.fillStyle(0xa78bfa, 0.15)
this.buildModeGraphics.fillRect(x + 2, y + 2, cellW - 4, cellH - 4)
}
private handleTileClick(col: number, row: number, cellW: number, cellH: number): void {
if (this.selectedTowerType) {
if (PATH_TILES.has(`${col},${row}`)) return
const placed = this.towerManager.placeTower(col, row, this.selectedTowerType)
if (placed) {
this.buildModeGraphics.clear()
this.selectedTowerType = null
// 通知 React 取消选中状态
if (typeof window !== 'undefined') {
;(window as any).__gameOnTowerDeselect?.()
}
} else {
this.showTip(col, row, cellW, cellH, 'HC不足或格子已占用', '#EF4444')
}
return
}
const hasTower = this.towerManager.handleTileClick(col, row)
if (!hasTower && !PATH_TILES.has(`${col},${row}`)) {
this.showTip(col, row, cellW, cellH, '请先从底部选择塔', '#A78BFA')
}
}
private showTip(col: number, row: number, cellW: number, cellH: number, msg: string, color: string): void {
const x = col * cellW + cellW / 2
const y = HUD_HEIGHT + row * cellH + cellH / 2
const tip = this.add.text(x, y - 20, msg, {
fontFamily: 'VT323, monospace', fontSize: '16px',
color, backgroundColor: '#0a1628', padding: { x: 8, y: 4 },
}).setOrigin(0.5, 1).setDepth(20)
this.time.delayedCall(1200, () => tip.destroy())
}
}
return GameScene
}