1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-25 17:44:24 +00:00

Create SkitterFromMouse behaviour

This commit is contained in:
Steffo 2024-04-14 00:26:24 +02:00
parent 6939796cdf
commit d4ddd4e49c
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
2 changed files with 91 additions and 0 deletions

View file

@ -0,0 +1,67 @@
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"
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()
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:
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
func _on_calm_timer_timeout() -> void:
if mouse_inside:
calm_timer.start(scare_secs)
else:
state = State.CALM
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.")

View file

@ -0,0 +1,24 @@
[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
[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="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="timeout" from="CalmTimer" to="." method="_on_calm_timer_timeout"]