diff --git a/behaviours/move_towards.gd b/behaviours/move_towards.gd index 952da47..e0593a5 100644 --- a/behaviours/move_towards.gd +++ b/behaviours/move_towards.gd @@ -1,44 +1,22 @@ +@icon("res://behaviours/move_towards.svg") extends Node2D class_name MoveTowards -signal move(movement: Vector2) -signal detached -signal captured +## A node emitting the [signal move] signal each physics timestep to move towards the position of [field target]. -@export var speed: float = 100.0 -@export var can_detach: bool = false +signal move(norm: Vector2) -enum State { DETACHED, CAPTURED } - -var state: State = State.DETACHED - -func get_followed_global_position(): - return Vector2.ZERO # OVERRIDE ME! +@export var target: Node2D = null -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 _ready() -> void: + if target == null: + Log.w(self, "No target is set, no signals will be emitted.") - -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() +func _physics_process(_delta: float) -> void: + var gap = target.global_position - global_position + var norm = gap.normalized() + move.emit(norm) diff --git a/behaviours/move_towards.svg b/behaviours/move_towards.svg new file mode 100644 index 0000000..af25ecb --- /dev/null +++ b/behaviours/move_towards.svg @@ -0,0 +1,39 @@ + + + + + + + diff --git a/behaviours/move_towards.svg.import b/behaviours/move_towards.svg.import new file mode 100644 index 0000000..87cd989 --- /dev/null +++ b/behaviours/move_towards.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://d10wobjiuh1mm" +path="res://.godot/imported/move_towards.svg-f5d5c8fa66b43d0301cc029ecee0a291.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://behaviours/move_towards.svg" +dest_files=["res://.godot/imported/move_towards.svg-f5d5c8fa66b43d0301cc029ecee0a291.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/behaviours/move_towards.tscn b/behaviours/move_towards.tscn new file mode 100644 index 0000000..41b4c4a --- /dev/null +++ b/behaviours/move_towards.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://cml7rqvyfuagx"] + +[ext_resource type="Script" path="res://behaviours/move_towards.gd" id="1_256ue"] + +[node name="MoveTowards" type="Node2D"] +script = ExtResource("1_256ue")