1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-21 23:54:23 +00:00

Refactor MoveTowards so that it only handles deciding a direction

This commit is contained in:
Steffo 2024-04-16 01:57:22 +02:00
parent e2f4ad461e
commit d222e8c066
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 93 additions and 33 deletions

View file

@ -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)

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 512 512"
version="1.1"
id="svg1"
sodipodi:docname="move_towards.svg"
width="16"
height="16"
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25, custom)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs1" />
<sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:zoom="1.6464844"
inkscape:cx="223.8102"
inkscape:cy="236.56465"
inkscape:window-width="1920"
inkscape:window-height="1020"
inkscape:window-x="1280"
inkscape:window-y="32"
inkscape:window-maximized="1"
inkscape:current-layer="svg1" />
<!--! Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc. -->
<path
d="m 470.6125,278.6 c 12.5,-12.5 12.5,-32.8 0,-45.3 l -160,-160 c -12.5,-12.5 -32.8,-12.5 -45.3,0 -12.5,12.5 -12.5,32.8 0,45.3 l 105.5,105.4 h -306.8 c -17.7,0 -32,14.3 -32,32 0,17.7 14.3,32 32,32 h 306.7 l -105.3,105.4 c -12.5,12.5 -12.5,32.8 0,45.3 12.5,12.5 32.8,12.5 45.3,0 l 160,-160 z"
id="path1"
style="fill:#8da5f3" />
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -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

View file

@ -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")