ProjectDDD/Assets/_DDD/_Scripts/RestaurantEvent/Solvers/RestaurantSubsystemSolver.cs

48 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace DDD
{
public abstract class RestaurantSubsystemSolver<T> : MonoBehaviour, IInteractionSolver where T : Enum
{
private Dictionary<T, IInteractionSubsystemSolver<T>> _solvers = new();
protected abstract Dictionary<T, Type> GetSubsystemSolverTypeMappings();
private void Start()
{
foreach (var subsystemSolverType in GetSubsystemSolverTypeMappings())
{
var solver = (IInteractionSubsystemSolver<T>)gameObject.AddComponent(subsystemSolverType.Value);
_solvers.Add(subsystemSolverType.Key, solver);
}
}
public bool ExecuteInteraction(IInteractor interactor, IInteractable interactable, ScriptableObject causerPayload = null, ScriptableObject targetPayloadSo = null)
{
return TryGetSolver(interactable, out var solver) &&
solver.ExecuteInteractionSubsystem(interactor, interactable, causerPayload, targetPayloadSo);
}
public bool CanExecuteInteraction(IInteractor interactor = null, IInteractable interactable = null, ScriptableObject causerPayload = null, ScriptableObject targetPayloadSo = null)
{
return TryGetSolver(interactable, out var solver) &&
solver.CanExecuteInteractionSubsystem(interactor, interactable, causerPayload, targetPayloadSo);
}
// Solver를 가져오는 공통 로직
private bool TryGetSolver(IInteractable interactable, out IInteractionSubsystemSolver<T> solver)
{
solver = null;
var owner = interactable as IInteractionSubsystemOwner;
IInteractionSubsystemObject<T> subsystem = null;
bool isExist = owner != null && owner.TryGetSubsystemObject<T>(out subsystem);
if (!isExist || subsystem == null) return false;
var type = subsystem.GetInteractionSubsystemType();
return _solvers.TryGetValue(type, out solver);
}
}
}