From 9248f5ac405737341c09137f2a85a05a3317ed67 Mon Sep 17 00:00:00 2001 From: aronand <71503746+aronand@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:02:22 +0300 Subject: [PATCH 1/4] refactor: Simplifies PlayerInteractionComponent Simplifies PlayerInteractionComponent by merging input checks for interact and interact2 to a single block. Removes RayCast3D.is_colliding() check and gets that information via RayCast3D.get_collider() (null if no collision) --- .../Components/PlayerInteractionComponent.gd | 48 ++++++------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/COGITO/Components/PlayerInteractionComponent.gd b/COGITO/Components/PlayerInteractionComponent.gd index c5ebe795..cb47ca11 100644 --- a/COGITO/Components/PlayerInteractionComponent.gd +++ b/COGITO/Components/PlayerInteractionComponent.gd @@ -78,47 +78,29 @@ func interactive_object_exit(): object_detected = false -func _input(event): - if event.is_action_pressed("interact"): - - # if carrying an object, drop it. - if is_carrying and is_instance_valid(carried_object) and carried_object.input_map_action == "interact": - carried_object.throw(throw_power) - elif is_carrying and !is_instance_valid(carried_object): - stop_carrying() - - # Checks if raycast is hitting an interactable object that has an interaction for this input action. - if interaction_raycast.is_colliding(): - interactable = interaction_raycast.get_collider() - if interactable.is_in_group("interactable"): - for node in interactable.interaction_nodes: - if node.input_map_action == "interact": - node.interact(self) - interactive_object_exit() - if is_wielding: - equipped_wieldable_item.update_wieldable_data(self) - - - if event.is_action_pressed("interact2"): +func _input(event: InputEvent) -> void: + if event.is_action_pressed("interact") or event.is_action_pressed("interact2"): + var action: String = "interact" if event.is_action_pressed("interact") else "interact2" + # if carrying an object, drop it. - if is_carrying and is_instance_valid(carried_object) and carried_object.input_map_action == "interact2": + if is_carrying and is_instance_valid(carried_object) and carried_object.input_map_action == action: carried_object.throw(throw_power) elif is_carrying and !is_instance_valid(carried_object): stop_carrying() - - + # Checks if raycast is hitting an interactable object that has an interaction for this input action. - if interaction_raycast.is_colliding(): - interactable = interaction_raycast.get_collider() - if interactable.is_in_group("interactable"): - for node in interactable.interaction_nodes: - if node.input_map_action == "interact2": - node.interact(self) + # This could be run each frame instead and the key be checked when finding an interactable, + # which would be slower, but would easily allow any key to be used in the + # interaction component and help reduce the nesting. + interactable = interaction_raycast.get_collider() + if interactable != null and interactable.is_in_group("interactable"): + for node: InteractionComponent in interactable.interaction_nodes: + if node.input_map_action == action: + node.interact(self) interactive_object_exit() if is_wielding: equipped_wieldable_item.update_wieldable_data(self) - - + # Wieldable primary Action Input if !get_parent().is_movement_paused: if is_wielding and Input.is_action_just_pressed("action_primary"): From 56c94258ce3656349d20a1e4ebb05d41243ba25d Mon Sep 17 00:00:00 2001 From: aronand <71503746+aronand@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:42:40 +0300 Subject: [PATCH 2/4] refactor: Simplifies attempt_reload() Simplifies attempt_reload() by decreasing the level of nesting and by rewriting the reload logic: The reload now loops through the item slots, looking for suitable ammo. When ammo is found, the entire quantity is used instead of one by one. --- .../Components/PlayerInteractionComponent.gd | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/COGITO/Components/PlayerInteractionComponent.gd b/COGITO/Components/PlayerInteractionComponent.gd index cb47ca11..3f551533 100644 --- a/COGITO/Components/PlayerInteractionComponent.gd +++ b/COGITO/Components/PlayerInteractionComponent.gd @@ -33,7 +33,7 @@ var is_changing_wieldables : bool = false #Used to avoid any input acitons while @export var wieldable_nodes : Array[Node] @export var wieldable_container : Node3D # Various variables used for wieldable handling -var equipped_wieldable_item = null +var equipped_wieldable_item: WieldableItemPD = null var equipped_wieldable_node = null var is_wielding : bool var player_rid @@ -204,51 +204,52 @@ func attempt_action_secondary(is_released:bool): func attempt_reload(): - var inventory = get_parent().inventory_data + var inventory: InventoryPD = get_parent().inventory_data # Some safety checks if reload should even be triggered. if inventory == null: print("Player inventory was null!") return - + if equipped_wieldable_node.animation_player.is_playing(): print("Can't interrupt current action / animation") return - + # If the item doesn't use reloading, return. if equipped_wieldable_item.no_reload: return - - var ammo_needed : int = abs(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current) + + var ammo_needed: int = abs(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current) if ammo_needed <= 0: print("Wieldable is fully charged.") return - + if equipped_wieldable_item.get_item_amount_in_inventory(equipped_wieldable_item.ammo_item_name) <= 0: print("You have no ammo for this wieldable.") return - - if !equipped_wieldable_node.animation_player.is_playing(): #Make sure reload isn't interrupting another animation. - equipped_wieldable_node.reload() - - while ammo_needed > 0: - if equipped_wieldable_item.get_item_amount_in_inventory(equipped_wieldable_item.ammo_item_name) <=0: - print("No more ammo in inventory.") - break - for slot in inventory.inventory_slots: - if slot != null and slot.inventory_item.name == equipped_wieldable_item.ammo_item_name and ammo_needed > 0: - inventory.remove_item_from_stack(slot) - ammo_needed -= slot.inventory_item.reload_amount - if ammo_needed < 0: - ammo_needed = 0 - - equipped_wieldable_item.charge_current += slot.inventory_item.reload_amount - if equipped_wieldable_item.charge_current > equipped_wieldable_item.charge_max: - equipped_wieldable_item.charge_current = equipped_wieldable_item.charge_max - - print("RELOAD: Found ", slot.inventory_item.name, ". Removed one and added ", slot.inventory_item.reload_amount, " charge. Still needed: ", ammo_needed) - - inventory.inventory_updated.emit(inventory) - equipped_wieldable_item.update_wieldable_data(self) + + if equipped_wieldable_node.animation_player.is_playing(): # Make sure reload isn't interrupting another animation. + return + + equipped_wieldable_node.reload() + + for slot: InventorySlotPD in inventory.inventory_slots: + if ammo_needed <= 0: + break + if slot == null or slot.inventory_item.name != equipped_wieldable_item.ammo_item_name: + continue + + var ammo_used: int + if ammo_needed >= slot.quantity: + ammo_used = slot.quantity + inventory.remove_slot_data(slot) + elif ammo_needed < slot.quantity: + ammo_used = ammo_needed + slot.quantity -= ammo_used + equipped_wieldable_item.charge_current += ammo_used + ammo_needed -= ammo_used + + inventory.inventory_updated.emit(inventory) + equipped_wieldable_item.update_wieldable_data(self) func on_death(): From 0e8d0aa39cffd8693059fd470126443690959da3 Mon Sep 17 00:00:00 2001 From: aronand <71503746+aronand@users.noreply.github.com> Date: Sat, 20 Apr 2024 15:43:03 +0300 Subject: [PATCH 3/4] fix: Changes reload to account for reload ammount of ammo type --- .../Components/PlayerInteractionComponent.gd | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/COGITO/Components/PlayerInteractionComponent.gd b/COGITO/Components/PlayerInteractionComponent.gd index 3f551533..7c7028ed 100644 --- a/COGITO/Components/PlayerInteractionComponent.gd +++ b/COGITO/Components/PlayerInteractionComponent.gd @@ -218,7 +218,8 @@ func attempt_reload(): if equipped_wieldable_item.no_reload: return - var ammo_needed: int = abs(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current) + # Round up the ammo calculation as the charges are floats, not ints + var ammo_needed: int = abs(ceili(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current)) if ammo_needed <= 0: print("Wieldable is fully charged.") return @@ -239,13 +240,17 @@ func attempt_reload(): continue var ammo_used: int - if ammo_needed >= slot.quantity: - ammo_used = slot.quantity + var slot_ammo: AmmoItemPD = slot.inventory_item + var quantity_needed: int = ceili(float(ammo_needed) / slot_ammo.reload_amount) + + if slot.quantity <= quantity_needed: + ammo_used = slot_ammo.reload_amount * slot.quantity inventory.remove_slot_data(slot) - elif ammo_needed < slot.quantity: - ammo_used = ammo_needed - slot.quantity -= ammo_used - equipped_wieldable_item.charge_current += ammo_used + elif slot.quantity > quantity_needed: + ammo_used = slot_ammo.reload_amount * quantity_needed + slot.quantity -= quantity_needed + + equipped_wieldable_item.add(ammo_used) ammo_needed -= ammo_used inventory.inventory_updated.emit(inventory) From 3ca9a0814956aa29bf8cfdcf780caa47264daa69 Mon Sep 17 00:00:00 2001 From: aronand <71503746+aronand@users.noreply.github.com> Date: Sat, 20 Apr 2024 15:52:00 +0300 Subject: [PATCH 4/4] refactor: Removes a ceili() that was used for testing --- COGITO/Components/PlayerInteractionComponent.gd | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/COGITO/Components/PlayerInteractionComponent.gd b/COGITO/Components/PlayerInteractionComponent.gd index 7c7028ed..2cb9a6d7 100644 --- a/COGITO/Components/PlayerInteractionComponent.gd +++ b/COGITO/Components/PlayerInteractionComponent.gd @@ -218,8 +218,7 @@ func attempt_reload(): if equipped_wieldable_item.no_reload: return - # Round up the ammo calculation as the charges are floats, not ints - var ammo_needed: int = abs(ceili(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current)) + var ammo_needed: int = abs(equipped_wieldable_item.charge_max - equipped_wieldable_item.charge_current) if ammo_needed <= 0: print("Wieldable is fully charged.") return