diff --git a/C7/ComponentManager.cs b/C7/ComponentManager.cs deleted file mode 100644 index ffb5f2de..00000000 --- a/C7/ComponentManager.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -// modeled after the Unity Toolbox pattern from https://wiki.unity3d.com/index.php/Toolbox -public sealed class ComponentManager -{ - // singleton implemenation taken from example at https://csharpindepth.com/Articles/Singleton - private static readonly ComponentManager _instance = new ComponentManager(); - - static ComponentManager() - { - - } - - private ComponentManager() - { - - } - - public static ComponentManager Instance - { - get { return _instance; } - } - - // type dictionary taken from Jon Skeet's implementation at https://codeblog.jonskeet.uk/2008/10/08/mapping-from-a-type-to-an-instance-of-that-type/ - private Dictionary _components = new Dictionary(); - - public void AddComponent(T component) where T : GameComponent - { - _components.Add(typeof(T), component); - } - - public T GetComponent() where T : GameComponent - { - GameComponent component; - if (_components.TryGetValue(typeof(T), out component)) - { - return (T)component; - } - return default(T); - } -} - -public class GameComponent : Godot.Object -{ - -} \ No newline at end of file diff --git a/C7/Game.cs b/C7/Game.cs index cdf1e19a..ec293bc3 100644 --- a/C7/Game.cs +++ b/C7/Game.cs @@ -92,6 +92,7 @@ public override void _Ready() Player = GetNode("KinematicBody2D"); //TODO: What was this supposed to do? It throws errors and occasinally causes crashes now, because _OnViewportSizeChanged doesn't exist // GetTree().Root.Connect("size_changed", this, "_OnViewportSizeChanged"); + GetNode("CanvasLayer/GameStatus").RegisterEvents(); // Hide slideout bar on startup _on_SlideToggle_toggled(false); diff --git a/C7/UIElements/GameStatus/GameStatus.cs b/C7/UIElements/GameStatus/GameStatus.cs index 4132061f..7bdc6013 100644 --- a/C7/UIElements/GameStatus/GameStatus.cs +++ b/C7/UIElements/GameStatus/GameStatus.cs @@ -1,5 +1,5 @@ using Godot; -using System; +using C7Engine.Components; using C7GameData; public class GameStatus : MarginContainer @@ -17,6 +17,11 @@ public override void _Ready() MarginTop = -(137 + 1); AddChild(LowerRightInfoBox); } + + public void RegisterEvents() + { + ComponentManager.Instance.GetComponent().TurnStarted += (obj, args) => LowerRightInfoBox.SetTurn(args.Turn.TurnDate); + } public void OnNewUnitSelected(ParameterWrapper wrappedMapUnit) { @@ -33,7 +38,7 @@ private void OnTurnEnded() private void OnTurnStarted(int turnNumber) { //Oh hai, we do need this handler here! - LowerRightInfoBox.SetTurn(turnNumber); + //LowerRightInfoBox.SetTurn(turnNumber); } private void OnNoMoreAutoselectableUnits() diff --git a/C7/UIElements/GameStatus/LowerRightInfoBox.cs b/C7/UIElements/GameStatus/LowerRightInfoBox.cs index 1274cfca..e24d0a3b 100644 --- a/C7/UIElements/GameStatus/LowerRightInfoBox.cs +++ b/C7/UIElements/GameStatus/LowerRightInfoBox.cs @@ -155,9 +155,9 @@ public void UpdateUnitInfo(MapUnit NewUnit, TerrainType terrain) ///This is going to evolve a lot over time. Probably this info box will need to keep some local state. ///But for now it'll show the changing turn number, providing some interactivity - public void SetTurn(int turnNumber) + public void SetTurn(string turn) { - yearAndGold.Text = "Turn " + turnNumber + " 10 Gold (+0 per turn)"; + yearAndGold.Text = turn + " 10 Gold (+0 per turn)"; } // // Called every frame. 'delta' is the elapsed time since the previous frame. diff --git a/C7Engine/Components/AutosaveComponent.cs b/C7Engine/Components/AutosaveComponent.cs new file mode 100644 index 00000000..3d15ac56 --- /dev/null +++ b/C7Engine/Components/AutosaveComponent.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; +using System.Linq; +using C7GameData; + +namespace C7Engine.Components +{ + public class GameSaveEventArgs : EventArgs + { + public string SaveFilePath { get; } + + public GameSaveEventArgs(string path) + { + SaveFilePath = path; + } + } + + public class AutosaveComponent : GameComponent + { + public event EventHandler AutosaveCreated; + private GameData _gameData; + + public AutosaveComponent(GameData gameData) + { + _gameData = gameData; + } + + public void Initialize() + { + ComponentManager.Instance.GetComponent().TurnStarted += OnTurnStarted; + } + + public void OnTurnStarted(object source, TurnEventArgs args) + { + string date = args.Turn.TurnDate; + date = String.Concat(date.Where(c => !char.IsWhiteSpace(c))); + date = String.Join("_", date.Split(Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries)); + string filename = $"auto_{date}.json"; + Console.WriteLine($"I would save {filename} right now if I knew how..."); + AutosaveCreated?.Invoke(this, new GameSaveEventArgs(filename)); + } + } +} \ No newline at end of file diff --git a/C7Engine/Components/CalendarComponent.cs b/C7Engine/Components/CalendarComponent.cs new file mode 100644 index 00000000..6d5e2212 --- /dev/null +++ b/C7Engine/Components/CalendarComponent.cs @@ -0,0 +1,66 @@ +using System; +using C7GameData; + +namespace C7Engine.Components +{ + public class TurnEventArgs : EventArgs + { + public GameTurn Turn { get; } + + public TurnEventArgs(GameTurn turn) + { + Turn = turn; + } + } + + public class GameTurn + { + public int TurnNumber { get; } + public string TurnDate { get; } + + public GameTurn(int num, string date) + { + TurnNumber = num; + TurnDate = date; + } + } + + public class CalendarComponent : GameComponent + { + public event EventHandler TurnStarted; + public GameTurn CurrentTurn { get; private set; } + private GameData _gameData; + //TODO add interace for turn settings + + public CalendarComponent(GameData gameData) + { + _gameData = gameData; + CurrentTurn = GetGameTurn(_gameData.turn); + } + + public void Initialize() + { + TurnHandling.TurnEnded += (obj, args) => AdvanceTurn(); + Console.WriteLine("Initialized CalendarComponent at turn " + CurrentTurn.TurnNumber); + } + + private void AdvanceTurn() + { + int nextTurnNumber = CurrentTurn.TurnNumber + 1; + _gameData.turn = nextTurnNumber; + CurrentTurn = GetGameTurn(nextTurnNumber); + + Console.WriteLine("Date is now " + CurrentTurn.TurnDate); + TurnStarted?.Invoke(this, new TurnEventArgs(CurrentTurn)); + } + + public GameTurn GetGameTurn(int turnNumber) + { + // TODO use some interface to calculate the date + // based on turn settings in the rules + int year = -4000 + (50 * (turnNumber-1)); + string era = year < 0 ? "BC" : "AD"; + return new GameTurn(turnNumber, $"{Math.Abs(year)} {era}"); + } + } +} \ No newline at end of file diff --git a/C7Engine/Components/ComponentManager.cs b/C7Engine/Components/ComponentManager.cs new file mode 100644 index 00000000..a1e0586a --- /dev/null +++ b/C7Engine/Components/ComponentManager.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +// modeled after the Unity Toolbox pattern from https://wiki.unity3d.com/index.php/Toolbox +namespace C7Engine.Components +{ + public sealed class ComponentManager + { + // singleton implemenation taken from example at https://csharpindepth.com/Articles/Singleton + private static readonly ComponentManager _instance = new ComponentManager(); + + static ComponentManager() + { + + } + + private ComponentManager() + { + + } + + public static ComponentManager Instance + { + get { return _instance; } + } + + // type dictionary taken from Jon Skeet's implementation at https://codeblog.jonskeet.uk/2008/10/08/mapping-from-a-type-to-an-instance-of-that-type/ + private Dictionary _components = new Dictionary(); + + public ComponentManager AddComponent(T component) where T : GameComponent + { + _components.Add(typeof(T), component); + return this; + } + + public T GetComponent() where T : GameComponent + { + GameComponent component; + if (_components.TryGetValue(typeof(T), out component)) + { + return (T)component; + } + return default(T); + } + + public void InitializeComponents() + { + _components.ToList().ForEach(c => c.Value.Initialize()); + } + } + + public interface GameComponent + { + public void Initialize(); + } +} \ No newline at end of file diff --git a/C7Engine/EntryPoints/CreateGame.cs b/C7Engine/EntryPoints/CreateGame.cs index 26bdf89c..45c2a88a 100644 --- a/C7Engine/EntryPoints/CreateGame.cs +++ b/C7Engine/EntryPoints/CreateGame.cs @@ -2,6 +2,7 @@ namespace C7Engine { using System; using C7GameData; + using C7Engine.Components; public class CreateGame { @@ -23,11 +24,20 @@ public static Player createGame(string loadFilePath, string defaultBicPath) var humanPlayer = save.GameData.CreateDummyGameData(EngineStorage.rules); EngineStorage.uiControllerID = humanPlayer.guid; + InitializeGameComponents(); TurnHandling.OnBeginTurn(); // Call for the first turn TurnHandling.AdvanceTurn(); EngineStorage.gameDataMutex.ReleaseMutex(); return humanPlayer; } + + private static void InitializeGameComponents() + { + ComponentManager.Instance + .AddComponent(new CalendarComponent(EngineStorage.gameData)) + .AddComponent(new AutosaveComponent(EngineStorage.gameData)) + .InitializeComponents(); + } } } diff --git a/C7Engine/EntryPoints/TurnHandling.cs b/C7Engine/EntryPoints/TurnHandling.cs index a8b8d1f1..b2c2b94a 100644 --- a/C7Engine/EntryPoints/TurnHandling.cs +++ b/C7Engine/EntryPoints/TurnHandling.cs @@ -6,6 +6,7 @@ namespace C7Engine using System; public class TurnHandling { + public static event EventHandler TurnEnded; internal static void OnBeginTurn() { GameData gameData = EngineStorage.gameData; @@ -130,8 +131,7 @@ internal static void AdvanceTurn() } // END Production phase - - gameData.turn++; + TurnEnded?.Invoke(null, null); OnBeginTurn(); } }