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