mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-22 08:04:23 +00:00
Use HoverDetector
in SkitterFromMouse
This commit is contained in:
parent
260a39371c
commit
6dde00120d
2 changed files with 26 additions and 38 deletions
|
@ -12,24 +12,22 @@ signal calmed
|
||||||
@export var directions: Array[Vector2] = []
|
@export var directions: Array[Vector2] = []
|
||||||
|
|
||||||
@onready var calm_timer: Timer = $"CalmTimer"
|
@onready var calm_timer: Timer = $"CalmTimer"
|
||||||
|
@onready var hover_detector: HoverDetector = $"HoverDetector"
|
||||||
|
|
||||||
|
|
||||||
var mouse_inside: bool = false
|
|
||||||
|
|
||||||
enum State { CALM, SCARED }
|
enum State { CALM, SCARED }
|
||||||
|
|
||||||
var state: State = State.CALM:
|
var state: State = State.CALM
|
||||||
get:
|
|
||||||
return state
|
|
||||||
set(value):
|
func calm():
|
||||||
if state != value:
|
state = State.CALM
|
||||||
state = value
|
calmed.emit()
|
||||||
match state:
|
|
||||||
State.CALM:
|
func scare():
|
||||||
calmed.emit()
|
state = State.SCARED
|
||||||
State.SCARED:
|
calm_timer.start(scare_secs)
|
||||||
calm_timer.start(scare_secs)
|
scared.emit()
|
||||||
scared.emit()
|
|
||||||
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
|
@ -39,22 +37,20 @@ func _ready():
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
match state:
|
match state:
|
||||||
State.SCARED:
|
State.SCARED:
|
||||||
var idx := Random.rng.randi_range(0, len(directions))
|
if len(directions) > 0:
|
||||||
var direction: Vector2 = directions[idx]
|
var idx := Random.rng.randi_range(0, len(directions))
|
||||||
var movement: Vector2 = direction * delta * speed
|
var direction: Vector2 = directions[idx]
|
||||||
move.emit(movement)
|
var movement: Vector2 = direction * delta * speed
|
||||||
|
move.emit(movement)
|
||||||
func _on_scare_area_mouse_entered() -> void:
|
|
||||||
mouse_inside = true
|
|
||||||
|
|
||||||
func _on_scare_area_mouse_exited() -> void:
|
|
||||||
mouse_inside = false
|
|
||||||
|
|
||||||
func _on_calm_timer_timeout() -> void:
|
func _on_calm_timer_timeout() -> void:
|
||||||
if mouse_inside:
|
if hover_detector.mouse_inside:
|
||||||
calm_timer.start(scare_secs)
|
scare()
|
||||||
else:
|
else:
|
||||||
state = State.CALM
|
calm()
|
||||||
|
|
||||||
|
func _on_hover_detector_mouse_entered() -> void:
|
||||||
|
scare()
|
||||||
|
|
||||||
func _on_move(movement: Vector2) -> void:
|
func _on_move(movement: Vector2) -> void:
|
||||||
Log.p(self, "Moving by: %s" % movement)
|
Log.p(self, "Moving by: %s" % movement)
|
||||||
|
@ -64,4 +60,3 @@ func _on_scared() -> void:
|
||||||
|
|
||||||
func _on_calmed() -> void:
|
func _on_calmed() -> void:
|
||||||
Log.p(self, "Calmed down.")
|
Log.p(self, "Calmed down.")
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,17 @@
|
||||||
[gd_scene load_steps=3 format=3 uid="uid://c1wqgyakaultt"]
|
[gd_scene load_steps=3 format=3 uid="uid://c1wqgyakaultt"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://behaviours/skitter_from_mouse.gd" id="1_ftcf8"]
|
[ext_resource type="Script" path="res://behaviours/skitter_from_mouse.gd" id="1_ftcf8"]
|
||||||
|
[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/hover_detector.tscn" id="2_cuaq0"]
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_2d7x6"]
|
|
||||||
radius = 30.0
|
|
||||||
|
|
||||||
[node name="SkitterFromMouse" type="Node2D"]
|
[node name="SkitterFromMouse" type="Node2D"]
|
||||||
script = ExtResource("1_ftcf8")
|
script = ExtResource("1_ftcf8")
|
||||||
|
|
||||||
[node name="ScareArea" type="Area2D" parent="."]
|
[node name="HoverDetector" parent="." instance=ExtResource("2_cuaq0")]
|
||||||
|
|
||||||
[node name="Shape" type="CollisionShape2D" parent="ScareArea"]
|
|
||||||
shape = SubResource("CircleShape2D_2d7x6")
|
|
||||||
debug_color = Color(0.72549, 0.47451, 0, 0.419608)
|
|
||||||
|
|
||||||
[node name="CalmTimer" type="Timer" parent="."]
|
[node name="CalmTimer" type="Timer" parent="."]
|
||||||
|
|
||||||
[connection signal="calmed" from="." to="." method="_on_calmed"]
|
[connection signal="calmed" from="." to="." method="_on_calmed"]
|
||||||
[connection signal="move" from="." to="." method="_on_move"]
|
[connection signal="move" from="." to="." method="_on_move"]
|
||||||
[connection signal="scared" from="." to="." method="_on_scared"]
|
[connection signal="scared" from="." to="." method="_on_scared"]
|
||||||
[connection signal="mouse_entered" from="ScareArea" to="." method="_on_scare_area_mouse_entered"]
|
[connection signal="mouse_entered" from="HoverDetector" to="." method="_on_hover_detector_mouse_entered"]
|
||||||
[connection signal="mouse_exited" from="ScareArea" to="." method="_on_scare_area_mouse_exited"]
|
|
||||||
[connection signal="timeout" from="CalmTimer" to="." method="_on_calm_timer_timeout"]
|
[connection signal="timeout" from="CalmTimer" to="." method="_on_calm_timer_timeout"]
|
||||||
|
|
Loading…
Reference in a new issue