Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make waiting units more aggressive and allows double clicking #96

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
11 changes: 11 additions & 0 deletions source/match/units/Unit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ var action = null:

var _action_locked = false

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
13 changes: 9 additions & 4 deletions source/match/units/actions/WaitingForTargets.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ func is_idle():


func _get_units_to_attack():
var self_position_yless = _unit.global_position * Vector3(1, 0, 1)
return get_tree().get_nodes_in_group("units").filter(
func(unit): return (
unit.player != _unit.player
and unit.movement_domain in _unit.attack_domains
and (
self_position_yless.distance_to(unit.global_position * Vector3(1, 0, 1))
<= _unit.attack_range
_unit.location.distance_to(unit.location)
<= _unit.sight_range
)
)
)
Expand All @@ -53,7 +52,13 @@ func _attack_unit(unit):
func _on_timer_timeout():
var units_to_attack = _get_units_to_attack()
if not units_to_attack.is_empty():
_attack_unit(units_to_attack.pick_random())
var dist = _unit.sight_range
var target_unit = units_to_attack[0]
for unit in units_to_attack:
if unit.location.distance_to(_unit.location) < dist:
target_unit = unit
dist = unit.location.distance_to(_unit.location)
_attack_unit(target_unit)


func _on_attack_finished():
Expand Down
Loading