using System; using System.Collections.Generic; using Unity.BossRoom.Gameplay.GameplayObjects; using UnityEngine.Pool; using Object = UnityEngine.Object; namespace Unity.BossRoom.Gameplay.Actions { public static class ActionFactory { private static Dictionary> s_ActionPools = new Dictionary>(); private static ObjectPool GetActionPool(ActionID actionID) { if (!s_ActionPools.TryGetValue(actionID, out var actionPool)) { actionPool = new ObjectPool( createFunc: () => Object.Instantiate(GameDataSource.Instance.GetActionPrototypeByID(actionID)), actionOnRelease: action => action.Reset(), actionOnDestroy: Object.Destroy); s_ActionPools.Add(actionID, actionPool); } return actionPool; } /// /// Factory method that creates Actions from their request data. /// /// the data to instantiate this skill from. /// the newly created action. public static Action CreateActionFromData(ref ActionRequestData data) { var ret = GetActionPool(data.ActionID).Get(); ret.Initialize(ref data); return ret; } public static void ReturnAction(Action action) { var pool = GetActionPool(action.ActionID); pool.Release(action); } public static void PurgePooledActions() { foreach (var actionPool in s_ActionPools.Values) { actionPool.Clear(); } } } }