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
|
||||
@@ -0,0 +1,5 @@
|
||||
extends Resource
|
||||
class_name InventoryGD
|
||||
|
||||
@export var size: int = 36
|
||||
@export var slots : Array[InventorySlotDataGD] = [] # Data regarding what's in each slot
|
||||
@@ -0,0 +1 @@
|
||||
uid://dila5f4tuiun1
|
||||
@@ -0,0 +1,5 @@
|
||||
extends Resource
|
||||
class_name InventorySlotDataGD
|
||||
|
||||
@export var item: ItemDataGD
|
||||
@export var count: int
|
||||
@@ -0,0 +1 @@
|
||||
uid://mv6k4s5wf52h
|
||||
@@ -0,0 +1,64 @@
|
||||
extends Panel
|
||||
class_name InventorySlotGD
|
||||
|
||||
@onready var icon_rect: TextureRect = $Icon
|
||||
@onready var count_label: Label = $CountLabel
|
||||
|
||||
@export var slot_data : InventorySlotDataGD # Slot data as defined in the InventoryGD class
|
||||
@export var slot_index: int
|
||||
@export var main_inventory: InventoryGD
|
||||
|
||||
# Refresh the current slot with the data of it's inventory slot
|
||||
func refresh():
|
||||
if (slot_index < main_inventory.slots.size()):
|
||||
slot_data = main_inventory.slots[slot_index]
|
||||
|
||||
if slot_data == null:
|
||||
icon_rect.texture = null
|
||||
count_label.text = ""
|
||||
else:
|
||||
icon_rect.texture = slot_data.item.icon
|
||||
count_label.text = str(slot_data.count) if slot_data.count > 1 else ""
|
||||
|
||||
# Handle Item being dragged elsewhere
|
||||
func _get_drag_data(at_position: Vector2) -> Variant:
|
||||
if slot_data == null:
|
||||
return null
|
||||
|
||||
# Create a preview of the item being dragged
|
||||
var preview = TextureRect.new()
|
||||
preview.texture = slot_data.item.icon
|
||||
preview.custom_minimum_size = Vector2(32,32)
|
||||
|
||||
set_drag_preview(preview)
|
||||
|
||||
var data = { "from_slot": slot_index, "payload": slot_data }
|
||||
|
||||
return data
|
||||
|
||||
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
|
||||
return typeof(data) == TYPE_DICTIONARY and data.has("from_slot")
|
||||
|
||||
# Can we drop the item where we are?
|
||||
func _drop_data(_pos, data):
|
||||
var from_index = data.from_slot
|
||||
|
||||
# Swap the 2 indexes
|
||||
var temp = main_inventory.slots[slot_index]
|
||||
main_inventory.slots[slot_index] = main_inventory.slots[from_index]
|
||||
main_inventory.slots[from_index] = temp
|
||||
|
||||
# Call the refresh method for all objects that belong in the inventory_slots group
|
||||
get_tree().call_group("inventory_slots", "refresh")
|
||||
|
||||
func initialize_slot(index: int, inventory: InventoryGD):
|
||||
slot_index = index
|
||||
main_inventory = inventory
|
||||
|
||||
if index < main_inventory.slots.size():
|
||||
var inventory_slot = main_inventory.slots[slot_index]
|
||||
|
||||
if inventory_slot != null :
|
||||
slot_data = inventory_slot
|
||||
|
||||
refresh()
|
||||
@@ -0,0 +1 @@
|
||||
uid://b0uy0my40tjsv
|
||||
@@ -0,0 +1,7 @@
|
||||
extends Resource
|
||||
class_name ItemDataGD
|
||||
|
||||
@export var id: String
|
||||
@export var display_name: String
|
||||
@export var icon: Texture2D
|
||||
@export var max_stack: int = 64
|
||||
@@ -0,0 +1 @@
|
||||
uid://b8cygu5gimsgi
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_resource type="Resource" script_class="ItemData" format=3 uid="uid://d0w7bxo6cotsc"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://bgam5s5hw83cf" path="res://Inventory/Textures/Apple.png" id="1_5kwn6"]
|
||||
[ext_resource type="Script" uid="uid://b8yemi4n7cqls" path="res://Inventory/CSharp/ItemData.cs" id="1_bou2a"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_bou2a")
|
||||
Id = "1"
|
||||
DisplayName = "Apple"
|
||||
Icon = ExtResource("1_5kwn6")
|
||||
metadata/_custom_type_script = "uid://b8yemi4n7cqls"
|
||||
@@ -0,0 +1,45 @@
|
||||
[gd_resource type="Resource" script_class="InventoryGD" format=3 uid="uid://d378a606v8xdt"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dila5f4tuiun1" path="res://Inventory/GDScript/InventoryGD.gd" id="1_8jwj0"]
|
||||
[ext_resource type="Script" uid="uid://mv6k4s5wf52h" path="res://Inventory/GDScript/InventorySlotDataGD.gd" id="2_yctcd"]
|
||||
[ext_resource type="Resource" uid="uid://cqyo3lbguv5hg" path="res://Inventory/Resources/GDItemApple.tres" id="3_yctcd"]
|
||||
[ext_resource type="Script" uid="uid://b8cygu5gimsgi" path="res://Inventory/GDScript/ItemDataGD.gd" id="4_ev12v"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_7tu70"]
|
||||
script = ExtResource("2_yctcd")
|
||||
item = ExtResource("3_yctcd")
|
||||
count = 24
|
||||
metadata/_custom_type_script = "uid://mv6k4s5wf52h"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_mrqgh"]
|
||||
script = ExtResource("2_yctcd")
|
||||
item = ExtResource("3_yctcd")
|
||||
count = 14
|
||||
metadata/_custom_type_script = "uid://mv6k4s5wf52h"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_jwwx0"]
|
||||
script = ExtResource("2_yctcd")
|
||||
item = ExtResource("3_yctcd")
|
||||
count = 14
|
||||
metadata/_custom_type_script = "uid://mv6k4s5wf52h"
|
||||
|
||||
[sub_resource type="CompressedTexture2D" id="CompressedTexture2D_7tu70"]
|
||||
load_path = "res://.godot/imported/Apple.png-d164bb800f362dfbf6df55c5ab1627cc.ctex"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_3eq5k"]
|
||||
script = ExtResource("4_ev12v")
|
||||
id = "1"
|
||||
display_name = "Apple"
|
||||
icon = SubResource("CompressedTexture2D_7tu70")
|
||||
metadata/_custom_type_script = "uid://b8cygu5gimsgi"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_1cv4e"]
|
||||
script = ExtResource("2_yctcd")
|
||||
item = SubResource("Resource_3eq5k")
|
||||
count = 14
|
||||
metadata/_custom_type_script = "uid://mv6k4s5wf52h"
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_8jwj0")
|
||||
slots = Array[ExtResource("2_yctcd")]([SubResource("Resource_7tu70"), null, null, SubResource("Resource_mrqgh"), null, SubResource("Resource_jwwx0"), null, null, null, null, null, null, null, null, null, SubResource("Resource_1cv4e"), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null])
|
||||
metadata/_custom_type_script = "uid://dila5f4tuiun1"
|
||||
@@ -0,0 +1,11 @@
|
||||
[gd_resource type="Resource" script_class="ItemDataGD" format=3 uid="uid://cqyo3lbguv5hg"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://bgam5s5hw83cf" path="res://Inventory/Textures/Apple.png" id="1_b3dgm"]
|
||||
[ext_resource type="Script" uid="uid://b8cygu5gimsgi" path="res://Inventory/GDScript/ItemDataGD.gd" id="1_sdqjv"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_sdqjv")
|
||||
id = "1"
|
||||
display_name = "Apple"
|
||||
icon = ExtResource("1_b3dgm")
|
||||
metadata/_custom_type_script = "uid://b8cygu5gimsgi"
|
||||
@@ -0,0 +1,22 @@
|
||||
[gd_resource type="Resource" script_class="Inventory" format=3 uid="uid://bdnml67fnkqw2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://7dsydoyjsley" path="res://Inventory/CSharp/Inventory.cs" id="1_0mvht"]
|
||||
[ext_resource type="Resource" uid="uid://d0w7bxo6cotsc" path="res://Inventory/Resources/CSharpItemApple.tres" id="1_twj40"]
|
||||
[ext_resource type="Script" uid="uid://c61uo84g0gp1v" path="res://Inventory/CSharp/InventorySlotData.cs" id="2_55fp8"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_qjyhv"]
|
||||
script = ExtResource("2_55fp8")
|
||||
Item = ExtResource("1_twj40")
|
||||
Count = 32
|
||||
metadata/_custom_type_script = "uid://c61uo84g0gp1v"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_6dau2"]
|
||||
script = ExtResource("2_55fp8")
|
||||
Item = ExtResource("1_twj40")
|
||||
Count = 12
|
||||
metadata/_custom_type_script = "uid://c61uo84g0gp1v"
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_0mvht")
|
||||
Slots = Array[Resource]([null, null, null, null, SubResource("Resource_qjyhv"), null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, SubResource("Resource_6dau2"), null, null, null, null, null, null, null, null, null])
|
||||
metadata/_custom_type_script = "uid://7dsydoyjsley"
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 296 B |
@@ -0,0 +1,40 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://bgam5s5hw83cf"
|
||||
path="res://.godot/imported/Apple.png-d164bb800f362dfbf6df55c5ab1627cc.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://Inventory/Textures/Apple.png"
|
||||
dest_files=["res://.godot/imported/Apple.png-d164bb800f362dfbf6df55c5ab1627cc.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=0
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/uastc_level=0
|
||||
compress/rdo_quality_loss=0.0
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=0
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=false
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/channel_remap/red=0
|
||||
process/channel_remap/green=1
|
||||
process/channel_remap/blue=2
|
||||
process/channel_remap/alpha=3
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=1
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user