From d4ddd4e49cda9232ed677d323014430581ea033e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 14 Apr 2024 00:26:24 +0200 Subject: [PATCH] Create `SkitterFromMouse` behaviour --- behaviours/skitter_from_mouse.gd | 67 ++++++++++++++++++++++++++++++ behaviours/skitter_from_mouse.tscn | 24 +++++++++++ 2 files changed, 91 insertions(+) create mode 100644 behaviours/skitter_from_mouse.gd create mode 100644 behaviours/skitter_from_mouse.tscn diff --git a/behaviours/skitter_from_mouse.gd b/behaviours/skitter_from_mouse.gd new file mode 100644 index 0000000..52676ba --- /dev/null +++ b/behaviours/skitter_from_mouse.gd @@ -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.") + diff --git a/behaviours/skitter_from_mouse.tscn b/behaviours/skitter_from_mouse.tscn new file mode 100644 index 0000000..354c3ed --- /dev/null +++ b/behaviours/skitter_from_mouse.tscn @@ -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"]