From 0be8633a7e1d3165247df87ac9ff74d828cfbb83 Mon Sep 17 00:00:00 2001 From: LUCASTUCIOUS Date: Sat, 3 Aug 2024 22:42:06 +0200 Subject: [PATCH] sticker detection --- core/stickernode.gd | 44 ++++++++++++++----- maps/map1.tscn | 105 +++++++++++--------------------------------- 2 files changed, 58 insertions(+), 91 deletions(-) diff --git a/core/stickernode.gd b/core/stickernode.gd index 510f1cd..5509655 100644 --- a/core/stickernode.gd +++ b/core/stickernode.gd @@ -14,6 +14,13 @@ class_name Sticker extends Area2D @export var WorldSprite:Sprite2D ## Optional - The look of the object when grabbed. If empty, OutlineMat will be applied to WorldSprite @export var StickerSprite:Sprite2D +## Optional - The detection zone, if not the same as the sticker area +@export var DetectionArea:Area2D: + set(new_area): + if(new_area != self): + DetectionArea = new_area + else: + printerr("DetectionArea cannot be the sticker itself") @export_group("Material") @export var OutlineMat:ShaderMaterial = preload("res://shaders/shaderMaterial_Outline.tres") @export var Foiled:bool #TODO: Foil material and logic @@ -28,11 +35,14 @@ class_name Sticker extends Area2D var meta:PackedStringArray = ["sticker"] var detected_solids:Array -var detected_zone:Array +var detected_zones:Array #region Functions - +func _get_configuration_warnings(): + if (get_parent() == get_tree().get_edited_scene_root()): + return ["This node should have a parent"] + return [] func _init(): collision_layer = 2 @@ -40,21 +50,31 @@ func _init(): collision_mask = 61 set_meta("tags",meta) monitoring = true + if Engine.is_editor_hint(): + update_configuration_warnings() func _enter_tree(): set_meta("tags",meta) if (get_parent() is Sprite2D and WorldSprite == null): WorldSprite = get_parent() - + if Engine.is_editor_hint(): + update_configuration_warnings() func _ready(): if (get_parent() == get_tree().root): printerr("stickers should always have a parent") breakpoint - body_entered.connect(_on_body_entered) - body_exited.connect(_on_body_exited) - area_entered.connect(_on_area_entered) - area_exited.connect(_on_area_exited) + if (DetectionArea != null): + DetectionArea.area_entered.connect(_on_area_entered) + DetectionArea.area_exited.connect(_on_area_exited) + DetectionArea.body_entered.connect(_on_body_entered) + DetectionArea.body_exited.connect(_on_body_exited) + DetectionArea.collision_mask = 61 + else: + area_entered.connect(_on_area_entered) + area_exited.connect(_on_area_exited) + body_entered.connect(_on_body_entered) + body_exited.connect(_on_body_exited) # TODO : Solids and interactive zone detection area should not be the same as the sticker area. # IDEA: Need to export a child Area2D @@ -67,18 +87,18 @@ func _on_body_exited(body:Node2D): func _on_area_entered(area:Area2D): print("area entered",area) #TODO:Filter by type - detected_zone.append(area) + detected_zones.append(area) func _on_area_exited(area:Area2D): print("area exited",area) - detected_zone.erase(area) + detected_zones.erase(area) -func _process(delta): +func _process(_delta): pass ## When the sticker is released -func on_released(CastResult:Array=[]): +func on_released(_CastResult:Array=[]): print(self," released") - #TODO: Need a traceresult to know if it should stay in the stickerstate or be released in the world + #Need a traceresult to know if it should stay in the stickerstate or be released in the world if(detected_solids.size()==0): get_parent().top_level = false disable_ChildNodes_collisions(false) diff --git a/maps/map1.tscn b/maps/map1.tscn index 691c497..7f1bf8d 100644 --- a/maps/map1.tscn +++ b/maps/map1.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=32 format=3 uid="uid://wlqsvbqpcbh"] +[gd_scene load_steps=33 format=3 uid="uid://wlqsvbqpcbh"] [ext_resource type="Texture2D" uid="uid://cacwy4tka88k1" path="res://maps/map1.tres" id="1_pt5vq"] [ext_resource type="Texture2D" uid="uid://bm2nsrfle5nsd" path="res://textures/sprites/floor_door_grass_right1.tres" id="2_0jvyl"] @@ -15,6 +15,7 @@ [ext_resource type="Texture2D" uid="uid://dcgjlblm2rpy4" path="res://textures/2d_lights_and_shadows_neutral_point_light.webp" id="7_4swoj"] [ext_resource type="Texture2D" uid="uid://cup0xru4j84wj" path="res://textures/sprites/rocks_rock2.tres" id="7_mbwmw"] [ext_resource type="Texture2D" uid="uid://c6acjgu8jnrkl" path="res://textures/sprites/rocks_rock1.tres" id="8_bhcew"] +[ext_resource type="PackedScene" uid="uid://kvjctagkwivk" path="res://prefab/prefab_bush1.tscn" id="8_tag7x"] [ext_resource type="Texture2D" uid="uid://dnomlcslicb3k" path="res://textures/sprites/props_fire1.tres" id="10_2ugv3"] [ext_resource type="Texture2D" uid="uid://qsse8uwt06ns" path="res://textures/sprites/plants_bush2.tres" id="11_dibxq"] [ext_resource type="Texture2D" uid="uid://51ntd0qmjw5j" path="res://textures/sprites/props_woodbarrer.tres" id="14_n4lnm"] @@ -23,15 +24,15 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_yeof8"] size = Vector2(202.667, 60.3336) -[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_lfx7b"] -radius = 80.0 -height = 254.0 - [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_dqo8w"] resource_name = "TreeShape" radius = 70.0 height = 512.0 +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_lfx7b"] +radius = 80.0 +height = 254.0 + [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_b3366"] radius = 65.0 height = 184.05 @@ -138,25 +139,7 @@ top_level = true position = Vector2(78, -148) scale = Vector2(6.15, 6.15) -[node name="Bush1" type="Sprite2D" parent="."] -position = Vector2(-2161, 1140) -scale = Vector2(3.80334, 3.95089) -texture = ExtResource("4_m2p3o") -centered = false -offset = Vector2(-161.055, -179.785) -metadata/tags = ["sticker"] - -[node name="Sticker" type="Area2D" parent="Bush1" node_paths=PackedStringArray("WorldSprite")] -collision_layer = 2 -collision_mask = 29 -script = ExtResource("5_ek34p") -WorldSprite = NodePath("..") -metadata/tags = PackedStringArray("sticker") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Bush1/Sticker"] -position = Vector2(-4.46973, -71.6294) -rotation = 1.6 -shape = SubResource("CapsuleShape2D_lfx7b") +[node name="Bush1" parent="." instance=ExtResource("8_tag7x")] [node name="Tree2" type="Sprite2D" parent="."] position = Vector2(-2399, -55) @@ -168,7 +151,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Tree2" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -188,7 +171,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Tree5" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -208,7 +191,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Tree6" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -229,7 +212,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Tree4" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -249,7 +232,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Bush6" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -269,7 +252,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Bush7" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -289,7 +272,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Bush11" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -300,47 +283,6 @@ rotation = 1.6 shape = SubResource("CapsuleShape2D_lfx7b") metadata/_edit_lock_ = true -[node name="Bush9" type="Sprite2D" parent="."] -position = Vector2(1004, -2257) -scale = Vector2(3.80334, 3.95089) -texture = ExtResource("4_m2p3o") -centered = false -offset = Vector2(-161.055, -179.785) -metadata/tags = ["sticker"] - -[node name="Sticker" type="Area2D" parent="Bush9" node_paths=PackedStringArray("WorldSprite")] -collision_layer = 2 -collision_mask = 29 -script = ExtResource("5_ek34p") -WorldSprite = NodePath("..") -metadata/tags = PackedStringArray("sticker") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Bush9/Sticker"] -position = Vector2(-4.46976, -71.6294) -rotation = 1.6 -shape = SubResource("CapsuleShape2D_lfx7b") - -[node name="Bush8" type="Sprite2D" parent="."] -position = Vector2(-1616, -1651) -scale = Vector2(3.80334, 3.95089) -texture = ExtResource("4_m2p3o") -centered = false -offset = Vector2(-137.285, -179.785) -flip_h = true -metadata/tags = ["sticker"] - -[node name="Sticker" type="Area2D" parent="Bush8" node_paths=PackedStringArray("WorldSprite")] -collision_layer = 2 -collision_mask = 29 -script = ExtResource("5_ek34p") -WorldSprite = NodePath("..") -metadata/tags = PackedStringArray("sticker") - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Bush8/Sticker"] -position = Vector2(29.7107, -72.6419) -rotation = 1.6 -shape = SubResource("CapsuleShape2D_lfx7b") - [node name="Bush10" type="Sprite2D" parent="."] position = Vector2(-2063, -2116) scale = Vector2(3.80334, 3.95089) @@ -351,7 +293,7 @@ metadata/tags = ["sticker"] [node name="Sticker" type="Area2D" parent="Bush10" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -408,7 +350,7 @@ debug_color = Color(0.999472, 0.00663362, 0.0810784, 0.42) [node name="Sticker" type="Area2D" parent="Log" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -449,7 +391,7 @@ debug_color = Color(0.999472, 0.00663362, 0.0810784, 0.42) [node name="Sticker" type="Area2D" parent="Log3" node_paths=PackedStringArray("WorldSprite")] collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("..") metadata/tags = PackedStringArray("sticker") @@ -501,7 +443,7 @@ metadata/_edit_lock_ = true position = Vector2(11, -128) scale = Vector2(4, 4) collision_layer = 2 -collision_mask = 29 +collision_mask = 61 script = ExtResource("5_ek34p") WorldSprite = NodePath("../FirePitSprite") StickerSprite = NodePath("../FirePitSticker") @@ -516,7 +458,14 @@ scale = Vector2(1.8, 1.8) texture = ExtResource("19_d8hr8") offset = Vector2(0, -95.355) -[connection signal="property_list_changed" from="Bush1" to="Bush1" method="_on_property_list_changed"] +[node name="Bush2" parent="." instance=ExtResource("8_tag7x")] +position = Vector2(-1449, -1696) +flip_h = true + +[node name="Bush3" parent="." instance=ExtResource("8_tag7x")] +position = Vector2(1000, -2228) +flip_h = true + [connection signal="property_list_changed" from="Tree2" to="Tree2" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Tree5" to="Tree5" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Tree6" to="Tree6" method="_on_property_list_changed"] @@ -524,8 +473,6 @@ offset = Vector2(0, -95.355) [connection signal="property_list_changed" from="Bush6" to="Bush6" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Bush7" to="Bush7" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Bush11" to="Bush11" method="_on_property_list_changed"] -[connection signal="property_list_changed" from="Bush9" to="Bush9" method="_on_property_list_changed"] -[connection signal="property_list_changed" from="Bush8" to="Bush8" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Bush10" to="Bush10" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Rock1" to="Rock1" method="_on_property_list_changed"] [connection signal="property_list_changed" from="Rock2" to="Rock2" method="_on_property_list_changed"]