mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:25:35 +00:00
45 lines
No EOL
1.5 KiB
C#
45 lines
No EOL
1.5 KiB
C#
using System.Diagnostics;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Cirno.Scripts.Components;
|
|
|
|
public partial class ProximityPlayerDetection : PlayerDetection
|
|
{
|
|
public override bool IsPlayerInRange { get; set; }
|
|
|
|
public override bool IsPlayerInSight(uint collisionMask)
|
|
{
|
|
if (_cachedPlayer == null) return false;
|
|
|
|
var spaceState = GetWorld2D().DirectSpaceState;
|
|
|
|
// I have no idea why I need to use the parent's collision mask instead of the local one but it doesn't detect the player otherwise
|
|
|
|
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, collisionMask, new Array<Rid> { GetRid() });
|
|
|
|
// var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Array<Rid> { GetRid() });
|
|
var result = spaceState.IntersectRay(query);
|
|
|
|
// If count is 0 then the player is in sight, otherwise there is level geometry in the way
|
|
return result.Count == 0;
|
|
}
|
|
|
|
private void _on_area_entered(Area2D area)
|
|
{
|
|
// Assume area is player for now
|
|
if (area is not InteractionController player) return;
|
|
|
|
Debug.WriteLine("Enemy detection area Entered by interaction controller");
|
|
|
|
_cachedPlayer = player;
|
|
|
|
IsPlayerInRange = true;
|
|
}
|
|
|
|
private void _on_area_exited(Area2D area)
|
|
{
|
|
if (area is not InteractionController player) return;
|
|
IsPlayerInRange = false;
|
|
}
|
|
} |