1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-23 00:24:21 +00:00
hella-farm/behaviours/skitter_from_mouse.gd

71 lines
1.5 KiB
GDScript3
Raw Normal View History

2024-04-13 22:26:24 +00:00
extends Node2D
class_name SkitterFromMouse
signal move(movement: Vector2)
signal scared
signal calmed
2024-04-15 23:00:49 +00:00
@export var state_holder: StateHolder;
@export_range(1, 500, 1) var speed: float = 300.0
@export_range(0.1, 5, 0.1) var scare_secs: float = 0.2
2024-04-13 22:26:24 +00:00
@export var directions: Array[Vector2] = []
@onready var calm_timer: Timer = $"CalmTimer"
@onready var scare_area: HoverDetector = $"ScareArea"
2024-04-13 22:26:24 +00:00
var direction := Vector2.ZERO
2024-04-15 23:00:49 +00:00
const SCARED_PRIORITY = 50
func recheck():
2024-04-15 23:00:49 +00:00
calm_timer.stop()
if scare_area.mouse_inside:
scare()
else:
calm()
func calm():
2024-04-15 23:00:49 +00:00
if state_holder.state == 'scared':
state_holder.reset_state()
calmed.emit()
func scare():
2024-04-15 23:00:49 +00:00
if not state_holder.propose_state('scared', SCARED_PRIORITY, recheck):
return
# Pick a direction
var idx := Random.rng.randi_range(0, len(directions) - 1)
direction = directions[idx]
# Start the timeout for calming down
calm_timer.start(scare_secs)
scared.emit()
2024-04-13 22:26:24 +00:00
func _ready():
2024-04-15 23:00:49 +00:00
if state_holder == null:
state_holder = StateHolder.get_default(self)
2024-04-13 22:26:24 +00:00
if not directions:
Log.w(self, "No directions defined, object won't skitter.")
func _physics_process(delta: float) -> void:
2024-04-15 23:00:49 +00:00
if state_holder.state == 'scared':
if len(directions) > 0:
var movement: Vector2 = direction * delta * speed
move.emit(movement)
2024-04-13 22:26:24 +00:00
func _on_calm_timer_timeout() -> void:
recheck()
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.")