From 48dcee72b42df3765364e95fd2f70bfc0ce3f089 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Sat, 5 Oct 2024 14:13:04 -0700 Subject: [PATCH] Implement saving and loading inventory items --- Characters/Player.tscn | 5 ----- Items/Inventory.cs | 20 +------------------- State/Global/GlobalState.cs | 4 ++++ State/Global/Stats.cs | 4 ++++ Utils/World.cs | 26 ++++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Characters/Player.tscn b/Characters/Player.tscn index fde79a3..3eeaf22 100644 --- a/Characters/Player.tscn +++ b/Characters/Player.tscn @@ -838,11 +838,6 @@ y_sort_enabled = true script = ExtResource("30_y2wmw") Hotbar = [null, null, null] Items = Array[Object]([ExtResource("33_3qyfl"), ExtResource("34_70ron"), ExtResource("35_4pap1")]) -InventoryMap = { -"equip_1": 0, -"equip_2": 1, -"equip_3": 2 -} [node name="Hurtbox" parent="." node_paths=PackedStringArray("InvincibilityTimer") instance=ExtResource("9_avyu4")] visible = false diff --git a/Items/Inventory.cs b/Items/Inventory.cs index 8c5f876..6bffa50 100644 --- a/Items/Inventory.cs +++ b/Items/Inventory.cs @@ -12,10 +12,7 @@ public partial class Inventory : Node2D, IItemCollection public Array Hotbar { get; private set; } [Export] - public Array Items { get; private set; } - - [Export] - public Dictionary InventoryMap { get; set; } + public Array Items { get; set; } [Signal] public delegate void UsedItemEventHandler(Item item); @@ -130,21 +127,6 @@ public partial class Inventory : Node2D, IItemCollection return EquipIndex(index); } - [System.Obsolete] - public Item GetItemByMap(string keymap) - { - if (InventoryMap.ContainsKey(keymap)) - { - int idx = InventoryMap[keymap]; - if (idx < Hotbar.Count) - { - return Hotbar[InventoryMap[keymap]]; - } - } - else GD.Print(keymap + " does not exist"); - return null; - } - public Item AddToHotbar(ItemMetadata metadata) { //AddItemMetadata(metadata); diff --git a/State/Global/GlobalState.cs b/State/Global/GlobalState.cs index 8c56b17..dd3de73 100644 --- a/State/Global/GlobalState.cs +++ b/State/Global/GlobalState.cs @@ -76,6 +76,8 @@ public partial class GlobalState : Node Progression = save.Progression; MapState = save.MapState; Stats = save.Stats; + + World.Instance.CurrentPlayer.Inventory.Items = Stats.Items; } public void ExportToSave(Save save) @@ -83,5 +85,7 @@ public partial class GlobalState : Node save.Progression = Progression; save.MapState = MapState; save.Stats = Stats; + + Stats.Items = World.Instance.CurrentPlayer.Inventory.Items; } } diff --git a/State/Global/Stats.cs b/State/Global/Stats.cs index a78c222..94b2ed4 100644 --- a/State/Global/Stats.cs +++ b/State/Global/Stats.cs @@ -1,4 +1,5 @@ using Godot; +using Godot.Collections; namespace SupaLidlGame.State.Global; @@ -13,4 +14,7 @@ public partial class Stats : Resource [Export] public int DeathCount { get; set; } = 0; + + [Export] + public Array Items { get; set; } = new(); } diff --git a/Utils/World.cs b/Utils/World.cs index 90ce2a6..423b5e2 100644 --- a/Utils/World.cs +++ b/Utils/World.cs @@ -84,6 +84,13 @@ public partial class World : Node { throw new System.Exception("Another World instance is running."); } + + Debug = new DebugCommands(this); + } + + ~World() + { + Debug.Free(); } public override void _Ready() @@ -335,4 +342,23 @@ public partial class World : Node } public Node FindEntity(string name) => CurrentMap.Entities.GetNode(name); + + internal DebugCommands Debug { get; set; } +} + +internal partial class DebugCommands : GodotObject +{ + private World _world; + + internal DebugCommands(World world) + { + _world = world; + } + + internal void GiveItem(string item) + { + var itemMetadata = ResourceLoader + .Load($"res://Items/{item}.tres"); + _world.CurrentPlayer.Inventory.Items.Add(itemMetadata); + } }