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

Add a HoverDetector acting as CaptureArea to MoveTowardsMouse

This commit is contained in:
Steffo 2024-04-14 03:55:57 +02:00
parent ff0c49cf07
commit ed8e09bd60
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
2 changed files with 22 additions and 5 deletions

View file

@ -3,6 +3,7 @@ class_name MoveTowardsMouse
signal move(movement: Vector2)
signal captured
@export_range(-5, 5, 1) var speed := 1.0
@ -10,6 +11,11 @@ signal move(movement: Vector2)
@onready var viewport: Viewport = get_viewport()
enum State { STILL, CAPTURED }
var state: State = State.CAPTURED
func get_relative_mouse_position():
var viewport_position: Vector2 = viewport.position
var viewport_mouse_position: Vector2 = viewport.get_mouse_position()
@ -19,7 +25,12 @@ func get_relative_mouse_position():
func _physics_process(delta: float) -> void:
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
var movement: Vector2 = direction * delta * speed
Log.p(self, "Moving: %s" % movement)
move.emit(movement)
match state:
State.CAPTURED:
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
var movement: Vector2 = direction * delta * speed
move.emit(movement)
func _on_capture_area_mouse_entered() -> void:
state = State.CAPTURED
captured.emit()

View file

@ -1,6 +1,12 @@
[gd_scene load_steps=2 format=3 uid="uid://bvrxvrjlo5130"]
[gd_scene load_steps=3 format=3 uid="uid://bvrxvrjlo5130"]
[ext_resource type="Script" path="res://behaviours/move_towards_mouse.gd" id="1_nbja1"]
[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/hover_detector.tscn" id="2_qru2e"]
[node name="MoveTowardsMouse" type="Node2D"]
script = ExtResource("1_nbja1")
speed = null
[node name="CaptureArea" parent="." instance=ExtResource("2_qru2e")]
[connection signal="mouse_entered" from="CaptureArea" to="." method="_on_trigger_area_mouse_entered"]