117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using BehaviorDesigner.Runtime.Tasks;
|
|
using Framework;
|
|
using Gameplay.AI;
|
|
using Gameplay.BT.Variables;
|
|
using Gameplay.Character;
|
|
using Gameplay.Unit;
|
|
|
|
public class BTDefendWithinSight : Conditional
|
|
{
|
|
public SharedBattleInfo SharedBattleInfo;
|
|
private 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_FOR_AI,
|
|
OnDamaged);
|
|
_isRegistered = true;
|
|
_isBeingAttacked = false;
|
|
}
|
|
|
|
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerID);
|
|
}
|
|
|
|
private void OnDamaged(EventDataGetDamaged data)
|
|
{
|
|
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.targetID = _target.GetID();
|
|
}
|
|
}
|
|
}
|
|
|
|
public override TaskStatus OnUpdate()
|
|
{
|
|
if (_owner == null) return TaskStatus.Running;
|
|
|
|
AIUtils.ActiveAIAttack(_owner);
|
|
|
|
// 有目标敌人
|
|
if (_owner.statusData.isAttackActive && _owner.controlData.targetEnemy != null)
|
|
{
|
|
_owner.statusData.isInFight = true;
|
|
_target = _owner.controlData.targetEnemy;
|
|
SharedBattleInfo.Value.targetID = _owner.controlData.targetEnemy.GetID();
|
|
return TaskStatus.Success;
|
|
}
|
|
|
|
// 被攻击
|
|
if (_owner.statusData.isAttackActive && _isBeingAttacked &&
|
|
SharedBattleInfo.Value.targetID != Constants.INVALID_UINT_ID)
|
|
{
|
|
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.targetID))
|
|
{
|
|
var enemy = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.targetID);
|
|
if (enemy != null && !enemy.statusData.isDead)
|
|
{
|
|
_owner.statusData.isInFight = true;
|
|
_target = enemy;
|
|
return TaskStatus.Success;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
|
}
|
|
}
|
|
|
|
// 如果目标都为空 寻找敌人
|
|
if (_owner.controlData.targetEnemy == null && _owner.controlData.followUnit == null)
|
|
{
|
|
var enemy = AIUtils.FindNearestEnemyInSightDistance(_owner);
|
|
if (enemy != null)
|
|
{
|
|
_owner.statusData.isInFight = true;
|
|
_target = enemy;
|
|
SharedBattleInfo.Value.targetID = enemy.GetID();
|
|
return TaskStatus.Success;
|
|
}
|
|
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
|
|
|
return TaskStatus.Failure;
|
|
}
|
|
|
|
return TaskStatus.Running;
|
|
}
|
|
|
|
public override void OnEnd()
|
|
{
|
|
if (_isBeingAttacked)
|
|
{
|
|
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED_FOR_AI,
|
|
OnDamaged);
|
|
_isRegistered = false;
|
|
}
|
|
}
|
|
|
|
public override void OnBehaviorComplete()
|
|
{
|
|
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED_FOR_AI,
|
|
OnDamaged);
|
|
_isRegistered = false;
|
|
}
|
|
} |