Interactor 인터페이스에 Solver type fetch하는 함수 추가

This commit is contained in:
Jeonghyeon Ha 2025-08-29 16:50:54 +09:00
parent 6d1973a312
commit 8a1b13d075
4 changed files with 25 additions and 26 deletions

View File

@ -56,6 +56,7 @@ public interface IInteractor
GameObject GetInteractorGameObject(); GameObject GetInteractorGameObject();
IInteractable GetFocusedInteractable(); IInteractable GetFocusedInteractable();
bool CanSolveInteractionType(InteractionType interactionType); bool CanSolveInteractionType(InteractionType interactionType);
bool FetchSolverTypeForInteraction(InteractionType type, out Type solverType);
bool IsInteractionHidden(IInteractable interactable); bool IsInteractionHidden(IInteractable interactable);
bool CanInteractTo(IInteractable interactable, bool CanInteractTo(IInteractable interactable,
ScriptableObject payloadSo = null); ScriptableObject payloadSo = null);

View File

@ -108,7 +108,7 @@ public bool IsInteractionHidden(IInteractable interactable)
return true; return true;
} }
protected virtual bool FetchSolverTypeForInteraction(InteractionType type, out Type solverType) public virtual bool FetchSolverTypeForInteraction(InteractionType type, out Type solverType)
{ {
return RestaurantInteractionEventSolvers.TypeToSolver.TryGetValue(type, out solverType); return RestaurantInteractionEventSolvers.TypeToSolver.TryGetValue(type, out solverType);
} }

View File

@ -201,7 +201,7 @@ protected IInteractable GetNearestInteractable()
return closest; return closest;
} }
protected override bool FetchSolverTypeForInteraction(InteractionType type, out Type solverType) public override bool FetchSolverTypeForInteraction(InteractionType type, out Type solverType)
{ {
if (RestaurantInteractionEventSolvers.TypeToPlayerSolver.TryGetValue(type, out solverType)) if (RestaurantInteractionEventSolvers.TypeToPlayerSolver.TryGetValue(type, out solverType))
{ {

View File

@ -1,18 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine; using UnityEngine;
namespace DDD.Restaurant namespace DDD.Restaurant
{ {
public static class RestaurantInteractionEventSolvers public static class RestaurantInteractionEventSolvers
{ {
public static Dictionary<InteractionType, Type> TypeToSolver = new() public static readonly Dictionary<InteractionType, Type> TypeToSolver = new()
{ {
{InteractionType.RestaurantManagement, typeof(RestaurantManagementSolver)}, {InteractionType.RestaurantManagement, typeof(RestaurantManagementSolver)},
{InteractionType.RestaurantOrder, typeof(RestaurantOrderSolver)}, {InteractionType.RestaurantOrder, typeof(RestaurantOrderSolver)},
{InteractionType.RestaurantCook, typeof(RestaurantCookSolver)} {InteractionType.RestaurantCook, typeof(RestaurantCookSolver)}
}; };
public static Dictionary<InteractionType, Type> TypeToPlayerSolver = new() public static readonly Dictionary<InteractionType, Type> TypeToPlayerSolver = new()
{ {
{InteractionType.RestaurantOrder, typeof(RestaurantOrderPlayerSolver)}, {InteractionType.RestaurantOrder, typeof(RestaurantOrderPlayerSolver)},
}; };
@ -46,26 +47,21 @@ public bool RequestInteraction(GameObject causer, GameObject target, Interaction
var evt = MakeInteractionEvent(causer, target, interactionType, payload); var evt = MakeInteractionEvent(causer, target, interactionType, payload);
evt.EventResult = false; evt.EventResult = false;
// Solve event directly. 이벤트 처리는 여기서 하고, 이벤트 호출로는 이런 이벤트가 호출되었고 결과가 어떻다는 거 전파하는 식으로.
if (RestaurantInteractionEventSolvers.TypeToSolver.TryGetValue(interactionType, out var solverType))
{
Component solverComponent = causer.GetComponent(solverType);
IInteractionSolver solver = solverComponent as IInteractionSolver;
IInteractor interactor = causer.GetComponent<IInteractor>();
IInteractable interactable = target.GetComponent<IInteractable>();
// Cast solverComponent to IInteractable IInteractor interactor = causer.GetComponent<IInteractor>();
if (solver is not null && interactor is not null) if (interactor != null && interactor.CanSolveInteractionType(interactionType))
{
if (interactor.FetchSolverTypeForInteraction(interactionType, out var solverType))
{
// Solve event directly. 이벤트 처리는 여기서 하고, 이벤트 호출로는 이런 이벤트가 호출되었고 결과가 어떻다는 거 전파하는 식으로.
if (solverType != null)
{
IInteractionSolver solver = causer.GetComponent(solverType) as IInteractionSolver;
IInteractable interactable = target.GetComponent<IInteractable>();
if (solver is not null)
{ {
bool canExecute = solver.CanExecuteInteraction(interactor, interactable, evt.Payload); bool canExecute = solver.CanExecuteInteraction(interactor, interactable, evt.Payload);
if (canExecute) evt.EventResult = canExecute && solver.ExecuteInteraction(interactor, interactable, evt.Payload);
{
evt.EventResult = solver.ExecuteInteraction(interactor, interactable, evt.Payload);
}
else
{
evt.EventResult = false;
}
} }
else else
{ {
@ -73,6 +69,8 @@ public bool RequestInteraction(GameObject causer, GameObject target, Interaction
Debug.Assert(false, "Solver Component or Interactor is null"); Debug.Assert(false, "Solver Component or Interactor is null");
} }
} }
}
}
EventBus.Broadcast(evt);// 이벤트 결과를 이거 받아서 처리하면 될듯. EventBus.Broadcast(evt);// 이벤트 결과를 이거 받아서 처리하면 될듯.
return evt.EventResult; return evt.EventResult;