mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 07:41:29 +08:00
feat(item) 优化移动及添加测试场景
This commit is contained in:
1 parent
2f7210f47e
commit
b9a5180293
15 files changed
+1428
-62
No files matched your search
@@ -0,0 +1,143 @@
|
||||
// 测试场景6:小地图测试
|
||||
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 { MiniMap } from '../ui/MiniMap.js';
|
||||
|
||||
export default class MovementTestScene6 extends Phaser.Scene {
|
||||
constructor() {
|
||||
super({ key: 'MovementTestScene6' });
|
||||
}
|
||||
|
||||
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 = 2000;
|
||||
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: true,
|
||||
mouse: true,
|
||||
joystick: false,
|
||||
selection: true
|
||||
});
|
||||
this.selectionSystem = new SelectionSystem(this);
|
||||
this.commandSystem = new CommandSystem(this);
|
||||
|
||||
// 创建单位组
|
||||
this.unitGroup = this.add.group();
|
||||
|
||||
// 创建多个测试单位,分布在不同位置
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const x = Math.random() * 2000 - 1000;
|
||||
const y = Math.random() * 2000 - 1000;
|
||||
const unit = new Unit(this, x, y, 'player');
|
||||
this.unitGroup.add(unit);
|
||||
|
||||
// 自动选中第一个单位
|
||||
if (i === 0) {
|
||||
this.events.emit('select-unit', unit);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加碰撞检测
|
||||
this.physics.add.collider(this.unitGroup, this.unitGroup);
|
||||
|
||||
// 创建小地图
|
||||
this.miniMap = new MiniMap(this, {
|
||||
worldSize: worldSize,
|
||||
mapSize: 200,
|
||||
x: this.sys.game.config.width - 210,
|
||||
y: 10
|
||||
});
|
||||
|
||||
// 添加场景说明文本
|
||||
const titleText = this.add.text(10, 10, '测试场景6:小地图测试', {
|
||||
fontSize: '20px',
|
||||
fill: '#ffffff'
|
||||
});
|
||||
titleText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
||||
titleText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
||||
|
||||
const miniMapText = this.add.text(10, 40, '使用鼠标拖拽小地图或点击小地图快速移动视角', {
|
||||
fontSize: '16px',
|
||||
fill: '#ffffff'
|
||||
});
|
||||
miniMapText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
||||
miniMapText.setDepth(10); // 设置深度,确保显示在游戏元素上方
|
||||
|
||||
const controlsText = this.add.text(10, 70, 'WASD或方向键移动视角,鼠标右键移动单位', {
|
||||
fontSize: '16px',
|
||||
fill: '#ffffff'
|
||||
});
|
||||
controlsText.setScrollFactor(0, 0); // 固定在屏幕上,不随相机移动
|
||||
controlsText.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.cameraSpeed = 2;
|
||||
this.cameraKeys = this.input.keyboard.addKeys({
|
||||
up: Phaser.Input.Keyboard.KeyCodes.W,
|
||||
down: Phaser.Input.Keyboard.KeyCodes.S,
|
||||
left: Phaser.Input.Keyboard.KeyCodes.A,
|
||||
right: Phaser.Input.Keyboard.KeyCodes.D
|
||||
});
|
||||
}
|
||||
|
||||
update() {
|
||||
this.inputSystem.update();
|
||||
|
||||
// 更新相机移动
|
||||
if (this.cameraKeys.left.isDown) {
|
||||
this.cameras.main.scrollX -= this.cameraSpeed;
|
||||
}
|
||||
if (this.cameraKeys.right.isDown) {
|
||||
this.cameras.main.scrollX += this.cameraSpeed;
|
||||
}
|
||||
if (this.cameraKeys.up.isDown) {
|
||||
this.cameras.main.scrollY -= this.cameraSpeed;
|
||||
}
|
||||
if (this.cameraKeys.down.isDown) {
|
||||
this.cameras.main.scrollY += this.cameraSpeed;
|
||||
}
|
||||
|
||||
this.unitGroup.getChildren().forEach(unit => {
|
||||
if (unit.update) {
|
||||
unit.update();
|
||||
}
|
||||
unit.updateMovement();
|
||||
});
|
||||
|
||||
// 更新小地图显示
|
||||
if (this.miniMap) {
|
||||
this.miniMap.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user