|
|
@ -0,0 +1,68 @@ |
|
|
|
class_name Inventory |
|
|
|
extends Control |
|
|
|
|
|
|
|
|
|
|
|
@export var inventory_resource: InventoryResource |
|
|
|
@export var item_rect_scene: PackedScene |
|
|
|
@export var grid_container: GridContainer |
|
|
|
|
|
|
|
|
|
|
|
func _input(event: InputEvent) -> void: |
|
|
|
if event.is_action_pressed("open_inventory"): |
|
|
|
toggle_inventory() |
|
|
|
|
|
|
|
func _ready() -> void: |
|
|
|
for item_resource: ItemResource in inventory_resource.inventory: |
|
|
|
print(item_resource.name + " - " + str(item_resource.amount)) |
|
|
|
add_item(item_resource) |
|
|
|
|
|
|
|
|
|
|
|
func add_item(item_resource: ItemResource) -> 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.item_resource = item_resource |
|
|
|
item_rect.update_rect() |
|
|
|
|
|
|
|
func find_item_rect(item_resource: ItemResource) -> InventoryItemRect: |
|
|
|
var rect: InventoryItemRect = null |
|
|
|
|
|
|
|
for container: InventoryItemRect in grid_container.get_children(): |
|
|
|
if container.item_resource.id == item_resource.id: |
|
|
|
rect = container |
|
|
|
break |
|
|
|
|
|
|
|
return rect |
|
|
|
|
|
|
|
func subtract_item(item_resource: ItemResource) -> 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.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 toggle_inventory() -> void: |
|
|
|
visible = not visible |
|
|
|
|
|
|
|
if visible: |
|
|
|
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE |
|
|
|
InventoryManager.inventory_opened.emit() |
|
|
|
get_tree().paused = true |
|
|
|
else: |
|
|
|
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED |
|
|
|
InventoryManager.inventory_closed.emit() |
|
|
|
get_tree().paused = false |