Rework game over logic and game initialization

This commit is contained in:
2025-10-27 15:04:01 -07:00
parent 720696aed0
commit 7e6dca1c29
46 changed files with 653 additions and 610 deletions

View File

@@ -91,8 +91,8 @@ public class EffectService
if (currentRoom is not MonsterRoom)
return;
currentRoom.EnemiesInRoom.ForEach(e => e.HealthComponent.SetHealth(e.HealthComponent.MaximumHP.Value));
_player.HealthComponent.SetHealth(_player.HealthComponent.MaximumHP.Value);
currentRoom.EnemiesInRoom.ForEach(e => e.HealthComponent.SetCurrentHealth(e.HealthComponent.MaximumHP.Value));
_player.HealthComponent.SetCurrentHealth(_player.HealthComponent.MaximumHP.Value);
}
public void AbsorbHPFromAllEnemiesInRoom()
@@ -134,7 +134,7 @@ public class EffectService
var oldHp = _player.HealthComponent.CurrentHP.Value;
var oldVt = _player.VTComponent.CurrentVT.Value;
_player.HealthComponent.SetHealth(oldVt);
_player.HealthComponent.SetCurrentHealth(oldVt);
_player.VTComponent.SetVT(oldHp);
}

View File

@@ -2,6 +2,7 @@
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
using Zennysoft.Game.Abstractions;
@@ -17,6 +18,8 @@ public partial class Inventory : Node, IInventory
// TODO: Constants class with export
private const int _maxInventorySize = 20;
public event Action<string> BroadcastMessage;
public Inventory()
{
Items = [];
@@ -25,6 +28,20 @@ public partial class Inventory : Node, IInventory
[Save("inventory_items")]
public List<InventoryItem> Items { get; private set; }
public bool PickUpItem(InventoryItem item)
{
var isAdded = TryAdd(item);
if (isAdded)
{
BroadcastMessage?.Invoke($"{item.ItemName} picked up.");
item.QueueFree();
}
else
BroadcastMessage?.Invoke($"Could not pick up {item.ItemName}.");
return isAdded;
}
public bool TryAdd(InventoryItem inventoryItem)
{
if (Items.Count >= _maxInventorySize)

View File

@@ -1,4 +1,5 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
@@ -6,11 +7,16 @@ using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
public partial class ItemDatabase : Node
public class ItemDatabase
{
private static readonly Lazy<ItemDatabase> lazy =
new Lazy<ItemDatabase>(() => new ItemDatabase());
public static ItemDatabase Instance { get { return lazy.Value; } }
public ImmutableList<InventoryItem> Items { get; set; }
public InventoryItem PickItem<T>(T itemToExclude = null)
public T PickItem<T>(T itemToExclude = null)
where T : InventoryItem
{
var rng = new RandomNumberGenerator();
@@ -27,10 +33,10 @@ public partial class ItemDatabase : Node
if (selectedItem is ThrowableItem throwableItem)
throwableItem.SetCount(rng.RandiRange(throwableItem.Stats.MinimumCount, throwableItem.Stats.MaximumCount));
return selectedItem;
return (T)selectedItem;
}
public ItemDatabase()
private ItemDatabase()
{
var database = new List<InventoryItem>();
var armorResources = DirAccess.GetFilesAt("res://src/items/armor/resources/");

View File

@@ -0,0 +1,36 @@
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
public class ItemReroller
{
private readonly ItemDatabase _database;
public ItemReroller(ItemDatabase database)
{
_database = database;
}
public T RerollItem<T>(T itemToReroll, IInventory inventory, bool insertIntoInventory = true)
where T : InventoryItem
{
var currentIndex = inventory.Items.IndexOf(itemToReroll);
if (insertIntoInventory)
inventory.Remove(itemToReroll);
var rolledItem = RerollItemInternal((dynamic)itemToReroll);
if (insertIntoInventory)
inventory.TryInsert(rolledItem, currentIndex);
return rolledItem;
}
private Weapon RerollItemInternal(Weapon itemToReroll) => _database.PickItem(itemToReroll);
private Armor RerollItemInternal(Armor itemToReroll) => _database.PickItem(itemToReroll);
private Accessory RerollItemInternal(Accessory itemToReroll) => _database.PickItem(itemToReroll);
private ConsumableItem RerollItemInternal(ConsumableItem itemToReroll) => _database.PickItem(itemToReroll);
private ThrowableItem RerollItemInternal(ThrowableItem itemToReroll) => _database.PickItem(itemToReroll);
private EffectItem RerollItemInternal(EffectItem itemToReroll) => _database.PickItem(itemToReroll);
}

View File

@@ -1,17 +1,10 @@
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
using Zennysoft.Game.Implementation;
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
public interface IDroppedItem : IRigidBody3D
{
void RescueItem();
}
[Meta(typeof(IAutoNode))]
public partial class DroppedItem : RigidBody3D, IDroppedItem
{

View File

@@ -7,7 +7,7 @@ using Zennysoft.Ma.Adapter.Entity;
namespace Zennysoft.Game.Ma;
[Meta(typeof(IAutoNode))]
public partial class ThrownItem : RigidBody3D
public partial class ThrownItem : RigidBody3D, IThrownItem
{
public override void _Notification(int what) => this.Notify(what);
@@ -15,9 +15,10 @@ public partial class ThrownItem : RigidBody3D
[Dependency] public IGame Game => this.DependOn<IGame>();
public InventoryItem ItemThatIsThrown;
public InventoryItem ItemThatIsThrown { get; set; }
private EffectService _effectService;
private ItemReroller _itemReroller;
[Node] public Sprite2D Sprite { get; set; } = default!;
@@ -31,7 +32,7 @@ public partial class ThrownItem : RigidBody3D
Sprite.Texture = ItemThatIsThrown.GetTexture();
AddCollisionExceptionWith((Node)Player);
Collision.SetCollisionLayerValue(3, false);
_itemReroller = new ItemReroller(ItemDatabase.Instance);
}
private void Collision_AreaEntered(Area3D area)
@@ -102,14 +103,14 @@ public partial class ThrownItem : RigidBody3D
private void CalculateEffect(IEnemy enemy)
{
if (ItemThatIsThrown.ItemTag == ItemTag.MysteryItem)
ItemThatIsThrown = Game.RerollItem(ItemThatIsThrown, false);
ItemThatIsThrown = _itemReroller.RerollItem(ItemThatIsThrown, Player.Inventory, false);
if (ItemThatIsThrown is ThrowableItem throwableItem)
{
switch (throwableItem.ThrowableItemTag)
{
case ThrowableItemTag.LowerTargetTo1HP:
enemy.HealthComponent.SetHealth(1);
enemy.HealthComponent.SetCurrentHealth(1);
break;
case ThrowableItemTag.TeleportToRandomLocation:
_effectService.TeleportToRandomRoom(enemy);