85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
using BehaviorDesigner.Runtime.Tasks;
|
||
using Framework;
|
||
using Gameplay.AI;
|
||
using Gameplay.BT.Variables;
|
||
using Gameplay.Character;
|
||
using Gameplay.Common;
|
||
using Gameplay.Level;
|
||
using Gameplay.Unit;
|
||
|
||
namespace Code.Scripts.Gameplay.BT.Conditional
|
||
{
|
||
public class BTBeingAttacked : BehaviorDesigner.Runtime.Tasks.Conditional
|
||
{
|
||
public SharedBattleInfo SharedBattleInfo;
|
||
public bool _isBeingAttacked;
|
||
private bool _isRegistered;
|
||
private GameUnit _owner;
|
||
private GameUnit _target;
|
||
|
||
|
||
public override void OnStart()
|
||
{
|
||
|
||
if (!_isRegistered)
|
||
{
|
||
EventManager.Instance.Register<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED, OnDamaged);
|
||
_isRegistered = true;
|
||
_isBeingAttacked = false;
|
||
}
|
||
|
||
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerId);
|
||
}
|
||
|
||
private void OnDamaged(EventDataGetDamaged data)
|
||
{
|
||
//data.sender 发送受伤的角色 data.owner 受到伤害的角色
|
||
if (data.owner.GetID() == _owner.GetID())
|
||
{
|
||
_isBeingAttacked = true;
|
||
_owner.statusData.isAttackActive = true;
|
||
if (data.sender != null)
|
||
{
|
||
_target = _target != null
|
||
? _target = AIUtils.GetNearestGameUnit(data.owner, data.sender, _target)
|
||
: _target = data.sender;
|
||
_target = AIUtils.GetNearestGameUnit(data.owner, data.sender, _target);
|
||
SharedBattleInfo.Value.theChosenOneId = _target.GetID();
|
||
DebugUtil.LogG($"BT harmed by: {data.sender?.GetID()} ,But Target is :{_target.GetID()}, owner: {_owner?.GetID()}");
|
||
}
|
||
}
|
||
}
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
if (_isBeingAttacked && SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID
|
||
&& SharedBattleInfo.Value.AIName == LevelData.AIType.FallBackAI)
|
||
{
|
||
return TaskStatus.Success;
|
||
}
|
||
if (_isBeingAttacked && SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID
|
||
&& SharedBattleInfo.Value.AIName == LevelData.AIType.FallBackIntensifyAI)
|
||
{
|
||
return AIUtils.FindEnemiesInAttackDistance(_owner).Contains(_target) ? TaskStatus.Running : TaskStatus.Success;
|
||
}
|
||
return TaskStatus.Running;
|
||
}
|
||
|
||
public override void OnEnd()
|
||
{
|
||
if (_isBeingAttacked)
|
||
{
|
||
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED, OnDamaged);
|
||
_isRegistered = false;
|
||
}
|
||
_isBeingAttacked = false;
|
||
}
|
||
|
||
public override void OnBehaviorComplete()
|
||
{
|
||
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED, OnDamaged);
|
||
_isBeingAttacked = false;
|
||
_isRegistered = false;
|
||
}
|
||
}
|
||
} |