|
|
@@ -437,56 +437,7 @@ export class EnemyInstance extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 动态调整血条位置,使其始终显示在碰撞体上方
|
|
|
- */
|
|
|
- private updateHPBarPosition(): void {
|
|
|
- // 获取血条根节点
|
|
|
- const hpBarRoot = this.node.getChildByName('HPBar');
|
|
|
- if (!hpBarRoot) {
|
|
|
- console.warn('[EnemyInstance] 未找到HPBar节点,无法调整血条位置');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取EnemySprite节点和其碰撞器
|
|
|
- const enemySprite = this.node.getChildByName('EnemySprite');
|
|
|
- if (!enemySprite) {
|
|
|
- console.error('[EnemyInstance] 未找到EnemySprite子节点,无法计算血条位置');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const collider = enemySprite.getComponent(Collider2D);
|
|
|
- const uiTransform = enemySprite.getComponent(UITransform);
|
|
|
- if (!collider || !uiTransform) {
|
|
|
- console.warn('[EnemyInstance] EnemySprite节点缺少必要组件,无法计算血条位置');
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 计算碰撞体的顶部位置
|
|
|
- let colliderTopY = 0;
|
|
|
- const anchorPoint = uiTransform.anchorPoint;
|
|
|
-
|
|
|
- if (collider instanceof BoxCollider2D) {
|
|
|
- const boxCollider = collider as BoxCollider2D;
|
|
|
- // 碰撞体顶部 = 碰撞体偏移Y + 碰撞体高度的一半
|
|
|
- colliderTopY = boxCollider.offset.y + boxCollider.size.height / 2;
|
|
|
- } else if (collider instanceof CircleCollider2D) {
|
|
|
- const circleCollider = collider as CircleCollider2D;
|
|
|
- // 碰撞体顶部 = 碰撞体偏移Y + 半径
|
|
|
- colliderTopY = circleCollider.offset.y + circleCollider.radius;
|
|
|
- }
|
|
|
-
|
|
|
- // 血条位置设置为碰撞体顶部上方一定距离
|
|
|
- const hpBarOffset = -35; // 血条距离碰撞体顶部的距离
|
|
|
- const hpBarY = colliderTopY + hpBarOffset;
|
|
|
-
|
|
|
- // 设置血条位置
|
|
|
- const hpBarTransform = hpBarRoot.getComponent(UITransform);
|
|
|
- if (hpBarTransform) {
|
|
|
- hpBarRoot.setPosition(0, hpBarY, 0);
|
|
|
- console.log(`[EnemyInstance] 血条位置已更新: Y = ${hpBarY.toFixed(2)} (碰撞体顶部: ${colliderTopY.toFixed(2)})`);
|
|
|
- }
|
|
|
- }
|
|
|
+ // 已统一到 HPBarAnimation.updateBarPosition,移除本地冗余定位实现
|
|
|
|
|
|
// 碰撞开始事件
|
|
|
onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
|
@@ -1042,7 +993,7 @@ export class EnemyInstance extends Component {
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
}, 0.1);
|
|
|
|
|
|
// 获取动画时长并在动画即将结束时发射抛掷物
|
|
|
@@ -1153,7 +1104,7 @@ export class EnemyInstance extends Component {
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
}, 0.1);
|
|
|
}
|
|
|
|
|
|
@@ -1173,7 +1124,7 @@ export class EnemyInstance extends Component {
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
}, 0.1);
|
|
|
}
|
|
|
|
|
|
@@ -1193,7 +1144,7 @@ export class EnemyInstance extends Component {
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
}, 0.1);
|
|
|
}
|
|
|
|
|
|
@@ -1211,9 +1162,9 @@ export class EnemyInstance extends Component {
|
|
|
console.log(`[EnemyInstance] 安全播放待机动画: ${idleName}`);
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
- this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
- }, 0.1);
|
|
|
+ this.scheduleOnce(() => {
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
+ }, 0.1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1238,9 +1189,9 @@ export class EnemyInstance extends Component {
|
|
|
console.log(`[EnemyInstance] 播放近战攻击动画: ${attackName}`);
|
|
|
|
|
|
// 动画切换后延迟更新血条位置,确保动画尺寸已生效
|
|
|
- this.scheduleOnce(() => {
|
|
|
- this.updateHPBarPosition();
|
|
|
- }, 0.1);
|
|
|
+ this.scheduleOnce(() => {
|
|
|
+ this.hpBarAnimation?.updateBarPosition(true);
|
|
|
+ }, 0.1);
|
|
|
|
|
|
// 获取动画时长并在动画结束时造成伤害
|
|
|
const animationDuration = animation.duration;
|