mirror of
https://github.com/Stone-Red-Code/TextLore.git
synced 2026-09-04 00:56:26 +02:00
Improve inventory and room object system
This commit is contained in:
@@ -5,9 +5,17 @@ namespace TextLore.Components.Pages.Games.Roguelike.Builders;
|
||||
|
||||
public class EnemyBuilder : IBuilder<Enemy>
|
||||
{
|
||||
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<Enemy>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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> player, Func<RoomDefinition?> room)
|
||||
public sealed class ScriptContext(Func<Player> player, Func<Room?> room)
|
||||
{
|
||||
public Player Player => player();
|
||||
public RoomDefinition? Room => room();
|
||||
public Room? Room => room();
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,9 @@
|
||||
|
||||
public interface IRoomObject
|
||||
{
|
||||
public string Key { get; }
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string Description { get; }
|
||||
}
|
||||
@@ -12,11 +12,23 @@ public class Room(Position position, RoomDefinition roomDefinition)
|
||||
return objects.GetValueOrDefault(point);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<Position, IRoomObject> GetRooms()
|
||||
public IRoomObject? GetObject(string key)
|
||||
{
|
||||
return objects.Values.FirstOrDefault(x => x.Key == key);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<Position, IRoomObject> GetObjects()
|
||||
{
|
||||
return objects;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<Position, T> GetObjects<T>() 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))
|
||||
|
||||
@@ -6,5 +6,5 @@ public interface IInventoryItem
|
||||
|
||||
public string Description { get; }
|
||||
|
||||
public string Tag { get; }
|
||||
public string Key { get; }
|
||||
}
|
||||
Reference in New Issue
Block a user