NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/AI/Task/BTMoveTo.cs

96 lines
3.3 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.Unit;
namespace Code.Scripts.Gameplay.BT.Task
{
public class BTMoveTo : Action
{
public SharedBattleInfo SharedBattleInfo;
private GameUnit _owner;
private uint _lastTarget;
private bool _isRegistered;
private bool _isBeingAttacked;
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);
if (_owner != null)
{
// DebugUtil.Log("BT move to start, owner: {0}", _owner?.GetID());
}
}
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()
{
if (SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID)
{
if (_lastTarget != SharedBattleInfo.Value.theChosenOneId && !AIUtils.IsNeedStop(_owner))
{
_lastTarget = SharedBattleInfo.Value.theChosenOneId;
if (GameUnitManager.instance.infightUnits.Contains(SharedBattleInfo.Value.theChosenOneId))
{
var target = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.theChosenOneId);
if (target != null)
AIUtils.CheckAndMove(_owner, target.transData.cellIndex);
}
}
if (_owner?.controlData.targetEnemy == null/* && _owner?.controlData.followUnit == null*/)
{
_lastTarget = Constants.INVALID_UINT_ID;
}
return TaskStatus.Running;
}
return TaskStatus.Failure;
}
public override void OnEnd()
{
if (_isBeingAttacked)
{
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED_FOR_AI,
OnDamaged);
_isRegistered = false;
}
}
}
}