mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
feat: add energy system
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c78247f0460346b9ac065e10c8200ad1
|
||||
timeCreated: 1771750078
|
||||
@@ -0,0 +1,23 @@
|
||||
using _Project.Develop.Runtime.Entities;
|
||||
using _Project.Develop.Runtime.Utilities.Conditions;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
|
||||
|
||||
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Energy
|
||||
{
|
||||
public class CurrentEnergy : IEntityComponent { public ReactiveVariable<int> Value; }
|
||||
public class MaxEnergy : IEntityComponent { public ReactiveVariable<int> Value; }
|
||||
|
||||
public class CanUseEnergy : IEntityComponent { public ICompositeCondition Value; }
|
||||
public class UseEnergyRequest : IEntityComponent { public ReactiveEvent<int> Value; }
|
||||
public class UseEnergyEvent : IEntityComponent { public ReactiveEvent<int> Value; }
|
||||
|
||||
public class CanRegenEnergy : IEntityComponent { public ICompositeCondition Value; }
|
||||
public class RegenEnergyRequest : IEntityComponent { public ReactiveEvent<int> Value; }
|
||||
public class RegenEnergyEvent : IEntityComponent { public ReactiveEvent<int> Value; }
|
||||
|
||||
public class AutoRegenEnergyAmount : IEntityComponent { public ReactiveVariable<int> Value; }
|
||||
public class InAutoRegenEnergy : IEntityComponent { public ReactiveVariable<bool> Value; }
|
||||
public class EnergyAutoRegenInitialTime : IEntityComponent { public ReactiveVariable<float> Value; }
|
||||
public class EnergyAutoRegenCurrentTime : IEntityComponent { public ReactiveVariable<float> Value; }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a68596f1e7224a52a8b0fd4f8fa6d615
|
||||
timeCreated: 1771750088
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 42c288d27514401b8476a278887ed7b2
|
||||
timeCreated: 1771750377
|
||||
@@ -0,0 +1,43 @@
|
||||
using _Project.Develop.Runtime.Entities;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
|
||||
|
||||
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Energy.Systems
|
||||
{
|
||||
public class AutoRegenEnergyTimerSystem : IInitializableSystem, IUpdatableSystem
|
||||
{
|
||||
private ReactiveEvent<int> _regenEnergyRequest;
|
||||
|
||||
private ReactiveVariable<float> _initialTime;
|
||||
private ReactiveVariable<float> _currentTime;
|
||||
|
||||
private ReactiveVariable<int> _regenAmount;
|
||||
|
||||
private ReactiveVariable<bool> _inAutoRegen;
|
||||
|
||||
public void OnInit(Entity entity)
|
||||
{
|
||||
_inAutoRegen = entity.InAutoRegenEnergy;
|
||||
_regenAmount = entity.AutoRegenEnergyAmount;
|
||||
|
||||
_initialTime = entity.EnergyAutoRegenInitialTime;
|
||||
_currentTime = entity.EnergyAutoRegenCurrentTime;
|
||||
}
|
||||
|
||||
public void OnUpdate(float deltaTime)
|
||||
{
|
||||
if (_inAutoRegen.Value == false)
|
||||
return;
|
||||
|
||||
_currentTime.Value += deltaTime;
|
||||
|
||||
if (TimeIsDone(_currentTime.Value))
|
||||
{
|
||||
_currentTime.Value = 0;
|
||||
_regenEnergyRequest.Invoke(_regenAmount.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private bool TimeIsDone(float currentTime) => currentTime >= _initialTime.Value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13f11edf9c9548bbbe68d2762342dfac
|
||||
timeCreated: 1771751485
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using _Project.Develop.Runtime.Entities;
|
||||
using _Project.Develop.Runtime.Utilities.Conditions;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Energy.Systems
|
||||
{
|
||||
public class RegenEnergySystem : IInitializableSystem, IDisposableSystem
|
||||
{
|
||||
private ReactiveEvent<int> _regenEnergyRequest;
|
||||
private ReactiveEvent<int> _regenEnergyEvent;
|
||||
|
||||
private ReactiveVariable<int> _currentEnergy;
|
||||
private ReactiveVariable<int> _maxEnergy;
|
||||
|
||||
private ICompositeCondition _canRegen;
|
||||
|
||||
private IDisposable _regenRequestDispose;
|
||||
|
||||
public void OnInit(Entity entity)
|
||||
{
|
||||
_currentEnergy = entity.CurrentEnergy;
|
||||
_maxEnergy = entity.MaxEnergy;
|
||||
|
||||
_regenEnergyRequest = entity.RegenEnergyRequest;
|
||||
_regenEnergyEvent = entity.RegenEnergyEvent;
|
||||
|
||||
_canRegen = entity.CanRegenEnergy;
|
||||
|
||||
_regenRequestDispose = _regenEnergyRequest.Subscribe(OnRegenRequest);
|
||||
}
|
||||
|
||||
private void OnRegenRequest(int value)
|
||||
{
|
||||
if (value <= 0)
|
||||
throw new ArgumentException($"Energy regen value must be positive. Received: {value}", nameof(value));
|
||||
|
||||
if (_canRegen.Evaluate() == false)
|
||||
return;
|
||||
|
||||
int energyDifference = _maxEnergy.Value - _currentEnergy.Value;
|
||||
|
||||
if (energyDifference <= 0f)
|
||||
return;
|
||||
|
||||
int valueAdded = math.min(value, energyDifference);
|
||||
|
||||
_currentEnergy.Value += valueAdded;
|
||||
|
||||
_regenEnergyEvent.Invoke(value);
|
||||
}
|
||||
|
||||
public void OnDispose()
|
||||
{
|
||||
_regenRequestDispose.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ca447304efc4cf89e9aa4255df05e93
|
||||
timeCreated: 1771754143
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using _Project.Develop.Runtime.Entities;
|
||||
using _Project.Develop.Runtime.Utilities.Conditions;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
|
||||
|
||||
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Energy.Systems
|
||||
{
|
||||
public class UseEnergySystem : IInitializableSystem, IDisposableSystem
|
||||
{
|
||||
private ReactiveEvent<int> _useEnergyRequest;
|
||||
private ReactiveEvent<int> _useEnergyEvent;
|
||||
|
||||
private ReactiveVariable<int> _currentEnergy;
|
||||
|
||||
private ICompositeCondition _canUse;
|
||||
|
||||
private IDisposable _useRequestDispose;
|
||||
|
||||
public void OnInit(Entity entity)
|
||||
{
|
||||
_currentEnergy = entity.CurrentEnergy;
|
||||
|
||||
_useEnergyRequest = entity.RegenEnergyRequest;
|
||||
_useEnergyEvent = entity.RegenEnergyEvent;
|
||||
|
||||
_canUse = entity.CanRegenEnergy;
|
||||
|
||||
_useRequestDispose = _useEnergyRequest.Subscribe(OnRegenRequest);
|
||||
}
|
||||
|
||||
private void OnRegenRequest(int value)
|
||||
{
|
||||
if (value <= 0)
|
||||
throw new ArgumentException($"Energy use value must be positive. Received: {value}", nameof(value));
|
||||
|
||||
if (_canUse.Evaluate() == false || _currentEnergy.Value < value)
|
||||
return;
|
||||
|
||||
_currentEnergy.Value -= value;
|
||||
|
||||
_useEnergyEvent.Invoke(value);
|
||||
}
|
||||
|
||||
public void OnDispose()
|
||||
{
|
||||
_useRequestDispose.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fd8b95b8162461c995fd1c494efc614
|
||||
timeCreated: 1771754446
|
||||
Reference in New Issue
Block a user