RustHacker/inv_system/inventory.gd

67 lines
1.7 KiB
GDScript3
Raw Normal View History

2026-01-10 18:05:09 +11:00
extends PanelContainer
2026-01-10 23:17:55 +11:00
class_name Inventory
2026-01-10 18:05:09 +11:00
2026-01-10 23:17:55 +11:00
@export var inventory_name : String = "Inventory"
@export var slots : Array[ItemSlot]
2026-01-11 18:11:14 +11:00
@export var type: String
2026-01-10 23:17:55 +11:00
@export_group("Node References")
@export var grid : GridContainer
@export var inv_label : Label
2026-01-10 18:05:09 +11:00
2026-01-11 18:11:14 +11:00
2026-01-10 18:05:09 +11:00
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
2026-01-10 23:17:55 +11:00
slots.assign(grid.get_children())
inv_label.text = inventory_name
2026-01-11 18:11:14 +11:00
for slot in slots:
slot.type = type
if type == "Inventory":
MouseTweaks.player_inventory = self
2026-01-23 14:33:24 +11:00
elif type != "Install" and type != "Trash":
2026-01-11 18:11:14 +11:00
MouseTweaks.other_inventory = self
2026-01-10 18:05:09 +11:00
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Input.get_current_cursor_shape() == CURSOR_FORBIDDEN:
DisplayServer.cursor_set_shape(DisplayServer.CURSOR_ARROW)
var data_bk
func _notification(what: int) -> void:
if what == Node.NOTIFICATION_DRAG_BEGIN:
data_bk = get_viewport().gui_get_drag_data()
if what == Node.NOTIFICATION_DRAG_END:
if not is_drag_successful():
if data_bk:
data_bk.icon.show()
data_bk = null
2026-01-10 23:17:55 +11:00
#attempts to add the provided item to the inventory, returns true on success
func add_item(item : ItemData) -> bool:
for slot in slots:
if slot.item == null:
slot.item = item
slot.update_ui()
return true # item placed successfully
return false # theres no space to add the item
func filled_slots() -> int:
var count : int = 0
for slot in slots:
if slot.item:
count += 1
return count
2026-01-14 22:15:18 +11:00
2026-01-17 13:42:34 +11:00
func empty_spots() -> int:
var count : int = 0
for slot in slots:
if not slot.item:
count += 1
return count
2026-01-14 22:15:18 +11:00
func empty_grid():
for slot in slots:
slot.item = null
slot.update_ui()