Skip to content

Commit

Permalink
fix(detector): fix player detection by guards and cameras (#19)
Browse files Browse the repository at this point in the history
* fix(camera): use new Detector mechanism to detect player

* feat: rename Detector to DetectorArea
  • Loading branch information
olivierperez committed Sep 3, 2024
1 parent 28b08fd commit 250f2b1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 46 deletions.
22 changes: 11 additions & 11 deletions actors/Guard.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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_area = %DetectorArea
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var visible_area = %VisibleArea

Expand Down Expand Up @@ -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_area.rotation_degrees = 90
elif moving_direction.x > 0:
animated_sprite_2d.play("run_to_the_right")
detection_area.rotation_degrees = -90
detector_area.rotation_degrees = -90
else:
if moving_direction.y < 0:
animated_sprite_2d.play("run_to_the_top")
detection_area.rotation_degrees = 180
detector_area.rotation_degrees = 180
elif moving_direction.y > 0:
animated_sprite_2d.play("run_to_the_bottom")
detection_area.rotation_degrees = 0
detector_area.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_area.rotation_degrees = 180
Direction.RIGHT:
animated_sprite_2d.play("facing_right")
detection_area.rotation_degrees = -90
detector_area.rotation_degrees = -90
Direction.BOTTOM:
animated_sprite_2d.play("facing_bottom")
detection_area.rotation_degrees = 0
detector_area.rotation_degrees = 0
Direction.LEFT:
animated_sprite_2d.play("facing_left")
detection_area.rotation_degrees = 90
detector_area.rotation_degrees = 90


func _vector_to_direction(vector: Vector2) -> Direction:
Expand Down Expand Up @@ -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()

Expand Down
13 changes: 6 additions & 7 deletions actors/Guard.tscn
Original file line number Diff line number Diff line change
@@ -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/DetectorArea.tscn" id="3_wcu88"]

[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_frres"]
radius = 34.98
Expand Down Expand Up @@ -391,19 +392,17 @@ sprite_frames = SubResource("SpriteFrames_ttuab")
animation = &"facing_bottom"
frame_progress = 0.477112

[node name="DetectionArea" type="Area2D" parent="."]
[node name="DetectorArea" parent="." instance=ExtResource("3_wcu88")]
unique_name_in_owner = true
collision_layer = 8
collision_mask = 16

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DetectionArea"]
[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="DetectionArea/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="body_entered" from="DetectionArea" to="." method="_on_detection_area_body_entered"]
[connection signal="player_detected" from="DetectorArea" to="." method="_on_player_detected"]
34 changes: 34 additions & 0 deletions mechanism/DetectorArea.gd
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions mechanism/DetectorArea.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[gd_scene load_steps=2 format=3 uid="uid://ch0t0hu00p6er"]

[ext_resource type="Script" path="res://mechanism/DetectorArea.gd" id="1_21u6r"]

[node name="DetectorArea" type="Area2D"]
collision_layer = 0
collision_mask = 17
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"]
26 changes: 7 additions & 19 deletions objects/Camera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends Node2D

class_name Camera

@onready var detection_area = %DetectionArea
@onready var detector_area = %DetectorArea
@onready var animated_sprite = %AnimatedSprite
@onready var detection_shape = %DetectionShape
@onready var audio_off = %AudioOff
Expand Down Expand Up @@ -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_area.visible = true


func deactivateCamera():
Expand All @@ -68,7 +64,7 @@ func deactivateCamera():
animated_sprite.play("off")
audio_off.play()
detection_shape.disabled = true
detection_area.visible = false
detector_area.visible = false
await get_tree().create_timer(deactivation_camera_timer).timeout
activateCamera()

Expand All @@ -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()
15 changes: 6 additions & 9 deletions objects/Camera.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[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="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"]

Expand Down Expand Up @@ -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="DetectorArea" parent="." instance=ExtResource("2_n6ij3")]
unique_name_in_owner = true
collision_layer = 4
collision_mask = 16

[node name="DetectionShape" type="CollisionPolygon2D" parent="DetectionArea"]
[node name="DetectionShape" type="CollisionPolygon2D" parent="DetectorArea"]
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="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)
Expand All @@ -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="DetectorArea" to="." method="_on_player_detected"]

0 comments on commit 250f2b1

Please sign in to comment.