diff --git a/Assets/_DDD/_Scripts/AI/Common/IAISharedBlackboard.cs b/Assets/_DDD/_Scripts/AI/Common/IAISharedBlackboard.cs index 154eebd47..8136be741 100644 --- a/Assets/_DDD/_Scripts/AI/Common/IAISharedBlackboard.cs +++ b/Assets/_DDD/_Scripts/AI/Common/IAISharedBlackboard.cs @@ -9,7 +9,7 @@ namespace DDD /// public interface IAISharedBlackboard { - void SetCurrentInteractionTarget(GameObject targetGameObject); - GameObject GetCurrentInteractionTarget(); + void SetBlackboardGameObject(string key, GameObject inGameObject); + GameObject GetBlackboardGameObject(string key); } } diff --git a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/LookAtInteractionTarget.cs b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/LookAtInteractionTarget.cs index e00335603..1c0938cbe 100644 --- a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/LookAtInteractionTarget.cs +++ b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/LookAtInteractionTarget.cs @@ -30,42 +30,43 @@ public interface ILookAtVisual void EndLookAt(); } - private ILookAtVisual visual; - private GameObject cachedTarget; - private bool isLooking; - private Vector3 currentLookPosition; + private ILookAtVisual _visual; + private GameObject _cachedTarget; + private bool _isLooking; + private Vector3 _currentLookPosition; public override void OnStart() { - visual = gameObject.GetComponentInParent(); - cachedTarget = null; - isLooking = false; + _visual = gameObject.GetComponentInParent(); + _isLooking = false; + + var blackboard = gameObject.GetComponent(); + _cachedTarget = blackboard.GetBlackboardGameObject(nameof(RestaurantCustomerBlackboardKey.CurrentInteractionTarget)); } public override TaskStatus OnUpdate() - { - var target = GetTarget(); - if (target == null) + { + if (_cachedTarget == null) { - if (isLooking) + if (_isLooking) { // 타겟이 사라졌다면 정리 - visual?.EndLookAt(); - isLooking = false; + _visual?.EndLookAt(); + _isLooking = false; } return TaskStatus.Success; } - currentLookPosition = CalculateLookPosition(target); + _currentLookPosition = CalculateLookPosition(_cachedTarget); - if (!isLooking) + if (!_isLooking) { - visual?.TryBeginLookAt(currentLookPosition); - isLooking = true; + _visual?.TryBeginLookAt(_currentLookPosition); + _isLooking = true; } else { - visual?.UpdateLookAt(currentLookPosition); + _visual?.UpdateLookAt(_currentLookPosition); } // 연속 업데이트면 Running, 아니면 1회만 시도 후 Success 반환 @@ -74,33 +75,12 @@ public override TaskStatus OnUpdate() public override void OnEnd() { - if (isLooking) + if (_isLooking) { - visual?.EndLookAt(); - isLooking = false; + _visual?.EndLookAt(); + _isLooking = false; } - cachedTarget = null; - } - - private GameObject GetTarget() - { - // 캐시된 타겟이 유효하면 재사용 - if (IsValidTarget(cachedTarget)) - return cachedTarget; - - // 블랙보드에서 타겟 검색 - cachedTarget = gameObject.GetComponent() - ?.GetCurrentInteractionTarget(); - - if (IsValidTarget(cachedTarget)) - return cachedTarget; - - // Interactor의 포커스된 타겟 검색 - var interactor = gameObject.GetComponent(); - var focusedInteractable = interactor?.GetFocusedInteractable(); - cachedTarget = focusedInteractable?.GetInteractableGameObject(); - - return cachedTarget; + _cachedTarget = null; } private static bool IsValidTarget(GameObject target) => target != null && target; diff --git a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/MoveToInteractionTarget.cs b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/MoveToInteractionTarget.cs index 58a3ce5a7..9bf125e0f 100644 --- a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/MoveToInteractionTarget.cs +++ b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Common/Actions/MoveToInteractionTarget.cs @@ -23,32 +23,30 @@ public class MoveToInteractionTarget : Action private float _repathTimer; private Vector3 _currentDestination; private bool _isMoving; + private GameObject _target; public override void OnStart() { _movement = gameObject.GetComponent(); _repathTimer = 0f; _isMoving = false; - - Debug.Log($"MoveToInteractionTarget - GameObject: {gameObject.name}"); + + var blackboard = gameObject.GetComponent(); + _target = blackboard.GetBlackboardGameObject(nameof(RestaurantCustomerBlackboardKey.CurrentInteractionTarget)); } public override TaskStatus OnUpdate() { if (_movement == null) return TaskStatus.Failure; - - var target = GetTarget(); - if (target == null) + if (_target == null) { - return TaskStatus.Running; // If has no target, stuck in this state + return TaskStatus.Failure; } - - Debug.Log(target.name); - + if (ShouldUpdateDestination()) { - _currentDestination = CalculateDestination(target); + _currentDestination = CalculateDestination(_target); StartOrUpdateMovement(); } @@ -60,15 +58,6 @@ public override void OnEnd() StopMovement(); } - private GameObject GetTarget() - { - // Interactor의 포커스된 타겟 검색 - var interactor = gameObject.GetComponent(); - var focusedInteractable = interactor?.GetFocusedInteractable(); - if (focusedInteractable != null) return focusedInteractable.GetInteractableGameObject(); - return null; - } - private bool ShouldUpdateDestination() { _repathTimer -= Time.deltaTime; diff --git a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/Actions/StartRestaurantOrder.cs b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/Actions/StartRestaurantOrder.cs index f7b8a1acf..98d9b39c6 100644 --- a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/Actions/StartRestaurantOrder.cs +++ b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/Actions/StartRestaurantOrder.cs @@ -12,6 +12,8 @@ public class StartRestaurantOrder : Action [SerializeField] private bool _requireCanInteract = true; [Tooltip("성공 시 블랙보드에 현재 인터랙션 대상을 등록합니다")] [SerializeField] private bool _registerOnBlackboard = true; + [Tooltip("성공 시 블랙보드에 현재 인터랙션 대상을 등록합니다")] + [SerializeField] private bool _UnregisterOnBlackboard = false; private IInteractor _interactor; private bool _isGetInteractor; @@ -43,9 +45,14 @@ public override TaskStatus OnUpdate() if (_registerOnBlackboard) { - // 하위 호환: 고객 전용 블랙보드 지원 - var customerBlackboard = gameObject.GetComponent(); - customerBlackboard?.SetCurrentInteractionTarget(outInteractable.gameObject); + var customerBlackboard = gameObject.GetComponent(); + customerBlackboard?.SetBlackboardGameObject(nameof(RestaurantCustomerBlackboardKey.CurrentInteractionTarget), outInteractable.gameObject); + } + + if (_UnregisterOnBlackboard) + { + var customerBlackboard = gameObject.GetComponent(); + customerBlackboard?.SetBlackboardGameObject(nameof(RestaurantCustomerBlackboardKey.CurrentInteractionTarget), null); } return TaskStatus.Success; diff --git a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/CustomerBlackboardComponent.cs b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/CustomerBlackboardComponent.cs index d23f14605..8e594e7be 100644 --- a/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/CustomerBlackboardComponent.cs +++ b/Assets/_DDD/_Scripts/Restaurant/Character/AI/Customer/CustomerBlackboardComponent.cs @@ -6,8 +6,7 @@ namespace DDD public class CustomerBlackboardComponent : MonoBehaviour, ICustomerBlackboard, IAISharedBlackboard { private Subtree _subtree; - private GameObject _currentInteractionTarget; - + public void InitializeWithBehaviorTree(Subtree subtree) { _subtree = subtree; @@ -22,18 +21,17 @@ public void SetCustomerData(CustomerData inCustomerData) if (_subtree == null) return; _subtree.SetVariableValue(nameof(RestaurantCustomerBlackboardKey.CustomerData), inCustomerData); } - - public void SetCurrentInteractionTarget(GameObject targetGameObject) + + public void SetBlackboardGameObject(string key, GameObject inGameObject) { - _currentInteractionTarget = targetGameObject; if (_subtree == null) return; - _subtree.SetVariableValue(nameof(RestaurantCustomerBlackboardKey.CurrentInteractionTarget), targetGameObject); + _subtree.SetVariableValue(key, inGameObject); } - public GameObject GetCurrentInteractionTarget() + public GameObject GetBlackboardGameObject(string key) { - // 캐시 우선 반환. 필요 시 Subtree에서 직접 조회하도록 확장 가능. - return _currentInteractionTarget; + if (_subtree == null) return null; + return _subtree.GetVariable(key)?.Value; } } } \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/Restaurant/Character/Interfaces/ICustomerBlackboard.cs b/Assets/_DDD/_Scripts/Restaurant/Character/Interfaces/ICustomerBlackboard.cs index 3f811c8a3..31c03ad24 100644 --- a/Assets/_DDD/_Scripts/Restaurant/Character/Interfaces/ICustomerBlackboard.cs +++ b/Assets/_DDD/_Scripts/Restaurant/Character/Interfaces/ICustomerBlackboard.cs @@ -12,7 +12,5 @@ public enum RestaurantCustomerBlackboardKey public interface ICustomerBlackboard { void SetCustomerData(CustomerData inCustomerData); - void SetCurrentInteractionTarget(GameObject targetGameObject); - GameObject GetCurrentInteractionTarget(); } } \ No newline at end of file