diff --git a/debug.gd b/debug.gd index 55f9fc6..3bd08c0 100644 --- a/debug.gd +++ b/debug.gd @@ -4,14 +4,17 @@ extends Node @export var inventory:InventoryController @export var grid:GridContainer func _ready(): - var items = ItemDb.ITEMS + InventoryManager.connect("inventory_item_picked_up", item_picked_up) + var items = InventoryManager.cached_items for item in items: var button = Button.new() grid.add_child(button) button.text = "Spawn "+item button.connect("pressed", func(): inventory.pickup_item(item)) - + +func item_picked_up(grid_name, x, y, screen_x, screen_y, item_control): + print(item_control.item_config.item_name) func _on_button_3_pressed(): $"..".pickup_item("small_debug_item") diff --git a/grid_inventory_system/scripts/InventoryGrid.gd b/grid_inventory_system/scripts/InventoryGrid.gd index bfcb8e0..7e98386 100644 --- a/grid_inventory_system/scripts/InventoryGrid.gd +++ b/grid_inventory_system/scripts/InventoryGrid.gd @@ -3,7 +3,7 @@ class_name InventoryItemGrid # Arrays to store inventory items and the grid representation var inventory_item_grid_items = [] - +@export var inventory_grid_name = "default" # 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 @@ -50,6 +50,7 @@ func insert_item(item): parent.remove_child(item) add_child(item) inventory_item_grid_items.append(item) + InventoryManager.emit_signal("inventory_item_picked_up", inventory_grid_name, g_pos.x, g_pos.y, item_pos.x, item_pos.y, item) return true else: return false diff --git a/grid_inventory_system/scripts/InventoryManager.gd b/grid_inventory_system/scripts/InventoryManager.gd new file mode 100644 index 0000000..d694412 --- /dev/null +++ b/grid_inventory_system/scripts/InventoryManager.gd @@ -0,0 +1,30 @@ +extends Node + + +# Dictionary to store loaded items with their item_id as the key +var cached_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"): + if(i.ends_with(".tres")): + var item = load("res://grid_inventory_system/items/" + i) + cached_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 cached_items.has(item_id): + return cached_items[item_id] + else: + return null + +#signal inventory_open +#signal inventory_close +signal inventory_item_picked_up(grid_name, x, y, screen_x, screen_y, item_control) +#signal inventory_item_used +#signal inventory_item_opened +#signal inventory_item_dropped +#signal inventory_item_destroyed +#signal inventory_item_dragged(screex_x, screen_y, grid_x, grid_y, ) +#signal inventory_item_released(screen_x, screen_y, grid_x, grid_y, slot_control, grid_control, item_control) diff --git a/grid_inventory_system/scripts/Item.gd b/grid_inventory_system/scripts/Item.gd index b200047..145457d 100644 --- a/grid_inventory_system/scripts/Item.gd +++ b/grid_inventory_system/scripts/Item.gd @@ -4,7 +4,7 @@ class_name ItemGraphic @export var graphic:TextureRect func init_item(item_id): - var dbItem = ItemDb.get_item(item_id) + var dbItem = InventoryManager.get_item(item_id) item_config = dbItem graphic.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y)) diff --git a/grid_inventory_system/scripts/ItemDB.gd b/grid_inventory_system/scripts/ItemDB.gd deleted file mode 100644 index d7a5475..0000000 --- a/grid_inventory_system/scripts/ItemDB.gd +++ /dev/null @@ -1,19 +0,0 @@ -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"): - if(i.ends_with(".tres")): - 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/ItemSlot.gd b/grid_inventory_system/scripts/ItemSlot.gd index 13def12..bdf721f 100644 --- a/grid_inventory_system/scripts/ItemSlot.gd +++ b/grid_inventory_system/scripts/ItemSlot.gd @@ -28,7 +28,7 @@ func insert_item(item): return false # Retrieve item slot information from the ItemDb - var item_slot = ItemDb.get_item(item.get_meta("id")).item_equipment_slot + var item_slot = InventoryManager.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 @@ -49,8 +49,8 @@ func grab_item(pos): if item == null: return null - # Retrieve item slot information from the ItemDb - var item_slot = ItemDb.get_item(item.get_meta("id")).item_equipment_slot + # Retrieve item slot information from the InventoryManager + var item_slot = InventoryManager.get_item(item.get_meta("id")).item_equipment_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) diff --git a/project.godot b/project.godot index d596111..e05372f 100644 --- a/project.godot +++ b/project.godot @@ -17,7 +17,7 @@ config/icon="res://icon.svg" [autoload] -ItemDb="*res://grid_inventory_system/scripts/ItemDB.gd" +InventoryManager="*res://grid_inventory_system/scripts/InventoryManager.gd" [display]