main
zhangaotian 2024-07-15 16:51:32 +08:00
parent a6ed6ec97c
commit 21ecaa15b1
2 changed files with 58 additions and 13 deletions

View File

@ -11,7 +11,7 @@ namespace Code.Scripts.Gameplay.BT.Conditional
public class BTWithinSight : BehaviorDesigner.Runtime.Tasks.Conditional
{
public SharedBattleInfo SharedBattleInfo;
public bool _isBeingAttacked;
private bool _isBeingAttacked;
private bool _isRegistered;
private GameUnit _owner;
private Level _level;
@ -44,7 +44,8 @@ namespace Code.Scripts.Gameplay.BT.Conditional
: _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()}");
_owner.statusData.isAttackActive = true;
//DebugUtil.LogG($"BT harmed by: {data.sender?.GetID()} ,But Target is {_target.GetID()}, owner: {_owner?.GetID()}");
}
}
}
@ -54,7 +55,6 @@ namespace Code.Scripts.Gameplay.BT.Conditional
AIUtils.ActiveAIAttack(_owner);
if (_isBeingAttacked && SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID)
{
_owner.statusData.isAttackActive = true;
return TaskStatus.Success;
}
@ -70,6 +70,7 @@ namespace Code.Scripts.Gameplay.BT.Conditional
}
else
{
if (SharedBattleInfo.Value.theChosenOneId != Constants.INVALID_UINT_ID) return TaskStatus.Running;
return TaskStatus.Failure;
}
}
@ -101,15 +102,12 @@ namespace Code.Scripts.Gameplay.BT.Conditional
OnDamaged);
_isRegistered = false;
}
_isBeingAttacked = false;
}
public override void OnBehaviorComplete()
{
EventManager.Instance.Unregister<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
OnDamaged);
_isBeingAttacked = false;
_isRegistered = false;
}
}

View File

@ -2,8 +2,10 @@ using BehaviorDesigner.Runtime.Tasks;
using Framework;
using Gameplay.AI;
using Gameplay.BT.Variables;
using Gameplay.Character;
using Gameplay.Common;
using Gameplay.Unit;
using UnityEngine;
namespace Code.Scripts.Gameplay.BT.Task
{
@ -14,33 +16,78 @@ namespace Code.Scripts.Gameplay.BT.Task
private GameUnit _owner;
private uint _lastTarget;
private bool _isRegistered;
private bool _isBeingAttacked;
private GameUnit _target;
public override void OnStart()
{
_owner = GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.ownerId);
if (!_isRegistered)
{
EventManager.Instance.Register<EventDataGetDamaged>(EventManager.EventName.INFIGHT_GET_DAMAGED,
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());
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))
if (_lastTarget != SharedBattleInfo.Value.theChosenOneId && !AIUtils.IsNeedStop(_owner))
{
_lastTarget = SharedBattleInfo.Value.theChosenOneId;
_owner?.MoveTo(GameUnitManager.instance.infightUnits.Search(SharedBattleInfo.Value.theChosenOneId));
DebugUtil.LogG($"BT move to update success, owner: {_owner?.GetID()}, target: {SharedBattleInfo.Value.theChosenOneId}");
//DebugUtil.LogG($"BT move to update success, owner: {_owner?.GetID()}, target: {SharedBattleInfo.Value.theChosenOneId}");
}
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,
OnDamaged);
_isRegistered = false;
}
}
}
}