81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
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<ItemBase>
|
|
{
|
|
[ReadOnly]
|
|
public Dictionary<string, RuntimeFunctionUnit> functionUnits;
|
|
|
|
public RuntimeFunctionUnit this[string functionName] => functionUnits.ContainsKey(functionName) ? functionUnits[functionName] : null;
|
|
|
|
public FunctionSubmodule(ItemBase owner, FunctionData data) : base(owner)
|
|
{
|
|
functionUnits = new Dictionary<string, RuntimeFunctionUnit>();
|
|
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<FunctionSubmodule>
|
|
{
|
|
private CharacterBase character => owner.owner.player;
|
|
|
|
public ItemFunctionUnit data;
|
|
public float currentCooldown;
|
|
public float maxCooldown;
|
|
|
|
public RuntimeFunctionUnit(FunctionSubmodule owner, ItemFunctionUnit 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);
|
|
}
|
|
}
|
|
} |