feat setup initial files for global event system for inventory

This commit is contained in:
Damien Ostler 2024-01-09 00:45:51 -05:00
parent 6a3c366cd9
commit 9dd1e8a356
7 changed files with 42 additions and 27 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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