Skip to content

Commit

Permalink
Select multiple units by double clicking
Browse files Browse the repository at this point in the history
Give units a property "type" which is the resource name (e.g. 'tank')
  • Loading branch information
kbendler authored and Scony committed Jan 6, 2024
1 parent bb102d3 commit a7e986c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/match/handlers/SelectionHandler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ extends Node3D
var _rectangular_selection_3d = null
var _highlighted_units = Utils.Set.new()

var double_click_last_unit = null
var double_click_timer = 0

func _ready():
_rectangular_selection_3d = get_node_or_null(rectangular_selection_3d)
Expand All @@ -13,6 +15,7 @@ func _ready():
_rectangular_selection_3d.started.connect(_on_selection_started)
_rectangular_selection_3d.interrupted.connect(_on_selection_interrupted)
_rectangular_selection_3d.finished.connect(_on_selection_finished)
MatchSignals.unit_selected.connect(_on_unit_selected)


func _force_highlight(units_to_highlight):
Expand Down Expand Up @@ -113,3 +116,28 @@ func _on_selection_finished(topdown_polygon_2d):
)
)
_select_units(units_to_select)

func _on_unit_selected(unit):
if Time.get_ticks_msec() <= double_click_timer + 50:
return
if not unit.is_in_group("controlled_units"):
return
if unit == double_click_last_unit and Time.get_ticks_msec() <= double_click_timer + 600:
double_click_last_unit = unit
double_click_timer = Time.get_ticks_msec()
_double_click(unit)
else:
double_click_last_unit = unit
double_click_timer = Time.get_ticks_msec()



func _double_click(unit):
var units_to_select = Utils.Set.new();
var camera = get_viewport().get_camera_3d()
for u in get_tree().get_nodes_in_group("controlled_units"):
if not u.visible or not camera.is_position_in_frustum(u.global_position):
continue
if u.type == unit.type:
units_to_select.add(u)
_select_units(units_to_select)
7 changes: 7 additions & 0 deletions source/match/units/Unit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ var location:
get:
return global_position * Vector3(1, 0, 1)

var type:
get:
var unit_script_path = get_script().resource_path
var unit_file_name = unit_script_path.substr(unit_script_path.rfind("/") + 1)
var unit_name = unit_file_name.split(".")[0]
return unit_name

@onready var _match = find_parent("Match")


Expand Down

0 comments on commit a7e986c

Please sign in to comment.