From 98cb946c4ba103f814c713e903648d3865fd7deb Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Mon, 19 Aug 2024 22:31:54 +0200 Subject: [PATCH 1/2] fix(camera): use new Detector mechanism to detect player --- actors/Guard.gd | 22 +++++++++++----------- actors/Guard.tscn | 13 ++++++------- mechanism/Detector.gd | 34 ++++++++++++++++++++++++++++++++++ mechanism/Detector.tscn | 11 +++++++++++ objects/Camera.gd | 26 +++++++------------------- objects/Camera.tscn | 15 ++++++--------- 6 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 mechanism/Detector.gd create mode 100644 mechanism/Detector.tscn diff --git a/actors/Guard.gd b/actors/Guard.gd index 98d37f9..a514ac5 100644 --- a/actors/Guard.gd +++ b/actors/Guard.gd @@ -10,7 +10,7 @@ class_name Guard @export var facing_direction: Direction = Direction.BOTTOM @export var sharedFollowPath: bool = false -@onready var detection_area = %DetectionArea +@onready var detector = %Detector @onready var animated_sprite_2d = $AnimatedSprite2D @onready var visible_area = %VisibleArea @@ -63,33 +63,33 @@ func _ajust_moving_orientation(): if isHorizontalyMoved: if moving_direction.x < 0: animated_sprite_2d.play("run_to_the_left") - detection_area.rotation_degrees = 90 + detector.rotation_degrees = 90 elif moving_direction.x > 0: animated_sprite_2d.play("run_to_the_right") - detection_area.rotation_degrees = -90 + detector.rotation_degrees = -90 else: if moving_direction.y < 0: animated_sprite_2d.play("run_to_the_top") - detection_area.rotation_degrees = 180 + detector.rotation_degrees = 180 elif moving_direction.y > 0: animated_sprite_2d.play("run_to_the_bottom") - detection_area.rotation_degrees = 0 + detector.rotation_degrees = 0 func _ajust_facing_orientation(direction: Direction): match direction: Direction.TOP: animated_sprite_2d.play("facing_top") - detection_area.rotation_degrees = 180 + detector.rotation_degrees = 180 Direction.RIGHT: animated_sprite_2d.play("facing_right") - detection_area.rotation_degrees = -90 + detector.rotation_degrees = -90 Direction.BOTTOM: animated_sprite_2d.play("facing_bottom") - detection_area.rotation_degrees = 0 + detector.rotation_degrees = 0 Direction.LEFT: animated_sprite_2d.play("facing_left") - detection_area.rotation_degrees = 90 + detector.rotation_degrees = 90 func _vector_to_direction(vector: Vector2) -> Direction: @@ -121,8 +121,8 @@ func stun(): visible_area.visible = false -func _on_detection_area_body_entered(body): - if state != GuardState.STUN and body is Player and _can_see(body): +func _on_player_detected(): + if state != GuardState.STUN: print("Spotted") on_player_catch.emit() diff --git a/actors/Guard.tscn b/actors/Guard.tscn index f2f5714..39334b9 100644 --- a/actors/Guard.tscn +++ b/actors/Guard.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=50 format=3 uid="uid://15jte8wcowt5"] +[gd_scene load_steps=51 format=3 uid="uid://15jte8wcowt5"] [ext_resource type="Script" path="res://actors/Guard.gd" id="1_dvj3w"] [ext_resource type="Texture2D" uid="uid://kvneeqa6brvg" path="res://assets/characters/guard/universal-lpc-sprite_male_01_full.png" id="2_8enka"] +[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/Detector.tscn" id="3_onp32"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_frres"] radius = 34.98 @@ -391,19 +392,17 @@ sprite_frames = SubResource("SpriteFrames_ttuab") animation = &"facing_bottom" frame_progress = 0.477112 -[node name="DetectionArea" type="Area2D" parent="."] +[node name="Detector" parent="." instance=ExtResource("3_onp32")] unique_name_in_owner = true -collision_layer = 8 -collision_mask = 16 -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DetectionArea"] +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Detector"] polygon = PackedVector2Array(-50, 2.08165e-12, 50, 2.08165e-12, 130, 180, -130, 180) -[node name="VisibleArea" type="Polygon2D" parent="DetectionArea/CollisionPolygon2D"] +[node name="VisibleArea" type="Polygon2D" parent="Detector/CollisionPolygon2D"] unique_name_in_owner = true color = Color(1, 0.407843, 0.407843, 0.196078) polygon = PackedVector2Array(-50, 2.08165e-12, 50, 2.08165e-12, 130, 180, -130, 180) [connection signal="input_event" from="ClickableArea" to="." method="_on_actionnable_input_event"] [connection signal="mouse_exited" from="ClickableArea" to="." method="_on_area_mouse_exited"] -[connection signal="body_entered" from="DetectionArea" to="." method="_on_detection_area_body_entered"] +[connection signal="player_detected" from="Detector" to="." method="_on_player_detected"] diff --git a/mechanism/Detector.gd b/mechanism/Detector.gd new file mode 100644 index 0000000..6c0145c --- /dev/null +++ b/mechanism/Detector.gd @@ -0,0 +1,34 @@ +extends Area2D + +signal player_detected + + +var _player: Player + + +func _process(delta): + if _player != null: + if _can_see(_player): + player_detected.emit() + + +func _on_body_entered_detector(body): + if body is Player: + _player = body + + +func _on_body_exited_detector(body): + if body is Player: + _player = null + + +## Check if guard can see the body or if there something between them +func _can_see(player: Player) -> bool: + var space_state = get_world_2d().direct_space_state + var query = PhysicsRayQueryParameters2D.create(global_position, player.global_position) + query.collide_with_areas = false + query.collide_with_bodies = true + query.hit_from_inside = true + query.collision_mask = collision_mask + var result = space_state.intersect_ray(query) + return result.has("collider") and result["collider"] == player diff --git a/mechanism/Detector.tscn b/mechanism/Detector.tscn new file mode 100644 index 0000000..494bce7 --- /dev/null +++ b/mechanism/Detector.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=2 format=3 uid="uid://ch0t0hu00p6er"] + +[ext_resource type="Script" path="res://mechanism/Detector.gd" id="1_lyhly"] + +[node name="Detector" type="Area2D"] +collision_layer = 0 +collision_mask = 17 +script = ExtResource("1_lyhly") + +[connection signal="body_entered" from="." to="." method="_on_body_entered_detector"] +[connection signal="body_exited" from="." to="." method="_on_body_exited_detector"] diff --git a/objects/Camera.gd b/objects/Camera.gd index 0f87b67..6c107a7 100644 --- a/objects/Camera.gd +++ b/objects/Camera.gd @@ -2,7 +2,7 @@ extends Node2D class_name Camera -@onready var detection_area = %DetectionArea +@onready var detector = %Detector @onready var animated_sprite = %AnimatedSprite @onready var detection_shape = %DetectionShape @onready var audio_off = %AudioOff @@ -48,17 +48,13 @@ func _create_animation_player() -> AnimationPlayer: return animPlayer -func _on_body_entered(body): - if body is Player and _can_see(body): - on_player_catch.emit() - func activateCamera(): if not _scanning: _scanning = true animated_sprite.play("on") audio_on.play() detection_shape.disabled = false - detection_area.visible = true + detector.visible = true func deactivateCamera(): @@ -68,7 +64,7 @@ func deactivateCamera(): animated_sprite.play("off") audio_off.play() detection_shape.disabled = true - detection_area.visible = false + detector.visible = false await get_tree().create_timer(deactivation_camera_timer).timeout activateCamera() @@ -80,17 +76,9 @@ func _on_actionnable_input_event(viewport, event, shape_idx): deactivateCamera() -## Check if guard can see the body or if there something between them -func _can_see(body) -> bool: - var space_state = get_world_2d().direct_space_state - var query = PhysicsRayQueryParameters2D.create(global_position, body.global_position) - query.collide_with_areas = false - query.collide_with_bodies = true - query.hit_from_inside = true - query.exclude = [self] - var result = space_state.intersect_ray(query) - return result.has("collider") and result["collider"] == body - - func _on_area_mouse_exited(): CursorController.reset_default() + + +func _on_player_detected(): + on_player_catch.emit() diff --git a/objects/Camera.tscn b/objects/Camera.tscn index 2619f52..65d1ea5 100644 --- a/objects/Camera.tscn +++ b/objects/Camera.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=9 format=3 uid="uid://7oyofxooer87"] +[gd_scene load_steps=10 format=3 uid="uid://7oyofxooer87"] [ext_resource type="Script" path="res://objects/Camera.gd" id="1_2cpas"] [ext_resource type="Texture2D" uid="uid://dhgxstfex3l5p" path="res://assets/camera/camera-on.png" id="1_jbi7c"] [ext_resource type="Texture2D" uid="uid://dalurg7lemyuc" path="res://assets/camera/camera-off.png" id="2_2mssq"] [ext_resource type="AudioStream" uid="uid://cfkt5daaaa602" path="res://assets/audio/sfx/camera-off.mp3" id="4_fuae1"] [ext_resource type="AudioStream" uid="uid://boo5037p0nd4f" path="res://assets/audio/sfx/camera-on.mp3" id="5_66gbt"] +[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/Detector.tscn" id="6_lu8gv"] [sub_resource type="CircleShape2D" id="CircleShape2D_4cot1"] @@ -39,18 +40,15 @@ unique_name_in_owner = true scale = Vector2(4, 4) shape = SubResource("CircleShape2D_4cot1") -[node name="DetectionArea" type="Area2D" parent="."] +[node name="Detector" parent="." instance=ExtResource("6_lu8gv")] unique_name_in_owner = true -collision_layer = 4 -collision_mask = 16 -[node name="DetectionShape" type="CollisionPolygon2D" parent="DetectionArea"] +[node name="DetectionShape" type="CollisionPolygon2D" parent="Detector"] unique_name_in_owner = true -self_modulate = Color(0.988235, 1, 1, 1) show_behind_parent = true polygon = PackedVector2Array(0, 0, 300, 2.08165e-12, 285.317, 92.705, 242.705, 176.336, 176.336, 242.705, 92.705, 285.317, 2.08165e-12, 300) -[node name="Polygon2D" type="Polygon2D" parent="DetectionArea/DetectionShape"] +[node name="Polygon2D" type="Polygon2D" parent="Detector/DetectionShape"] color = Color(1, 0.407843, 0.407843, 0.196078) texture = SubResource("CanvasTexture_o81d5") polygon = PackedVector2Array(0, 0, 300, 2.08165e-12, 285.317, 92.705, 242.705, 176.336, 176.336, 242.705, 92.705, 285.317, 2.08165e-12, 300) @@ -73,5 +71,4 @@ volume_db = 10.0 [connection signal="input_event" from="ActionnableArea" to="." method="_on_actionnable_input_event"] [connection signal="mouse_exited" from="ActionnableArea" to="." method="_on_area_mouse_exited"] -[connection signal="body_entered" from="DetectionArea" to="." method="_on_body_entered"] -[connection signal="input_event" from="DetectionArea" to="DetectionArea" method="_on_input_event"] +[connection signal="player_detected" from="Detector" to="." method="_on_player_detected"] From b72f196dfdff87642f946a5fddbc88687fdf31bd Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Mon, 2 Sep 2024 22:45:41 +0200 Subject: [PATCH 2/2] feat: rename Detector to DetectorArea --- actors/Guard.gd | 18 +++++++++--------- actors/Guard.tscn | 10 +++++----- mechanism/{Detector.gd => DetectorArea.gd} | 0 mechanism/{Detector.tscn => DetectorArea.tscn} | 6 +++--- objects/Camera.gd | 6 +++--- objects/Camera.tscn | 10 +++++----- 6 files changed, 25 insertions(+), 25 deletions(-) rename mechanism/{Detector.gd => DetectorArea.gd} (100%) rename mechanism/{Detector.tscn => DetectorArea.tscn} (63%) diff --git a/actors/Guard.gd b/actors/Guard.gd index a514ac5..4d20417 100644 --- a/actors/Guard.gd +++ b/actors/Guard.gd @@ -10,7 +10,7 @@ class_name Guard @export var facing_direction: Direction = Direction.BOTTOM @export var sharedFollowPath: bool = false -@onready var detector = %Detector +@onready var detector_area = %DetectorArea @onready var animated_sprite_2d = $AnimatedSprite2D @onready var visible_area = %VisibleArea @@ -63,33 +63,33 @@ func _ajust_moving_orientation(): if isHorizontalyMoved: if moving_direction.x < 0: animated_sprite_2d.play("run_to_the_left") - detector.rotation_degrees = 90 + detector_area.rotation_degrees = 90 elif moving_direction.x > 0: animated_sprite_2d.play("run_to_the_right") - detector.rotation_degrees = -90 + detector_area.rotation_degrees = -90 else: if moving_direction.y < 0: animated_sprite_2d.play("run_to_the_top") - detector.rotation_degrees = 180 + detector_area.rotation_degrees = 180 elif moving_direction.y > 0: animated_sprite_2d.play("run_to_the_bottom") - detector.rotation_degrees = 0 + detector_area.rotation_degrees = 0 func _ajust_facing_orientation(direction: Direction): match direction: Direction.TOP: animated_sprite_2d.play("facing_top") - detector.rotation_degrees = 180 + detector_area.rotation_degrees = 180 Direction.RIGHT: animated_sprite_2d.play("facing_right") - detector.rotation_degrees = -90 + detector_area.rotation_degrees = -90 Direction.BOTTOM: animated_sprite_2d.play("facing_bottom") - detector.rotation_degrees = 0 + detector_area.rotation_degrees = 0 Direction.LEFT: animated_sprite_2d.play("facing_left") - detector.rotation_degrees = 90 + detector_area.rotation_degrees = 90 func _vector_to_direction(vector: Vector2) -> Direction: diff --git a/actors/Guard.tscn b/actors/Guard.tscn index 39334b9..12a9c3a 100644 --- a/actors/Guard.tscn +++ b/actors/Guard.tscn @@ -2,7 +2,7 @@ [ext_resource type="Script" path="res://actors/Guard.gd" id="1_dvj3w"] [ext_resource type="Texture2D" uid="uid://kvneeqa6brvg" path="res://assets/characters/guard/universal-lpc-sprite_male_01_full.png" id="2_8enka"] -[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/Detector.tscn" id="3_onp32"] +[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/DetectorArea.tscn" id="3_wcu88"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_frres"] radius = 34.98 @@ -392,17 +392,17 @@ sprite_frames = SubResource("SpriteFrames_ttuab") animation = &"facing_bottom" frame_progress = 0.477112 -[node name="Detector" parent="." instance=ExtResource("3_onp32")] +[node name="DetectorArea" parent="." instance=ExtResource("3_wcu88")] unique_name_in_owner = true -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Detector"] +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DetectorArea"] polygon = PackedVector2Array(-50, 2.08165e-12, 50, 2.08165e-12, 130, 180, -130, 180) -[node name="VisibleArea" type="Polygon2D" parent="Detector/CollisionPolygon2D"] +[node name="VisibleArea" type="Polygon2D" parent="DetectorArea/CollisionPolygon2D"] unique_name_in_owner = true color = Color(1, 0.407843, 0.407843, 0.196078) polygon = PackedVector2Array(-50, 2.08165e-12, 50, 2.08165e-12, 130, 180, -130, 180) [connection signal="input_event" from="ClickableArea" to="." method="_on_actionnable_input_event"] [connection signal="mouse_exited" from="ClickableArea" to="." method="_on_area_mouse_exited"] -[connection signal="player_detected" from="Detector" to="." method="_on_player_detected"] +[connection signal="player_detected" from="DetectorArea" to="." method="_on_player_detected"] diff --git a/mechanism/Detector.gd b/mechanism/DetectorArea.gd similarity index 100% rename from mechanism/Detector.gd rename to mechanism/DetectorArea.gd diff --git a/mechanism/Detector.tscn b/mechanism/DetectorArea.tscn similarity index 63% rename from mechanism/Detector.tscn rename to mechanism/DetectorArea.tscn index 494bce7..1e4bff2 100644 --- a/mechanism/Detector.tscn +++ b/mechanism/DetectorArea.tscn @@ -1,11 +1,11 @@ [gd_scene load_steps=2 format=3 uid="uid://ch0t0hu00p6er"] -[ext_resource type="Script" path="res://mechanism/Detector.gd" id="1_lyhly"] +[ext_resource type="Script" path="res://mechanism/DetectorArea.gd" id="1_21u6r"] -[node name="Detector" type="Area2D"] +[node name="DetectorArea" type="Area2D"] collision_layer = 0 collision_mask = 17 -script = ExtResource("1_lyhly") +script = ExtResource("1_21u6r") [connection signal="body_entered" from="." to="." method="_on_body_entered_detector"] [connection signal="body_exited" from="." to="." method="_on_body_exited_detector"] diff --git a/objects/Camera.gd b/objects/Camera.gd index 6c107a7..b3af6a3 100644 --- a/objects/Camera.gd +++ b/objects/Camera.gd @@ -2,7 +2,7 @@ extends Node2D class_name Camera -@onready var detector = %Detector +@onready var detector_area = %DetectorArea @onready var animated_sprite = %AnimatedSprite @onready var detection_shape = %DetectionShape @onready var audio_off = %AudioOff @@ -54,7 +54,7 @@ func activateCamera(): animated_sprite.play("on") audio_on.play() detection_shape.disabled = false - detector.visible = true + detector_area.visible = true func deactivateCamera(): @@ -64,7 +64,7 @@ func deactivateCamera(): animated_sprite.play("off") audio_off.play() detection_shape.disabled = true - detector.visible = false + detector_area.visible = false await get_tree().create_timer(deactivation_camera_timer).timeout activateCamera() diff --git a/objects/Camera.tscn b/objects/Camera.tscn index 65d1ea5..b1a7974 100644 --- a/objects/Camera.tscn +++ b/objects/Camera.tscn @@ -3,9 +3,9 @@ [ext_resource type="Script" path="res://objects/Camera.gd" id="1_2cpas"] [ext_resource type="Texture2D" uid="uid://dhgxstfex3l5p" path="res://assets/camera/camera-on.png" id="1_jbi7c"] [ext_resource type="Texture2D" uid="uid://dalurg7lemyuc" path="res://assets/camera/camera-off.png" id="2_2mssq"] +[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/DetectorArea.tscn" id="2_n6ij3"] [ext_resource type="AudioStream" uid="uid://cfkt5daaaa602" path="res://assets/audio/sfx/camera-off.mp3" id="4_fuae1"] [ext_resource type="AudioStream" uid="uid://boo5037p0nd4f" path="res://assets/audio/sfx/camera-on.mp3" id="5_66gbt"] -[ext_resource type="PackedScene" uid="uid://ch0t0hu00p6er" path="res://mechanism/Detector.tscn" id="6_lu8gv"] [sub_resource type="CircleShape2D" id="CircleShape2D_4cot1"] @@ -40,15 +40,15 @@ unique_name_in_owner = true scale = Vector2(4, 4) shape = SubResource("CircleShape2D_4cot1") -[node name="Detector" parent="." instance=ExtResource("6_lu8gv")] +[node name="DetectorArea" parent="." instance=ExtResource("2_n6ij3")] unique_name_in_owner = true -[node name="DetectionShape" type="CollisionPolygon2D" parent="Detector"] +[node name="DetectionShape" type="CollisionPolygon2D" parent="DetectorArea"] unique_name_in_owner = true show_behind_parent = true polygon = PackedVector2Array(0, 0, 300, 2.08165e-12, 285.317, 92.705, 242.705, 176.336, 176.336, 242.705, 92.705, 285.317, 2.08165e-12, 300) -[node name="Polygon2D" type="Polygon2D" parent="Detector/DetectionShape"] +[node name="Polygon2D" type="Polygon2D" parent="DetectorArea/DetectionShape"] color = Color(1, 0.407843, 0.407843, 0.196078) texture = SubResource("CanvasTexture_o81d5") polygon = PackedVector2Array(0, 0, 300, 2.08165e-12, 285.317, 92.705, 242.705, 176.336, 176.336, 242.705, 92.705, 285.317, 2.08165e-12, 300) @@ -71,4 +71,4 @@ volume_db = 10.0 [connection signal="input_event" from="ActionnableArea" to="." method="_on_actionnable_input_event"] [connection signal="mouse_exited" from="ActionnableArea" to="." method="_on_area_mouse_exited"] -[connection signal="player_detected" from="Detector" to="." method="_on_player_detected"] +[connection signal="player_detected" from="DetectorArea" to="." method="_on_player_detected"]