-
Notifications
You must be signed in to change notification settings - Fork 26
132 component event framework #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CalendarComponent>().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..."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In part that's part of card #223 , in part it would use SaveFormat.Save in the C7GameData project. But I could also see having a component for saving/loading (not just autosaving...), and that's really slightly beyond this proof-of-concept. (Also slightly beyond, but it occurred to me that this method, or at least the file name creation part of it, would lend itself nicely to some self-documenting units tests) |
||
AutosaveCreated?.Invoke(this, new GameSaveEventArgs(filename)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TurnEventArgs> 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably ought to switch these to Serilogs now that we have that in the engine. |
||
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}"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the Unity 3D Wiki moved, or maybe is down; when I try to go to that site I get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what the original tutorial was but this looks somewhat related https://learn.unity.com/project/65de084fedbc2a0699d68bfb There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the archive of the wiki page: https://web.archive.org/web/20210304075110/https://wiki.unity3d.com/index.php/Toolbox There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me clarify, the Unity and Godot references are irrelevant here; this is pure C# in the engine layer. Even the Unity implementation was derived from the original source on an IBM blog, now mirrored here: https://gurkantech.blogspot.com/2008/10/use-your-singletons-wisely_02.html |
||
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<Type, GameComponent> _components = new Dictionary<Type, GameComponent>(); | ||
|
||
public ComponentManager AddComponent<T>(T component) where T : GameComponent | ||
{ | ||
_components.Add(typeof(T), component); | ||
return this; | ||
} | ||
|
||
public T GetComponent<T>() 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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason the |
||
OnBeginTurn(); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably can take this method out now, I think... or maybe we should leave it for now since there are other things that will need updated as we add more features?