Files
GameJamDungeon/Zennysoft.Game.Ma.Implementation/Game/GameRepo.cs

114 lines
2.1 KiB
C#

using Chickensoft.Collections;
using Godot;
namespace Zennysoft.Game.Ma.Implementation;
public interface IGameRepo : IDisposable
{
event Action? Ended;
event Action? OpenInventory;
event Action? CloseInventory;
event Action<string>? AnnounceMessage;
event Action<int>? DoubleExpTimeStart;
event Action? DoubleExpTimeEnd;
void Pause();
void Resume();
IAutoProp<bool> IsPaused { get; }
public void StartDoubleEXP(TimeSpan lengthOfEffect);
public void EndDoubleExp();
public void AnnounceMessageOnMainScreen(string message);
public double ExpRate { get; }
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
public event Action? OpenInventory;
public event Action? CloseInventory;
public event Action<string>? AnnounceMessage;
public event Action<int>? DoubleExpTimeStart;
public event Action? DoubleExpTimeEnd;
public IAutoProp<bool> IsPaused => _isPaused;
private readonly AutoProp<bool> _isPaused;
public double ExpRate { get; private set; }
private bool _disposedValue;
public GameRepo()
{
_isPaused = new AutoProp<bool>(true);
ExpRate = 1;
}
public void Pause()
{
_isPaused.OnNext(true);
GD.Print("Paused");
}
public void Resume()
{
_isPaused.OnNext(false);
GD.Print("Resume");
}
public void StartDoubleEXP(TimeSpan lengthOfEffect)
{
CloseInventory?.Invoke();
AnnounceMessageOnMainScreen("Experience points temporarily doubled.");
DoubleExpTimeStart?.Invoke(lengthOfEffect.Seconds);
ExpRate = 2;
}
public void EndDoubleExp()
{
AnnounceMessageOnMainScreen("Experience points effect wore off.");
ExpRate = 1;
}
public void AnnounceMessageOnMainScreen(string message)
{
AnnounceMessage?.Invoke(message);
}
public void OnGameEnded()
{
Pause();
Ended?.Invoke();
}
protected void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_isPaused.OnCompleted();
_isPaused.Dispose();
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}