Files
GameJamDungeon/Zennysoft.Game.Ma.Implementation/Game/GameRepo.cs
2026-03-02 22:28:23 -08:00

134 lines
2.9 KiB
C#

using Chickensoft.Collections;
using Godot;
using Zennysoft.Game.Abstractions;
using Zennysoft.Game.Implementation;
using Zennysoft.Ma.Adapter.Entity;
namespace Zennysoft.Ma.Adapter;
public interface IGameRepo : IDisposable
{
event Action? Ended;
event Action? CloseInventoryEvent;
event Action<string>? AnnounceMessageOnMainScreenEvent;
event Action<string>? AnnounceMessageInInventoryEvent;
event Action<IBaseInventoryItem>? RemoveItemFromInventoryEvent;
event Action<IEquipableItem>? EquippedItem;
event Action<IEquipableItem>? UnequippedItem;
event Action<IEnemy>? EnemyDied;
void Pause();
void Resume();
IAutoProp<bool> IsPaused { get; }
public void AnnounceMessageOnMainScreen(string message);
public void AnnounceMessageInInventory(string message);
public void RemoveItemFromInventory(IBaseInventoryItem item);
public void CloseInventory();
public void GameEnded();
public void OnEquippedItem(IEquipableItem item);
public void OnUnequippedItem(IEquipableItem item);
public void OnEnemyDied(IEnemy enemy);
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
public event Action? CloseInventoryEvent;
public event Action<string>? AnnounceMessageOnMainScreenEvent;
public event Action<string>? AnnounceMessageInInventoryEvent;
public event Action<IBaseInventoryItem>? RemoveItemFromInventoryEvent;
public event Action<IEquipableItem>? EquippedItem;
public event Action<IEquipableItem>? UnequippedItem;
public event Action<IEnemy>? EnemyDied;
public IAutoProp<bool> IsPaused => _isPaused;
private readonly AutoProp<bool> _isPaused;
private bool _disposedValue;
public GameRepo()
{
_isPaused = new AutoProp<bool>(true);
}
public void Pause()
{
_isPaused.OnNext(true);
GD.Print("Paused");
}
public void Resume()
{
_isPaused.OnNext(false);
GD.Print("Resume");
}
public void AnnounceMessageOnMainScreen(string message)
{
AnnounceMessageOnMainScreenEvent?.Invoke(message);
}
public void AnnounceMessageInInventory(string message)
{
AnnounceMessageInInventoryEvent?.Invoke(message);
}
public void RemoveItemFromInventory(IBaseInventoryItem item)
{
RemoveItemFromInventoryEvent?.Invoke(item);
}
public void CloseInventory()
{
CloseInventoryEvent?.Invoke();
}
public void OnEquippedItem(IEquipableItem item) => EquippedItem?.Invoke(item);
public void OnUnequippedItem(IEquipableItem item) => UnequippedItem?.Invoke(item);
public void OnEnemyDied(IEnemy enemy) => EnemyDied?.Invoke(enemy);
public void GameEnded()
{
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);
}
}