2024-04-13 22:26:24 +00:00
|
|
|
extends Node2D
|
|
|
|
class_name SkitterFromMouse
|
|
|
|
|
|
|
|
|
|
|
|
signal move(movement: Vector2)
|
|
|
|
signal scared
|
|
|
|
signal calmed
|
|
|
|
|
|
|
|
|
|
|
|
@export_range(1, 5, 1) var speed := 2.0
|
|
|
|
@export_range(1, 5, 1) var scare_secs := 1.0
|
|
|
|
@export var directions: Array[Vector2] = []
|
|
|
|
|
|
|
|
@onready var calm_timer: Timer = $"CalmTimer"
|
2024-04-14 01:49:28 +00:00
|
|
|
@onready var hover_detector: HoverDetector = $"HoverDetector"
|
2024-04-13 22:26:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
enum State { CALM, SCARED }
|
|
|
|
|
2024-04-14 01:49:28 +00:00
|
|
|
var state: State = State.CALM
|
|
|
|
|
|
|
|
|
|
|
|
func calm():
|
|
|
|
state = State.CALM
|
|
|
|
calmed.emit()
|
|
|
|
|
|
|
|
func scare():
|
|
|
|
state = State.SCARED
|
|
|
|
calm_timer.start(scare_secs)
|
|
|
|
scared.emit()
|
2024-04-13 22:26:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
if not directions:
|
|
|
|
Log.w(self, "No directions defined, object won't skitter.")
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
match state:
|
|
|
|
State.SCARED:
|
2024-04-14 01:49:28 +00:00
|
|
|
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)
|
2024-04-13 22:26:24 +00:00
|
|
|
|
|
|
|
func _on_calm_timer_timeout() -> void:
|
2024-04-14 01:49:28 +00:00
|
|
|
if hover_detector.mouse_inside:
|
|
|
|
scare()
|
2024-04-13 22:26:24 +00:00
|
|
|
else:
|
2024-04-14 01:49:28 +00:00
|
|
|
calm()
|
|
|
|
|
|
|
|
func _on_hover_detector_mouse_entered() -> void:
|
|
|
|
scare()
|
2024-04-13 22:26:24 +00:00
|
|
|
|
|
|
|
func _on_move(movement: Vector2) -> void:
|
|
|
|
Log.p(self, "Moving by: %s" % movement)
|
|
|
|
|
|
|
|
func _on_scared() -> void:
|
|
|
|
Log.p(self, "Scared!")
|
|
|
|
|
|
|
|
func _on_calmed() -> void:
|
|
|
|
Log.p(self, "Calmed down.")
|