Adjusting submarine spawnposition if the sub (or any docked subs) intersect with walls
This commit is contained in:
@@ -200,7 +200,7 @@ namespace Barotrauma
|
||||
{
|
||||
level.Generate();
|
||||
|
||||
submarine.SetPosition(level.StartPosition - new Vector2(0.0f, 2000.0f));
|
||||
submarine.SetPosition(submarine.FindSpawnPos(level.StartPosition - new Vector2(0.0f, 2000.0f)));
|
||||
|
||||
//secondSub.SetPosition(level.EndPosition - new Vector2(0.0f, 2000.0f));
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Xml.Linq;
|
||||
using Voronoi2;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -311,6 +312,67 @@ namespace Barotrauma
|
||||
dockedBorders.Y += dockedBorders.Height;
|
||||
return dockedBorders;
|
||||
}
|
||||
|
||||
public Vector2 FindSpawnPos(Vector2 spawnPos)
|
||||
{
|
||||
Rectangle dockedBorders = GetDockedBorders();
|
||||
|
||||
int iterations = 0;
|
||||
bool wallTooClose = false;
|
||||
do
|
||||
{
|
||||
Rectangle worldBorders = new Rectangle(
|
||||
dockedBorders.X + (int)spawnPos.X,
|
||||
dockedBorders.Y + (int)spawnPos.Y,
|
||||
dockedBorders.Width,
|
||||
dockedBorders.Height);
|
||||
|
||||
wallTooClose = false;
|
||||
|
||||
var nearbyCells = Level.Loaded.GetCells(
|
||||
spawnPos, (int)Math.Ceiling(Math.Max(dockedBorders.Width, dockedBorders.Height) / (float)Level.GridCellSize));
|
||||
|
||||
foreach (VoronoiCell cell in nearbyCells)
|
||||
{
|
||||
if (cell.CellType == CellType.Empty) continue;
|
||||
|
||||
foreach (GraphEdge e in cell.edges)
|
||||
{
|
||||
List<Vector2> intersections = MathUtils.GetLineRectangleIntersections(e.point1, e.point2, worldBorders);
|
||||
foreach (Vector2 intersection in intersections)
|
||||
{
|
||||
wallTooClose = true;
|
||||
|
||||
if (intersection.X < spawnPos.X)
|
||||
{
|
||||
spawnPos.X += intersection.X - worldBorders.X;
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnPos.X += intersection.X - worldBorders.Right;
|
||||
}
|
||||
|
||||
if (intersection.Y < spawnPos.Y)
|
||||
{
|
||||
spawnPos.Y += intersection.Y - (worldBorders.Y - worldBorders.Height);
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnPos.Y += intersection.Y - worldBorders.Y;
|
||||
}
|
||||
|
||||
spawnPos.Y = Math.Min(spawnPos.Y, Level.Loaded.Size.Y - dockedBorders.Height / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
iterations++;
|
||||
} while (wallTooClose && iterations < 10);
|
||||
|
||||
return spawnPos;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//drawing ----------------------------------------------------
|
||||
|
||||
|
||||
@@ -233,6 +233,37 @@ namespace Barotrauma
|
||||
new Vector2(rect.Right, rect.Y - rect.Height));
|
||||
}
|
||||
|
||||
public static List<Vector2> GetLineRectangleIntersections(Vector2 a1, Vector2 a2, Rectangle rect)
|
||||
{
|
||||
List<Vector2> intersections = new List<Vector2>();
|
||||
|
||||
Vector2? intersection = GetLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y),
|
||||
new Vector2(rect.Right, rect.Y));
|
||||
|
||||
if (intersection != null) intersections.Add((Vector2)intersection);
|
||||
|
||||
intersection = GetLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y - rect.Height),
|
||||
new Vector2(rect.Right, rect.Y - rect.Height));
|
||||
|
||||
if (intersection != null) intersections.Add((Vector2)intersection);
|
||||
|
||||
intersection = GetLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y),
|
||||
new Vector2(rect.X, rect.Y - rect.Height));
|
||||
|
||||
if (intersection != null) intersections.Add((Vector2)intersection);
|
||||
|
||||
intersection = GetLineIntersection(a1, a2,
|
||||
new Vector2(rect.Right, rect.Y),
|
||||
new Vector2(rect.Right, rect.Y - rect.Height));
|
||||
|
||||
if (intersection != null) intersections.Add((Vector2)intersection);
|
||||
|
||||
return intersections;
|
||||
}
|
||||
|
||||
public static float LineToPointDistance(Vector2 lineA, Vector2 lineB, Vector2 point)
|
||||
{
|
||||
float xDiff = lineB.X - lineA.X;
|
||||
|
||||
Reference in New Issue
Block a user