From fa502ba267ed24f4f6b681a0a54d146f72679c8e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 16 Apr 2024 00:20:21 +0200 Subject: [PATCH] Refactor `Hunter` into `EdibleTracker` --- behaviours/edible_tracker.gd | 34 ++++++++++++++++++++++++ behaviours/edible_tracker.svg | 39 ++++++++++++++++++++++++++++ behaviours/edible_tracker.svg.import | 37 ++++++++++++++++++++++++++ behaviours/edible_tracker.tscn | 11 ++++++++ behaviours/hunt_target.tscn | 2 +- behaviours/hunter.gd | 31 ---------------------- behaviours/hunter.tscn | 16 ------------ 7 files changed, 122 insertions(+), 48 deletions(-) create mode 100644 behaviours/edible_tracker.gd create mode 100644 behaviours/edible_tracker.svg create mode 100644 behaviours/edible_tracker.svg.import create mode 100644 behaviours/edible_tracker.tscn delete mode 100644 behaviours/hunter.gd delete mode 100644 behaviours/hunter.tscn diff --git a/behaviours/edible_tracker.gd b/behaviours/edible_tracker.gd new file mode 100644 index 0000000..bcf1bef --- /dev/null +++ b/behaviours/edible_tracker.gd @@ -0,0 +1,34 @@ +@icon("res://behaviours/edible_tracker.svg") +extends Area2D +class_name EdibleTracker + +## Keeps track of what [Edible]s are inside the area. + + +signal tracked(body: Node2D) +signal untracked(body: Node2D) + + +@export var acceptable_diets: Array[StringName] = [] + +var tracking: Array = [] + + +func _on_body_entered(body: Node2D) -> void: + var edibles: Array = body.find_children("Edible", "Edible", false, false) + for edible in edibles: + if edible.tag in acceptable_diets: + tracking.push_back(body) + tracked.emit(body) + +func _on_body_exited(body: Node2D) -> void: + if body in tracking: + tracking.erase(body) + untracked.emit(body) + + +func _on_tracked(body: Node2D) -> void: + Log.p(self, "Tracking a new target: %s" % body) + +func _on_untracked(body:Node2D) -> void: + Log.p(self, "Not tracking anymore target: %s" % body) diff --git a/behaviours/edible_tracker.svg b/behaviours/edible_tracker.svg new file mode 100644 index 0000000..34115c7 --- /dev/null +++ b/behaviours/edible_tracker.svg @@ -0,0 +1,39 @@ + + + + + + + diff --git a/behaviours/edible_tracker.svg.import b/behaviours/edible_tracker.svg.import new file mode 100644 index 0000000..b8c06f4 --- /dev/null +++ b/behaviours/edible_tracker.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://8443tt517pkv" +path="res://.godot/imported/edible_tracker.svg-9169f9c07be35f707b3c167799ba3a76.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://behaviours/edible_tracker.svg" +dest_files=["res://.godot/imported/edible_tracker.svg-9169f9c07be35f707b3c167799ba3a76.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/behaviours/edible_tracker.tscn b/behaviours/edible_tracker.tscn new file mode 100644 index 0000000..2d75fbd --- /dev/null +++ b/behaviours/edible_tracker.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=2 format=3 uid="uid://ctpn4hvkhxoi3"] + +[ext_resource type="Script" path="res://behaviours/edible_tracker.gd" id="1_vo18u"] + +[node name="EdibleTracker" type="Area2D"] +script = ExtResource("1_vo18u") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] +[connection signal="body_exited" from="." to="." method="_on_body_exited"] +[connection signal="tracked" from="." to="." method="_on_tracked"] +[connection signal="untracked" from="." to="." method="_on_untracked"] diff --git a/behaviours/hunt_target.tscn b/behaviours/hunt_target.tscn index 6d191d1..dc8bf57 100644 --- a/behaviours/hunt_target.tscn +++ b/behaviours/hunt_target.tscn @@ -1,7 +1,7 @@ [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"] +[ext_resource type="PackedScene" uid="uid://ctpn4hvkhxoi3" path="res://behaviours/edible_tracker.tscn" id="2_vjdtc"] [node name="HuntTarget" type="Node2D"] script = ExtResource("1_1ex7u") diff --git a/behaviours/hunter.gd b/behaviours/hunter.gd deleted file mode 100644 index 3a07ac5..0000000 --- a/behaviours/hunter.gd +++ /dev/null @@ -1,31 +0,0 @@ -extends Node2D -class_name Hunter - - -signal tracked(body: Node2D) -signal untracked(body: Node2D) - - -@export var tag: StringName - -var possible_targets: Array = [] - - -func _on_detection_area_body_entered(body: Node2D) -> void: - var edibles: Array = body.find_children("Edible", "Edible", false, false) - for edible in edibles: - if edible.tag == tag: - possible_targets.push_back(body) - tracked.emit(body) - -func _on_detection_area_body_exited(body: Node2D) -> void: - if body in possible_targets: - possible_targets.erase(body) - untracked.emit(body) - - -func _on_tracked(body: Node2D) -> void: - Log.p(self, "Tracking a new target: %s" % body) - -func _on_untracked(body:Node2D) -> void: - Log.p(self, "Not tracking anymore target: %s" % body) diff --git a/behaviours/hunter.tscn b/behaviours/hunter.tscn deleted file mode 100644 index 2c2c9e7..0000000 --- a/behaviours/hunter.tscn +++ /dev/null @@ -1,16 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://ctpn4hvkhxoi3"] - -[ext_resource type="Script" path="res://behaviours/hunter.gd" id="1_3bmd5"] - -[node name="Hunter" type="Node2D"] -script = ExtResource("1_3bmd5") - -[node name="DetectionArea" type="Area2D" parent="."] -collision_layer = 0 -collision_mask = 24 -monitorable = false - -[connection signal="tracked" from="." to="." method="_on_tracked"] -[connection signal="untracked" from="." to="." method="_on_untracked"] -[connection signal="body_entered" from="DetectionArea" to="." method="_on_detection_area_body_entered"] -[connection signal="body_exited" from="DetectionArea" to="." method="_on_detection_area_body_exited"]