From af5c7d76858ea3805ce1426594d8d0a1c184fef4 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 7 Feb 2024 22:33:24 +0100 Subject: [PATCH] Improve inventory and room object system --- .../Games/Roguelike/Builders/EnemyBuilder.cs | 16 ++++++- .../Pages/Games/Roguelike/Models/Enemy.cs | 6 ++- .../Pages/Games/Roguelike/Models/Weapon.cs | 5 ++- src/TextLore/Shared/Logic/GameManager.cs | 44 ++++++++++++------- .../Shared/Models/Level/IRoomObject.cs | 4 ++ src/TextLore/Shared/Models/Level/Room.cs | 14 +++++- .../Shared/Models/Player/IInventoryItem.cs | 2 +- 7 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/TextLore/Components/Pages/Games/Roguelike/Builders/EnemyBuilder.cs b/src/TextLore/Components/Pages/Games/Roguelike/Builders/EnemyBuilder.cs index 21ef914..66024fc 100644 --- a/src/TextLore/Components/Pages/Games/Roguelike/Builders/EnemyBuilder.cs +++ b/src/TextLore/Components/Pages/Games/Roguelike/Builders/EnemyBuilder.cs @@ -5,9 +5,17 @@ namespace TextLore.Components.Pages.Games.Roguelike.Builders; public class EnemyBuilder : IBuilder { + private string key = string.Empty; private string name = string.Empty; private string description = string.Empty; private int maxHealth = 0; + private Weapon? weapon = null; + + public EnemyBuilder WithKey(string key) + { + this.key = key; + return this; + } public EnemyBuilder WithName(string name) { @@ -27,8 +35,14 @@ public class EnemyBuilder : IBuilder return this; } + public EnemyBuilder WithWeapon(Weapon weapon) + { + this.weapon = weapon; + return this; + } + public Enemy Build() { - return new Enemy(name, description, maxHealth); + return new Enemy(key, name, description, maxHealth, weapon); } } \ No newline at end of file diff --git a/src/TextLore/Components/Pages/Games/Roguelike/Models/Enemy.cs b/src/TextLore/Components/Pages/Games/Roguelike/Models/Enemy.cs index ad195d2..8474130 100644 --- a/src/TextLore/Components/Pages/Games/Roguelike/Models/Enemy.cs +++ b/src/TextLore/Components/Pages/Games/Roguelike/Models/Enemy.cs @@ -2,8 +2,10 @@ namespace TextLore.Components.Pages.Games.Roguelike.Models; -public class Enemy(string name, string description, int maxHealth) : IRoomObject +public class Enemy(string key, string name, string description, int maxHealth, Weapon? weapon) : IRoomObject { + public string Key => key; + public string Name => name; public string Description => description; @@ -11,4 +13,6 @@ public class Enemy(string name, string description, int maxHealth) : IRoomObject public int MaxHealth { get; } = maxHealth; public int Health { get; set; } = maxHealth; + + public Weapon? Weapon { get; set; } = weapon; } \ No newline at end of file diff --git a/src/TextLore/Components/Pages/Games/Roguelike/Models/Weapon.cs b/src/TextLore/Components/Pages/Games/Roguelike/Models/Weapon.cs index 0c56657..91700fd 100644 --- a/src/TextLore/Components/Pages/Games/Roguelike/Models/Weapon.cs +++ b/src/TextLore/Components/Pages/Games/Roguelike/Models/Weapon.cs @@ -2,11 +2,12 @@ namespace TextLore.Components.Pages.Games.Roguelike.Models; -public class Weapon(string name, string description, string tag) : IInventoryItem +public class Weapon(string key, string name, string description) : IInventoryItem { + public string Key { get; } = key; + public string Name { get; } = name; public string Description { get; } = description; - public string Tag { get; } = tag; public void Shoot() { diff --git a/src/TextLore/Shared/Logic/GameManager.cs b/src/TextLore/Shared/Logic/GameManager.cs index 93dda5f..c331edf 100644 --- a/src/TextLore/Shared/Logic/GameManager.cs +++ b/src/TextLore/Shared/Logic/GameManager.cs @@ -18,7 +18,7 @@ public partial class GameManager { PlayState = new(level, new()); - ScriptContext currentScriptContext = new(() => PlayState.Player, () => PlayState.CurrentRoom?.Definition); + ScriptContext currentScriptContext = new(() => PlayState.Player, () => PlayState.CurrentRoom); scriptEngine.AddHostObject("context", currentScriptContext); } @@ -54,45 +54,55 @@ public partial class GameManager public virtual void StartGame() { - SetPlayerPositionInLevel(PlayState.Level.StartRoom.Position); + _ = SetPlayerPositionInLevel(PlayState.Level.StartRoom.Position); Size currentRoomSize = PlayState.CurrentRoom!.Definition.Size; - SetPlayerPositionInRoom(new(currentRoomSize.Width / 2, currentRoomSize.Height / 2)); + _ = SetPlayerPositionInRoom(new(currentRoomSize.Width / 2, currentRoomSize.Height / 2)); } - public void MovePlayerInLevel(Direction direction) + public bool MovePlayerInLevel(Direction direction) { Position newPosition = PlayState.Player.PositionInLevel + direction; - if (PlayState.Level.IsPositionInBounds(newPosition)) - { - SetPlayerPositionInLevel(newPosition); - } + + return SetPlayerPositionInLevel(newPosition); } - public void MovePlayerInRoom(Direction direction) + public bool MovePlayerInRoom(Direction direction) { Position newPosition = PlayState.Player.PositionInLevel + direction; - if (PlayState.CurrentRoom?.IsPositionInBounds(newPosition) == true) - { - SetPlayerPositionInRoom(newPosition); - } + + return SetPlayerPositionInRoom(newPosition); } - public virtual void SetPlayerPositionInLevel(Position position) + public virtual bool SetPlayerPositionInLevel(Position position) { + if (PlayState.Level.IsPositionInBounds(position)) + { + return false; + } + PlayState.Player.PositionInLevel = position; SetScript(PlayState.CurrentRoom?.Definition.Script ?? string.Empty); _ = ExecuteMethod("onPlayerEnterRoom"); + + return true; } - public virtual void SetPlayerPositionInRoom(Position position) + public virtual bool SetPlayerPositionInRoom(Position position) { + if (!PlayState.CurrentRoom?.IsPositionInBounds(position) == true) + { + return false; + } + PlayState.Player.PositionInLevel = position; _ = ExecuteMethod("onPlayerMove"); + + return true; } public void Update() @@ -130,9 +140,9 @@ public partial class GameManager scriptEngine.Execute(script); } - public sealed class ScriptContext(Func player, Func room) + public sealed class ScriptContext(Func player, Func room) { public Player Player => player(); - public RoomDefinition? Room => room(); + public Room? Room => room(); } } \ No newline at end of file diff --git a/src/TextLore/Shared/Models/Level/IRoomObject.cs b/src/TextLore/Shared/Models/Level/IRoomObject.cs index 095e2d1..1f15ea1 100644 --- a/src/TextLore/Shared/Models/Level/IRoomObject.cs +++ b/src/TextLore/Shared/Models/Level/IRoomObject.cs @@ -2,5 +2,9 @@ public interface IRoomObject { + public string Key { get; } + public string Name { get; } + + public string Description { get; } } \ No newline at end of file diff --git a/src/TextLore/Shared/Models/Level/Room.cs b/src/TextLore/Shared/Models/Level/Room.cs index bbb5bbf..161eb01 100644 --- a/src/TextLore/Shared/Models/Level/Room.cs +++ b/src/TextLore/Shared/Models/Level/Room.cs @@ -12,11 +12,23 @@ public class Room(Position position, RoomDefinition roomDefinition) return objects.GetValueOrDefault(point); } - public IReadOnlyDictionary GetRooms() + public IRoomObject? GetObject(string key) + { + return objects.Values.FirstOrDefault(x => x.Key == key); + } + + public IReadOnlyDictionary GetObjects() { return objects; } + public IReadOnlyDictionary GetObjects() where T : IRoomObject + { + return objects + .Where(x => x.Value is T) + .ToDictionary(x => x.Key, x => (T)x.Value); + } + public bool AddObject(Position point, IRoomObject roomObject) { if (!IsPositionInBounds(point)) diff --git a/src/TextLore/Shared/Models/Player/IInventoryItem.cs b/src/TextLore/Shared/Models/Player/IInventoryItem.cs index 891ead2..f160e97 100644 --- a/src/TextLore/Shared/Models/Player/IInventoryItem.cs +++ b/src/TextLore/Shared/Models/Player/IInventoryItem.cs @@ -6,5 +6,5 @@ public interface IInventoryItem public string Description { get; } - public string Tag { get; } + public string Key { get; } } \ No newline at end of file