mirror of
https://github.com/D4M13N-D3V/godot_grid_inventory.git
synced 2025-03-14 08:14:55 +00:00
feat setup initial files for global event system for inventory
This commit is contained in:
parent
6a3c366cd9
commit
9dd1e8a356
5
debug.gd
5
debug.gd
@ -4,7 +4,8 @@ extends Node
|
|||||||
@export var inventory:InventoryController
|
@export var inventory:InventoryController
|
||||||
@export var grid:GridContainer
|
@export var grid:GridContainer
|
||||||
func _ready():
|
func _ready():
|
||||||
var items = ItemDb.ITEMS
|
InventoryManager.connect("inventory_item_picked_up", item_picked_up)
|
||||||
|
var items = InventoryManager.cached_items
|
||||||
for item in items:
|
for item in items:
|
||||||
var button = Button.new()
|
var button = Button.new()
|
||||||
grid.add_child(button)
|
grid.add_child(button)
|
||||||
@ -12,6 +13,8 @@ func _ready():
|
|||||||
button.connect("pressed", func():
|
button.connect("pressed", func():
|
||||||
inventory.pickup_item(item))
|
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():
|
func _on_button_3_pressed():
|
||||||
$"..".pickup_item("small_debug_item")
|
$"..".pickup_item("small_debug_item")
|
||||||
|
@ -3,7 +3,7 @@ class_name InventoryItemGrid
|
|||||||
|
|
||||||
# Arrays to store inventory items and the grid representation
|
# Arrays to store inventory items and the grid representation
|
||||||
var inventory_item_grid_items = []
|
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
|
# 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 = {}
|
||||||
var inventory_item_grid_cell_size = 32
|
var inventory_item_grid_cell_size = 32
|
||||||
@ -50,6 +50,7 @@ func insert_item(item):
|
|||||||
parent.remove_child(item)
|
parent.remove_child(item)
|
||||||
add_child(item)
|
add_child(item)
|
||||||
inventory_item_grid_items.append(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
|
return true
|
||||||
else:
|
else:
|
||||||
return false
|
return false
|
||||||
|
30
grid_inventory_system/scripts/InventoryManager.gd
Normal file
30
grid_inventory_system/scripts/InventoryManager.gd
Normal 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)
|
@ -4,7 +4,7 @@ class_name ItemGraphic
|
|||||||
@export var graphic:TextureRect
|
@export var graphic:TextureRect
|
||||||
|
|
||||||
func init_item(item_id):
|
func init_item(item_id):
|
||||||
var dbItem = ItemDb.get_item(item_id)
|
var dbItem = InventoryManager.get_item(item_id)
|
||||||
item_config = dbItem
|
item_config = dbItem
|
||||||
graphic.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
graphic.set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
||||||
set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
set_size(Vector2(dbItem.item_size.x, dbItem.item_size.y))
|
||||||
|
@ -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
|
|
@ -28,7 +28,7 @@ func insert_item(item):
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
# Retrieve item slot information from the ItemDb
|
# 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
|
# Check if the item's slot matches the target slot
|
||||||
if item_slot != slot.name:
|
if item_slot != slot.name:
|
||||||
return false
|
return false
|
||||||
@ -49,8 +49,8 @@ func grab_item(pos):
|
|||||||
if item == null:
|
if item == null:
|
||||||
return null
|
return null
|
||||||
|
|
||||||
# Retrieve item slot information from the ItemDb
|
# Retrieve item slot information from the InventoryManager
|
||||||
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
|
||||||
# Remove the item from the slot and emit the weapon_unequipped signal
|
# Remove the item from the slot and emit the weapon_unequipped signal
|
||||||
items[item_slot] = null
|
items[item_slot] = null
|
||||||
weapon_unequipped.emit(item.item_config.item_id, item.item_config)
|
weapon_unequipped.emit(item.item_config.item_id, item.item_config)
|
||||||
|
@ -17,7 +17,7 @@ config/icon="res://icon.svg"
|
|||||||
|
|
||||||
[autoload]
|
[autoload]
|
||||||
|
|
||||||
ItemDb="*res://grid_inventory_system/scripts/ItemDB.gd"
|
InventoryManager="*res://grid_inventory_system/scripts/InventoryManager.gd"
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user