164 lines
6.0 KiB
C#
164 lines
6.0 KiB
C#
using BehaviorDesigner.Runtime.Tasks;
|
|
using Gameplay.BT.Variables;
|
|
using Gameplay.Character;
|
|
using Gameplay.Level;
|
|
using Gameplay.Unit;
|
|
using Gameplay.AI;
|
|
using Framework;
|
|
|
|
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 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);
|
|
|
|
// 优先攻击Npc
|
|
if (SharedBattleInfo.Value.AttachAIName == LevelData.AttachAIType.AttackProtectUnitAI)
|
|
{
|
|
if (CheckEnemyNpc()) return TaskStatus.Success;
|
|
}
|
|
|
|
// 有目标敌人 高优先级 GameUnit会自动标记targetEnemy
|
|
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 && 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)
|
|
{
|
|
var cellIndex = enemy.transData.cellIndex;
|
|
if (AIUtils.CheckPathExits(_owner, cellIndex))
|
|
{
|
|
_target = enemy;
|
|
_owner.statusData.isInFight = true;
|
|
return TaskStatus.Success;
|
|
}
|
|
else
|
|
{
|
|
SharedBattleInfo.Value.cannotReach.Add(cellIndex);
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
|
}
|
|
}
|
|
}
|
|
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) return TaskStatus.Failure;
|
|
|
|
var cellIndex = enemy.transData.cellIndex;
|
|
if (SharedBattleInfo.Value.cannotReach.Contains(cellIndex)) return TaskStatus.Failure;
|
|
|
|
if (AIUtils.CheckPathExits(_owner, cellIndex))
|
|
{
|
|
_target = enemy;
|
|
_owner.statusData.isInFight = true;
|
|
SharedBattleInfo.Value.targetID = enemy.GetID();
|
|
return TaskStatus.Success;
|
|
}
|
|
else
|
|
{
|
|
SharedBattleInfo.Value.cannotReach.Add(cellIndex);
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
|
}
|
|
}
|
|
|
|
return TaskStatus.Failure;
|
|
}
|
|
|
|
private bool CheckEnemyNpc()
|
|
{
|
|
var enemy = AIUtils.FindNearestEnemyNpcInShootRange(_owner);
|
|
if (enemy == null) return false;
|
|
|
|
var cellIndex = enemy.transData.cellIndex;
|
|
if (SharedBattleInfo.Value.cannotReach.Contains(cellIndex)) return false;
|
|
|
|
if (AIUtils.CheckPathExits(_owner, cellIndex))
|
|
{
|
|
_target = enemy;
|
|
SharedBattleInfo.Value.targetID = enemy.GetID();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
SharedBattleInfo.Value.cannotReach.Add(cellIndex);
|
|
SharedBattleInfo.Value.targetID = Constants.INVALID_UINT_ID;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |