Skip to content

Commit

Permalink
Refactoring after merging #96
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jan 6, 2024
1 parent bcd1572 commit 06dfd98
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
18 changes: 9 additions & 9 deletions source/match/handlers/SelectionHandler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ extends Node3D

@export var rectangular_selection_3d: NodePath

var double_click_last_unit = null
var double_click_timer = 0

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 Down Expand Up @@ -120,17 +120,17 @@ func _on_selection_finished(topdown_polygon_2d):


func _on_unit_selected(unit):
if Time.get_ticks_msec() <= double_click_timer + 50:
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()
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()
_double_click_last_unit = unit
_double_click_timer = Time.get_ticks_msec()


func _double_click(unit):
Expand Down
26 changes: 11 additions & 15 deletions source/match/units/Unit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ var attack_damage = null
var attack_interval = null
var attack_range = null
var attack_domains = []
var radius = null:
set = _ignore,
var radius:
get = _get_radius
var movement_domain = null:
set = _ignore,
var movement_domain:
get = _get_movement_domain
var movement_speed = null:
set = _ignore,
var movement_speed:
get = _get_movement_speed
var sight_range = null
var player:
Expand All @@ -39,11 +36,7 @@ var global_position_yless:
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
get = _get_type

var _action_locked = false

Expand All @@ -62,10 +55,6 @@ func is_revealing():
return is_in_group("revealed_units") and visible


func _ignore(_value):
pass


func _set_hp(value):
hp = max(0, value)
hp_changed.emit()
Expand Down Expand Up @@ -130,6 +119,13 @@ func _set_action(action_node):
action_changed.emit(action)


func _get_type():
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


func _teardown_current_action():
if action != null and action.is_inside_tree():
action.queue_free()
Expand Down
3 changes: 1 addition & 2 deletions source/match/units/actions/AttackingWhileInRange.gd
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ func _hit_target():


func _teardown_if_out_of_range():
var self_position_yless = _unit.global_position * Vector3(1, 0, 1)
if (
self_position_yless.distance_to(_target_unit.global_position * Vector3(1, 0, 1))
_unit.global_position_yless.distance_to(_target_unit.global_position_yless)
> _unit.attack_range
):
queue_free()
Expand Down
3 changes: 1 addition & 2 deletions source/match/units/actions/AutoAttacking.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ func _to_string():


func _target_in_range():
var self_position_yless = _unit.global_position * Vector3(1, 0, 1)
return (
self_position_yless.distance_to(_target_unit.global_position * Vector3(1, 0, 1))
_unit.global_position_yless.distance_to(_target_unit.global_position_yless)
<= _unit.attack_range
)

Expand Down
3 changes: 1 addition & 2 deletions source/match/units/actions/FollowingToReachDistance.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func _refresh():


func _teardown_if_distance_reached():
var self_position_yless = _unit.global_position * Vector3(1, 0, 1)
if (
self_position_yless.distance_to(_target_unit.global_position * Vector3(1, 0, 1))
_unit.global_position_yless.distance_to(_target_unit.global_position_yless)
<= _distance_to_reach
):
queue_free()
Expand Down
22 changes: 15 additions & 7 deletions source/match/units/actions/WaitingForTargets.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ func _attack_unit(unit):
func _on_timer_timeout():
var units_to_attack = _get_units_to_attack()
if not units_to_attack.is_empty():
var dist = _unit.sight_range
var target_unit = units_to_attack[0]
for unit in units_to_attack:
if unit.global_position_yless.distance_to(_unit.global_position_yless) < dist:
target_unit = unit
dist = unit.global_position_yless.distance_to(_unit.global_position_yless)
_attack_unit(target_unit)
_attack_unit(_pick_closest_unit(units_to_attack, _unit))


func _on_attack_finished():
Expand All @@ -67,3 +61,17 @@ func _on_attack_finished():
_sub_action = null
_unit.action_updated.emit()
_timer.timeout.connect(_on_timer_timeout)


static func _pick_closest_unit(units, unit):
assert(not units.is_empty())
var distance_to_closest_unit = unit.global_position_yless.distance_to(
units[0].global_position_yless
)
var closest_unit = units[0]
for unit_to_check in units:
var distance = unit.global_position_yless.distance_to(unit_to_check.global_position_yless)
if distance < distance_to_closest_unit:
distance_to_closest_unit = distance
closest_unit = unit_to_check
return closest_unit

0 comments on commit 06dfd98

Please sign in to comment.