114 lines
4.2 KiB
C#
114 lines
4.2 KiB
C#
using BehaviorDesigner.Runtime.Tasks;
|
||
using Framework;
|
||
using Gameplay.AI;
|
||
using Gameplay.BT.Variables;
|
||
using Gameplay.Character;
|
||
using Gameplay.Level;
|
||
using Gameplay.Unit;
|
||
|
||
namespace Code.Scripts.Gameplay.BT.Conditional
|
||
{
|
||
public class BTWithinSight : BehaviorDesigner.Runtime.Tasks.Conditional
|
||
{
|
||
public SharedBattleInfo SharedBattleInfo;
|
||
private bool _isBeingAttacked;
|
||
private bool _isRegistered;
|
||
private GameUnit _owner;
|
||
private Level _level;
|
||
private GameUnit _target;
|
||
|
||
public override void OnStart()
|
||
{
|
||
if (!_isRegistered)
|
||
{
|
||
EventManager.Instance.Register<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
|
||
OnDamaged);
|
||
_isRegistered = true;
|
||
_isBeingAttacked = false;
|
||
}
|
||
|
||
_level = LevelManager.Instance.CurrentLevel;
|
||
_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;
|
||
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();
|
||
_owner.statusData.isAttackActive = true;
|
||
//DebugUtil.LogG($"BT harmed by: {data.sender?.GetID()} ,But Target is :{_target.GetID()}, owner: {_owner?.GetID()}");
|
||
}
|
||
}
|
||
}
|
||
|
||
public override TaskStatus OnUpdate()
|
||
{
|
||
AIUtils.ActiveAIAttack(_owner);
|
||
if (_isBeingAttacked || _owner.statusData.isAttackActive)
|
||
{
|
||
if (SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID)
|
||
return TaskStatus.Success;
|
||
}
|
||
|
||
if (_owner != null && _level != null
|
||
&& _owner.controlData.followUnit == null
|
||
&& _owner.controlData.targetEnemy == null)
|
||
{
|
||
var enemy = AIUtils.FindNearestEnemyInSightDistance(_owner);
|
||
if (enemy != null)
|
||
{
|
||
SharedBattleInfo.Value.theChosenOneId = enemy.GetID();
|
||
return TaskStatus.Success;
|
||
}
|
||
|
||
if (SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID) return TaskStatus.Running;
|
||
return TaskStatus.Failure;
|
||
}
|
||
|
||
if (_owner != null && _level != null
|
||
&& (_owner.controlData.followUnit != null ||
|
||
_owner.controlData.targetEnemy != null))
|
||
{
|
||
if (_owner.controlData.targetEnemy != null && _owner.statusData.isAttackActive)
|
||
{
|
||
SharedBattleInfo.Value.theChosenOneId = _owner.controlData.targetEnemy.GetID();
|
||
return TaskStatus.Success;
|
||
}
|
||
|
||
if (_owner.controlData.followUnit != null && _owner.statusData.isAttackActive)
|
||
{
|
||
SharedBattleInfo.Value.theChosenOneId = _owner.controlData.followUnit.GetID();
|
||
return TaskStatus.Success;
|
||
}
|
||
}
|
||
|
||
return TaskStatus.Running;
|
||
}
|
||
|
||
public override void OnEnd()
|
||
{
|
||
if (_isBeingAttacked)
|
||
{
|
||
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
|
||
OnDamaged);
|
||
_isRegistered = false;
|
||
}
|
||
}
|
||
|
||
public override void OnBehaviorComplete()
|
||
{
|
||
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
|
||
OnDamaged);
|
||
_isRegistered = false;
|
||
}
|
||
}
|
||
} |