diff --git a/behaviours/move_towards.gd b/behaviours/move_towards.gd new file mode 100644 index 0000000..952da47 --- /dev/null +++ b/behaviours/move_towards.gd @@ -0,0 +1,44 @@ +extends Node2D +class_name MoveTowards + + +signal move(movement: Vector2) +signal detached +signal captured + + +@export var speed: float = 100.0 +@export var can_detach: bool = false + + +enum State { DETACHED, CAPTURED } + +var state: State = State.DETACHED + +func get_followed_global_position(): + return Vector2.ZERO # OVERRIDE ME! + + +func get_followed_mouse_position(): + var global_followed_position: Vector2 = get_followed_global_position() + var relative_followed_position: Vector2 = global_followed_position - global_position + return relative_followed_position + + +func _physics_process(delta: float) -> void: + match state: + State.CAPTURED: + var relative_followed_position: Vector2 = get_followed_mouse_position() + var direction: Vector2 = position.direction_to(relative_followed_position) + var actual_speed: float = min(delta * speed, relative_followed_position.length()) # Don't overshoot. + var movement: Vector2 = direction * actual_speed + move.emit(movement) + +func _on_capture_area_mouse_entered() -> void: + state = State.CAPTURED + captured.emit() + +func _on_capture_area_mouse_exited() -> void: + if can_detach: + state = State.DETACHED + detached.emit() diff --git a/behaviours/move_towards_mouse.gd b/behaviours/move_towards_mouse.gd index fc208e5..4abc5de 100644 --- a/behaviours/move_towards_mouse.gd +++ b/behaviours/move_towards_mouse.gd @@ -1,43 +1,8 @@ -extends Node2D +extends MoveTowards class_name MoveTowardsMouse - -signal move(movement: Vector2) -signal detached -signal captured - - -@export var speed: float = 100.0 -@export var can_detach: bool = false - @onready var game := MainGame.get_ancestor(self) -enum State { DETACHED, CAPTURED } - -var state: State = State.DETACHED - - -func get_relative_mouse_position(): - var global_mouse_position: Vector2 = game.camera.get_global_mouse_position() - var relative_mouse_position: Vector2 = global_mouse_position - global_position - return relative_mouse_position - - -func _physics_process(delta: float) -> void: - match state: - State.CAPTURED: - var relative_mouse_position: Vector2 = get_relative_mouse_position() - var direction: Vector2 = position.direction_to(relative_mouse_position) - var actual_speed: float = min(delta * speed, relative_mouse_position.length()) # Don't overshoot. - var movement: Vector2 = direction * actual_speed - move.emit(movement) - -func _on_capture_area_mouse_entered() -> void: - state = State.CAPTURED - captured.emit() - -func _on_capture_area_mouse_exited() -> void: - if can_detach: - state = State.DETACHED - detached.emit() +func get_followed_global_position(): + return game.camera.get_global_mouse_position() diff --git a/behaviours/move_towards_target.gd b/behaviours/move_towards_target.gd new file mode 100644 index 0000000..eadfaf2 --- /dev/null +++ b/behaviours/move_towards_target.gd @@ -0,0 +1,8 @@ +extends MoveTowards +class_name MoveTowardsTarget + +@export var target: Node2D + + +func get_followed_global_position(): + return target.global_position diff --git a/behaviours/move_towards_target.tscn b/behaviours/move_towards_target.tscn new file mode 100644 index 0000000..0a18f6e --- /dev/null +++ b/behaviours/move_towards_target.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=3 uid="uid://c7b4v6h7a3dy6"] + +[ext_resource type="Script" path="res://behaviours/move_towards_target.gd" id="1_703gy"] +[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/hover_detector.tscn" id="2_nq6qb"] + +[node name="MoveTowardsTarget" type="Node2D"] +script = ExtResource("1_703gy") + +[node name="CaptureArea" parent="." instance=ExtResource("2_nq6qb")] + +[connection signal="mouse_entered" from="CaptureArea" to="." method="_on_capture_area_mouse_entered"] +[connection signal="mouse_exited" from="CaptureArea" to="." method="_on_capture_area_mouse_exited"]