mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 23:41:29 +08:00
161 lines
4.8 KiB
JavaScript
161 lines
4.8 KiB
JavaScript
// 测试场景5:移动更新逻辑
|
|
import Unit from '../../objects/Unit.js';
|
|
import { InputSystem } from '../../systems/InputSystem.js';
|
|
import { SelectionSystem } from '../../systems/SelectionSystem.js';
|
|
import { CommandSystem } from '../../systems/CommandSystem.js';
|
|
|
|
export default class AutoMovementTestScene extends Phaser.Scene {
|
|
constructor() {
|
|
super({ key: 'AutoMovementTestScene' });
|
|
}
|
|
|
|
create() {
|
|
// 创建玩家纹理
|
|
if (!this.textures.exists('player')) {
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = 32;
|
|
canvas.height = 32;
|
|
const ctx = canvas.getContext('2d');
|
|
ctx.fillStyle = '#000000ff';
|
|
ctx.beginPath();
|
|
ctx.arc(16, 16, 14, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
this.textures.addCanvas('player', canvas);
|
|
}
|
|
|
|
// 物理世界和相机边界
|
|
const worldSize = 1000;
|
|
this.physics.world.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2);
|
|
this.cameras.main.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2);
|
|
this.cameras.main.centerOn(0, 0);
|
|
|
|
// 初始化系统 - 这个场景单位自动移动,禁用所有输入和框选
|
|
this.inputSystem = new InputSystem(this, {
|
|
keyboard: false,
|
|
mouse: false,
|
|
joystick: false,
|
|
selection: false
|
|
});
|
|
this.selectionSystem = new SelectionSystem(this);
|
|
this.commandSystem = new CommandSystem(this);
|
|
|
|
// 创建单位组
|
|
this.unitGroup = this.add.group();
|
|
|
|
// 创建多个测试单位,分布在不同位置
|
|
const unitPositions = [
|
|
{ x: -300, y: -300 },
|
|
{ x: 300, y: -300 },
|
|
{ x: -300, y: 300 },
|
|
{ x: 300, y: 300 }
|
|
];
|
|
|
|
// 自动选中第一个单位
|
|
let firstUnit = null;
|
|
|
|
unitPositions.forEach(pos => {
|
|
const unit = new Unit(this, pos.x, pos.y, 'player');
|
|
// 为每个单位设置不同的目标点,观察它们的移动
|
|
const targetX = Math.random() * 400 - 200;
|
|
const targetY = Math.random() * 400 - 200;
|
|
unit.setTarget(targetX, targetY);
|
|
this.unitGroup.add(unit);
|
|
|
|
// 保存第一个单位的引用
|
|
if (!firstUnit) {
|
|
firstUnit = unit;
|
|
}
|
|
});
|
|
|
|
// 自动选中第一个单位
|
|
if (firstUnit) {
|
|
this.events.emit('select-unit', firstUnit);
|
|
}
|
|
|
|
// 添加碰撞检测
|
|
this.physics.add.collider(this.unitGroup, this.unitGroup);
|
|
|
|
// 添加场景说明文本
|
|
const titleText = this.add.text(10, 10, '测试场景5:移动更新逻辑', {
|
|
fontSize: '20px',
|
|
fill: '#ffffff'
|
|
});
|
|
titleText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
|
titleText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
|
|
|
const autoMoveText = this.add.text(10, 40, '观察单位自动向随机目标点移动', {
|
|
fontSize: '16px',
|
|
fill: '#ffffff'
|
|
});
|
|
autoMoveText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
|
autoMoveText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
|
|
|
const pathText = this.add.text(10, 70, '单位会自动计算路径并在到达后停止', {
|
|
fontSize: '16px',
|
|
fill: '#ffffff'
|
|
});
|
|
pathText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
|
pathText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
|
|
|
const spaceText = this.add.text(10, 100, '按空格键返回菜单', {
|
|
fontSize: '16px',
|
|
fill: '#ffffff'
|
|
});
|
|
spaceText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
|
spaceText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
|
|
|
// 空格键切换场景
|
|
this.input.keyboard.on('keydown-SPACE', () => {
|
|
this.scene.start('BootScene');
|
|
});
|
|
|
|
// 添加返回菜单按钮
|
|
this.createReturnButton();
|
|
}
|
|
|
|
/**
|
|
* 创建返回菜单按钮
|
|
*/
|
|
createReturnButton() {
|
|
// 创建按钮背景
|
|
const button = this.add.rectangle(10, 130, 150, 40, 0x333333);
|
|
button.setOrigin(0, 0);
|
|
button.setScrollFactor(0, 0);
|
|
button.setDepth(10);
|
|
button.setInteractive({ useHandCursor: true });
|
|
|
|
// 按钮文本
|
|
const buttonText = this.add.text(25, 140, '返回菜单', {
|
|
fontSize: '16px',
|
|
fill: '#ffffff',
|
|
fontFamily: 'Arial'
|
|
});
|
|
buttonText.setScrollFactor(0, 0);
|
|
buttonText.setDepth(11);
|
|
|
|
// 按钮事件
|
|
button.on('pointerdown', () => {
|
|
this.scene.start('BootScene');
|
|
});
|
|
|
|
button.on('pointerover', () => {
|
|
button.setFillStyle(0x555555);
|
|
});
|
|
|
|
button.on('pointerout', () => {
|
|
button.setFillStyle(0x333333);
|
|
});
|
|
}
|
|
|
|
update() {
|
|
this.inputSystem.update();
|
|
|
|
this.unitGroup.getChildren().forEach(unit => {
|
|
if (unit.update) {
|
|
unit.update();
|
|
}
|
|
unit.updateMovement();
|
|
});
|
|
}
|
|
}
|