mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 23:31:29 +08:00
305 lines
8.1 KiB
JavaScript
305 lines
8.1 KiB
JavaScript
// 角色动画测试场景
|
|
// 使用Player0和Player1图片作为移动帧
|
|
|
|
export default class CharacterAnimationTestScene extends Phaser.Scene {
|
|
constructor() {
|
|
super({ key: 'CharacterAnimationTestScene' });
|
|
this.tileSize = 32; // 瓦片大小
|
|
this.player = null; // 玩家角色
|
|
this.cursors = null; // 键盘控制
|
|
}
|
|
|
|
preload() {
|
|
// 加载玩家瓦片图片
|
|
console.log('加载玩家瓦片图片...');
|
|
|
|
// 加载Player0的动画帧(假设tile_0000.png到tile_0007.png是Player0的帧)
|
|
for (let i = 0; i < 8; i++) {
|
|
this.load.image(`player0-${i}`, `assets/PNG/Players/Tiles/tile_${String(i).padStart(4, '0')}.png`);
|
|
}
|
|
|
|
// 加载Player1的动画帧(假设tile_0008.png到tile_0015.png是Player1的帧)
|
|
for (let i = 8; i < 16; i++) {
|
|
this.load.image(`player1-${i-8}`, `assets/PNG/Players/Tiles/tile_${String(i).padStart(4, '0')}.png`);
|
|
}
|
|
|
|
console.log('玩家瓦片图片加载完成');
|
|
}
|
|
|
|
create() {
|
|
// 物理世界和相机边界
|
|
const worldSize = 2000;
|
|
this.physics.world.setBounds(0, 0, worldSize, worldSize);
|
|
this.cameras.main.setBounds(0, 0, worldSize, worldSize);
|
|
|
|
// 设置背景色
|
|
this.cameras.main.setBackgroundColor('#222222');
|
|
|
|
// 创建地面网格
|
|
this.createGrid();
|
|
|
|
// 创建角色动画
|
|
this.createCharacterAnimations();
|
|
|
|
// 创建玩家角色
|
|
this.createPlayer();
|
|
|
|
// 设置输入控制
|
|
this.setupInput();
|
|
|
|
// 显示提示信息
|
|
this.showInstructions();
|
|
|
|
// 相机跟随玩家
|
|
this.cameras.main.startFollow(this.player, true, 0.1, 0.1);
|
|
}
|
|
|
|
/**
|
|
* 创建地面网格
|
|
*/
|
|
createGrid() {
|
|
// 创建网格线
|
|
const gridGraphics = this.add.graphics();
|
|
gridGraphics.lineStyle(1, 0x444444, 0.5);
|
|
|
|
// 绘制垂直线
|
|
for (let x = 0; x <= 2000; x += this.tileSize) {
|
|
gridGraphics.moveTo(x, 0);
|
|
gridGraphics.lineTo(x, 2000);
|
|
}
|
|
|
|
// 绘制水平线
|
|
for (let y = 0; y <= 2000; y += this.tileSize) {
|
|
gridGraphics.moveTo(0, y);
|
|
gridGraphics.lineTo(2000, y);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建角色动画
|
|
*/
|
|
createCharacterAnimations() {
|
|
console.log('创建角色动画...');
|
|
|
|
// 创建Player0的移动动画
|
|
this.anims.create({
|
|
key: 'player0-move',
|
|
frames: [
|
|
{ key: 'player0-0' },
|
|
{ key: 'player0-1' },
|
|
{ key: 'player0-2' },
|
|
{ key: 'player0-3' },
|
|
{ key: 'player0-4' },
|
|
{ key: 'player0-5' },
|
|
{ key: 'player0-6' },
|
|
{ key: 'player0-7' }
|
|
],
|
|
frameRate: 10,
|
|
repeat: -1
|
|
});
|
|
|
|
// 创建Player1的移动动画
|
|
this.anims.create({
|
|
key: 'player1-move',
|
|
frames: [
|
|
{ key: 'player1-0' },
|
|
{ key: 'player1-1' },
|
|
{ key: 'player1-2' },
|
|
{ key: 'player1-3' },
|
|
{ key: 'player1-4' },
|
|
{ key: 'player1-5' },
|
|
{ key: 'player1-6' },
|
|
{ key: 'player1-7' }
|
|
],
|
|
frameRate: 10,
|
|
repeat: -1
|
|
});
|
|
|
|
console.log('角色动画创建完成');
|
|
}
|
|
|
|
/**
|
|
* 创建玩家角色
|
|
*/
|
|
createPlayer() {
|
|
console.log('创建玩家角色...');
|
|
|
|
// 创建玩家角色
|
|
this.player = this.physics.add.sprite(1000, 1000, 'player0-0');
|
|
this.player.setScale(2); // 放大角色
|
|
this.player.setBounce(0.2); // 反弹效果
|
|
this.player.setCollideWorldBounds(true); // 碰撞世界边界
|
|
|
|
console.log('玩家角色创建完成');
|
|
}
|
|
|
|
/**
|
|
* 设置输入控制
|
|
*/
|
|
setupInput() {
|
|
// 键盘控制 - 方向键
|
|
this.cursors = this.input.keyboard.createCursorKeys();
|
|
|
|
// 添加字母键控制
|
|
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)
|
|
};
|
|
|
|
// 切换角色按钮
|
|
this.createButton(this.sys.game.config.width - 200, 100, '切换角色', () => {
|
|
this.switchCharacter();
|
|
});
|
|
|
|
// 返回启动场景按钮
|
|
this.createButton(this.sys.game.config.width - 200, 150, '返回启动场景', () => {
|
|
this.scene.start('BootScene');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建按钮
|
|
*/
|
|
createButton(x, y, text, callback) {
|
|
// 按钮背景
|
|
const button = this.add.rectangle(x, y, 180, 40, 0x333333);
|
|
button.setOrigin(0.5, 0.5);
|
|
button.setScrollFactor(0, 0);
|
|
button.setDepth(10);
|
|
button.setInteractive({ useHandCursor: true });
|
|
|
|
// 按钮文本
|
|
const buttonText = this.add.text(x - 70, y - 15, text, {
|
|
fontSize: '14px',
|
|
fill: '#ffffff',
|
|
fontFamily: 'Arial'
|
|
});
|
|
buttonText.setScrollFactor(0, 0);
|
|
buttonText.setDepth(11);
|
|
|
|
// 按钮事件
|
|
button.on('pointerdown', callback);
|
|
|
|
button.on('pointerover', () => {
|
|
button.setFillStyle(0x555555);
|
|
});
|
|
|
|
button.on('pointerout', () => {
|
|
button.setFillStyle(0x333333);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 切换角色
|
|
*/
|
|
switchCharacter() {
|
|
// 根据当前使用的纹理切换角色
|
|
if (this.player.anims.currentAnim && this.player.anims.currentAnim.key === 'player0-move') {
|
|
// 切换到Player1
|
|
this.player.play('player1-move');
|
|
console.log('切换到Player1');
|
|
} else {
|
|
// 切换到Player0
|
|
this.player.play('player0-move');
|
|
console.log('切换到Player0');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示提示信息
|
|
*/
|
|
showInstructions() {
|
|
// 标题
|
|
this.add.text(20, 20, '角色动画测试场景', {
|
|
fontSize: '20px',
|
|
fill: '#ffffff',
|
|
fontFamily: 'Arial',
|
|
fontWeight: 'bold'
|
|
}).setScrollFactor(0, 0).setDepth(11);
|
|
|
|
// 操作提示
|
|
const instructions = [
|
|
'WASD或方向键移动角色',
|
|
'角色移动时播放动画',
|
|
'点击"切换角色"按钮切换Player0和Player1',
|
|
'角色动画使用Player0和Player1的图片作为移动帧',
|
|
'按空格键返回启动场景'
|
|
];
|
|
|
|
for (let i = 0; i < instructions.length; i++) {
|
|
this.add.text(20, 50 + i * 25, instructions[i], {
|
|
fontSize: '14px',
|
|
fill: '#ffffff',
|
|
fontFamily: 'Arial'
|
|
}).setScrollFactor(0, 0).setDepth(11);
|
|
}
|
|
|
|
// 显示当前角色
|
|
this.currentCharacterText = this.add.text(20, 200, '当前角色: Player0', {
|
|
fontSize: '16px',
|
|
fill: '#00ff00',
|
|
fontFamily: 'Arial',
|
|
fontWeight: 'bold'
|
|
}).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() {
|
|
// 更新玩家移动
|
|
this.updatePlayerMovement();
|
|
|
|
// 按空格键返回启动场景
|
|
if (this.keys.space.isDown) {
|
|
this.scene.start('BootScene');
|
|
}
|
|
}
|
|
} |