* Ajout d'un mode infini (pour nos hard core gamers) * Ajout d'un message de découverte d'un nouvel outil * Séparation de la pelle en deux outils : la pioche et la fourche * Amélioration de la lisibilité des capsules d'énergies * Changement léger des texture du sol et de la pierre * Correction d'un bug lors du clic frénétique sur le porte de sortie du vaisseau * Ajout d'un icône de recharge * Fix de la mutation Ancien qui ne s'améliorait pas au niveau 4 + début de dev des artefacts avec un distributeur
141 lines
3.5 KiB
GDScript
141 lines
3.5 KiB
GDScript
extends Resource
|
|
class_name Inventory
|
|
|
|
signal updated(inventory: Inventory)
|
|
signal tool_added(item: Item)
|
|
|
|
@export var items: Array[Item] = []
|
|
@export var current_item_ind: int = 0 # over both tools and items
|
|
@export var n_tools: int = 0
|
|
|
|
func _init(inventory_size: int = 1):
|
|
set_size(inventory_size)
|
|
|
|
func get_n_item_slots() -> int:
|
|
return items.size() - n_tools
|
|
|
|
func set_size(new_size: int):
|
|
if new_size >= 0:
|
|
items.resize(n_tools + new_size)
|
|
updated.emit(self )
|
|
|
|
func change_size(size_mod: int):
|
|
set_size(items.size() - n_tools + size_mod)
|
|
|
|
func current_is_tool() -> bool:
|
|
return current_item_ind >= 0 && current_item_ind < n_tools
|
|
|
|
func get_best_available_slot_ind():
|
|
if !current_is_tool() && items[current_item_ind] == null:
|
|
return current_item_ind
|
|
for i in range(n_tools, items.size()):
|
|
if items[i] == null:
|
|
return i
|
|
# it's full
|
|
if !current_is_tool():
|
|
return current_item_ind
|
|
else:
|
|
return n_tools
|
|
|
|
func set_current_item(new_ind: int):
|
|
if new_ind >= items.size():
|
|
return
|
|
|
|
if new_ind != current_item_ind:
|
|
current_item_ind = new_ind
|
|
updated.emit(self )
|
|
|
|
func change_current_item(ind_mod: int):
|
|
if items.size() == 0:
|
|
current_item_ind = 0
|
|
return
|
|
var new_ind: int = current_item_ind + ind_mod
|
|
new_ind = new_ind % items.size()
|
|
if new_ind < 0:
|
|
new_ind += items.size()
|
|
set_current_item(new_ind)
|
|
|
|
func add_item(item: Item) -> bool:
|
|
if item.type != Item.ItemType.TOOL_ITEM:
|
|
var best_ind = get_best_available_slot_ind()
|
|
return set_item(item, best_ind)
|
|
elif item.type == Item.ItemType.TOOL_ITEM and not has_item_with_name(item.get_item_name()):
|
|
items.insert(n_tools, item)
|
|
if current_item_ind >= n_tools:
|
|
current_item_ind += 1
|
|
n_tools += 1
|
|
tool_added.emit(item)
|
|
updated.emit(self )
|
|
return true
|
|
else:
|
|
return false
|
|
|
|
func set_item(item: Item, ind: int = 0) -> bool:
|
|
if ind < 0 || ind >= items.size():
|
|
return false
|
|
items[ind] = item
|
|
updated.emit(self )
|
|
return true
|
|
|
|
func get_item(ind: int = current_item_ind) -> Item:
|
|
if ind < 0 || ind > items.size():
|
|
return null
|
|
return items[ind]
|
|
|
|
func has_item(item: Item) -> bool:
|
|
return items.has(item)
|
|
|
|
func has_item_with_name(name: String) -> bool:
|
|
var id = items.find_custom(
|
|
(func (i : Item):
|
|
return i and i.get_item_name() == name)
|
|
)
|
|
return id != -1
|
|
|
|
func remove_item(item: Item):
|
|
if item.type == Item.ItemType.TOOL_ITEM:
|
|
printerr("trying to remove a tool")
|
|
else:
|
|
var ind = items.find(item)
|
|
if ind >= 0:
|
|
items[ind] = null
|
|
updated.emit(self )
|
|
|
|
func remove_item_at(ind: int = current_item_ind):
|
|
if ind < 0 || ind > items.size():
|
|
return
|
|
|
|
if ind < n_tools:
|
|
printerr("trying to remove a tool")
|
|
return
|
|
|
|
items[ind] = null
|
|
updated.emit(self )
|
|
|
|
func remove_current_item():
|
|
remove_item_at()
|
|
|
|
func pop_item(ind: int = current_item_ind) -> Item:
|
|
if ind < 0 || ind > items.size():
|
|
return
|
|
|
|
if ind < n_tools:
|
|
printerr("trying to remove a tool")
|
|
return
|
|
|
|
var item_removed: Item = items[ind]
|
|
items[ind] = null
|
|
updated.emit(self )
|
|
return item_removed
|
|
|
|
func is_full():
|
|
for i in range(n_tools, items.size()):
|
|
if items[i] == null:
|
|
return false
|
|
return true
|
|
|
|
func clear_items():
|
|
for i in range(n_tools, items.size()):
|
|
items[i] = null
|
|
updated.emit(self )
|