inventory refactor + inv ui interactable

This commit is contained in:
Altaezio
2025-10-13 18:38:47 +02:00
parent 433b3dfd28
commit 3ee2c8bdf1
7 changed files with 86 additions and 59 deletions

View File

@@ -4,15 +4,22 @@ class_name Inventory
signal inventory_changed(inventory: Inventory)
@export var items: Array[Item] = []
@export var size: int = 3
var current_item_ind: int = 0
func _init(_inventory_size : int = 1):
size = _inventory_size
func _init(inventory_size: int = 1):
items.resize(inventory_size)
func get_best_available_slot_ind():
if items[current_item_ind] == null:
return current_item_ind
for i in items.size():
if items[i] == null:
return i
return current_item_ind
func set_current_item(new_ind: int):
if new_ind >= length():
if new_ind >= items.size():
return
if new_ind != current_item_ind:
@@ -20,42 +27,25 @@ func set_current_item(new_ind: int):
emit_signal("inventory_changed", self)
func change_current_item(ind_mod: int):
if length() == 0:
if items.size() == 0:
current_item_ind = 0
return
var new_ind : int = current_item_ind + ind_mod
new_ind = new_ind % length()
var new_ind: int = current_item_ind + ind_mod
new_ind = new_ind % items.size()
if new_ind < 0:
new_ind += length()
new_ind += items.size()
set_current_item(new_ind)
func add_item(item: Item):
if items.size() < size:
items.append(item)
emit_signal("inventory_changed", self)
var best_ind = get_best_available_slot_ind()
if best_ind != current_item_ind:
set_item(item, best_ind)
return true
else:
return false
func add_items(items_to_add: Array[Item], fillup: bool = false):
if fillup:
var has_changed := false
for i in min(items_to_add.size(), size - items.size()):
items.append(items_to_add[i])
has_changed = true
if has_changed:
emit_signal("inventory_changed", self)
return has_changed
elif !fillup && items.size() + items_to_add.size() < size:
items.append_array(items_to_add)
emit_signal("inventory_changed", self)
return true
func length() -> int:
return len(items)
func set_item(item: Item, ind: int = 0) -> bool:
if ind >= size:
if ind < 0 || ind >= items.size():
return false
while len(items) <= ind:
items.append(null)
@@ -64,7 +54,7 @@ func set_item(item: Item, ind: int = 0) -> bool:
return true
func get_item(ind: int = current_item_ind) -> Item:
if len(items) <= ind:
if ind < 0 || items.size() <= ind:
return null
return items[ind]
@@ -72,27 +62,27 @@ func has_item(item: Item) -> bool:
return item in items
func remove_item(item: Item):
items.erase(item)
emit_signal("inventory_changed", self)
var ind = items.find(item)
if ind >= 0:
items[ind] = null
emit_signal("inventory_changed", self)
func remove_item_at(ind: int = current_item_ind):
if len(items) <= ind:
if items.size() <= ind:
return
items.remove_at(ind)
if current_item_ind >= length():
change_current_item(-1)
items[ind] = null
emit_signal("inventory_changed", self)
func remove_current_item():
remove_item_at()
func pop_item(ind: int = current_item_ind) -> Item:
if length() == 0:
if items.size() == 0:
return
var item_removed: Item = items.pop_at(ind)
if current_item_ind >= length():
change_current_item(-1)
var item_removed: Item = items[ind]
items[ind] = null
emit_signal("inventory_changed", self)
return item_removed