NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/Conditional/BTWithinSight.cs

116 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
public 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();
// 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 && SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID)
{
_owner.statusData.isAttackActive = true;
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;
}
else
{
return TaskStatus.Failure;
}
}
else 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;
}
_isBeingAttacked = false;
}
public override void OnBehaviorComplete()
{
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
OnDamaged);
_isBeingAttacked = false;
_isRegistered = false;
}
}
}