49 lines
1.9 KiB
C#
49 lines
1.9 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 payloadSo = null)
|
||
|
{
|
||
|
return TryGetSolver(interactable, out var solver) &&
|
||
|
solver.ExecuteInteractionSubsystem(interactor, interactable, payloadSo);
|
||
|
}
|
||
|
|
||
|
public bool CanExecuteInteraction(IInteractor interactor = null, IInteractable interactable = null,
|
||
|
ScriptableObject payloadSo = null)
|
||
|
{
|
||
|
return TryGetSolver(interactable, out var solver) &&
|
||
|
solver.CanExecuteInteractionSubsystem(interactor, interactable, payloadSo);
|
||
|
}
|
||
|
|
||
|
// 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);
|
||
|
}
|
||
|
}
|
||
|
}
|