public static bool IsSightBlocked(HitableObject origin, HitableObject target, int lineWidth) { if (origin.InteriorID != target.InteriorID) { return(true); } Point originPoint = origin.Position.ToPoint(); Point targetPoint = target.Position.ToPoint(); Rect unionRect = ShapeCollision.ConvertLineToRect(originPoint, targetPoint); List <HitableObject> hittedObjects = GetHittedObjects(unionRect, origin.InteriorID, origin); Vector2[] polyRect = GeometryUtils.GetRectangleFromLine(originPoint, targetPoint, lineWidth); foreach (var hitableObject in hittedObjects) { if (hitableObject != target && ShapeCollision.RectIntersectsPoly(hitableObject.HitBoxBounds, polyRect)) { return(true); } } // Check if blocks are of type hitable && if they intersect with the polyRect Point topLeft = GeometryUtils.GetChunkPosition(unionRect.Left, unionRect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(unionRect.Right, unionRect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { if (IsBlockHitable(blockX, blockY, origin.InteriorID) && ShapeCollision.RectIntersectsPoly(new Rect(blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y), polyRect)) { return(true); } } } return(false); }
public static bool IsSightBlocked(HitableObject origin, HitableObject target) { if (origin.InteriorID != target.InteriorID) { return(true); } Vector2 originVector = origin.Position.ToVector(); Vector2 targetVector = target.Position.ToVector(); Rect unionRect = ShapeCollision.ConvertLineToRect(originVector, targetVector); List <HitableObject> hittedObjects = GetHittedObjects(unionRect, origin.InteriorID, origin); foreach (var hitableObject in hittedObjects) { if (hitableObject != target && ShapeCollision.LineIntersectsRectangle(originVector, targetVector, hitableObject.HitBoxBounds)) { return(true); } } // Check if blocks are of type hitable Point topLeft = GeometryUtils.GetChunkPosition(unionRect.Left, unionRect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(unionRect.Right, unionRect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { if (IsBlockHitable(blockX, blockY, origin.InteriorID) && ShapeCollision.LineIntersectsRectangle(originVector, targetVector, new Rect(blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y))) { return(true); } } } return(false); }