mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-22 16:14:22 +00:00
Add a HoverDetector
acting as CaptureArea
to MoveTowardsMouse
This commit is contained in:
parent
ff0c49cf07
commit
ed8e09bd60
2 changed files with 22 additions and 5 deletions
|
@ -3,6 +3,7 @@ class_name MoveTowardsMouse
|
||||||
|
|
||||||
|
|
||||||
signal move(movement: Vector2)
|
signal move(movement: Vector2)
|
||||||
|
signal captured
|
||||||
|
|
||||||
|
|
||||||
@export_range(-5, 5, 1) var speed := 1.0
|
@export_range(-5, 5, 1) var speed := 1.0
|
||||||
|
@ -10,6 +11,11 @@ signal move(movement: Vector2)
|
||||||
@onready var viewport: Viewport = get_viewport()
|
@onready var viewport: Viewport = get_viewport()
|
||||||
|
|
||||||
|
|
||||||
|
enum State { STILL, CAPTURED }
|
||||||
|
|
||||||
|
var state: State = State.CAPTURED
|
||||||
|
|
||||||
|
|
||||||
func get_relative_mouse_position():
|
func get_relative_mouse_position():
|
||||||
var viewport_position: Vector2 = viewport.position
|
var viewport_position: Vector2 = viewport.position
|
||||||
var viewport_mouse_position: Vector2 = viewport.get_mouse_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:
|
func _physics_process(delta: float) -> void:
|
||||||
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
match state:
|
||||||
var movement: Vector2 = direction * delta * speed
|
State.CAPTURED:
|
||||||
Log.p(self, "Moving: %s" % movement)
|
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
||||||
move.emit(movement)
|
var movement: Vector2 = direction * delta * speed
|
||||||
|
move.emit(movement)
|
||||||
|
|
||||||
|
func _on_capture_area_mouse_entered() -> void:
|
||||||
|
state = State.CAPTURED
|
||||||
|
captured.emit()
|
||||||
|
|
|
@ -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="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"]
|
[node name="MoveTowardsMouse" type="Node2D"]
|
||||||
script = ExtResource("1_nbja1")
|
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"]
|
||||||
|
|
Loading…
Reference in a new issue