Enemy strafing

This commit is contained in:
MaddoScientisto 2025-03-23 18:22:12 +01:00
commit b35772f519
6 changed files with 112 additions and 16 deletions

View file

@ -60,18 +60,20 @@ public partial class PlayerDetectionModule : Area2D
//if (_cachedPlayer == null) return false;
if (!GameManager.Instance.PlayerPosition.HasValue) return false;
var spaceState = GetWorld2D().DirectSpaceState;
var found = HasLineOfSight(this.GlobalPosition, GameManager.Instance.PlayerPosition.Value);
// It needs to use its own collision mask because it's detecting obstacles rather than the player
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, GameManager.Instance.PlayerPosition.Value, ObstaclesCollisionMask,
[GetRid()]);
//query.CollideWithBodies = true;
//query.CollideWithAreas = true;
// 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
var found = result.Count == 0;
// var spaceState = GetWorld2D().DirectSpaceState;
//
// // It needs to use its own collision mask because it's detecting obstacles rather than the player
// var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, GameManager.Instance.PlayerPosition.Value, ObstaclesCollisionMask,
// [GetRid()]);
// //query.CollideWithBodies = true;
// //query.CollideWithAreas = true;
// // 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
// var found = result.Count == 0;
if (found)
{
LastKnownPlayerPosition = GameManager.Instance.PlayerPosition;
@ -92,4 +94,22 @@ public partial class PlayerDetectionModule : Area2D
EmitSignal(SignalName.PlayerOutOfRange);
//PlayerInActiveArea = false;
}
public bool HasLineOfSight(Vector2 startPos, Vector2 endPos)
{
var spaceState = GetWorld2D().DirectSpaceState;
// It needs to use its own collision mask because it's detecting obstacles rather than the player
var query = PhysicsRayQueryParameters2D.Create(startPos, endPos, ObstaclesCollisionMask,
[GetRid()]);
//query.CollideWithBodies = true;
//query.CollideWithAreas = true;
// 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
var found = result.Count == 0;
return found;
}
}