perf:优化攻击判定
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[
|
||||
{
|
||||
"__type__": "cc.AnimationClip",
|
||||
"_name": "attack",
|
||||
"_name": "attack5",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {
|
||||
"embeddedPlayerGroups": []
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.5166666666666667,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.5166666666666667,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.5166666666666667,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[
|
||||
{
|
||||
"__type__": "cc.AnimationClip",
|
||||
"_name": "guai_2_stand",
|
||||
"_name": "guai_4_stand",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {
|
||||
"embeddedPlayerGroups": []
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.5166666666666667,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"_native": "",
|
||||
"sample": 60,
|
||||
"speed": 1,
|
||||
"wrapMode": 2,
|
||||
"wrapMode": 1,
|
||||
"enableTrsBlending": false,
|
||||
"_duration": 0.6,
|
||||
"_hash": 500763545,
|
||||
|
||||
@@ -5830,8 +5830,8 @@
|
||||
"_prefab": null,
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": -400.503,
|
||||
"y": 15.062,
|
||||
"x": -447.377,
|
||||
"y": 57.053,
|
||||
"z": 0
|
||||
},
|
||||
"_lrot": {
|
||||
@@ -7098,8 +7098,8 @@
|
||||
"_prefab": null,
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": -115.124,
|
||||
"y": 474.868,
|
||||
"x": -75.252,
|
||||
"y": 454.446,
|
||||
"z": 0
|
||||
},
|
||||
"_lrot": {
|
||||
|
||||
@@ -730,16 +730,22 @@ export class PlayerController extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
// 播放攻击动画
|
||||
const animNode = otherCollider.node.getChildByName('Anim');
|
||||
const monsterAnimation = animNode ? animNode.getComponent(Animation) : null;
|
||||
if (monsterAnimation) {
|
||||
monsterAnimation.play(`${otherCollider.node.name}_attack`);
|
||||
}
|
||||
this.switchAnimation(this.currentDirection === 3 ? 'attack3' : 'attack5');
|
||||
// 获取玩家动画组件
|
||||
const playerAnimNode = this.player.getChildByName('Anim');
|
||||
const playerAnimation = playerAnimNode ? playerAnimNode.getComponent(Animation) : null;
|
||||
|
||||
// 1.2秒后判定攻击结果
|
||||
this.scheduleOnce(async () => {
|
||||
if (!playerAnimation) {
|
||||
console.warn('未找到玩家动画组件');
|
||||
this.isAttacking = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 播放玩家攻击动画
|
||||
const attackAnimationName = this.currentDirection === 3 ? 'attack3' : 'attack5';
|
||||
this.switchAnimation(attackAnimationName);
|
||||
|
||||
// 监听玩家攻击动画结束事件
|
||||
playerAnimation.once(Animation.EventType.FINISHED, async () => {
|
||||
if (!this.player || !playerLabel.isValid || !monsterLabel.isValid) {
|
||||
this.isAttacking = false;
|
||||
if (this.attackAudio) {
|
||||
@@ -750,20 +756,46 @@ export class PlayerController extends Component {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 比较生命值,判断输赢
|
||||
console.log('判定攻击结果,玩家HP:', playerHp, '怪物HP:', monsterHp);
|
||||
if (playerHp >= monsterHp) {
|
||||
const hit = otherCollider.node.getChildByName('Hit');
|
||||
|
||||
if (playerHp >= monsterHp) {
|
||||
// 玩家获胜,直接执行后续流程,不需要播放怪物攻击动画
|
||||
await this.handlePlayerWin(otherCollider, playerLabel, monsterHp);
|
||||
} else {
|
||||
// 玩家输了,需要播放怪物攻击动画
|
||||
await this.handleMonsterAttack(otherCollider, playerLabel, monsterHp);
|
||||
}
|
||||
|
||||
this.isAttacking = false;
|
||||
|
||||
// 停止攻击音效
|
||||
if (this.attackAudio) {
|
||||
const audioSource = this.attackAudio.getComponent(AudioSource);
|
||||
if (audioSource) {
|
||||
audioSource.stop();
|
||||
console.log('停止攻击音效');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理玩家获胜的情况
|
||||
*/
|
||||
private async handlePlayerWin(otherCollider: Collider2D, playerLabel: Label, monsterHp: number) {
|
||||
const hit = otherCollider.node.getChildByName('Hit');
|
||||
if (hit) {
|
||||
hit.active = true;
|
||||
}
|
||||
|
||||
this.hasWinTimes++;
|
||||
// 玩家获胜
|
||||
console.log('玩家获胜!更新玩家生命值为:', playerHp + monsterHp);
|
||||
console.log('玩家获胜!更新玩家生命值为:', parseInt(playerLabel.string) + monsterHp);
|
||||
|
||||
// 玩家生命值增加怪物生命值
|
||||
const playerHp = parseInt(playerLabel.string) || 0;
|
||||
const newPlayerHp = playerHp + monsterHp;
|
||||
playerLabel.string = newPlayerHp.toString();
|
||||
|
||||
@@ -771,6 +803,8 @@ export class PlayerController extends Component {
|
||||
this.playLabelEmphasisAnimation(playerLabel);
|
||||
|
||||
// 播放怪物死亡动画
|
||||
const animNode = otherCollider.node.getChildByName('Anim');
|
||||
const monsterAnimation = animNode ? animNode.getComponent(Animation) : null;
|
||||
if (monsterAnimation) {
|
||||
monsterAnimation.play(`${otherCollider.node.name}_die`);
|
||||
}
|
||||
@@ -793,13 +827,41 @@ export class PlayerController extends Component {
|
||||
this.isWin = true;
|
||||
this.showBonusPopup();
|
||||
}
|
||||
|
||||
}, 1);
|
||||
|
||||
this.switchAnimation('stand'); // 玩家站立
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理怪物攻击的情况
|
||||
*/
|
||||
private async handleMonsterAttack(otherCollider: Collider2D, playerLabel: Label, monsterHp: number) {
|
||||
// 播放怪物攻击动画
|
||||
const animNode = otherCollider.node.getChildByName('Anim');
|
||||
const monsterAnimation = animNode ? animNode.getComponent(Animation) : null;
|
||||
|
||||
if (monsterAnimation) {
|
||||
// 创建Promise来监听怪物攻击动画结束
|
||||
return new Promise<void>((resolve) => {
|
||||
monsterAnimation.play(`${otherCollider.node.name}_attack`);
|
||||
|
||||
// 监听怪物攻击动画结束
|
||||
monsterAnimation.once(Animation.EventType.FINISHED, () => {
|
||||
// 怪物攻击动画结束后,执行玩家死亡逻辑
|
||||
this.executePlayerDefeat(otherCollider, playerLabel);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// 如果没有怪物动画组件,直接执行玩家死亡逻辑
|
||||
this.executePlayerDefeat(otherCollider, playerLabel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行玩家失败逻辑
|
||||
*/
|
||||
private executePlayerDefeat(otherCollider: Collider2D, playerLabel: Label) {
|
||||
// 怪物获胜
|
||||
console.log('怪物获胜!玩家生命值变为0');
|
||||
|
||||
@@ -813,6 +875,8 @@ export class PlayerController extends Component {
|
||||
this.switchAnimation('die');
|
||||
|
||||
// 怪物站立动画
|
||||
const animNode = otherCollider.node.getChildByName('Anim');
|
||||
const monsterAnimation = animNode ? animNode.getComponent(Animation) : null;
|
||||
if (monsterAnimation) {
|
||||
monsterAnimation.play(`${otherCollider.node.name}_stand`);
|
||||
}
|
||||
@@ -827,19 +891,6 @@ export class PlayerController extends Component {
|
||||
}, 1); // 延迟1秒显示失败弹窗,让玩家死亡动画播放完成
|
||||
}
|
||||
|
||||
this.isAttacking = false;
|
||||
|
||||
// 停止攻击音效
|
||||
if (this.attackAudio) {
|
||||
const audioSource = this.attackAudio.getComponent(AudioSource);
|
||||
if (audioSource) {
|
||||
audioSource.stop();
|
||||
console.log('停止攻击音效');
|
||||
}
|
||||
}
|
||||
}, 1.2);
|
||||
}
|
||||
|
||||
private handleBoxCollision(otherCollider: Collider2D) {
|
||||
if (!this.player) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user