mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-26 01:54:23 +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:
|
||||||
|
match state:
|
||||||
|
State.CAPTURED:
|
||||||
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
var direction: Vector2 = position.direction_to(get_relative_mouse_position())
|
||||||
var movement: Vector2 = direction * delta * speed
|
var movement: Vector2 = direction * delta * speed
|
||||||
Log.p(self, "Moving: %s" % movement)
|
|
||||||
move.emit(movement)
|
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