/
mrFeld
/
Platformer
Обзор
Документация
Войти
/
mrFeld
/
Platformer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
inventory
Script/Player/PlayerInventory.gd
76 строк
2 KB
Neterov Vladimir
Inventory v0.2
23 сен 2025, 12:17
23 сен 2025, 12:17
d4cf390
Код
Авторство
О чём код?
extends Node signal active_item_updated const SlotClass = preload("res://Script/UI/Inventory/slot.gd") const ItemClass = preload("res://Script/Items/item.gd") const NUM_INVENTORY_SLOTS = 20 const NUM_HOTBAR_SLOTS = 5 var inventory = { #--> slot_index: [item_name, item_quantity] } var hotbar = { 0: ["Bomb", 1], 1: ["Meat", 1], #--> slot_index: [item_name, item_quantity] } var active_item_slot = 0 func add_item(item_name, item_quantity): for item in inventory: if inventory[item][0] == item_name: var stack_size = int(JsonData.item_data[item_name]["StackSize"]) var able_to_add = stack_size - inventory[item][1] if able_to_add >= item_quantity: inventory[item][1] += item_quantity update_slot_visual(item, inventory[item][0], inventory[item][1]) return else: inventory[item][1] += able_to_add update_slot_visual(item, inventory[item][0], inventory[item][1]) item_quantity -= able_to_add for i in range(NUM_INVENTORY_SLOTS): if inventory.has(i) == false: inventory[i] = [item_name, item_quantity] update_slot_visual(i, inventory[i][0], inventory[i][1]) return func remove_item(slot: SlotClass, is_hotbar: bool = false): if is_hotbar: hotbar.erase(slot.slot_index) else: inventory.erase(slot.slot_index) func add_item_to_empty_slot(item: ItemClass, slot: SlotClass, is_hotbar: bool = false): if is_hotbar: hotbar[slot.slot_index] = [item.item_name, item.item_quantity] else: inventory[slot.slot_index] = [item.item_name, item.item_quantity] func add_item_quantity(slot: SlotClass, quantity_to_add: int, is_hotbar: bool = false): if is_hotbar: hotbar[slot.slot_index][1] += quantity_to_add else: inventory[slot.slot_index][1] += quantity_to_add func update_slot_visual(slot_index, item_name, new_quantity): var slot = get_tree().root.get_node("/root/Main/Player/UserInterface/Inventory/GridContainer/Slot" + str(slot_index + 1)) if slot.item != null: slot.item.set_item(item_name, new_quantity) else: slot.initialize_item(item_name, new_quantity) func active_item_scroll_up(): active_item_slot = (active_item_slot + 1) % NUM_HOTBAR_SLOTS active_item_updated.emit() func active_item_scroll_down(): if active_item_slot == 0: active_item_slot = NUM_HOTBAR_SLOTS - 1 else: active_item_slot -= 1 active_item_updated.emit()