diff --git a/debug.gd b/debug.gd new file mode 100644 index 0000000..18bee30 --- /dev/null +++ b/debug.gd @@ -0,0 +1,21 @@ +extends Node + + +func _on_button_3_pressed(): + $"..".pickup_item("small_debug_item") + + +func _on_button_2_pressed(): + $"..".pickup_item("medium_debug_item") + + +func _on_button_pressed(): + $"..".pickup_item("large_debug_item") + + +func _on_button_4_pressed(): + $"..".pickup_item("shirt") + + +func _on_button_5_pressed(): + $"..".pickup_item("hat") diff --git a/grid_inventory_system/debug/debug_ui.tscn b/grid_inventory_system/debug/debug_ui.tscn new file mode 100644 index 0000000..12607e5 --- /dev/null +++ b/grid_inventory_system/debug/debug_ui.tscn @@ -0,0 +1,47 @@ +[gd_scene load_steps=2 format=3 uid="uid://c2c7v5idagdqr"] + +[ext_resource type="Script" path="res://debug.gd" id="1_qtffx"] + +[node name="DEBUG" type="Node"] +script = ExtResource("1_qtffx") + +[node name="Button" type="Button" parent="."] +offset_left = 493.0 +offset_top = 112.0 +offset_right = 653.0 +offset_bottom = 143.0 +text = "GIVE LARGE ITEM" + +[node name="Button4" type="Button" parent="."] +offset_left = 672.0 +offset_top = 151.0 +offset_right = 832.0 +offset_bottom = 182.0 +text = "GIVE SHIRT" + +[node name="Button5" type="Button" parent="."] +offset_left = 673.0 +offset_top = 110.0 +offset_right = 833.0 +offset_bottom = 141.0 +text = "GIVE HAT" + +[node name="Button2" type="Button" parent="."] +offset_left = 496.0 +offset_top = 149.0 +offset_right = 653.0 +offset_bottom = 180.0 +text = "GIVE MEDIUM ITEM" + +[node name="Button3" type="Button" parent="."] +offset_left = 498.0 +offset_top = 186.0 +offset_right = 656.0 +offset_bottom = 217.0 +text = "GIVE SMALL ITEM" + +[connection signal="pressed" from="Button" to="." method="_on_button_pressed"] +[connection signal="pressed" from="Button4" to="." method="_on_button_4_pressed"] +[connection signal="pressed" from="Button5" to="." method="_on_button_5_pressed"] +[connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"] +[connection signal="pressed" from="Button3" to="." method="_on_button_3_pressed"] diff --git a/grid_inventory_system/debug/hat.png b/grid_inventory_system/debug/hat.png new file mode 100644 index 0000000..a0c47f1 Binary files /dev/null and b/grid_inventory_system/debug/hat.png differ diff --git a/grid_inventory_system/debug/hat.png.import b/grid_inventory_system/debug/hat.png.import new file mode 100644 index 0000000..d0e4589 --- /dev/null +++ b/grid_inventory_system/debug/hat.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://ghlfm76i4kve" +path="res://.godot/imported/hat.png-706aaac0cb9cc74c2e7d8c97ed023b0c.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://grid_inventory_system/debug/hat.png" +dest_files=["res://.godot/imported/hat.png-706aaac0cb9cc74c2e7d8c97ed023b0c.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +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/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 diff --git a/grid_inventory_system/debug/main.tscn b/grid_inventory_system/debug/main.tscn new file mode 100644 index 0000000..1adf745 --- /dev/null +++ b/grid_inventory_system/debug/main.tscn @@ -0,0 +1,103 @@ +[gd_scene load_steps=5 format=3 uid="uid://brs6u78mwggbp"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryController.gd" id="1_hmtt6"] +[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryGrid.gd" id="2_8imhp"] +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemEquipmentSlots.gd" id="3_sri3o"] +[ext_resource type="PackedScene" uid="uid://c2c7v5idagdqr" path="res://grid_inventory_system/debug/debug_ui.tscn" id="4_abx0e"] + +[node name="Inventory" type="Control" node_paths=PackedStringArray("inventory_grid", "inventory_background", "inventory_equipment_slots")] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_hmtt6") +inventory_grid = NodePath("UI/Background/Items") +inventory_background = NodePath("UI/Background") +inventory_equipment_slots = NodePath("UI/Background/Equipment") + +[node name="UI" type="CanvasLayer" parent="."] + +[node name="Background" type="ColorRect" parent="UI"] +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -473.5 +offset_top = -235.5 +offset_right = 473.5 +offset_bottom = 235.5 +grow_horizontal = 2 +grow_vertical = 2 +color = Color(0.121569, 0.121569, 0.121569, 1) + +[node name="Equipment" type="ColorRect" parent="UI/Background"] +layout_mode = 1 +anchors_preset = 11 +anchor_left = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -237.0 +offset_top = 4.0 +offset_right = -4.0 +offset_bottom = -4.0 +grow_horizontal = 0 +grow_vertical = 2 +color = Color(0.184314, 0.184314, 0.184314, 1) +script = ExtResource("3_sri3o") + +[node name="HEAD" type="ColorRect" parent="UI/Background/Equipment"] +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -37.0 +offset_top = 16.5 +offset_right = 38.0 +offset_bottom = 91.5 +grow_horizontal = 2 +color = Color(0.278431, 0.278431, 0.278431, 1) + +[node name="BODY" type="ColorRect" parent="UI/Background/Equipment"] +layout_mode = 1 +anchors_preset = 4 +anchor_top = 0.5 +anchor_bottom = 0.5 +offset_left = 32.5 +offset_top = -121.0 +offset_right = 202.5 +offset_bottom = 49.0 +grow_vertical = 2 +color = Color(0.278431, 0.278431, 0.278431, 1) + +[node name="WEAPON" type="ColorRect" parent="UI/Background/Equipment"] +layout_mode = 1 +anchors_preset = 4 +anchor_top = 0.5 +anchor_bottom = 0.5 +offset_left = 7.5 +offset_top = 91.0 +offset_right = 227.5 +offset_bottom = 194.0 +grow_vertical = 2 +color = Color(0.278431, 0.278431, 0.278431, 1) + +[node name="Items" type="ColorRect" parent="UI/Background"] +layout_mode = 1 +anchors_preset = 9 +anchor_bottom = 1.0 +offset_left = 12.0 +offset_top = 8.0 +offset_right = 701.0 +offset_bottom = -8.0 +grow_vertical = 2 +color = Color(0.164706, 0.164706, 0.164706, 1) +script = ExtResource("2_8imhp") +inventory_item_grid_width = 21 +inventory_item_grid_height = 14 +inventory_grid_default_color = Color(1, 1, 1, 1) +inventory_grid_used_color = Color(0, 1, 0, 1) + +[node name="DEBUG" parent="." instance=ExtResource("4_abx0e")] diff --git a/grid_inventory_system/debug/shirt.png b/grid_inventory_system/debug/shirt.png new file mode 100644 index 0000000..73a53ea Binary files /dev/null and b/grid_inventory_system/debug/shirt.png differ diff --git a/grid_inventory_system/debug/shirt.png.import b/grid_inventory_system/debug/shirt.png.import new file mode 100644 index 0000000..e6c6a7f --- /dev/null +++ b/grid_inventory_system/debug/shirt.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bdfcmsyry3sg3" +path="res://.godot/imported/shirt.png-c04d1f8c53dc5f843f267734a8b0f810.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://grid_inventory_system/debug/shirt.png" +dest_files=["res://.godot/imported/shirt.png-c04d1f8c53dc5f843f267734a8b0f810.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +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/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 diff --git a/grid_inventory_system/items/body.tres b/grid_inventory_system/items/body.tres new file mode 100644 index 0000000..3b6be7f --- /dev/null +++ b/grid_inventory_system/items/body.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://bu1t7ogoct2pm"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_f4bsa"] +[ext_resource type="Texture2D" uid="uid://bdfcmsyry3sg3" path="res://grid_inventory_system/debug/shirt.png" id="1_gmlai"] + +[resource] +script = ExtResource("1_f4bsa") +item_id = "shirt" +item_name = "Shirt" +item_description = "This is a shirt" +item_size = Vector2(256, 256) +item_usable = false +item_equipment = true +item_equipment_slot = "BODY" +item_texture = ExtResource("1_gmlai") diff --git a/grid_inventory_system/items/head.tres b/grid_inventory_system/items/head.tres new file mode 100644 index 0000000..19423aa --- /dev/null +++ b/grid_inventory_system/items/head.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://cauw4i0kveld6"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_hflmv"] +[ext_resource type="Texture2D" uid="uid://ghlfm76i4kve" path="res://grid_inventory_system/debug/hat.png" id="1_vm0jo"] + +[resource] +script = ExtResource("1_hflmv") +item_id = "hat" +item_name = "Hat" +item_description = "This is a hat" +item_size = Vector2(224, 160) +item_usable = false +item_equipment = true +item_equipment_slot = "HEAD" +item_texture = ExtResource("1_vm0jo") diff --git a/grid_inventory_system/items/large_debug_item.tres b/grid_inventory_system/items/large_debug_item.tres new file mode 100644 index 0000000..6363a8b --- /dev/null +++ b/grid_inventory_system/items/large_debug_item.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://d2orfaiyhg0sa"] + +[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_l6twi"] +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_40ofh"] + +[resource] +script = ExtResource("2_40ofh") +item_id = "large_debug_item" +item_name = "Debug Item Large" +item_description = "A large item for debugging." +item_size = Vector2(256, 256) +item_usable = false +item_equipment = false +item_equipment_slot = "NONE" +item_texture = ExtResource("1_l6twi") diff --git a/grid_inventory_system/items/medium_debug_item.tres b/grid_inventory_system/items/medium_debug_item.tres new file mode 100644 index 0000000..4253052 --- /dev/null +++ b/grid_inventory_system/items/medium_debug_item.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://drkcx6hkx0ay8"] + +[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_jwlst"] +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_xdqd2"] + +[resource] +script = ExtResource("2_xdqd2") +item_id = "medium_debug_item" +item_name = "Debug Item Medium" +item_description = "A medium item for debugging." +item_size = Vector2(128, 128) +item_usable = false +item_equipment = false +item_equipment_slot = "NONE" +item_texture = ExtResource("1_jwlst") diff --git a/grid_inventory_system/items/small_debug_item.tres b/grid_inventory_system/items/small_debug_item.tres new file mode 100644 index 0000000..7073bf8 --- /dev/null +++ b/grid_inventory_system/items/small_debug_item.tres @@ -0,0 +1,15 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=3 format=3 uid="uid://p3m1yi3kw1q6"] + +[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_g0vsl"] +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="2_lq0i6"] + +[resource] +script = ExtResource("2_lq0i6") +item_id = "small_debug_item" +item_name = "Debug Item Small" +item_description = "A small item for debugging." +item_size = Vector2(64, 64) +item_usable = false +item_equipment = false +item_equipment_slot = "NONE" +item_texture = ExtResource("1_g0vsl") diff --git a/grid_inventory_system/items/weapon.tres b/grid_inventory_system/items/weapon.tres new file mode 100644 index 0000000..8805e94 --- /dev/null +++ b/grid_inventory_system/items/weapon.tres @@ -0,0 +1,13 @@ +[gd_resource type="Resource" script_class="ItemConfiguration" load_steps=2 format=3 uid="uid://cvy3nwmcgx5le"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemConfiguration.gd" id="1_jvmcf"] + +[resource] +script = ExtResource("1_jvmcf") +item_id = "item" +item_name = "Item Name" +item_description = "This is the default item description." +item_size = Vector2(3, 3) +item_usable = false +item_equipment = false +item_equipment_slot = "NONE" diff --git a/grid_inventory_system/scenes/ItemGraphic.tscn b/grid_inventory_system/scenes/ItemGraphic.tscn new file mode 100644 index 0000000..e3fbc2c --- /dev/null +++ b/grid_inventory_system/scenes/ItemGraphic.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3 uid="uid://dtir5hovd6i3h"] + +[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemGraphic.gd" id="1_bih4t"] + +[node name="Item" type="TextureRect"] +expand_mode = 2 +script = ExtResource("1_bih4t") diff --git a/grid_inventory_system/scripts/InventoryController.gd b/grid_inventory_system/scripts/InventoryController.gd new file mode 100644 index 0000000..661b229 --- /dev/null +++ b/grid_inventory_system/scripts/InventoryController.gd @@ -0,0 +1,118 @@ +extends Control +class_name InventoryController +const ITEM_BASE = preload("res://grid_inventory_system/scenes/ItemGraphic.tscn") + +@export var inventory_open_input:String = "inventory_open" +@export var inventory_close_input:String = "inventory_close" +@export var inventory_use_input:String = "inventory_use" +@export var inventory_grab_input:String = "inventory_grab" +@export var inventory_rotate_input:String = "inventory_rotate" +@export var inventory_grid:Control +@export var inventory_background:Control +@export var inventory_equipment_slots:Control + +var inventory_open = true +var inventory_item_dragged = null +var inventory_item_cursor_offset = Vector2() +var inventory_item_dragged_last_container = null +var inventory_item_dragged_last_pos = Vector2() + +signal inventory_item_equipped(item_config,slot) +signal inventory_item_unequipped(item_config,slot) +signal inventory_item_dropped(item_config) +signal inventory_item_grabbed(item_config, x, y, container) +signal inventory_item_released(item_config, x, y, container) + +func _ready(): + if(inventory_grid==null): + printerr("The inventory grid was not assigned to the inventory controller!") + if(inventory_background==null): + printerr("The inventory background was not assigned to the inventory controller!") + if(inventory_grid==null): + printerr("The inventory equipment slots was not assigned to the inventory controller!") + +func _process(delta): + if(Input.is_action_just_pressed(inventory_open_input) and inventory_open==false): + inventory_open = true + inventory_background.visible=true + + if(Input.is_action_just_pressed(inventory_close_input) and inventory_open==true): + inventory_open = false + inventory_background.visible=false + + var cursor_pos = get_global_mouse_position() + + if Input.is_action_just_pressed("inventory_grab"): + grab(cursor_pos) + + if Input.is_action_just_released("inventory_grab"): + release(cursor_pos) + + if Input.is_action_just_pressed("inventory_use"): + use(cursor_pos) + if inventory_item_dragged != null: + inventory_item_dragged.global_position = cursor_pos + inventory_item_cursor_offset + +func grab(cursor_pos): + var c = _get_container_mouse_over() + if c != null and c.has_method("grab_item"): + inventory_item_dragged = c.grab_item(cursor_pos) + if inventory_item_dragged != null: + add_child(inventory_item_dragged) + inventory_item_dragged_last_container = c + inventory_item_dragged_last_pos = inventory_item_dragged.global_position + inventory_item_cursor_offset = inventory_item_dragged.global_position - cursor_pos + +func release(cursor_pos): + if inventory_item_dragged == null: + return + var c = _get_container_mouse_over() + if c == null: + drop_item() + elif c.has_method("insert_item"): + if c.insert_item(inventory_item_dragged): + inventory_item_dragged = null + else: + return_item() + else: + return_item() + +func use(cursor_pos): + pass + +func pickup_item(item_id): + var item = ITEM_BASE.instantiate() + item.set_meta("id", item_id) + var dbItem = ItemDb.get_item(item_id) + item.item_config = dbItem + item.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) + item.texture = dbItem.item_texture + if not inventory_grid.insert_item_at_first_available_spot(item): + item.queue_free() + return false + return true + +func drop_item(): + inventory_item_dropped.emit(inventory_item_dragged.item_config) + inventory_item_dragged.queue_free() + inventory_item_dragged = null + +# Function to return the dragged item to its original position or container +func return_item(): + inventory_item_dragged.global_position = inventory_item_dragged_last_pos + inventory_item_dragged_last_container.insert_item(inventory_item_dragged) + inventory_item_dragged = null + +func _get_container_mouse_over(): + + if(_is_mouse_ontop_of_control(inventory_grid)==true): + return inventory_grid + elif(_is_mouse_ontop_of_control(inventory_equipment_slots)==true): + return inventory_equipment_slots + elif(_is_mouse_ontop_of_control(inventory_background)==true): + return inventory_background + return null + +func _is_mouse_ontop_of_control(c): + var cursor_pos = get_global_mouse_position() + return c.get_global_rect().has_point(cursor_pos) diff --git a/grid_inventory_system/scripts/InventoryGrid.gd b/grid_inventory_system/scripts/InventoryGrid.gd new file mode 100644 index 0000000..baa506b --- /dev/null +++ b/grid_inventory_system/scripts/InventoryGrid.gd @@ -0,0 +1,138 @@ +extends ColorRect +class_name InventoryItemGrid + +# Arrays to store inventory items and the grid representation +var inventory_item_grid_items = [] + +# 2D dictionary to represent the inventory grid, where each cell has information about usage and a corresponding ColorRect node +var inventory_item_grid = {} +var inventory_item_grid_cell_size = 32 +@export var inventory_item_grid_width = 0 +@export var inventory_item_grid_height = 0 +@export var inventory_grid_default_color: Color +@export var inventory_grid_used_color: Color + +# Signals for item-related events +signal weapon_equipped(weapon_id, item) +signal weapon_unequipped(weapon_id, item) +signal item_used(item_id) + +func _ready(): + # Initialize the inventory grid + for x in range(inventory_item_grid_width): + inventory_item_grid[x] = {} + for y in range(inventory_item_grid_height): + inventory_item_grid[x][y] = {} + inventory_item_grid[x][y]["used"] = false + + # Create ColorRect nodes for each grid cell and set their default colors + for y in range(inventory_item_grid_height): + for x in range(inventory_item_grid_width): + var color_rect = ColorRect.new() + color_rect.global_position = Vector2(x * inventory_item_grid_cell_size, y * inventory_item_grid_cell_size) + color_rect.size = Vector2(inventory_item_grid_cell_size - 2, inventory_item_grid_cell_size - 2) + add_child(color_rect) + color_rect.color = inventory_grid_default_color + inventory_item_grid[x][y]["rect"] = color_rect + +# Function to insert an item into the inventory grid +func insert_item(item): + var item_pos = item.position + var g_pos = pos_to_grid_coord(item_pos) + var item_size = get_grid_size(item) + + # Check if there is enough space to insert the item + if is_grid_space_available(g_pos.x, g_pos.y, item_size.x, item_size.y)==true: + set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, true) + item.position = Vector2(g_pos.x, g_pos.y) * inventory_item_grid_cell_size + add_child(item) + inventory_item_grid_items.append(item) + return true + else: + return false + +# Function to grab an item from the inventory grid based on a position +func grab_item(pos): + var item = get_item_under_pos(pos) + if item == null: + return null + + var item_pos = item.position + var g_pos = pos_to_grid_coord(item_pos) + var item_size = get_grid_size(item) + set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, false) + + inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item)) + return item + +# Function to use an item from the inventory grid based on a position +func use_item(pos): + var item = get_item_under_pos(pos) + if item == null: + return null + + # Emit signal if the item is usable + if item.item_config.item_usable == true: + item_used.emit(item.item_config.item_id) + item.queue_free() + + var item_size = get_grid_size(item) + var item_pos = item.position + var g_pos = pos_to_grid_coord(item_pos) + inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item)) + set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, false) + +# Function to convert global position to grid coordinates +func pos_to_grid_coord(pos): + var results = {} + results.x = int(pos.x / inventory_item_grid_cell_size) + results.y = int(pos.y / inventory_item_grid_cell_size) + return results + +# Function to get the grid size of an item in terms of grid cells +func get_grid_size(item): + var results = {} + var s = item.size + results.x = int(clamp(s.x / inventory_item_grid_cell_size, 1.0, 500.0)) + results.y = int(clamp(s.y / inventory_item_grid_cell_size, 1.0, 500.0)) + return results + +# Function to check if there is enough space in the grid to place an item +func is_grid_space_available(x, y, w, h): + if x < 0 or y < 0: + return false + if x + w > inventory_item_grid_width or y + h > inventory_item_grid_height: + return false + for i in range(x, x + w): + for j in range(y, y + h): + if inventory_item_grid[i][j]["used"] == true: + return false + return true + +# Function to set the usage state of grid cells and update ColorRect colors accordingly +func set_grid_space(x, y, w, h, state): + for i in range(x, x + w): + for j in range(y, y + h): + if state == true: + inventory_item_grid[i][j]["used"] = true + inventory_item_grid[i][j]["rect"].color = inventory_grid_used_color + else: + inventory_item_grid[i][j]["used"] = false + inventory_item_grid[i][j]["rect"].color = inventory_grid_default_color + +# Function to get the item under a given position +func get_item_under_pos(pos): + for item in inventory_item_grid_items: + if item.get_global_rect().has_point(pos): + return item + return null + +# Function to insert an item at the first available spot in the grid +func insert_item_at_first_available_spot(item): + for x in inventory_item_grid_width: + for y in inventory_item_grid_height: + if not inventory_item_grid[x][y]["used"]: + item.global_position = Vector2(x* inventory_item_grid_cell_size, (y)* inventory_item_grid_cell_size) + if insert_item(item)==true: + return true + return false diff --git a/grid_inventory_system/scripts/ItemConfiguration.gd b/grid_inventory_system/scripts/ItemConfiguration.gd new file mode 100644 index 0000000..d6fd975 --- /dev/null +++ b/grid_inventory_system/scripts/ItemConfiguration.gd @@ -0,0 +1,10 @@ +extends Resource +class_name ItemConfiguration +@export var item_id:String = "item" +@export var item_name:String = "Item Name" +@export var item_description:String = "This is the default item description." +@export var item_size:Vector2 = Vector2(3,3) +@export var item_usable:bool = false +@export var item_equipment:bool = false +@export var item_equipment_slot:String = "NONE" +@export var item_texture:Texture2D diff --git a/grid_inventory_system/scripts/ItemDB.gd b/grid_inventory_system/scripts/ItemDB.gd new file mode 100644 index 0000000..b51f757 --- /dev/null +++ b/grid_inventory_system/scripts/ItemDB.gd @@ -0,0 +1,18 @@ +extends Node + +# Dictionary to store loaded items with their item_id as the key +var ITEMS = {} + +func _ready(): + # Load items from the "items" directory and populate the ITEMS dictionary + for i in DirAccess.get_files_at("res://grid_inventory_system/items"): + var item = load("res://grid_inventory_system/items/" + i) + ITEMS[item.item_id] = item + +# Function to retrieve an item based on its item_id +func get_item(item_id): + # Check if the item_id exists in the ITEMS dictionary + if ITEMS.has(item_id): + return ITEMS[item_id] + else: + return null diff --git a/grid_inventory_system/scripts/ItemEquipmentSlots.gd b/grid_inventory_system/scripts/ItemEquipmentSlots.gd new file mode 100644 index 0000000..0335aee --- /dev/null +++ b/grid_inventory_system/scripts/ItemEquipmentSlots.gd @@ -0,0 +1,71 @@ +extends ColorRect + +# Reference to child slots +@onready var slots = get_children() + +# Dictionary to store items associated with their respective slots +var items = {} + +# Signals for item-related events +signal weapon_equipped(weapon_id, item) +signal weapon_unequipped(weapon_id, item) + +func _ready(): + # Initialize the items dictionary with slots + for slot in slots: + items[slot.name] = null + +# Function to insert an item into the equipment slots +func insert_item(item): + # Calculate the center position of the item + var item_pos = item.global_position + item.size / 2 + # Get the slot under the item's position + var slot = get_slot_under_pos(item_pos) + + # Check if the item can be inserted into the slot + if slot == null: + return false + + # Retrieve item slot information from the ItemDb + var item_slot = ItemDb.get_item(item.get_meta("id")).item_equipment_slot + # Check if the item's slot matches the target slot + if item_slot != slot.name: + return false + # Check if the slot is already occupied + if items[item_slot] != null: + return false + + # Place the item in the slot and emit the weapon_equipped signal + items[item_slot] = item + item.global_position = slot.global_position + slot.size / 2 - item.size / 2 + weapon_equipped.emit(item.item_config.item_id, item.item_config) + return true + +# Function to grab an item from the equipment slots +func grab_item(pos): + # Get the item under the specified position + var item = get_item_under_pos(pos) + if item == null: + return null + + # Retrieve item slot information from the ItemDb + var item_slot = ItemDb.get_item(item.get_meta("id")).item_slot + # Remove the item from the slot and emit the weapon_unequipped signal + items[item_slot] = null + weapon_unequipped.emit(item.item_config.item_id, item.item_config) + return item + +# Function to get the slot under a specified position +func get_slot_under_pos(pos): + return get_thing_under_pos(slots, pos) + +# Function to get the item under a specified position +func get_item_under_pos(pos): + return get_thing_under_pos(items.values(), pos) + +# Generic function to get the object (slot or item) under a specified position +func get_thing_under_pos(arr, pos): + for thing in arr: + if thing != null and thing.get_global_rect().has_point(pos): + return thing + return null diff --git a/grid_inventory_system/scripts/ItemGraphic.gd b/grid_inventory_system/scripts/ItemGraphic.gd new file mode 100644 index 0000000..59d7753 --- /dev/null +++ b/grid_inventory_system/scripts/ItemGraphic.gd @@ -0,0 +1,3 @@ +extends TextureRect +class_name ItemGraphic +@export var item_config:ItemConfiguration diff --git a/main.tscn b/main.tscn deleted file mode 100644 index d823b82..0000000 --- a/main.tscn +++ /dev/null @@ -1,9 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://brs6u78mwggbp"] - -[ext_resource type="Texture2D" uid="uid://bdvx58rwnaeev" path="res://icon.svg" id="1_xhw8e"] - -[node name="Node2D" type="Node2D"] - -[node name="Icon" type="Sprite2D" parent="."] -position = Vector2(552, 307) -texture = ExtResource("1_xhw8e") diff --git a/project.godot b/project.godot index 87d11a1..62244f8 100644 --- a/project.godot +++ b/project.godot @@ -10,7 +10,49 @@ config_version=5 [application] -config/name="Godot Template" -run/main_scene="res://main.tscn" +config/name="Grid Inventory System" +run/main_scene="res://grid_inventory_system/debug/main.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" + +[autoload] + +ItemDb="*res://grid_inventory_system/scripts/ItemDB.gd" + +[display] + +window/size/viewport_width=1920 +window/size/viewport_height=1080 +window/stretch/mode="canvas_items" + +[input] + +inventory_open={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194306,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +inventory_close={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194306,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +inventory_use={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":2,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} +inventory_grab={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) +] +} +inventory_rotate={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null) +] +} +mouse={ +"deadzone": 0.5, +"events": [] +}