perf: 优化

This commit is contained in:
richarjiang
2025-09-28 10:04:21 +08:00
parent e09c9a84cb
commit 1a6580e195
347 changed files with 6650 additions and 1203 deletions

View File

@@ -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();
}
})