mirror of
https://github.com/omegaleo/Tutorials.git
synced 2026-11-09 01:53:29 +00:00
Added Inventory System
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class Inventory : Resource
|
||||
{
|
||||
[Export] public int Size = 36;
|
||||
[Export] public InventorySlotData[] Slots;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://7dsydoyjsley
|
||||
@@ -0,0 +1,18 @@
|
||||
using Godot;
|
||||
|
||||
public partial class InventoryController : Control
|
||||
{
|
||||
[Export] public Inventory Inventory;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var inventorySlots = GetTree().GetNodesInGroup("inventory_slots_cs");
|
||||
|
||||
for (int i = 0; i < inventorySlots.Count; i++)
|
||||
{
|
||||
var slot = (InventorySlot)inventorySlots[i];
|
||||
|
||||
slot.InitializeSlot(i, Inventory);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://rcwsi83u3a2d
|
||||
@@ -0,0 +1,74 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public partial class InventorySlot : Panel
|
||||
{
|
||||
[Export] public TextureRect IconRect;
|
||||
[Export] public Label CountLabel;
|
||||
|
||||
public InventorySlotData SlotData;
|
||||
public int SlotIndex;
|
||||
public Inventory Inventory;
|
||||
|
||||
// Refresh the current slot with the data of it's inventory slot
|
||||
public void Refresh()
|
||||
{
|
||||
if (SlotIndex < Inventory.Slots.Count())
|
||||
{
|
||||
SlotData = Inventory.Slots[SlotIndex];
|
||||
}
|
||||
|
||||
if (SlotData == null)
|
||||
{
|
||||
IconRect.Texture = null;
|
||||
CountLabel.Text = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
IconRect.Texture = SlotData.Item.Icon;
|
||||
CountLabel.Text = (SlotData.Count > 1) ? SlotData.Count.ToString() : "";
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Item being dragged elsewhere
|
||||
public override Variant _GetDragData(Vector2 atPosition)
|
||||
{
|
||||
if (SlotData == null)
|
||||
return default;
|
||||
|
||||
// Create a preview of the item being dragged
|
||||
var preview = new TextureRect { Texture = SlotData.Item.Icon, CustomMinimumSize = new Vector2(32f, 32f) };
|
||||
SetDragPreview(preview);
|
||||
|
||||
var data = new Dictionary { { "from_slot", SlotIndex } };
|
||||
return data;
|
||||
}
|
||||
|
||||
// Can we drop the item where we are?
|
||||
public override bool _CanDropData(Vector2 atPosition, Variant data)
|
||||
{
|
||||
return data.VariantType == Variant.Type.Dictionary
|
||||
&& ((Dictionary)data).ContainsKey("from_slot");
|
||||
}
|
||||
|
||||
public override void _DropData(Vector2 atPosition, Variant data)
|
||||
{
|
||||
int fromIndex = (int)((Dictionary)data)["from_slot"];
|
||||
|
||||
// Swap the 2 indexes
|
||||
(Inventory.Slots[SlotIndex], Inventory.Slots[fromIndex]) = (Inventory.Slots[fromIndex], Inventory.Slots[SlotIndex]);
|
||||
|
||||
// Call the refresh method for all objects that belong in the inventory_slots group
|
||||
GetTree().CallGroup("inventory_slots_cs", InventorySlot.MethodName.Refresh);
|
||||
}
|
||||
|
||||
public void InitializeSlot(int slotIndex, Inventory inventory)
|
||||
{
|
||||
SlotIndex = slotIndex;
|
||||
Inventory = inventory;
|
||||
|
||||
Refresh();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://6px2tco0hdyp
|
||||
@@ -0,0 +1,8 @@
|
||||
using Godot;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class InventorySlotData : Resource
|
||||
{
|
||||
[Export] public ItemData Item;
|
||||
[Export] public int Count;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c61uo84g0gp1v
|
||||
@@ -0,0 +1,11 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ItemData : Resource
|
||||
{
|
||||
[Export] public string Id { get; set; }
|
||||
[Export] public string DisplayName { get; set; }
|
||||
[Export] public Texture2D Icon { get; set; }
|
||||
[Export] public int MaxStack { get; set; } = 64;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://b8yemi4n7cqls
|
||||
Reference in New Issue
Block a user