From a073a89da2d5e3edcf935cd26ba4603071ffb733 Mon Sep 17 00:00:00 2001 From: NancokPS2 Date: Sun, 28 Jan 2024 11:38:56 -0300 Subject: [PATCH] Final touches --- classes/Item.gd | 11 +++++-- .../EquipmentSynchronizerComponent.gd | 10 +++--- scenes/player/inventory/Inventory.gd | 30 +++++++++--------- scenes/player/inventory/InventoryPanel.gd | 31 ++++++++++--------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/classes/Item.gd b/classes/Item.gd index de8b15eb..02018b86 100644 --- a/classes/Item.gd +++ b/classes/Item.gd @@ -2,7 +2,7 @@ extends StaticBody2D class_name Item -signal amount_changed(this: Item, new_amount: int) +signal amount_changed(new_amount: int) enum MODE { LOOT, ITEMSLOT } @@ -30,8 +30,8 @@ var amount_max: int = 1 var amount: int = 1: set(val): amount = val + amount_changed.emit(amount) - amount_changed.emit(self, amount) var price: int = 0 var value: int = 0 @@ -100,10 +100,17 @@ func server_use(player: Player) -> bool: ITEM_TYPE.CONSUMABLE: if boost.hp > 0: player.stats.heal(self.name, boost.hp) + + if amount <= 1: + player.inventory.remove_item(uuid) + else: + player.inventory.set_item_amount(uuid, amount - 1) return true + ITEM_TYPE.EQUIPMENT: if player.equipment and player.equipment.server_equip_item(self): return true + else: GodotLogger.info("%s could not equip item %s" % [player.name, item_class]) return false diff --git a/components/player/equipmentsynchronizercomponent/EquipmentSynchronizerComponent.gd b/components/player/equipmentsynchronizercomponent/EquipmentSynchronizerComponent.gd index 9cbc428b..3f3fa08f 100644 --- a/components/player/equipmentsynchronizercomponent/EquipmentSynchronizerComponent.gd +++ b/components/player/equipmentsynchronizercomponent/EquipmentSynchronizerComponent.gd @@ -14,7 +14,7 @@ var _target_node: Node var _equipment_synchronizer_rpc: EquipmentSynchronizerRPC = null -var items = { +var items: Dictionary = { "Head": null, "Body": null, "Legs": null, @@ -144,7 +144,7 @@ func _unequip_item(item_uuid: String) -> Item: func get_item(item_uuid: String) -> Item: - for equipment_slot in items: + for equipment_slot: String in items: var item: Item = items[equipment_slot] if item != null and item.uuid == item_uuid: return item @@ -156,7 +156,7 @@ func get_boost() -> Boost: var boost: Boost = Boost.new() boost.identifier = "equipment" - for equipment_slot in items: + for equipment_slot: String in items: var item: Item = items[equipment_slot] if item != null: boost.combine_boost(item.boost) @@ -167,7 +167,7 @@ func get_boost() -> Boost: func to_json() -> Dictionary: var output: Dictionary = {} - for slot in items: + for slot: String in items: if items[slot] != null: var item: Item = items[slot] output[slot] = item.to_json() @@ -176,7 +176,7 @@ func to_json() -> Dictionary: func from_json(data: Dictionary) -> bool: - for slot in data: + for slot: String in data: if not slot in items: GodotLogger.warn("Slot=[%s] does not exist in equipment items" % slot) return false diff --git a/scenes/player/inventory/Inventory.gd b/scenes/player/inventory/Inventory.gd index dd81b899..25da697a 100644 --- a/scenes/player/inventory/Inventory.gd +++ b/scenes/player/inventory/Inventory.gd @@ -72,8 +72,8 @@ func get_panel_at_pos(pos: Vector2) -> InventoryPanel: func get_panels(occupied_only: bool) -> Array[InventoryPanel]: var output: Array[InventoryPanel] = [] - var temp_arr: Array = [] - + var temp_arr: Array = [] + if occupied_only: for row: Array in panels: for panel: InventoryPanel in row: @@ -82,7 +82,7 @@ func get_panels(occupied_only: bool) -> Array[InventoryPanel]: else: for row: Array in panels: temp_arr += row - + output.assign(temp_arr) return output @@ -98,41 +98,39 @@ func update_inventory(): var occupied_panels: Array[InventoryPanel] = get_panels(true) var panel_item_uuid_dictionary: Dictionary = {} var panels_with_invalid_items: Dictionary = {} - + #Ensure all panels have an item assert( - occupied_panels.all( - func(panel_occupied: InventoryPanel): - return panel_occupied.item is Item - ) + occupied_panels.all( + func(panel_occupied: InventoryPanel): return panel_occupied.item is Item ) - + ) + #Store the uuid of the item that each panel has to accelerate the rest of the update. for panel: InventoryPanel in occupied_panels: panel_item_uuid_dictionary[panel.item.uuid] = panel - + #All panels are assumed invalid until proven otherwise. panels_with_invalid_items = panel_item_uuid_dictionary.duplicate() - + #Check every inventory item for inv_item: Item in player.inventory.items: - #Unmark as invalid those who have been found in the inventory panels_with_invalid_items.erase(inv_item.uuid) - + #No item with this uuid has been found in the displayed inventory, add it. if not inv_item.uuid in panel_item_uuid_dictionary.keys(): place_item_at_free_slot(inv_item) - + else: #Item found, update it. panel_item_uuid_dictionary[inv_item.uuid].item = inv_item panel_item_uuid_dictionary[inv_item.uuid].queue_redraw() - + #Clear any panels with items that are NOT in the inventory for item_uuid: String in panels_with_invalid_items: panels_with_invalid_items[item_uuid].item = null - + func place_item_at_free_slot(item: Item) -> bool: for y in range(SIZE.y): diff --git a/scenes/player/inventory/InventoryPanel.gd b/scenes/player/inventory/InventoryPanel.gd index 4a8aaac3..189bfdd5 100644 --- a/scenes/player/inventory/InventoryPanel.gd +++ b/scenes/player/inventory/InventoryPanel.gd @@ -7,18 +7,17 @@ const DEFAULT_FONT: Font = preload("res://addons/gut/fonts/LobsterTwo-Regular.tt @export var item: Item: set(new_item): if item is Item and item.amount_changed.is_connected(on_item_amount_changed): - item.amount_changed.disconnect(on_item_amount_changed) - + item.amount_changed.disconnect(on_item_amount_changed) + item = new_item - + if item is Item: if not item.amount_changed.is_connected(on_item_amount_changed): - item.amount_changed.connect(on_item_amount_changed) + item.amount_changed.connect(on_item_amount_changed) $TextureRect.texture = item.get_node("Icon").texture else: - $TextureRect.texture = null - + queue_redraw() @onready var inventory: Inventory = $"../.." @@ -52,14 +51,18 @@ func _ready(): mouse_exited.connect(_on_mouse_exited) - - - func _draw(): - if item is Item: + if item is Item and item.amount != 1: var font_height: int = 14 - draw_string(DEFAULT_FONT, Vector2(0, size.y - font_height), str(item.amount),HORIZONTAL_ALIGNMENT_CENTER, -1, font_height) - + draw_string( + DEFAULT_FONT, + Vector2(0, size.y - font_height), + str(item.amount), + HORIZONTAL_ALIGNMENT_CENTER, + -1, + font_height + ) + func _gui_input(event: InputEvent): if event.is_action_pressed("j_left_click"): @@ -87,7 +90,7 @@ func _gui_input(event: InputEvent): drag_panel.hide() -func _physics_process(_delta): +func _physics_process(_delta: float): if selected: drag_panel.position = get_local_mouse_position() + drag_panel_offset @@ -100,5 +103,5 @@ func _on_mouse_exited(): inventory.mouse_above_this_panel = null -func on_item_amount_changed(item: Item, amount: int): +func on_item_amount_changed(_amount: int): queue_redraw()