Refactored unit
This commit is contained in:
@@ -4,13 +4,13 @@ const SPEED = 192.0
|
||||
|
||||
@export var dl_map: CombatMap
|
||||
|
||||
signal combat_requested(attacker: Unit, defender: Unit)
|
||||
signal combat_requested(attacker: DeployedUnit, defender: DeployedUnit)
|
||||
signal mouse_grid_changed(coords: Vector2i)
|
||||
signal camera_drag(delta: Vector2)
|
||||
|
||||
var input_disabled := false
|
||||
|
||||
var _selected_unit: Unit = null
|
||||
var _selected_unit: DeployedUnit = null
|
||||
var _target_pos: Vector2
|
||||
var _goal_pos: Vector2
|
||||
var _moving := false
|
||||
@@ -24,19 +24,19 @@ const DRAG_THRESHOLD := 8.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
for unit: Unit in get_tree().get_nodes_in_group("units"):
|
||||
unit.unit_died.connect(_on_unit_died)
|
||||
for deployed: DeployedUnit in get_tree().get_nodes_in_group("deployed_units"):
|
||||
deployed.unit_died.connect(_on_unit_died)
|
||||
get_tree().node_added.connect(_on_node_added)
|
||||
|
||||
|
||||
func _on_node_added(node: Node) -> void:
|
||||
if node is Unit and node.is_in_group("units"):
|
||||
if node is DeployedUnit and node.is_in_group("deployed_units"):
|
||||
if not node.unit_died.is_connected(_on_unit_died):
|
||||
node.unit_died.connect(_on_unit_died)
|
||||
|
||||
|
||||
func _on_unit_died(unit: Unit) -> void:
|
||||
if _selected_unit == unit:
|
||||
func _on_unit_died(deployed: DeployedUnit) -> void:
|
||||
if _selected_unit == deployed:
|
||||
_selected_unit = null
|
||||
_moving = false
|
||||
|
||||
@@ -146,22 +146,22 @@ func _handle_left_click(screen_pos: Vector2) -> void:
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
|
||||
func _select_unit(unit: Unit) -> void:
|
||||
func _select_unit(deployed: DeployedUnit) -> void:
|
||||
if _selected_unit:
|
||||
_selected_unit.set_selected(false)
|
||||
_selected_unit = unit
|
||||
_selected_unit = deployed
|
||||
_selected_unit.set_selected(true)
|
||||
_goal_pos = _selected_unit.position
|
||||
_target_pos = _selected_unit.position
|
||||
_moving = false
|
||||
|
||||
|
||||
func _get_unit_at(world_pos: Vector2) -> Unit:
|
||||
func _get_unit_at(world_pos: Vector2) -> DeployedUnit:
|
||||
var snapped_coords := dl_map.snap_to_grid(world_pos)
|
||||
for unit: Unit in get_tree().get_nodes_in_group("units"):
|
||||
if not unit.is_alive():
|
||||
for deployed: DeployedUnit in get_tree().get_nodes_in_group("deployed_units"):
|
||||
if not deployed.is_alive():
|
||||
continue
|
||||
var unit_snapped := dl_map.snap_to_grid(unit.global_position)
|
||||
var unit_snapped := dl_map.snap_to_grid(deployed.global_position)
|
||||
if unit_snapped == snapped_coords:
|
||||
return unit
|
||||
return deployed
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user