mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-08 17:35:53 +00:00
UI Inventory management
This commit is contained in:
parent
f4f193af7a
commit
c9abac3dc1
20 changed files with 262 additions and 72 deletions
|
|
@ -20,7 +20,7 @@ public partial class Chest : Interactable
|
|||
if (!MeetsRequirements()) return false;
|
||||
foreach (var item in LootTable)
|
||||
{
|
||||
_inventoryManager.AddItem(item.Item, item.Amount);
|
||||
_inventoryManager.AddItem(item);
|
||||
}
|
||||
|
||||
_sprite.Play("Opening");
|
||||
|
|
|
|||
|
|
@ -59,8 +59,8 @@ public partial class GameManager : Node2D
|
|||
|
||||
if (_inventoryManager != null && _hud != null)
|
||||
{
|
||||
_inventoryManager.ItemAdded += (item) => _hud.AddInventoryItem(item);
|
||||
_inventoryManager.ItemRemoved += (item) => _hud.RemoveInventoryItem(item);
|
||||
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
|
||||
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
|
||||
}
|
||||
|
||||
if (_player != null && _hud != null)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cirno.Scripts.Resources;
|
||||
|
||||
public partial class Hud : CanvasLayer
|
||||
|
|
@ -18,6 +20,8 @@ public partial class Hud : CanvasLayer
|
|||
[Export] private Label _healthLabel;
|
||||
[Export] private Container _itemsContainer;
|
||||
|
||||
private Dictionary<string, HudItem> _items = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// Assuming the HUD has a Label node named "HealthLabel"
|
||||
|
|
@ -61,16 +65,92 @@ public partial class Hud : CanvasLayer
|
|||
//_selector.Position = _selector.tolo
|
||||
}
|
||||
|
||||
public void AddInventoryItem(LootItem item)
|
||||
public void AddInventoryItem(LootItem item, int currentAmount)
|
||||
{
|
||||
TextureRect texture = new TextureRect();
|
||||
texture.Texture = item.InventorySprite;
|
||||
if (item.UiType == UiItemType.NoUI) return;
|
||||
|
||||
_itemsContainer.AddChild(texture);
|
||||
if (!_items.TryGetValue(item.ItemKey, out var item1))
|
||||
{
|
||||
var hbox = new HBoxContainer();
|
||||
_itemsContainer.AddChild(hbox);
|
||||
|
||||
var instance = item.HudItemScene.Instantiate<TextureRect>();
|
||||
//_in.CallDeferred("add_child", instance);
|
||||
hbox.AddChild(instance);
|
||||
|
||||
var hudItem = new HudItem()
|
||||
{
|
||||
Item = item,
|
||||
Container = hbox,
|
||||
};
|
||||
|
||||
if (item.UiType == UiItemType.IconText)
|
||||
{
|
||||
var label = new Label();
|
||||
label.Text = currentAmount.ToString();
|
||||
label.LabelSettings = new LabelSettings()
|
||||
{
|
||||
FontSize = 8
|
||||
};
|
||||
hbox.AddChild(label);
|
||||
|
||||
hudItem.Label = label;
|
||||
}
|
||||
|
||||
_items.Add(item.ItemKey, hudItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.UiType == UiItemType.IconText && item1.Label != null)
|
||||
{
|
||||
item1.Label.Text = currentAmount.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
// TextureRect texture = new TextureRect();
|
||||
// texture.Texture = item.InventorySprite;
|
||||
//
|
||||
// texture.ExpandMode = TextureRect.ExpandModeEnum.KeepSize;
|
||||
// texture.StretchMode = TextureRect.StretchModeEnum.Keep;
|
||||
//
|
||||
// _itemsContainer.AddChild(texture);
|
||||
}
|
||||
|
||||
public void RemoveInventoryItem(LootItem item)
|
||||
public void RemoveInventoryItem(LootItem item, int currentAmount)
|
||||
{
|
||||
|
||||
if (_items.TryGetValue(item.ItemKey, out var itm))
|
||||
{
|
||||
if (currentAmount <= 0)
|
||||
{
|
||||
itm.Container.QueueFree();
|
||||
_items.Remove(item.ItemKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
itm.Label.Text = currentAmount.ToString();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"Tried to remove item {item.ItemName} {item.ItemKey} but it was not found");
|
||||
}
|
||||
|
||||
// var containerItem = _items.FirstOrDefault(x => x.Item == item);
|
||||
// if (containerItem == null)
|
||||
// {
|
||||
// GD.Print($"Tried to remove item {item.ItemName} but it was not found");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// containerItem.Container.QueueFree();
|
||||
|
||||
}
|
||||
|
||||
public class HudItem
|
||||
{
|
||||
public LootItem Item { get; set; }
|
||||
public HBoxContainer Container { get; set; }
|
||||
public Label Label { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public partial class ItemPickup : Interactable
|
|||
if (!MeetsRequirements()) return false;
|
||||
foreach (var item in LootTable)
|
||||
{
|
||||
_inventoryManager.AddItem(item.Item, item.Amount);
|
||||
_inventoryManager.AddItem(item);
|
||||
}
|
||||
|
||||
// Delet This
|
||||
|
|
|
|||
|
|
@ -11,11 +11,15 @@ public partial class InventoryManager : Node2D
|
|||
|
||||
private List<LootItem> _items = new List<LootItem>();
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemAddedEventHandler(LootItem item);
|
||||
private Dictionary<string, ItemContainer> _itemsDict = new Dictionary<string, ItemContainer>();
|
||||
|
||||
public Dictionary<string, ItemContainer> Items => _itemsDict;
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemRemovedEventHandler(LootItem item);
|
||||
public delegate void ItemAddedEventHandler(LootItem item, int currentAmount);
|
||||
|
||||
[Signal]
|
||||
public delegate void ItemRemovedEventHandler(LootItem item, int currentAmount);
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
|
|
@ -36,48 +40,66 @@ public partial class InventoryManager : Node2D
|
|||
{
|
||||
return _items.Any(x => x.Item == type && x.Amount > 0);
|
||||
}
|
||||
|
||||
public bool AddItem(ItemTypes type, int amount = 1)
|
||||
{
|
||||
var item = new LootItem() { Item = type, Amount = amount };
|
||||
_items.Add(item);
|
||||
GD.Print($"Added {type} x{amount}");
|
||||
EmitSignal(nameof(ItemAdded), item);
|
||||
// switch (type)
|
||||
// {
|
||||
// // case ItemTypes.KeycardRed:
|
||||
// // RedKeycard = true;
|
||||
// // GD.Print($"Red Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.KeycardBlue:
|
||||
// // GD.Print($"Blue Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.KeycardGreen:
|
||||
// // GD.Print($"Green Keycard x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Ammo:
|
||||
// // GD.Print($"Ammo x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Medkit:
|
||||
// // GD.Print($"Medkit x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.CrabBomb:
|
||||
// // GD.Print($"CrabBomb x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Bomb:
|
||||
// // GD.Print($"Bomb x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Mine:
|
||||
// // GD.Print($"Mine x{amount}");
|
||||
// // break;
|
||||
// // case ItemTypes.Battery:
|
||||
// // GD.Print($"Battery x{amount}");
|
||||
// // break;
|
||||
// // default:
|
||||
// // return false;
|
||||
// }
|
||||
|
||||
return true; // TODO: Return false if could not be added
|
||||
public int RemoveItem(LootItem item, int amount)
|
||||
{
|
||||
if (_itemsDict.ContainsKey(item.ItemKey))
|
||||
{
|
||||
var itm = _itemsDict[item.ItemKey];
|
||||
|
||||
int removed = 0;
|
||||
|
||||
itm.Count -= amount;
|
||||
|
||||
if (itm.Count <= 0)
|
||||
{
|
||||
_itemsDict.Remove(item.ItemKey);
|
||||
}
|
||||
|
||||
EmitSignal(nameof(ItemRemoved), item, item.Amount);
|
||||
|
||||
return itm.Count;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public bool AddItem(LootItem item)
|
||||
{
|
||||
//var item = new LootItem() { Item = type, Amount = amount };
|
||||
|
||||
if (!_itemsDict.TryGetValue(item.ItemKey, out var itm))
|
||||
{
|
||||
_itemsDict.Add(item.ItemKey, new ItemContainer()
|
||||
{
|
||||
Item = item,
|
||||
Count = item.Amount,
|
||||
});
|
||||
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
|
||||
|
||||
EmitSignal(nameof(ItemAdded), item, item.Amount);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (itm.Count < item.Amount)
|
||||
{
|
||||
itm.Count += item.Amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.PickupIfMaxed)
|
||||
{
|
||||
itm.Count = item.Max;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
EmitSignal(nameof(ItemAdded), item, itm.Count, item.Amount);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddRedKeycard()
|
||||
|
|
@ -90,3 +112,9 @@ public partial class InventoryManager : Node2D
|
|||
RedKeycard = false;
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemContainer
|
||||
{
|
||||
public LootItem Item { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public partial class Pickupper : Activable
|
|||
{
|
||||
foreach (var item in LootTable)
|
||||
{
|
||||
_inventoryManager.AddItem(item.Item, item.Amount);
|
||||
_inventoryManager.AddItem(item);
|
||||
}
|
||||
|
||||
//inventoryManager.AddRedKeycard();
|
||||
|
|
|
|||
|
|
@ -5,12 +5,24 @@ namespace Cirno.Scripts.Resources;
|
|||
[GlobalClass]
|
||||
public partial class LootItem : Resource
|
||||
{
|
||||
[Export] public string ItemName { get; set; }
|
||||
[Export] public string ItemKey { get; set; }
|
||||
[Export] public ItemTypes Item;
|
||||
[Export] public int Amount;
|
||||
[Export] public int Max;
|
||||
[Export] public bool PickupIfMaxed;
|
||||
[Export] public bool ConsumeOnUse;
|
||||
|
||||
[Export] public UiItemType UiType;
|
||||
|
||||
[Export] public AtlasTexture InventorySprite;
|
||||
[Export] public SpriteFrames WorldSprite;
|
||||
[Export] public PackedScene HudItemScene;
|
||||
|
||||
}
|
||||
|
||||
public enum UiItemType
|
||||
{
|
||||
NoUI,
|
||||
Icon,
|
||||
IconText
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue