87 lines
3.0 KiB
C#
87 lines
3.0 KiB
C#
using BehaviorDesigner.Runtime.Tasks;
|
|
using Gameplay.BT.Variables;
|
|
using Gameplay.Character;
|
|
using Gameplay.Unit;
|
|
using Gameplay.AI;
|
|
using Framework;
|
|
|
|
namespace Code.Scripts.Gameplay.BT.Task
|
|
{
|
|
public class BTMoveTo : Action
|
|
{
|
|
public SharedBattleInfo SharedBattleInfo;
|
|
private GameUnit _owner;
|
|
private GameUnit _target;
|
|
|
|
private uint _lastTarget;
|
|
private bool _isRegistered;
|
|
private bool _isBeingAttacked;
|
|
|
|
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)
|
|
{
|
|
//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;
|
|
}
|
|
}*/
|
|
}
|
|
|
|
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)
|
|
{
|
|
_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;
|
|
}
|
|
}
|
|
}
|
|
} |