// scenes/GameScene.js import Unit from '../objects/Unit.js'; import { InputSystem } from '../systems/InputSystem.js'; import { SelectionSystem } from '../systems/SelectionSystem.js'; import { CommandSystem } from '../systems/CommandSystem.js'; import { VirtualJoystick } from '../ui/VirtualJoystick.js'; import { MiniMap } from '../ui/MiniMap.js'; export default class GameScene extends Phaser.Scene { create() { // === 0. 添加窗口大小变化监听 === window.addEventListener('resize', this.resizeGame.bind(this)); // === 1. 创建玩家纹理(红圈)=== if (!this.textures.exists('player')) { const canvas = document.createElement('canvas'); canvas.width = 32; canvas.height = 32; const ctx = canvas.getContext('2d'); ctx.fillStyle = '#ff0000'; ctx.beginPath(); ctx.arc(16, 16, 14, 0, Math.PI * 2); ctx.fill(); this.textures.addCanvas('player', canvas); } // === 2. 扩大物理世界和相机边界(关键!)=== const worldSize = 2000; this.physics.world.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2); this.cameras.main.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2); // === 3. 创建单位组 === this.unitGroup = this.add.group(); for (let i = 0; i < 5; i++) { const worldSizePersent = worldSize/100 const unit = new Unit(this, worldSizePersent + 2*i * worldSizePersent, 2 * worldSizePersent, 'player'); this.unitGroup.add(unit); } // === 4. 初始化系统 === this.inputSystem = new InputSystem(this); this.selectionSystem = new SelectionSystem(this); this.commandSystem = new CommandSystem(this); this.virtualJoystick = new VirtualJoystick(this); // === 5. 添加角色之间的碰撞检测 === this.physics.add.collider(this.unitGroup, this.unitGroup); // === 6. 创建小地图 === this.miniMap = new MiniMap(this, { worldSize: worldSize, mapSize: 200, x: 10, y: 10 }); } update() { // 更新输入(键盘、鼠标) this.inputSystem.update(); // 更新所有单位的移动逻辑和选中状态(关键!) this.unitGroup.getChildren().forEach(unit => { if (unit.update) { unit.update(); } unit.updateMovement(); }); // 更新小地图 if (this.miniMap) { this.miniMap.update(); } } /** * 窗口大小变化时的处理函数 */ resizeGame() { this.scale.resize(window.innerWidth, window.innerHeight); } }