UI Inventory management

This commit is contained in:
Marco 2025-02-10 17:29:14 +01:00
commit c9abac3dc1
20 changed files with 262 additions and 72 deletions

View file

@ -58,5 +58,5 @@ WaitForCompletion = true
[resource]
script = ExtResource("3_o6234")
Threshold = 800
Threshold = 900
Patterns = Array[Resource]([SubResource("Resource_upj22"), SubResource("Resource_gm1rv"), SubResource("Resource_5fq8v"), SubResource("Resource_gm1rv")])

View file

@ -39,5 +39,5 @@ WaitForCompletion = false
[resource]
script = ExtResource("1_0cgch")
Threshold = 1000
Threshold = 999
Patterns = Array[Resource]([SubResource("Resource_acaax"), ExtResource("2_3486e"), ExtResource("3_mwcf3"), SubResource("Resource_o8win"), ExtResource("2_3486e"), ExtResource("3_mwcf3"), SubResource("Resource_k77ig"), ExtResource("2_3486e"), ExtResource("3_mwcf3"), SubResource("Resource_5ocg5"), ExtResource("2_3486e"), ExtResource("3_mwcf3")])

View file

@ -1,14 +1,21 @@
[gd_resource type="Resource" script_class="LootItem" load_steps=3 format=3 uid="uid://ct1fa2huvy34n"]
[gd_resource type="Resource" script_class="LootItem" load_steps=5 format=3 uid="uid://ct1fa2huvy34n"]
[ext_resource type="Script" path="res://Scripts/Resources/LootItem.cs" id="1_31o2l"]
[ext_resource type="SpriteFrames" uid="uid://pmwatoyt2ase" path="res://Resources/Sprites/Ammo_Inventory_Sprite.tres" id="1_fubhk"]
[ext_resource type="Texture2D" uid="uid://c5g0qg6u2gm6f" path="res://Sprites/Items/Ammo1.png" id="1_bpftr"]
[ext_resource type="PackedScene" uid="uid://cjv4yahpb0h2l" path="res://Scenes/HUD/Items/ammo.tscn" id="1_enq76"]
[sub_resource type="AtlasTexture" id="AtlasTexture_3y0hf"]
atlas = ExtResource("1_bpftr")
[resource]
script = ExtResource("1_31o2l")
ItemName = "Ammo"
ItemKey = "ammo1"
Item = 3
Amount = 100
Max = 800
PickupIfMaxed = false
ConsumeOnUse = false
InventorySprite = ExtResource("1_fubhk")
WorldSprite = ExtResource("1_fubhk")
ConsumeOnUse = true
UiType = 0
InventorySprite = SubResource("AtlasTexture_3y0hf")
HudItemScene = ExtResource("1_enq76")

View file

@ -4,6 +4,11 @@
[resource]
script = ExtResource("1_k8cnp")
ItemName = "Blue Keycard"
ItemKey = "KeycardBlue"
Item = 1
Amount = 1
Max = 1
PickupIfMaxed = false
ConsumeOnUse = false
UiType = 1

View file

@ -4,6 +4,11 @@
[resource]
script = ExtResource("1_t0s5w")
ItemName = "Green Keycard"
ItemKey = "KeycardGreen"
Item = 2
Amount = 1
Max = 1
PickupIfMaxed = false
ConsumeOnUse = false
UiType = 1

View file

@ -1,15 +1,20 @@
[gd_resource type="Resource" script_class="LootItem" load_steps=4 format=3 uid="uid://dibquna7fww7t"]
[gd_resource type="Resource" script_class="LootItem" load_steps=5 format=3 uid="uid://dibquna7fww7t"]
[ext_resource type="SpriteFrames" uid="uid://cikw4v6n0folb" path="res://Resources/Sprites/Red_Keycard_Overworld_Sprite.tres" id="1_1j6xs"]
[ext_resource type="PackedScene" uid="uid://xvgx3vda1a5o" path="res://Scenes/HUD/Items/red_keycard.tscn" id="1_30txj"]
[ext_resource type="Texture2D" uid="uid://bao53hesxreoh" path="res://Resources/Sprites/Red_Keycard_Inventory_Texture.tres" id="1_glhfu"]
[ext_resource type="Script" path="res://Scripts/Resources/LootItem.cs" id="1_istlt"]
[resource]
script = ExtResource("1_istlt")
ItemName = "Red Keycard"
ItemKey = "KeycardRed"
Item = 0
Amount = 1
Max = 1
PickupIfMaxed = false
ConsumeOnUse = false
UiType = 1
InventorySprite = ExtResource("1_glhfu")
WorldSprite = ExtResource("1_1j6xs")
HudItemScene = ExtResource("1_30txj")

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=7 format=3 uid="uid://dkwi1hu1bixoe"]
[gd_scene load_steps=8 format=3 uid="uid://dkwi1hu1bixoe"]
[ext_resource type="Script" path="res://Scripts/Hud.cs" id="1_m0hb0"]
[ext_resource type="FontFile" uid="uid://ccm3u37q1hvux" path="res://fonts/Xolonium-Regular.ttf" id="2_0xmx2"]
[ext_resource type="Texture2D" uid="uid://dde36pgb8fbwc" path="res://Sprites/selector_big.png" id="3_hee3i"]
[ext_resource type="PackedScene" uid="uid://cicthr1v5v4s2" path="res://Scenes/HUD/Items/icicle_gun.tscn" id="4_m333p"]
[sub_resource type="AtlasTexture" id="AtlasTexture_h626b"]
atlas = ExtResource("3_hee3i")
@ -63,6 +64,10 @@ frame_progress = 0.8514
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 4.0
offset_top = 6.0
offset_right = -4.0
offset_bottom = -3.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
@ -76,3 +81,14 @@ text = "100/100"
[node name="ItemsContainer" type="VBoxContainer" parent="VBoxContainer"]
layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/ItemsContainer"]
layout_mode = 2
[node name="IcicleGun" parent="VBoxContainer/ItemsContainer/HBoxContainer" instance=ExtResource("4_m333p")]
layout_mode = 2
[node name="Label" type="Label" parent="VBoxContainer/ItemsContainer/HBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 10
text = "100/300"

View file

@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://cjv4yahpb0h2l"]
[ext_resource type="Texture2D" uid="uid://c5g0qg6u2gm6f" path="res://Sprites/Items/Ammo1.png" id="1_at3u7"]
[node name="Ammo" type="TextureRect"]
texture = ExtResource("1_at3u7")
stretch_mode = 2

View file

@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://cicthr1v5v4s2"]
[ext_resource type="Texture2D" uid="uid://duwiasewxvcb5" path="res://Sprites/Items/Icicle_Gun.png" id="1_ebd3h"]
[node name="IcicleGun" type="TextureRect"]
texture = ExtResource("1_ebd3h")
stretch_mode = 2

View file

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://xvgx3vda1a5o"]
[ext_resource type="Texture2D" uid="uid://bad6gbl2gity3" path="res://Sprites/Items/RedKeycard_Small.png" id="1_slovd"]
[sub_resource type="AtlasTexture" id="AtlasTexture_cowsj"]
atlas = ExtResource("1_slovd")
region = Rect2(0, 0, 16, 16)
[node name="RedKeycard" type="TextureRect"]
texture = SubResource("AtlasTexture_cowsj")
stretch_mode = 2

View file

@ -1,11 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://cmr6fclttrpkb"]
[gd_scene load_steps=5 format=3 uid="uid://cmr6fclttrpkb"]
[ext_resource type="PackedScene" uid="uid://dxs3ks2ucaxl4" path="res://Scenes/Items/Red_Keycard.tscn" id="1_i3l7m"]
[ext_resource type="Script" path="res://Scripts/Interactables/ItemPickup.cs" id="1_it180"]
[ext_resource type="Resource" uid="uid://ct1fa2huvy34n" path="res://Resources/Items/Ammo1.tres" id="2_j10i4"]
[ext_resource type="SpriteFrames" uid="uid://pmwatoyt2ase" path="res://Resources/Sprites/Ammo_Inventory_Sprite.tres" id="3_6n103"]
[node name="Ammo1" instance=ExtResource("1_i3l7m")]
[sub_resource type="CircleShape2D" id="CircleShape2D_6vv2s"]
[node name="Ammo1" type="Area2D" groups=["Interactable"]]
collision_layer = 4
collision_mask = 2
script = ExtResource("1_it180")
LootTable = [ExtResource("2_j10i4")]
[node name="AnimatedSprite2D" parent="." index="1"]
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_6vv2s")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
sprite_frames = ExtResource("3_6n103")

View file

@ -1,11 +1,9 @@
[gd_scene load_steps=8 format=4 uid="uid://bu5fvatj2j08j"]
[gd_scene load_steps=6 format=4 uid="uid://bu5fvatj2j08j"]
[ext_resource type="Script" path="res://Scripts/GameManager.cs" id="1_paetl"]
[ext_resource type="PackedScene" uid="uid://d1rlw6ddpmrn8" path="res://Scenes/Actors/Rumia.tscn" id="2_47q21"]
[ext_resource type="PackedScene" uid="uid://bghghp5ep4w2j" path="res://Scenes/player.tscn" id="2_f17ts"]
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="3_cyhmq"]
[ext_resource type="Resource" uid="uid://ks6fypeil6gk" path="res://Resources/BossPhases/TestBoss1.tres" id="5_jkuab"]
[ext_resource type="Resource" uid="uid://ccj0cqbveey8c" path="res://Resources/BossPhases/Rumia_SP1.tres" id="6_1uicx"]
[ext_resource type="PackedScene" uid="uid://bdp710abe10s5" path="res://Scenes/Interactable/AreaTrigger.tscn" id="9_xhf0g"]
[node name="GameScene" type="Node2D" node_paths=PackedStringArray("PlayerSpawnMarker")]
@ -31,7 +29,6 @@ tile_set = ExtResource("3_cyhmq")
[node name="Rumia" parent="." instance=ExtResource("2_47q21")]
position = Vector2(176, 44)
Phases = Array[Resource]([ExtResource("5_jkuab"), ExtResource("6_1uicx")])
[node name="PlayerStartPosition" type="Marker2D" parent="."]
position = Vector2(178, 115)

View file

@ -82,9 +82,6 @@ Target = NodePath("../../HorizontalDoor2")
[node name="Debug Room" type="Node2D" parent="Factory Tilemaps"]
[node name="Ammo1" parent="Factory Tilemaps/Debug Room" instance=ExtResource("34_17pjh")]
position = Vector2(-873, -348)
[node name="BlueKeycard" parent="Factory Tilemaps/Debug Room" instance=ExtResource("35_rblsn")]
position = Vector2(-921, -348)
@ -206,9 +203,11 @@ position = Vector2(-710, -459)
[node name="Barrel" parent="Factory Tilemaps" instance=ExtResource("18_vy02r")]
position = Vector2(-711, -395)
ExplosionScene = null
[node name="Barrel2" parent="Factory Tilemaps" instance=ExtResource("18_vy02r")]
position = Vector2(-700, -381)
ExplosionScene = null
[node name="CameraController" type="Camera2D" parent="."]
script = ExtResource("6_t8ide")
@ -331,3 +330,6 @@ Target = NodePath("DialogueStarter")
[node name="DialogueStarter" parent="Computer2" instance=ExtResource("32_68v02")]
position = Vector2(291.847, -32.7126)
_trackName = "computer2"
[node name="Ammo1" parent="." instance=ExtResource("34_17pjh")]
position = Vector2(-792, -378)

View file

@ -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");

View file

@ -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)

View file

@ -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; }
}
}

View file

@ -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

View file

@ -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; }
}

View file

@ -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();

View file

@ -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
}