/
mrFeld
/
Platformer
Обзор
Документация
Войти
/
mrFeld
/
Platformer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Script/Player/PlayerInventory.gd
100 строк
3 KB
Neterov Vladimir
item_questV0.1
04 янв 2026, 15:42
04 янв 2026, 15:42
40fc304
Код
Авторство
О чём код?
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 = { #--> slot_index: [item_name, item_quantity] } var active_item_slot = 0 var holding_item = null func add_item(item_name, item_quantity): _notify_quests_item_collected(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/UserInterface/PlayerInventory/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() func _notify_quests_item_collected(item_name: String, amount: int) -> void: var quests_root := get_tree().root.get_node_or_null("Main/Quests") if quests_root == null: return for q in quests_root.get_children(): if q.has_method("on_item_collected"): q.on_item_collected(item_name, amount) func save(): var save_dict = { "filename" : get_scene_file_path(), "parent" : get_parent().get_path(), "hotbar_items" : hotbar, "inventory_items" : inventory } return save_dict