diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/CaveGenerator.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/CaveGenerator.cs index 790e08eff..9cccfc97f 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/CaveGenerator.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/CaveGenerator.cs @@ -323,18 +323,9 @@ namespace Barotrauma //choose random edge (ignoring ones where the adjacent cell is outside limits) else { - - - //if (allowedEdges.Count==0) - //{ - // edgeIndex = Rand.Int(currentCell.edges.Count, false); - //} - //else - //{ edgeIndex = Rand.Int(allowedEdges.Count, Rand.RandSync.Server); if (mirror && edgeIndex > 0) edgeIndex = allowedEdges.Count - edgeIndex; edgeIndex = currentCell.edges.IndexOf(allowedEdges[edgeIndex]); - //} } currentCell = currentCell.edges[edgeIndex].AdjacentCell(currentCell); @@ -357,8 +348,8 @@ namespace Barotrauma return pathCells; } - - public static List GeneratePolygons(List cells, out List renderTriangles, bool setSolid=true) + + public static List GeneratePolygons(List cells, out List renderTriangles, bool setSolid = true) { renderTriangles = new List(); var bodies = new List(); @@ -366,6 +357,14 @@ namespace Barotrauma List tempVertices = new List(); List bodyPoints = new List(); + Body cellBody = new Body(GameMain.World) + { + SleepingAllowed = false, + BodyType = BodyType.Static, + CollisionCategories = Physics.CollisionLevel + }; + bodies.Add(cellBody); + for (int n = cells.Count - 1; n >= 0; n-- ) { VoronoiCell cell = cells[n]; @@ -412,14 +411,12 @@ namespace Barotrauma cell.bodyVertices.Add(bodyPoints[i]); bodyPoints[i] = ConvertUnits.ToSimUnits(bodyPoints[i]); } - - + if (cell.CellType == CellType.Empty) continue; + cellBody.UserData = cell; var triangles = MathUtils.TriangulateConvexHull(bodyPoints, ConvertUnits.ToSimUnits(cell.Center)); - - Body cellBody = new Body(GameMain.World); - + for (int i = 0; i < triangles.Count; i++) { //don't create a triangle if any of the vertices are too close to each other @@ -429,16 +426,11 @@ namespace Barotrauma Vector2.Distance(triangles[i][1], triangles[i][2]) < 0.05f) continue; Vertices bodyVertices = new Vertices(triangles[i]); - FixtureFactory.AttachPolygon(bodyVertices, 5.0f, cellBody); + var newFixture = FixtureFactory.AttachPolygon(bodyVertices, 5.0f, cellBody); + newFixture.UserData = cell; } - cellBody.UserData = cell; - cellBody.SleepingAllowed = false; - cellBody.BodyType = BodyType.Kinematic; - cellBody.CollisionCategories = Physics.CollisionLevel; - cell.body = cellBody; - bodies.Add(cellBody); } return bodies; diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelWall.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelWall.cs index 059301862..a0445de16 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelWall.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelWall.cs @@ -21,7 +21,6 @@ namespace Barotrauma public LevelWall(List edgePositions, Vector2 extendAmount, Color color) { cells = new List(); - for (int i = 0; i < edgePositions.Count - 1; i++) { Vector2[] vertices = new Vector2[4]; @@ -47,8 +46,7 @@ namespace Barotrauma cells.Add(wallCell); } - List triangles; - bodies = CaveGenerator.GeneratePolygons(cells, out triangles, false); + bodies = CaveGenerator.GeneratePolygons(cells, out List triangles, false); #if CLIENT List bodyVertices = CaveGenerator.GenerateRenderVerticeList(triangles); diff --git a/Barotrauma/BarotraumaShared/Source/Map/SubmarineBody.cs b/Barotrauma/BarotraumaShared/Source/Map/SubmarineBody.cs index 6d623c73c..bdf43b42e 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/SubmarineBody.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/SubmarineBody.cs @@ -370,27 +370,22 @@ namespace Barotrauma public bool OnCollision(Fixture f1, Fixture f2, Contact contact) { - Limb limb = f2.Body.UserData as Limb; - if (limb != null) + if (f2.Body.UserData is Limb limb) { bool collision = CheckLimbCollision(contact, limb); if (collision) HandleLimbCollision(contact, limb); return collision; } - VoronoiCell cell = f2.Body.UserData as VoronoiCell; - if (cell != null) + if (f2.UserData is VoronoiCell cell) { HandleLevelCollision(contact, Vector2.Normalize(ConvertUnits.ToDisplayUnits(Body.SimPosition) - cell.Center)); return true; } - Structure structure = f2.Body.UserData as Structure; - if (structure != null) + if (f2.Body.UserData is Structure structure) { - Vector2 normal; - FixedArray2 points; - contact.GetWorldManifold(out normal, out points); + contact.GetWorldManifold(out Vector2 normal, out FixedArray2 points); if (contact.FixtureA.Body == f1.Body) { normal = -normal; @@ -400,8 +395,7 @@ namespace Barotrauma return true; } - Submarine otherSub = f2.Body.UserData as Submarine; - if (otherSub != null) + if (f2.Body.UserData is Submarine otherSub) { HandleSubCollision(contact, otherSub); return true; @@ -486,8 +480,8 @@ namespace Barotrauma levelContact.GetWorldManifold(out contactNormal, out temp); //if the contact normal is pointing from the limb towards the level cell it's touching, flip the normal - VoronoiCell cell = levelContact.FixtureB.Body.UserData is VoronoiCell ? - ((VoronoiCell)levelContact.FixtureB.Body.UserData) : ((VoronoiCell)levelContact.FixtureA.Body.UserData); + VoronoiCell cell = levelContact.FixtureB.UserData is VoronoiCell ? + ((VoronoiCell)levelContact.FixtureB.UserData) : ((VoronoiCell)levelContact.FixtureA.UserData); var cellDiff = ConvertUnits.ToDisplayUnits(limb.body.SimPosition) - cell.Center; if (Vector2.Dot(contactNormal, cellDiff) < 0) @@ -603,8 +597,8 @@ namespace Barotrauma levelContact.GetWorldManifold(out contactNormal, out temp); //if the contact normal is pointing from the sub towards the level cell we collided with, flip the normal - VoronoiCell cell = levelContact.FixtureB.Body.UserData is VoronoiCell ? - ((VoronoiCell)levelContact.FixtureB.Body.UserData) : ((VoronoiCell)levelContact.FixtureA.Body.UserData); + VoronoiCell cell = levelContact.FixtureB.UserData is VoronoiCell ? + ((VoronoiCell)levelContact.FixtureB.UserData) : ((VoronoiCell)levelContact.FixtureA.UserData); var cellDiff = ConvertUnits.ToDisplayUnits(Body.SimPosition) - cell.Center; if (Vector2.Dot(contactNormal, cellDiff) < 0)