From 0ab4521a7a0449df77036792c518a7089c4b08b4 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Tue, 6 Mar 2018 12:13:08 +0200 Subject: [PATCH] Fixed monsters being able to spawn under the ocean floor. Closes #319 --- .../Source/Events/MonsterEvent.cs | 12 +++++++++++- .../BarotraumaShared/Source/Map/Levels/Level.cs | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Events/MonsterEvent.cs b/Barotrauma/BarotraumaShared/Source/Events/MonsterEvent.cs index 217512107..f53c17de0 100644 --- a/Barotrauma/BarotraumaShared/Source/Events/MonsterEvent.cs +++ b/Barotrauma/BarotraumaShared/Source/Events/MonsterEvent.cs @@ -101,7 +101,17 @@ namespace Barotrauma var monsters = new Character[amount]; - if (spawnDeep) spawnPos.Y -= Level.Loaded.Size.Y; + if (spawnDeep) + { + spawnPos.Y -= Level.Loaded.Size.Y; + //disable the event if the ocean floor is too high up to spawn the monster deep + if (spawnPos.Y < Level.Loaded.GetBottomPosition(spawnPos.X).Y) + { + repeat = false; + Finished(); + return null; + } + } for (int i = 0; i < amount; i++) { diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/Level.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/Level.cs index 9db6aa49f..1ab021186 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/Level.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/Level.cs @@ -78,6 +78,8 @@ namespace Barotrauma private static BackgroundSpriteManager backgroundSpriteManager; + private List bottomPositions; + public Vector2 StartPosition { get { return startPosition; } @@ -668,7 +670,7 @@ namespace Barotrauma BottomPos = generationParams.SeaFloorDepth; SeaFloorTopPos = BottomPos; - List bottomPositions = new List(); + bottomPositions = new List(); bottomPositions.Add(new Vector2(0, BottomPos)); int mountainCount = Rand.Range(generationParams.MountainCountMin, generationParams.MountainCountMax, Rand.RandSync.Server); @@ -983,6 +985,19 @@ namespace Barotrauma #endif } + public Vector2 GetBottomPosition(float xPosition) + { + int index = (int)Math.Floor(xPosition / Size.X * (bottomPositions.Count - 1)); + if (index < 0 || index >= bottomPositions.Count - 1) return new Vector2(xPosition, BottomPos); + + float yPos = MathHelper.Lerp( + bottomPositions[index].Y, + bottomPositions[index + 1].Y, + (xPosition - bottomPositions[index].X) / (bottomPositions[index + 1].X - bottomPositions[index].X)); + + return new Vector2(xPosition, yPos); + } + public List GetCells(Vector2 pos, int searchDepth = 2) { int gridPosX = (int)Math.Floor(pos.X / GridCellSize);