/
mrFeld
/
Platformer
Обзор
Документация
Войти
/
mrFeld
/
Platformer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Script/UI/Inventory/inventory.gd
72 строки
3 KB
Neterov Vladimir
quests
19 ноя 2025, 17:36
19 ноя 2025, 17:36
ab546f8
Код
Авторство
О чём код?
extends Node2D const SlotClass = preload("res://Script/UI/Inventory/slot.gd") @onready var inventory_slots = $GridContainer func _ready(): var slots = inventory_slots.get_children() for i in range(slots.size()): var slot = slots[i] slot.slot_index = i # Критически важно! slot.gui_input.connect(slot_gui_input.bind(slot)) slot.slotType = SlotClass.SlotType.INVENTORY initialize_inventory() func initialize_inventory(): var slots = inventory_slots.get_children() for i in range(slots.size()): if PlayerInventory.inventory.has(i): slots[i].initialize_item(PlayerInventory.inventory[i][0], PlayerInventory.inventory[i][1]) func slot_gui_input(event: InputEvent, slot: SlotClass): if event is InputEventMouseButton: if event.button_index == MOUSE_BUTTON_LEFT && event.pressed: if find_parent("PlayerInventory").holding_item !=null: if !slot.item: left_click_empty_slot(slot) else: if find_parent("PlayerInventory").holding_item.item_name != slot.item.item_name: left_click_different_item(event, slot) else: left_click_same_item(slot) elif slot.item: left_click_not_holding(slot) func _input(_event): var ui = find_parent("PlayerInventory") if ui and ui.holding_item: ui.holding_item.global_position = get_global_mouse_position() func left_click_empty_slot(slot): PlayerInventory.add_item_to_empty_slot(find_parent("PlayerInventory").holding_item, slot) slot.putIntoSlot(find_parent("PlayerInventory").holding_item) find_parent("PlayerInventory").holding_item = null func left_click_different_item(event, slot): PlayerInventory.remove_item(slot) PlayerInventory.add_item_to_empty_slot(find_parent("PlayerInventory").holding_item, slot) var temp_item = slot.item slot.pickFromSlot() temp_item.global_position = event.global_position slot.putIntoSlot(find_parent("PlayerInventory").holding_item) find_parent("PlayerInventory").holding_item = temp_item func left_click_same_item(slot): var stack_size = int(JsonData.item_data[slot.item.item_name]["StackSize"]) var able_to_add = stack_size - slot.item.item_quantity if able_to_add >= find_parent("PlayerInventory").holding_item.item_quantity: PlayerInventory.add_item_quantity(slot, find_parent("PlayerInventory").holding_item.item_quantity) slot.item.add_item_quantity(find_parent("PlayerInventory").holding_item.item_quantity) find_parent("PlayerInventory").holding_item.queue_free() find_parent("PlayerInventory").holding_item = null else: PlayerInventory.add_item_quantity(slot, able_to_add) slot.item.add_item_quantity(able_to_add) find_parent("PlayerInventory").holding_item.decrease_item_quantity(able_to_add) func left_click_not_holding(slot): PlayerInventory.remove_item(slot) find_parent("PlayerInventory").holding_item = slot.item slot.pickFromSlot() find_parent("PlayerInventory").holding_item.global_position = get_global_mouse_position()