perf: 优化
This commit is contained in:
@@ -41,6 +41,7 @@ export class PlayerController extends Component {
|
||||
private isUpgraded: boolean = false; // 玩家是否已升级
|
||||
private isGameOver: boolean = false; // 游戏是否结束(玩家死亡)
|
||||
private isWin: boolean = false; // 游戏是否胜利(到达终点)
|
||||
private currentDirection: number = 5; // 当前玩家朝向:3表示左/上,5表示右/下,默认为5
|
||||
|
||||
private hasWinTimes = 0
|
||||
|
||||
@@ -254,10 +255,12 @@ export class PlayerController extends Component {
|
||||
|
||||
if (absX > absY) {
|
||||
// 水平移动为主
|
||||
return deltaX > 0 ? 'walk3' : 'walk5';
|
||||
this.currentDirection = deltaX < 0 ? 3 : 5;
|
||||
return deltaX < 0 ? 'walk3' : 'walk5';
|
||||
} else {
|
||||
// 垂直移动为主
|
||||
return deltaY > 0 ? 'walk3' : 'walk5'; // 上移用walk3,下移用walk5
|
||||
this.currentDirection = deltaY < 0 ? 3 : 5;
|
||||
return deltaY < 0 ? 'walk3' : 'walk5'; // 上移用walk3,下移用walk5
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,10 +273,16 @@ export class PlayerController extends Component {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果切换到站立动画,根据当前方向选择对应的站立动画
|
||||
let targetAnimationName = animationName;
|
||||
if (animationName === 'stand') {
|
||||
targetAnimationName = this.currentDirection === 3 ? 'stand3' : 'stand5';
|
||||
}
|
||||
|
||||
// 如果玩家已升级,在动画名称后添加 "_2" 后缀
|
||||
let finalAnimationName = animationName;
|
||||
if (this.isUpgraded && !animationName.endsWith('_2')) {
|
||||
finalAnimationName = animationName + '_2';
|
||||
let finalAnimationName = targetAnimationName;
|
||||
if (this.isUpgraded && !targetAnimationName.endsWith('_2')) {
|
||||
finalAnimationName = targetAnimationName + '_2';
|
||||
}
|
||||
|
||||
if (this.currentAnimation === finalAnimationName) {
|
||||
@@ -287,8 +296,8 @@ export class PlayerController extends Component {
|
||||
const state = animation.getState(finalAnimationName);
|
||||
if (!state) {
|
||||
console.warn(`动画 ${finalAnimationName} 不存在,使用默认动画`);
|
||||
this.currentAnimation = 'stand';
|
||||
animation.play('stand');
|
||||
this.currentAnimation = 'stand5';
|
||||
animation.play('stand5');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -364,6 +373,8 @@ export class PlayerController extends Component {
|
||||
update(deltaTime: number) {
|
||||
// 更新逻辑现在主要由缓动系统处理
|
||||
// 这里可以添加其他需要每帧更新的逻辑
|
||||
|
||||
|
||||
}
|
||||
|
||||
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
|
||||
@@ -417,7 +428,7 @@ export class PlayerController extends Component {
|
||||
if (monsterAnimation) {
|
||||
monsterAnimation.play(`${otherCollider.node.name}_attack`);
|
||||
}
|
||||
this.switchAnimation('attack');
|
||||
this.switchAnimation(this.currentDirection === 3 ? 'attack3' : 'attack5');
|
||||
|
||||
// 2秒后判定攻击结果
|
||||
this.scheduleOnce(async () => {
|
||||
@@ -449,14 +460,17 @@ export class PlayerController extends Component {
|
||||
this.scheduleOnce(() => {
|
||||
otherCollider.node.destroy();
|
||||
console.log('怪物已消失');
|
||||
|
||||
if (this.hasWinTimes === 10) {
|
||||
this.isWin = true
|
||||
this.showBonusPopup()
|
||||
}
|
||||
|
||||
}, 1);
|
||||
|
||||
this.switchAnimation('stand'); // 玩家站立
|
||||
|
||||
if (this.hasWinTimes === 10) {
|
||||
this.isWin = true
|
||||
this.showBonusPopup()
|
||||
}
|
||||
|
||||
} else {
|
||||
// 怪物获胜
|
||||
console.log('怪物获胜!玩家生命值变为0');
|
||||
@@ -587,9 +601,7 @@ export class PlayerController extends Component {
|
||||
}, {
|
||||
easing: 'quadOut',
|
||||
onComplete: () => {
|
||||
// 道具到达玩家位置后消失
|
||||
prop.destroy();
|
||||
console.log(`道具 ${prop.name} 已到达玩家位置并消失`);
|
||||
prop.active = false;
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user