using System.Collections.Generic; using Cielonos.MainGame.Characters; using Cielonos.UI; using Sirenix.OdinInspector; using SLSFramework.General; using UnityEngine; namespace Cielonos.MainGame.Inventory { public class FunctionSubmodule : SubmoduleBase { [ReadOnly] public Dictionary functionUnits; public RuntimeFunctionUnit this[string functionName] => functionUnits.ContainsKey(functionName) ? functionUnits[functionName] : null; public FunctionSubmodule(ItemBase owner, FunctionData data) : base(owner) { functionUnits = new Dictionary(); foreach (var kvp in data.functionUnits) { functionUnits[kvp.Key] = new RuntimeFunctionUnit(this, kvp.Value); } } public void Update(float deltaTime) { foreach (var unit in functionUnits.Values) { unit.Update(deltaTime); } } } public class RuntimeFunctionUnit : SubmoduleBase { private CharacterBase character => owner.owner.player; public FunctionData.FunctionUnit data; public float currentCooldown; public float maxCooldown; public RuntimeFunctionUnit(FunctionSubmodule owner, FunctionData.FunctionUnit data) : base(owner) { this.data = data; maxCooldown = data.interval; currentCooldown = 0f; } public void Update(float deltaTime) { currentCooldown -= deltaTime; currentCooldown = Mathf.Max(currentCooldown, 0f); } public bool IsAvailable() { bool cooldownAvailable = currentCooldown <= 0f; bool energyAvailable = character.attributeSm["Energy"] >= data.energyCost; return cooldownAvailable && energyAvailable; } public RuntimeFunctionUnit Execute() { ResetCooldown(); ConsumeEnergy(); return this; } private void ResetCooldown() { currentCooldown = maxCooldown; } private void ConsumeEnergy() { character.attributeSm["Energy"] -= data.energyCost; character.eventSm.onUseEnergy.Invoke(character, data.energyCost); } } }