From b1ab4cc5c7c2d4fd9dc248689089e2ff30d5dfea Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 14 Apr 2024 23:29:29 +0200 Subject: [PATCH] Create `HuntTarget` behaviour --- behaviours/hunt_target.gd | 50 +++++++++++++++++++++++++++++++++++++ behaviours/hunt_target.tscn | 17 +++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 behaviours/hunt_target.gd create mode 100644 behaviours/hunt_target.tscn diff --git a/behaviours/hunt_target.gd b/behaviours/hunt_target.gd new file mode 100644 index 0000000..de97456 --- /dev/null +++ b/behaviours/hunt_target.gd @@ -0,0 +1,50 @@ +extends Node2D +class_name HuntTarget + + +signal target_selected(target: Node2D) +signal target_abandoned(target: Node2D) + + +@export var tag: StringName: + get: + return tag + set(value): + tag = value + hunter.tag = value + +@export var give_up_secs: float = 5.0 + +@onready var give_up_timer: Timer = $"GiveUpTimer" +@onready var hunter: Hunter = $"Hunter" + +var target: Node2D = null + + +func pick_new_target(): + var idx = Random.rng.randi_range(0, len(hunter.possible_targets) - 1) + target = hunter.possible_targets[idx] + target_selected.emit(target) + give_up_timer.start(give_up_secs) + +func _ready(): + hunter.tag = tag + +func _on_hunter_tracked(_body: Node2D): + if target == null: + pick_new_target() + +func _on_hunter_untracked(body: Node2D): + if body == target: + target = null + pick_new_target() + +func _on_give_up_timer_timeout() -> void: + target = null + pick_new_target() + +func _on_target_selected(body: Node2D) -> void: + Log.p(self, "Target selected: %s" % body) + +func _on_target_abandoned(body: Node2D) -> void: + Log.p(self, "Target abandoned: %s" % body) diff --git a/behaviours/hunt_target.tscn b/behaviours/hunt_target.tscn new file mode 100644 index 0000000..31b6aa4 --- /dev/null +++ b/behaviours/hunt_target.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://dxmodn8mbvw0i"] + +[ext_resource type="Script" path="res://behaviours/hunt_target.gd" id="1_1ex7u"] +[ext_resource type="PackedScene" uid="uid://ctpn4hvkhxoi3" path="res://behaviours/hunter.tscn" id="2_vjdtc"] + +[node name="HuntTarget" type="Node2D"] +script = ExtResource("1_1ex7u") + +[node name="Hunter" parent="." instance=ExtResource("2_vjdtc")] + +[node name="GiveUpTimer" type="Timer" parent="."] + +[connection signal="target_abandoned" from="." to="." method="_on_target_abandoned"] +[connection signal="target_selected" from="." to="." method="_on_target_selected"] +[connection signal="tracked" from="Hunter" to="." method="_on_hunter_tracked"] +[connection signal="untracked" from="Hunter" to="." method="_on_hunter_untracked"] +[connection signal="timeout" from="GiveUpTimer" to="." method="_on_give_up_timer_timeout"]