Browse Source

wip: Reworking how inventory is generated (still non-functional)

pull/24/head
Ryan Reed 3 weeks ago
parent
commit
2db6edae06
5 changed files with 92 additions and 63 deletions
  1. +48
    -5
      autoloads/inventory_manager.gd
  2. +2
    -0
      resources/db_item_resource.gd
  3. +14
    -1
      resources/inventory/player_inventory_testing.tres
  4. +0
    -1
      resources/inventory_resource.gd
  5. +28
    -56
      scenes/ui/inventory/inventory.gd

+ 48
- 5
autoloads/inventory_manager.gd View File

@ -16,7 +16,9 @@ signal item_removed(item_id: String, amount: int)
var quick_slot_item_id: String = "001" var quick_slot_item_id: String = "001"
var inventory: Dictionary[String, DBItemResource]
var max_inventory_items: int = 40 # 4 rows of 10
var inventory: Array[DBItemResource] = []
func _ready() -> void: func _ready() -> void:
@ -28,16 +30,57 @@ func _ready() -> void:
self.remove_from_inventory.connect(_on_remove_from_inventory) self.remove_from_inventory.connect(_on_remove_from_inventory)
# TODO: REMOVE ME
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel"):
add_to_inventory.emit("003", 1)
func _find_first_stack(item_resource: DBItemResource, item_id: String) -> bool:
return (
item_resource.id == item_id and
item_resource.amount < item_resource.max_stack_size
)
func _on_add_to_inventory(item_id: String, amount: int = 1) -> void: func _on_add_to_inventory(item_id: String, amount: int = 1) -> void:
if not inventory.has(item_id):
inventory[item_id] = DBItems.data[item_id]
var item_resource: DBItemResource = DBItems.data[item_id]
# The logic below is a mess and needs to be reworked
var amount_remaining: int = amount
while amount_remaining > 0:
var first_stack_index: int = inventory.find_custom(_find_first_stack.bind(item_id))
if first_stack_index == -1 and inventory.size() < max_inventory_items:
inventory.append(item_resource)
if amount_remaining <= item_resource.max_stack_size:
inventory[-1].amount += amount_remaining
amount_remaining = 0
else:
inventory[-1].amount = item_resource.max_stack_size
amount_remaining -= item_resource.max_stack_size
item_added.emit(item_id, amount - amount_remaining)
elif first_stack_index > -1:
var current_amount: int = inventory[first_stack_index].amount
var total_amount: int = current_amount + amount_remaining
if item_resource.max_stack_size - total_amount < 0:
inventory[first_stack_index].amount += amount_remaining
amount_remaining = 0
else:
inventory[first_stack_index].amount = item_resource.max_stack_size
amount_remaining -= item_resource.max_stack_size
inventory[item_id].amount += amount
item_added.emit(item_id, amount - amount_remaining)
func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void: func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void:
if not inventory.has(item_id): return if not inventory.has(item_id): return
inventory[item_id].amount -= amount
#if inventory[item_id].amount > 0:
#inventory[item_id].amount -= amount
#item_removed.emit(item_id, max(0, amount))
func _on_item_dropped(item_id: String) -> void: func _on_item_dropped(item_id: String) -> void:
_on_remove_from_inventory(item_id, 1) _on_remove_from_inventory(item_id, 1)


+ 2
- 0
resources/db_item_resource.gd View File

@ -5,5 +5,7 @@ extends Resource
@export var id: String = "000" @export var id: String = "000"
@export var name: String = "Item Name" @export var name: String = "Item Name"
@export var amount: int = 0 @export var amount: int = 0
@export var max_stack_size: int = 999
@export var max_number_stacks: int = 0 ## 0 for unlimited stacks
@export var description: String = "Item Description" @export var description: String = "Item Description"
@export_file var item_texture: String = "res://assets/godot-icon.svg" @export_file var item_texture: String = "res://assets/godot-icon.svg"

+ 14
- 1
resources/inventory/player_inventory_testing.tres View File

@ -18,6 +18,8 @@ material_texture = ExtResource("2_8w148")
id = "001" id = "001"
name = "Dirt" name = "Dirt"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Block of dirt" description = "Block of dirt"
item_texture = "uid://li36txj7oweq" item_texture = "uid://li36txj7oweq"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -28,6 +30,8 @@ material_texture = ExtResource("4_7yuhn")
id = "002" id = "002"
name = "Stone" name = "Stone"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Block of stone" description = "Block of stone"
item_texture = "uid://ct1iawpfkdf5l" item_texture = "uid://ct1iawpfkdf5l"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -38,6 +42,8 @@ material_texture = ExtResource("5_p27bu")
id = "003" id = "003"
name = "Wood" name = "Wood"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Wood log" description = "Wood log"
item_texture = "uid://0mw651622h01" item_texture = "uid://0mw651622h01"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -48,6 +54,8 @@ material_texture = ExtResource("7_xd1nd")
id = "004" id = "004"
name = "Leaves" name = "Leaves"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Tree leaves" description = "Tree leaves"
item_texture = "uid://goygbpyqhych" item_texture = "uid://goygbpyqhych"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -58,6 +66,8 @@ material_texture = ExtResource("7_vgvac")
id = "005" id = "005"
name = "Grass" name = "Grass"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Block of grass and dirt" description = "Block of grass and dirt"
item_texture = "uid://li36txj7oweq" item_texture = "uid://li36txj7oweq"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -68,6 +78,8 @@ material_texture = ExtResource("8_7yuhn")
id = "006" id = "006"
name = "Glass" name = "Glass"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "Glass block" description = "Glass block"
item_texture = "uid://cpllegyqnfnrh" item_texture = "uid://cpllegyqnfnrh"
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" metadata/_custom_type_script = "uid://dwrmy4mx0mw18"
@ -78,6 +90,8 @@ resource_scene = ExtResource("9_viap3")
id = "007" id = "007"
name = "Torch" name = "Torch"
amount = 999 amount = 999
max_stack_size = 999
max_number_stacks = 0
description = "A torch to light the way" description = "A torch to light the way"
item_texture = "uid://dknv7amroftm8" item_texture = "uid://dknv7amroftm8"
metadata/_custom_type_script = "uid://m32ytcig5ha5" metadata/_custom_type_script = "uid://m32ytcig5ha5"
@ -85,5 +99,4 @@ metadata/_custom_type_script = "uid://m32ytcig5ha5"
[resource] [resource]
script = ExtResource("1_4v6mg") script = ExtResource("1_4v6mg")
inventory = Array[ExtResource("1_gg8jx")]([SubResource("Resource_jaam2"), SubResource("Resource_4kby3"), SubResource("Resource_byct5"), SubResource("Resource_qycqj"), SubResource("Resource_viap3"), SubResource("Resource_wqat2"), SubResource("Resource_xd1nd")]) inventory = Array[ExtResource("1_gg8jx")]([SubResource("Resource_jaam2"), SubResource("Resource_4kby3"), SubResource("Resource_byct5"), SubResource("Resource_qycqj"), SubResource("Resource_viap3"), SubResource("Resource_wqat2"), SubResource("Resource_xd1nd")])
max_stack_size = 999
metadata/_custom_type_script = "uid://becun6dj78v8d" metadata/_custom_type_script = "uid://becun6dj78v8d"

+ 0
- 1
resources/inventory_resource.gd View File

@ -3,4 +3,3 @@ extends Resource
@export var inventory: Array[DBItemResource] = [] @export var inventory: Array[DBItemResource] = []
@export var max_stack_size: int = 999

+ 28
- 56
scenes/ui/inventory/inventory.gd View File

@ -17,66 +17,39 @@ func _input(event: InputEvent) -> void:
toggle_inventory() toggle_inventory()
func _ready() -> void: func _ready() -> void:
InventoryManager.item_picked_up.connect(_on_item_picked_up)
InventoryManager.item_dropped.connect(_on_item_dropped)
InventoryManager.add_to_inventory.connect(_on_add_to_inventory)
InventoryManager.remove_from_inventory.connect(_on_remove_from_inventory)
generate_inventory_grid()
func add_item(item_resource: DBItemResource, amount: int = 1) -> void:
var item_rect: InventoryItemRect = find_item_rect(item_resource)
if item_rect != null:
if item_rect.item_resource.amount + item_resource.amount >= inventory_resource.max_stack_size:
item_rect.on_stack_full(true)
return
item_rect.item_resource.amount += item_resource.amount
item_rect.update_rect()
else:
item_rect = item_rect_scene.instantiate()
grid_container.add_child(item_rect)
item_rect.init(item_resource, highlight_theme)
update_inventory_with_resource()
refresh_inventory_grid()
InventoryManager.item_added.connect(_on_item_added) # Should be added after update_inventory_with_resource()
InventoryManager.item_removed.connect(_on_item_removed) # Should be added after update_inventory_with_resource()
func create_item_cell(item_resource: DBItemResource) -> void:
var item_rect: InventoryItemRect = item_rect_scene.instantiate()
grid_container.add_child(item_rect)
item_rect.init(item_resource, highlight_theme)
func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect: func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect:
var rect: InventoryItemRect = null var rect: InventoryItemRect = null
for container: InventoryItemRect in grid_container.get_children():
if container.item_resource.id == item_resource.id:
rect = container
for item_rect: InventoryItemRect in grid_container.get_children():
if item_rect.item_resource == null: continue
if item_rect.item_resource.id == item_resource.id:
rect = item_rect
break break
return rect return rect
func generate_inventory_grid() -> void:
func refresh_inventory_grid() -> void:
for item: InventoryItemRect in grid_container.get_children(): for item: InventoryItemRect in grid_container.get_children():
item.queue_free() item.queue_free()
# Add any items from the existing inventory resource
for item_resource: DBItemResource in inventory_resource.inventory:
InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount)
var empty_cells: int = max_items - inventory_resource.inventory.size()
for item: DBItemResource in InventoryManager.inventory:
create_item_cell(item)
# Add empty item cells
var empty_cells: int = InventoryManager.max_inventory_items - inventory_resource.inventory.size()
for _i: int in range(empty_cells): for _i: int in range(empty_cells):
var item_rect: InventoryItemRect = item_rect_scene.instantiate()
grid_container.add_child(item_rect)
item_rect.clear_rect()
func subtract_item(item_resource: DBItemResource, amount: int =1) -> void:
var item_rect: InventoryItemRect = find_item_rect(item_resource)
if item_rect != null:
if item_rect.item_resource.amount >= inventory_resource.max_stack_size:
item_rect.on_stack_full(false)
item_rect.item_resource.amount -= amount
item_rect.update_rect()
if item_rect.item_resource.amount < 1: # Empty stack
item_rect.queue_free()
else:
push_error("Attempting to subtract amount (" + str(amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")")
create_item_cell(null)
func toggle_inventory() -> void: func toggle_inventory() -> void:
visible = not visible visible = not visible
@ -90,15 +63,14 @@ func toggle_inventory() -> void:
InventoryManager.inventory_closed.emit() InventoryManager.inventory_closed.emit()
get_tree().paused = false get_tree().paused = false
func update_inventory_with_resource() -> void:
# Add any items from the existing inventory resource
for item_resource: DBItemResource in inventory_resource.inventory:
InventoryManager.add_to_inventory.emit(item_resource.id, item_resource.amount)
func _on_add_to_inventory(item_id: String, amount: int) -> void:
add_item(DBItems.data[item_id], amount)
func _on_remove_from_inventory(item_id: String, amount: int) -> void:
subtract_item(DBItems.data[item_id], amount)
func _on_item_picked_up(item_resource: DBItemResource, amount: int) -> void:
add_item(item_resource, amount)
func _on_item_added(_item_id: String, _amount: int) -> void:
refresh_inventory_grid()
func _on_item_dropped(item_resource: DBItemResource) -> void:
subtract_item(item_resource, 1)
func _on_item_removed(_item_id: String, _amount: int) -> void:
refresh_inventory_grid()

Loading…
Cancel
Save