2024-04-16 02:29:34 +00:00
|
|
|
extends Move
|
2024-04-14 16:20:08 +00:00
|
|
|
class_name MoveTowards
|
|
|
|
|
|
|
|
|
2024-04-16 02:29:34 +00:00
|
|
|
## A [Move] that moves towards the [field position] of a [field target].
|
2024-04-14 16:20:08 +00:00
|
|
|
|
|
|
|
|
2024-04-17 03:26:13 +00:00
|
|
|
signal changed_target(new: Node2D)
|
2024-04-17 02:52:11 +00:00
|
|
|
|
|
|
|
|
2024-04-17 03:29:45 +00:00
|
|
|
@export var speed: float = 100.0
|
|
|
|
|
2024-04-17 02:52:11 +00:00
|
|
|
@export var target: Node2D = null:
|
|
|
|
get:
|
|
|
|
return target
|
|
|
|
set(value):
|
2024-04-17 03:26:13 +00:00
|
|
|
target = value
|
|
|
|
changed_target.emit(value)
|
2024-04-14 16:20:08 +00:00
|
|
|
|
|
|
|
|
2024-04-16 00:47:31 +00:00
|
|
|
func set_target(value: Node2D) -> void:
|
|
|
|
target = value
|
|
|
|
|
|
|
|
func clear_target() -> void:
|
|
|
|
target = null
|
|
|
|
|
2024-04-18 23:57:26 +00:00
|
|
|
func log_target() -> void:
|
|
|
|
Log.p(self, "Target: %s" % target)
|
|
|
|
|
2024-04-16 00:47:31 +00:00
|
|
|
|
2024-04-17 03:29:45 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2024-04-17 02:52:11 +00:00
|
|
|
if enabled:
|
|
|
|
if target:
|
|
|
|
var gap = target.global_position - global_position
|
|
|
|
var norm = gap.normalized()
|
2024-04-17 03:29:45 +00:00
|
|
|
move.emit(norm * delta * speed)
|
2024-04-17 02:52:11 +00:00
|
|
|
else:
|
|
|
|
move.emit(Vector2.ZERO)
|