Browse Source

wip: Create empty cells and move inventory into InventoryManager

pull/24/head
Ryan Reed 3 weeks ago
parent
commit
077143a32c
5 changed files with 83 additions and 34 deletions
  1. +29
    -0
      autoloads/inventory_manager.gd
  2. +38
    -16
      scenes/ui/inventory/inventory.gd
  3. +4
    -12
      scenes/ui/inventory/inventory.tscn
  4. +11
    -3
      scenes/ui/inventory/item_rect.gd
  5. +1
    -3
      scenes/ui/inventory/item_rect.tscn

+ 29
- 0
autoloads/inventory_manager.gd View File

@ -9,12 +9,41 @@ signal item_dropped(item: DBItemResource)
signal inventory_opened
signal inventory_closed
signal add_to_inventory(item_id: String, amount: int)
signal remove_from_inventory(item_id: String, amount: int)
signal item_added(item_id: String, amount: int)
signal item_removed(item_id: String, amount: int)
var quick_slot_item_id: String = "001"
var inventory: Dictionary[String, DBItemResource]
func _ready() -> void:
self.quick_slot_item_changed.connect(_on_quick_slot_item_changed)
self.item_picked_up.connect(_on_item_picked_up)
self.item_dropped.connect(_on_item_dropped)
self.add_to_inventory.connect(_on_add_to_inventory)
self.remove_from_inventory.connect(_on_remove_from_inventory)
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]
inventory[item_id].amount += amount
func _on_remove_from_inventory(item_id: String, amount: int = 1) -> void:
if not inventory.has(item_id): return
inventory[item_id].amount -= amount
func _on_item_dropped(item_id: String) -> void:
_on_remove_from_inventory(item_id, 1)
func _on_item_picked_up(item_id: String) -> void:
_on_add_to_inventory(item_id, 1)
func _on_quick_slot_item_changed(item_id: String) -> void:
quick_slot_item_id = item_id

+ 38
- 16
scenes/ui/inventory/inventory.gd View File

@ -5,10 +5,10 @@ extends Control
@export var inventory_resource: InventoryResource
@export var item_rect_scene: PackedScene
@export var grid_container: GridContainer
@export var max_items: int = 40 # 4 rows of 10
@export var highlight_theme: Resource
var items: Array[Node] = []
var selected_item: int = 0
@ -17,16 +17,12 @@ func _input(event: InputEvent) -> void:
toggle_inventory()
func _ready() -> void:
InventoryManager.item_picked_up.connect(add_item)
InventoryManager.item_dropped.connect(subtract_item)
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)
for item: InventoryItemRect in grid_container.get_children():
item.queue_free()
for item_resource: DBItemResource in inventory_resource.inventory:
add_item(item_resource)
refresh_items()
generate_inventory_grid()
func add_item(item_resource: DBItemResource, amount: int = 1) -> void:
@ -52,22 +48,35 @@ func find_item_rect(item_resource: DBItemResource) -> InventoryItemRect:
return rect
func subtract_item(item_resource: DBItemResource) -> void:
func generate_inventory_grid() -> void:
for item: InventoryItemRect in grid_container.get_children():
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()
# Add empty item 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 -= item_resource.amount
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(item_resource.amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")")
func refresh_items() -> void:
items = grid_container.get_children()
push_error("Attempting to subtract amount (" + str(amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")")
func toggle_inventory() -> void:
visible = not visible
@ -80,3 +89,16 @@ func toggle_inventory() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
InventoryManager.inventory_closed.emit()
get_tree().paused = false
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_dropped(item_resource: DBItemResource) -> void:
subtract_item(item_resource, 1)

+ 4
- 12
scenes/ui/inventory/inventory.tscn View File

@ -1,10 +1,9 @@
[gd_scene load_steps=6 format=3 uid="uid://dcr25y1lw4wjp"]
[gd_scene load_steps=5 format=3 uid="uid://dcr25y1lw4wjp"]
[ext_resource type="Script" uid="uid://dybecq130mxhn" path="res://scenes/ui/inventory/inventory.gd" id="1_s6ek7"]
[ext_resource type="Resource" uid="uid://blfp6tiir282o" path="res://resources/inventory/player_inventory_testing.tres" id="2_pefhr"]
[ext_resource type="PackedScene" uid="uid://boueuk2hnfvg" path="res://scenes/ui/inventory/item_rect.tscn" id="3_xeaml"]
[ext_resource type="StyleBox" uid="uid://dlaswvta2mvim" path="res://resources/inventory/quickslot-panel-highlight-stylebox.tres" id="4_8ndef"]
[ext_resource type="Resource" uid="uid://b50yyok6ddux4" path="res://resources/blocks/005_grass.tres" id="4_lobmi"]
[node name="Inventory" type="Control" node_paths=PackedStringArray("grid_container")]
process_mode = 3
@ -57,15 +56,8 @@ text = "Inventory"
[node name="GridContainer" type="GridContainer" parent="Background/MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 3
theme_override_constants/h_separation = 20
theme_override_constants/v_separation = 20
theme_override_constants/h_separation = 5
theme_override_constants/v_separation = 5
columns = 10
[node name="ItemRect" parent="Background/MarginContainer/VBoxContainer/GridContainer" instance=ExtResource("3_xeaml")]
layout_mode = 2
item_resource = ExtResource("4_lobmi")
[node name="ItemRect2" parent="Background/MarginContainer/VBoxContainer/GridContainer" instance=ExtResource("3_xeaml")]
layout_mode = 2
item_resource = ExtResource("4_lobmi")

+ 11
- 3
scenes/ui/inventory/item_rect.gd View File

@ -18,10 +18,18 @@ func init(_item_resource: DBItemResource, _highlight_theme: Resource) -> void:
update_rect()
func clear_rect() -> void:
item_texture.texture_normal = null
amount_label.text = ""
item_texture.tooltip_text = ""
func update_rect() -> void:
item_texture.texture_normal = load(item_resource.item_texture)
amount_label.text = "x" + str(item_resource.amount)
item_texture.tooltip_text = item_resource.name + "\n" + item_resource.description
if not item_resource:
clear_rect()
else:
item_texture.texture_normal = load(item_resource.item_texture)
amount_label.text = "x" + str(item_resource.amount)
item_texture.tooltip_text = item_resource.name + "\n" + item_resource.description
func on_stack_full(is_full: bool) -> void:
if is_full:


+ 1
- 3
scenes/ui/inventory/item_rect.tscn View File

@ -1,6 +1,5 @@
[gd_scene load_steps=4 format=3 uid="uid://boueuk2hnfvg"]
[gd_scene load_steps=3 format=3 uid="uid://boueuk2hnfvg"]
[ext_resource type="Texture2D" uid="uid://dknv7amroftm8" path="res://assets/godot-icon.svg" id="1_o0kom"]
[ext_resource type="Script" uid="uid://cknl6i0jce5jr" path="res://scenes/ui/inventory/item_rect.gd" id="1_oderi"]
[sub_resource type="LabelSettings" id="LabelSettings_oderi"]
@ -31,7 +30,6 @@ offset_right = 32.0
offset_bottom = 32.0
grow_horizontal = 2
grow_vertical = 2
texture_normal = ExtResource("1_o0kom")
ignore_texture_size = true
stretch_mode = 4


Loading…
Cancel
Save