Last active
March 16, 2021 22:08
-
-
Save ScottLilly/010288af7696ec2103f6e977e2c58f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ConsoleUpdateExtensions | |
{ | |
internal static readonly Dictionary<IConsole, Dictionary<string, Func>> LineUpdates | |
= new Dictionary<IConsole, Dictionary<string, Func>>(); | |
public static Dictionary<string, Func> GetUpdates(this IConsole console) => LineUpdates.ContainsKey(console) ? LineUpdates[console] : null; | |
public static void AddLineUpdate(this IConsole console, string key, Func value) | |
{ | |
if (!LineUpdates.ContainsKey(console)) | |
{ | |
LineUpdates.Add(console, new Dictionary<string, Func>()); | |
LineUpdates[console].Add(key, value); | |
} | |
else | |
{ | |
LineUpdates[console].Add(key, value); | |
} | |
} | |
// add updates from a enumerable source | |
public static void AddLineUpdates(this IConsole console, IEnumerable source, Func getTitle, Func getValue) | |
{ | |
// remove any previous updates | |
if (LineUpdates.ContainsKey(console)) | |
{ | |
LineUpdates.Remove(console); | |
} | |
// add update handler for this console. | |
LineUpdates.Add(console, new Dictionary<string, Func>()); | |
// iterate source and add updates. | |
foreach (var data in source) | |
{ | |
console.AddLineUpdate(getTitle(data), () => getValue(data)); | |
} | |
} | |
// Execute stored updates | |
public static void ExecuteLineUpdates(this IConsole console) | |
{ | |
if (LineUpdates.ContainsKey(console)) | |
{ | |
// to ensure that if this is called after initial write, I clear the window and rewrite the data. | |
console.Clear(); | |
foreach (var key in LineUpdates[console].Keys) | |
{ | |
console.WriteLine($"{key} : {LineUpdates[console][key]()}"); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MessageHandlers | |
{ | |
public static IConsole LogScreen { get; set; } | |
internal static void OnMessageReceived(object sender, GameMessageEventArgs gameMessage) | |
{ | |
LogScreen.WriteLine(gameMessage.Message); | |
} | |
// PropertyChangedEvent Handler | |
internal static void OnPropertyChanged_TryMatchingUpdate(object sender, PropertyChangedEventArgs propertyChangedEventArgs) | |
{ | |
if (sender is Player player) | |
{ | |
foreach (IConsole console in ConsoleUpdateExtensions.LineUpdates.Keys) | |
{ | |
if (ConsoleUpdateExtensions.LineUpdates[console].ContainsKey(propertyChangedEventArgs.PropertyName)) | |
{ | |
console.Clear(); | |
foreach (var x in ConsoleUpdateExtensions.LineUpdates[console]) | |
{ | |
console.WriteLine($"{x.Key} : {x.Value()}"); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// divide playerWindow into display areas needed for game | |
var playerConsoles = PlayerWindow.SplitRows | |
( | |
new Split(8, "Basics"), | |
new Split(12, "Status"), | |
new Split(24, "Inventory"), | |
new Split(0, "Log") | |
); | |
BasicsWindow = playerConsoles[0]; | |
StatusWindow = playerConsoles[1]; | |
EquipWindow = playerConsoles[2]; | |
LogWindow = playerConsoles[3]; | |
// add messages handler output.. | |
MessageHandlers.LogScreen = LogWindow; | |
var currentPlayer = GameSession.CurrentPlayer; | |
// setup updates. | |
BasicsWindow.AddLineUpdate("Name", () => $"{currentPlayer.Name}"); | |
BasicsWindow.AddLineUpdate("CurrentHitPoints", () => $"{currentPlayer.CurrentHitPoints}"); | |
BasicsWindow.AddLineUpdate("Level", () => $"{currentPlayer.Level}"); | |
BasicsWindow.AddLineUpdate("ExperiencePoints", () => $"{currentPlayer.ExperiencePoints}"); | |
(IConsole statWindow, IConsole miscWindow) = StatusWindow.SplitLeftRight(); | |
// add enumerable to update | |
statWindow.AddLineUpdates(currentPlayer.Attributes, (x) => x.DisplayName, (y) => y.ModifiedValue.ToString()); | |
miscWindow.AddLineUpdate("MaximumHitPoints", () => $"{currentPlayer.MaximumHitPoints}"); | |
miscWindow.AddLineUpdate("Gold", () => $"{currentPlayer.Gold}"); | |
miscWindow.AddLineUpdate("CurrentWeapon", () => $"{currentPlayer?.CurrentWeapon?.Name}"); | |
miscWindow.AddLineUpdate("CurrentConsumable", () => $"{currentPlayer?.CurrentConsumable?.Name}"); | |
// Execute initial Update. | |
// subsequent updates are handled by propertychanged from player and hopefully soon monster. | |
BasicsWindow.ExecuteLineUpdates(); | |
statWindow.ExecuteLineUpdates(); | |
miscWindow.ExecuteLineUpdates(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment