Added inventory display and item display.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://com0u7nqag6pp
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://qdjn5oqn6p5d
|
||||
Reference in New Issue
Block a user