From d9f3216793c9ee3e5e0e65ca3b3a9e2639fcbe3b Mon Sep 17 00:00:00 2001 From: teatov Date: Mon, 21 Oct 2024 22:57:34 +1000 Subject: [PATCH] fix offscreen units deselection --- scripts/globals/selection_manager.gd | 8 ++++++++ scripts/units/abstract/controlled_unit.gd | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/scripts/globals/selection_manager.gd b/scripts/globals/selection_manager.gd index 21b265e..f1a31ac 100644 --- a/scripts/globals/selection_manager.gd +++ b/scripts/globals/selection_manager.gd @@ -1,6 +1,8 @@ extends Node ## Handles selection of units. +signal deselect + const MIN_DRAG_DISTANCE: float = 15 const UNIT_SELECT_OFFSET: float = 0.25 @@ -50,6 +52,7 @@ func _input(event: InputEvent) -> void: _selection_rect.size = Vector2.ZERO elif selecting: _set_selection_state(false) + deselect.emit() if event is InputEventMouseMotion: if _mouse_pressed: @@ -73,6 +76,11 @@ func remove_unit_from_visible(unit: Unit) -> void: _visible_units.erase(unit_id) +func is_unit_visible(unit: Unit) -> bool: + var unit_id := unit.get_instance_id() + return _visible_units.keys().has(unit_id) + + func _handle_advance_anim_step() -> void: var remapped_unclamped := remap( _visible_units.size(), diff --git a/scripts/units/abstract/controlled_unit.gd b/scripts/units/abstract/controlled_unit.gd index d998487..f1c766f 100644 --- a/scripts/units/abstract/controlled_unit.gd +++ b/scripts/units/abstract/controlled_unit.gd @@ -28,6 +28,7 @@ func _init() -> void: func _ready() -> void: assert(selection_indicator != null, "selection_indicator missing!") nav_agent.navigation_finished.connect(_on_nav_agent_navigation_finished) + SelectionManager.deselect.connect(_on_selection_manager_deselect) super._ready() @@ -98,3 +99,8 @@ func _click_raycast(mouse_pos: Vector2) -> Vector3: func _on_nav_agent_navigation_finished() -> void: _wandering_center = nav_agent.get_final_position() + + +func _on_selection_manager_deselect() -> void: + if _selected and not SelectionManager.is_unit_visible(self): + set_selected(false)