test(move) 角色移动结合动画测试

This commit is contained in:
chicrobot committed 2026-01-08 22:10:16 +08:00
1 parent f603fda4f9
commit 61cbebec3e
1 file changed
+108 -64
+108 -64
View File
@@ -1,12 +1,23 @@
// 角色动画测试场景 // 角色动画测试场景
// 使用Player0和Player1图片作为移动帧 // 使用Player0和Player1图片作为移动帧
// 导入系统和UI组件
import { InputSystem } from '../systems/InputSystem.js';
import { SelectionSystem } from '../systems/SelectionSystem.js';
import { CommandSystem } from '../systems/CommandSystem.js';
// 导入Unit类
import Unit from '../objects/Unit.js';
export default class CharacterAnimationTestScene extends Phaser.Scene { export default class CharacterAnimationTestScene extends Phaser.Scene {
constructor() { constructor() {
super({ key: 'CharacterAnimationTestScene' }); super({ key: 'CharacterAnimationTestScene' });
this.tileSize = 32; // 瓦片大小 this.tileSize = 32; // 瓦片大小
this.player = null; // 玩家角色 this.player = null; // 玩家角色
this.cursors = null; // 键盘控制 this.inputSystem = null; // 输入系统
this.selectionSystem = null; // 选择系统
this.commandSystem = null; // 命令系统
this.keys = null; // 键盘按键
} }
preload() { preload() {
@@ -29,8 +40,9 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
create() { create() {
// 物理世界和相机边界 // 物理世界和相机边界
const worldSize = 2000; const worldSize = 2000;
this.physics.world.setBounds(0, 0, worldSize, worldSize); this.physics.world.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2);
this.cameras.main.setBounds(0, 0, worldSize, worldSize); this.cameras.main.setBounds(-worldSize, -worldSize, worldSize * 2, worldSize * 2);
this.cameras.main.centerOn(0, 0);
// 设置背景色 // 设置背景色
this.cameras.main.setBackgroundColor('#222222'); this.cameras.main.setBackgroundColor('#222222');
@@ -41,17 +53,38 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
// 创建角色动画 // 创建角色动画
this.createCharacterAnimations(); this.createCharacterAnimations();
// 初始化系统 - 开启鼠标移动和选择
this.inputSystem = new InputSystem(this, {
keyboard: true,
mouse: true,
selection: true
});
this.selectionSystem = new SelectionSystem(this);
this.commandSystem = new CommandSystem(this);
// 创建单位组
this.unitGroup = this.add.group();
// 创建玩家角色 // 创建玩家角色
this.createPlayer(); this.createPlayer();
// 设置输入控制 // 设置输入控制
this.setupInput(); this.setupInput();
// 添加碰撞检测
this.physics.add.collider(this.unitGroup, this.unitGroup);
// 显示提示信息 // 显示提示信息
this.showInstructions(); this.showInstructions();
// 相机跟随玩家 // 添加相机移动控制
this.cameras.main.startFollow(this.player, true, 0.1, 0.1); 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
});
} }
/** /**
@@ -124,11 +157,16 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
createPlayer() { createPlayer() {
console.log('创建玩家角色...'); console.log('创建玩家角色...');
// 创建玩家角色 // 创建玩家角色使用Unit类
this.player = this.physics.add.sprite(1000, 1000, 'player0-0'); this.player = new Unit(this, 0, 0, 'player0-0');
this.player.setScale(2); // 放大角色 this.player.setScale(2); // 放大角色
this.player.setBounce(0.2); // 反弹效果 this.player.setBounce(0.2); // 反弹效果
this.player.setCollideWorldBounds(true); // 碰撞世界边界
// 自动选中玩家
this.events.emit('select-unit', this.player);
// 将玩家添加到单位组
this.unitGroup.add(this.player);
console.log('玩家角色创建完成'); console.log('玩家角色创建完成');
} }
@@ -137,15 +175,8 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
* 设置输入控制 * 设置输入控制
*/ */
setupInput() { setupInput() {
// 键盘控制 - 方向键 // 只保留必要的按键监听,键盘移动由InputSystem处理
this.cursors = this.input.keyboard.createCursorKeys();
// 添加字母键控制
this.keys = { this.keys = {
a: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A),
w: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W),
s: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S),
d: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D),
space: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE) space: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE)
}; };
@@ -160,6 +191,8 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
}); });
} }
/** /**
* 创建按钮 * 创建按钮
*/ */
@@ -246,60 +279,71 @@ export default class CharacterAnimationTestScene extends Phaser.Scene {
}).setScrollFactor(0, 0).setDepth(11); }).setScrollFactor(0, 0).setDepth(11);
} }
/**
* 更新玩家移动
*/
updatePlayerMovement() {
const speed = 200; // 移动速度
// 停止所有移动
this.player.setVelocity(0);
let isMoving = false;
// 水平移动
if (this.cursors.left.isDown || this.keys.a.isDown) {
this.player.setVelocityX(-speed);
this.player.setFlipX(true); // 向左翻转
isMoving = true;
} else if (this.cursors.right.isDown || this.keys.d.isDown) {
this.player.setVelocityX(speed);
this.player.setFlipX(false); // 向右不翻转
isMoving = true;
}
// 垂直移动
if (this.cursors.up.isDown || this.keys.w.isDown) {
this.player.setVelocityY(-speed);
isMoving = true;
} else if (this.cursors.down.isDown || this.keys.s.isDown) {
this.player.setVelocityY(speed);
isMoving = true;
}
// 根据移动状态播放或停止动画
if (isMoving) {
if (!this.player.anims.isPlaying) {
// 如果没有播放动画,根据当前角色播放对应的移动动画
if (this.currentCharacterText.text === '当前角色: Player0') {
this.player.play('player0-move');
} else {
this.player.play('player1-move');
}
}
} else {
// 停止动画,保持当前帧
this.player.anims.pause();
}
}
update() { update() {
// 更新玩家移动 // 更新输入系统
this.updatePlayerMovement(); 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();
// 处理角色动画
this.updateCharacterAnimation(unit);
});
// 按空格键返回启动场景 // 按空格键返回启动场景
if (this.keys.space.isDown) { if (this.keys.space.isDown) {
this.scene.start('BootScene'); this.scene.start('BootScene');
} }
} }
/**
* 更新角色动画
* @param {Object} unit - 单位对象
*/
updateCharacterAnimation(unit) {
// 检查单位是否在移动
const isMoving = Math.abs(unit.body.velocity.x) > 10 || Math.abs(unit.body.velocity.y) > 10;
if (isMoving) {
// 处理角色朝向
if (unit.body.velocity.x < -10) {
unit.setFlipX(true); // 向左翻转
} else if (unit.body.velocity.x > 10) {
unit.setFlipX(false); // 向右不翻转
}
// 播放动画
if (!unit.anims.isPlaying) {
// 根据当前角色播放对应的移动动画
if (this.currentCharacterText.text === '当前角色: Player0') {
unit.play('player0-move');
} else {
unit.play('player1-move');
}
}
} else {
// 停止动画,保持当前帧
unit.anims.pause();
}
}
} }