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 { 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(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()); } } 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) _owner?.MoveTo(target); //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(EventManager.EventName.INFIGHT_GET_DAMAGED, OnDamaged); _isRegistered = false; } } } }