From ffe1077abcd73c7ec70decc06f9f8ccce6c5f12f Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 5 May 2026 20:44:06 +0200 Subject: [PATCH] Added inventory display and item display. --- Assets/Images/InventorySymbol.png | Bin 0 -> 185 bytes Assets/Images/InventorySymbol.png.import | 40 ++++++++++++++++++ .../Images/Sources/InventorySymbol.aseprite | Bin 0 -> 397 bytes Prefabs/Crafting/ItemDisplay.tscn | 40 ++++++++++++++++++ Scenes/Game.tscn | 15 ++++++- Scripts/Crafting/Inventory.cs | 14 +++--- Scripts/Crafting/InventoryDisplay.cs | 35 +++++++++++++++ Scripts/Crafting/InventoryDisplay.cs.uid | 1 + Scripts/Crafting/Item.cs | 7 ++- Scripts/Crafting/ItemDisplay.cs | 19 +++++++++ Scripts/Crafting/ItemDisplay.cs.uid | 1 + Scripts/Helpers/ResourceLoader.cs | 10 +++++ Scripts/Helpers/UIHandler.cs | 8 +++- project.godot | 5 +++ 14 files changed, 184 insertions(+), 11 deletions(-) create mode 100644 Assets/Images/InventorySymbol.png create mode 100644 Assets/Images/InventorySymbol.png.import create mode 100644 Assets/Images/Sources/InventorySymbol.aseprite create mode 100644 Prefabs/Crafting/ItemDisplay.tscn create mode 100644 Scripts/Crafting/InventoryDisplay.cs create mode 100644 Scripts/Crafting/InventoryDisplay.cs.uid create mode 100644 Scripts/Crafting/ItemDisplay.cs create mode 100644 Scripts/Crafting/ItemDisplay.cs.uid diff --git a/Assets/Images/InventorySymbol.png b/Assets/Images/InventorySymbol.png new file mode 100644 index 0000000000000000000000000000000000000000..a27dd4962f7773daf7b117a242accda4c8ac233b GIT binary patch literal 185 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL|>AJY5_^B3hFZ zBv_ewBrg8=|1wvfdzX)QthI0My3AEB4~;KgF;hCQg4s>BVIp@2;|onmwi72EQ!E)~ zr|yt(-Nbytv?FG3vp%nYaQ~Dgj3s+hmNR-S>Plh{*t}2TgNebbhCucntAs0zi}@rz f^qtg@C}3dt;wi3wPUrnMp!E!%u6{1-oD!M<734l6 literal 0 HcmV?d00001 diff --git a/Assets/Images/InventorySymbol.png.import b/Assets/Images/InventorySymbol.png.import new file mode 100644 index 0000000..68252a7 --- /dev/null +++ b/Assets/Images/InventorySymbol.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://ciehcg34et0q3" +path="res://.godot/imported/InventorySymbol.png-992c7fa6db2b090a5926e973600828c4.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Images/InventorySymbol.png" +dest_files=["res://.godot/imported/InventorySymbol.png-992c7fa6db2b090a5926e973600828c4.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Assets/Images/Sources/InventorySymbol.aseprite b/Assets/Images/Sources/InventorySymbol.aseprite new file mode 100644 index 0000000000000000000000000000000000000000..4e3142dd276e6ff07fbdf82e70ef812eeb1e3a43 GIT binary patch literal 397 zcmeBWWMFu(l#xLJ41pYm6d*)}PiDX?C zkj=sX6jcB+loU){HEhx>4g15a?-#1wT#)we@T}jT%w|3f$l7OMJ|)jPQ$NUC#mL6e z-oC^jp(#4=Ve;(%@n@f$zWwL_|NpbsZf~h;&&RMxDbTd5pdiAdU>0Vv= z2J#@A6qtb|4EzT&*cp5hD^rUU4C85!)|o+h1N}v%qZ!@3Iny^(D3-C5ylH+;aA inventory = new(); + public List items = new(); public int maxInventorySize = 8; public bool AddItem(Item item, int amount) { - Item inventoryItem = inventory.Find(x => x.data.Id == item.data.Id && x.currentAmount + amount <= x.data.StackSize); + Item inventoryItem = items.Find(x => x.data.Id == item.data.Id && x.currentAmount + amount <= x.data.StackSize); if (inventoryItem != null) { - GD.Print(inventory.IndexOf(inventoryItem) + ": " + inventoryItem.currentAmount); + GD.Print(items.IndexOf(inventoryItem) + ": " + inventoryItem.currentAmount); inventoryItem.currentAmount += amount; return true; } else { - if (inventory.Count < maxInventorySize) + if (items.Count < maxInventorySize) { - inventory.Add(item); - inventory[inventory.Count - 1].currentAmount += amount; + items.Add(item); + items[items.Count - 1].currentAmount += amount; return true; } } @@ -34,7 +34,7 @@ public class Inventory Item item; foreach(Ingredient ingredient in neededIngredients) { - item = inventory.Find(x => x.data.Id == ingredient.item && x.currentAmount >= ingredient.amount * amount); + item = items.Find(x => x.data.Id == ingredient.item && x.currentAmount >= ingredient.amount * amount); if (item == null) { canCraft = false; diff --git a/Scripts/Crafting/InventoryDisplay.cs b/Scripts/Crafting/InventoryDisplay.cs new file mode 100644 index 0000000..b86a41b --- /dev/null +++ b/Scripts/Crafting/InventoryDisplay.cs @@ -0,0 +1,35 @@ +using Godot; + +public partial class InventoryDisplay : PanelContainer +{ + PackedScene itemDisplayPrefab = ResourceLoader.LoadItemDisplay(); + [Export] VBoxContainer itemList; + + public override void _Notification(int id) + { + if (id == NotificationVisibilityChanged) + { + if (Visible) ReloadItems(); + } + } + + public void ReloadItems() + { + foreach (Node node in itemList.GetChildren()) + { + itemList.RemoveChild(node); + node.QueueFree(); + } + ItemDisplay display; + + foreach (Item item in GameData.inventory.items) + { + display = itemDisplayPrefab.Instantiate(); + display.item = item; + display.text.Text = item.GetReadableName(); + display.amount.Text = item.currentAmount.ToString(); + display.texture.Texture = ResourceLoader.LoadPath(item.data.Texture); + itemList.AddChild(display); + } + } +} diff --git a/Scripts/Crafting/InventoryDisplay.cs.uid b/Scripts/Crafting/InventoryDisplay.cs.uid new file mode 100644 index 0000000..3bf3401 --- /dev/null +++ b/Scripts/Crafting/InventoryDisplay.cs.uid @@ -0,0 +1 @@ +uid://com0u7nqag6pp diff --git a/Scripts/Crafting/Item.cs b/Scripts/Crafting/Item.cs index 0854676..ef81147 100644 --- a/Scripts/Crafting/Item.cs +++ b/Scripts/Crafting/Item.cs @@ -8,7 +8,6 @@ public class Item public int currentAmount = 0; public double elapsedCraftTime = 0; public int amountCrafted = 0; - public Texture2D display; public CraftingResult Craft(int amount, double delta) { @@ -31,5 +30,11 @@ public class Item } return CraftingResult.FAILED; } + + public string GetReadableName() + { + string noUnderscore = data.Id.Replace("_", " ").ToLower(); + return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1); + } } diff --git a/Scripts/Crafting/ItemDisplay.cs b/Scripts/Crafting/ItemDisplay.cs new file mode 100644 index 0000000..c2003bb --- /dev/null +++ b/Scripts/Crafting/ItemDisplay.cs @@ -0,0 +1,19 @@ +using Godot; +using System; + +public partial class ItemDisplay : PanelContainer +{ + [Export] public TextureRect texture; + [Export] public RichTextLabel text; + [Export] public RichTextLabel amount; + public Item item; + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + } +} diff --git a/Scripts/Crafting/ItemDisplay.cs.uid b/Scripts/Crafting/ItemDisplay.cs.uid new file mode 100644 index 0000000..be1e4e3 --- /dev/null +++ b/Scripts/Crafting/ItemDisplay.cs.uid @@ -0,0 +1 @@ +uid://qdjn5oqn6p5d diff --git a/Scripts/Helpers/ResourceLoader.cs b/Scripts/Helpers/ResourceLoader.cs index bcf6c86..4a881e1 100644 --- a/Scripts/Helpers/ResourceLoader.cs +++ b/Scripts/Helpers/ResourceLoader.cs @@ -21,6 +21,16 @@ public partial class ResourceLoader return GD.Load($"res://Prefabs/Robot/RobotDisplay.tscn"); } + public static PackedScene LoadItemDisplay() + { + return GD.Load($"res://Prefabs/Crafting/ItemDisplay.tscn"); + } + + public static Texture2D LoadPath(string path) + { + return GD.Load(path); + } + public static Dictionary LoadTiles() { Dictionary tileMeshes = new Dictionary(); diff --git a/Scripts/Helpers/UIHandler.cs b/Scripts/Helpers/UIHandler.cs index 6eec69a..b56ddde 100644 --- a/Scripts/Helpers/UIHandler.cs +++ b/Scripts/Helpers/UIHandler.cs @@ -15,6 +15,7 @@ public partial class UIHandler : Control [Export] PanelContainer options; [Export] Control uiContent; [Export] PanelContainer menu; + [Export] PanelContainer inventory; bool receivedRobotJumpSignal = false; public override void _Ready() @@ -37,6 +38,7 @@ public partial class UIHandler : Control if (Input.IsActionJustPressed("map")) HandleMapButton(); if (Input.IsActionJustPressed("menu")) HandleMenuButton(); if (Input.IsActionJustPressed("robot_list")) HandleRobotListButton(); + if (Input.IsActionJustPressed("inventory")) HandleInventoryButton(); DisplayStats(); } @@ -56,6 +58,11 @@ public partial class UIHandler : Control OpenUIElement(robotList); } + public void HandleInventoryButton() + { + OpenUIElement(inventory); + } + public void DisplayStats() { FPS.Text = Engine.GetFramesPerSecond().ToString() + " FPS"; @@ -86,7 +93,6 @@ public partial class UIHandler : Control { foreach (PanelContainer child in uiContent.GetChildren()) { - GD.Print(child == element); if (child == element) continue; child.Visible = false; } diff --git a/project.godot b/project.godot index e99b3de..a9f277d 100644 --- a/project.godot +++ b/project.godot @@ -87,6 +87,11 @@ menu={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +inventory={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":73,"key_label":0,"unicode":105,"location":0,"echo":false,"script":null) +] +} [layer_names]