-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(detector): fix player detection by guards and cameras (#19)
* fix(camera): use new Detector mechanism to detect player * feat: rename Detector to DetectorArea
- Loading branch information
1 parent
28b08fd
commit 250f2b1
Showing
6 changed files
with
75 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters