Added inventory display and item display.

This commit is contained in:
=
2026-05-05 20:44:06 +02:00
parent abeb8c0902
commit ffe1077abc
14 changed files with 184 additions and 11 deletions
+7 -7
View File
@@ -3,25 +3,25 @@ using Godot;
public class Inventory
{
public List<Item> inventory = new();
public List<Item> 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;
+35
View File
@@ -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<ItemDisplay>();
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);
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://com0u7nqag6pp
+6 -1
View File
@@ -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);
}
}
+19
View File
@@ -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)
{
}
}
+1
View File
@@ -0,0 +1 @@
uid://qdjn5oqn6p5d
+10
View File
@@ -21,6 +21,16 @@ public partial class ResourceLoader
return GD.Load<PackedScene>($"res://Prefabs/Robot/RobotDisplay.tscn");
}
public static PackedScene LoadItemDisplay()
{
return GD.Load<PackedScene>($"res://Prefabs/Crafting/ItemDisplay.tscn");
}
public static Texture2D LoadPath(string path)
{
return GD.Load<Texture2D>(path);
}
public static Dictionary<string, MeshInstance3D> LoadTiles()
{
Dictionary<string, MeshInstance3D> tileMeshes = new Dictionary<string, MeshInstance3D>();
+7 -1
View File
@@ -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;
}