Refactoring game logic
This commit is contained in:
@@ -12,9 +12,9 @@ public partial class GameLogic
|
||||
|
||||
public readonly record struct ExitPauseMenu;
|
||||
|
||||
public readonly record struct OpenInventory();
|
||||
public readonly record struct OpenInventory;
|
||||
|
||||
public readonly record struct HideInventory;
|
||||
public readonly record struct CloseInventory;
|
||||
|
||||
public readonly record struct SetPauseMode(bool IsPaused);
|
||||
|
||||
@@ -41,5 +41,9 @@ public partial class GameLogic
|
||||
public readonly record struct GoToOverworld;
|
||||
|
||||
public readonly record struct SaveGame;
|
||||
|
||||
public readonly record struct AnnounceMessage(string Message);
|
||||
|
||||
public readonly record struct DoubleExpTimeStart(int lengthOfTimeInSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,14 +14,30 @@ public partial class GameLogic
|
||||
{
|
||||
var gameRepo = Get<IGameRepo>();
|
||||
gameRepo.IsPaused.Sync += OnIsPaused;
|
||||
gameRepo.OpenInventory += OnOpenInventory;
|
||||
gameRepo.CloseInventory += OnCloseInventory;
|
||||
gameRepo.AnnounceMessage += OnAnnounceMessage;
|
||||
gameRepo.DoubleExpTimeStart += OnDoubleExpTimeStart;
|
||||
});
|
||||
OnDetach(() =>
|
||||
{
|
||||
var gameRepo = Get<IGameRepo>();
|
||||
gameRepo.IsPaused.Sync -= OnIsPaused;
|
||||
gameRepo.OpenInventory -= OnOpenInventory;
|
||||
gameRepo.CloseInventory -= OnCloseInventory;
|
||||
gameRepo.AnnounceMessage -= OnAnnounceMessage;
|
||||
gameRepo.DoubleExpTimeStart -= OnDoubleExpTimeStart;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnOpenInventory() => Output(new Output.OpenInventory());
|
||||
|
||||
private void OnCloseInventory() => Output(new Output.CloseInventory());
|
||||
|
||||
private void OnAnnounceMessage(string message) => Output(new Output.AnnounceMessage(message));
|
||||
|
||||
private void OnDoubleExpTimeStart(int lengthOfTimeInSeconds) => Output(new Output.DoubleExpTimeStart(lengthOfTimeInSeconds));
|
||||
|
||||
public void OnIsPaused(bool isPaused) => Output(new Output.SetPauseMode(isPaused));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,27 +5,51 @@ 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 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 int MaxItemSize => 20;
|
||||
public double ExpRate { get; private set; }
|
||||
|
||||
private bool _disposedValue;
|
||||
|
||||
public GameRepo()
|
||||
{
|
||||
_isPaused = new AutoProp<bool>(true);
|
||||
ExpRate = 1;
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
@@ -40,6 +64,20 @@ public class GameRepo : IGameRepo
|
||||
GD.Print("Resume");
|
||||
}
|
||||
|
||||
public void StartDoubleEXP(TimeSpan lengthOfEffect)
|
||||
{
|
||||
CloseInventory?.Invoke();
|
||||
AnnounceMessage?.Invoke("Experience points temporarily doubled.");
|
||||
DoubleExpTimeStart?.Invoke(lengthOfEffect.Seconds);
|
||||
ExpRate = 2;
|
||||
}
|
||||
|
||||
public void EndDoubleExp()
|
||||
{
|
||||
AnnounceMessage?.Invoke("Experience points effect wore off.");
|
||||
ExpRate = 1;
|
||||
}
|
||||
|
||||
public void OnGameEnded()
|
||||
{
|
||||
Pause();
|
||||
|
||||
@@ -13,7 +13,7 @@ public partial class GameLogic
|
||||
public InventoryOpened()
|
||||
{
|
||||
this.OnEnter(() => { Get<IGameRepo>().Pause(); Output(new Output.OpenInventory()); });
|
||||
this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.HideInventory()); });
|
||||
this.OnExit(() => { Get<IGameRepo>().Resume(); Output(new Output.CloseInventory()); });
|
||||
}
|
||||
|
||||
public Transition On(in Input.CloseInventory input)
|
||||
|
||||
8
Zennysoft.Game.Ma.Implementation/GlobalSuppressions.cs
Normal file
8
Zennysoft.Game.Ma.Implementation/GlobalSuppressions.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
// This file is used by Code Analysis to maintain SuppressMessage
|
||||
// attributes that are applied to this project.
|
||||
// Project-level suppressions either have no target or are given
|
||||
// a specific target and scoped to a namespace, type, member, etc.
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[assembly: SuppressMessage("Style", "IDE0130:Namespace does not match folder structure", Justification = "<Pending>", Scope = "namespace", Target = "~N:Zennysoft.Game.Ma.Implementation")]
|
||||
@@ -3,6 +3,7 @@
|
||||
public enum ThrowableItemTag
|
||||
{
|
||||
None,
|
||||
DoubleExp,
|
||||
LowerTargetTo1HP,
|
||||
CanChangeAffinity,
|
||||
TeleportToRandomLocation,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
public enum UsableItemTag
|
||||
{
|
||||
None,
|
||||
DoubleEXP,
|
||||
IdentifyAllItemsCostHP,
|
||||
BriefImmunity,
|
||||
SwapHPAndVT,
|
||||
|
||||
@@ -12,15 +12,14 @@ namespace Zennysoft.Game.Ma.Implementation;
|
||||
public class SaveFileManager<T> : ISaveFileManager<T>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
public const string SAVE_FILE_NAME = "game.json";
|
||||
public JsonSerializerOptions JsonOptions { get; set; } = default!;
|
||||
|
||||
public string SaveFilePath { get; set; } = default!;
|
||||
private JsonSerializerOptions _jsonOptions;
|
||||
private string _defaultSaveLocation;
|
||||
public const string DEFAULT_SAVE_FILE_NAME = "game.json";
|
||||
|
||||
public SaveFileManager(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
SaveFilePath = _fileSystem.Path.Join(OS.GetUserDataDir(), SAVE_FILE_NAME);
|
||||
_defaultSaveLocation = _fileSystem.Path.Join(OS.GetUserDataDir(), DEFAULT_SAVE_FILE_NAME);
|
||||
|
||||
var resolver = new SerializableTypeResolver();
|
||||
GodotSerialization.Setup();
|
||||
@@ -28,7 +27,7 @@ public class SaveFileManager<T> : ISaveFileManager<T>
|
||||
|
||||
var upgradeDependencies = new Blackboard();
|
||||
|
||||
JsonOptions = new JsonSerializerOptions
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
Converters = {
|
||||
new SerializableTypeConverter(upgradeDependencies)
|
||||
@@ -39,18 +38,36 @@ public class SaveFileManager<T> : ISaveFileManager<T>
|
||||
}
|
||||
|
||||
|
||||
public async Task<T> ReadFromFile()
|
||||
public Task<T?> ReadFromFile()
|
||||
{
|
||||
if (!_fileSystem.File.Exists(SaveFilePath))
|
||||
if (!_fileSystem.File.Exists(_defaultSaveLocation))
|
||||
throw new FileNotFoundException();
|
||||
return ReadFromFile(_defaultSaveLocation);
|
||||
}
|
||||
|
||||
public async Task<T?> ReadFromFile(string filePath)
|
||||
{
|
||||
if (!_fileSystem.File.Exists(filePath))
|
||||
throw new FileNotFoundException();
|
||||
|
||||
var json = await _fileSystem.File.ReadAllTextAsync(SaveFilePath);
|
||||
return JsonSerializer.Deserialize<T>(json, JsonOptions);
|
||||
var json = await _fileSystem.File.ReadAllTextAsync(filePath);
|
||||
|
||||
return JsonSerializer.Deserialize<T?>(json, _jsonOptions);
|
||||
}
|
||||
|
||||
public async Task WriteToFile(T gameData)
|
||||
public Task WriteToFile(T gameData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(gameData, JsonOptions);
|
||||
await _fileSystem.File.WriteAllTextAsync(SaveFilePath, json);
|
||||
return WriteToFile(gameData, _defaultSaveLocation, _jsonOptions);
|
||||
}
|
||||
|
||||
public Task WriteToFile(T gameData, string filePath)
|
||||
{
|
||||
return WriteToFile(gameData, filePath, _jsonOptions);
|
||||
}
|
||||
|
||||
public async Task WriteToFile(T gameData, string filePath, JsonSerializerOptions jsonOptions)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(gameData, jsonOptions);
|
||||
await _fileSystem.File.WriteAllTextAsync(filePath, json);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user