Files
GameJamDungeon/src/game/IGameRepo.cs
2024-08-23 00:39:15 -07:00

99 lines
2.3 KiB
C#

using Chickensoft.Collections;
using Godot;
using System;
using System.Collections.Generic;
namespace GameJamDungeon
{
public interface IGameRepo : IDisposable
{
event Action? Ended;
IAutoProp<List<InventoryItem>> InventoryItems { get; }
IAutoProp<bool> IsInventoryScreenOpened { get; }
IAutoProp<bool> IsPaused { get; }
void Pause();
void Resume();
IAutoProp<Vector3> PlayerGlobalPosition { get; }
void SetPlayerGlobalPosition(Vector3 playerGlobalPosition);
}
public class GameRepo : IGameRepo
{
public event Action? Ended;
private readonly AutoProp<List<InventoryItem>> _inventoryItems;
private readonly AutoProp<bool> _isInventoryScreenOpened;
public IAutoProp<List<InventoryItem>> InventoryItems => _inventoryItems;
public IAutoProp<bool> IsInventoryScreenOpened => _isInventoryScreenOpened;
public IAutoProp<Vector3> PlayerGlobalPosition => _playerGlobalPosition;
private readonly AutoProp<Vector3> _playerGlobalPosition;
public IAutoProp<bool> IsPaused => _isPaused;
private readonly AutoProp<bool> _isPaused;
private bool _disposedValue;
public GameRepo()
{
_inventoryItems = new AutoProp<List<InventoryItem>>([]);
_isInventoryScreenOpened = new AutoProp<bool>(false);
_isPaused = new AutoProp<bool>(false);
_playerGlobalPosition = new AutoProp<Vector3>(Vector3.Zero);
}
public void Pause()
{
_isPaused.OnNext(true);
GD.Print("Paused");
}
public void Resume()
{
_isPaused.OnNext(false);
GD.Print("Resume");
}
public void SetPlayerGlobalPosition(Vector3 playerGlobalPosition) => _playerGlobalPosition.OnNext(playerGlobalPosition);
public void OnGameEnded()
{
Pause();
Ended?.Invoke();
}
protected void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_playerGlobalPosition.OnCompleted();
_playerGlobalPosition.Dispose();
_inventoryItems.OnCompleted();
_inventoryItems.Dispose();
_isInventoryScreenOpened.OnCompleted();
_isInventoryScreenOpened.Dispose();
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}