Preventing monsters from spawning too close to any sub (not just the main sub)

This commit is contained in:
Regalis
2016-10-06 21:17:33 +03:00
parent 0ea9600198
commit 70b8bf1937
3 changed files with 16 additions and 29 deletions
@@ -28,17 +28,7 @@ namespace Barotrauma
public override void Start(Level level) public override void Start(Level level)
{ {
float minDist = Math.Max(Submarine.MainSub.Borders.Width, Submarine.MainSub.Borders.Height); Vector2 spawnPos = Level.Loaded.GetRandomInterestingPosition(true, Level.PositionType.MainPath, true);
//find a random spawnpos that isn't too close to the main sub
int tries = 0;
Vector2 spawnPos = Vector2.Zero;
do
{
spawnPos = Level.Loaded.GetRandomInterestingPosition(true, Level.PositionType.MainPath);
tries++;
} while (tries < 50 && Vector2.Distance(spawnPos, Submarine.MainSub.WorldPosition) < minDist);
monster = Character.Create(monsterFile, spawnPos, null, GameMain.Client != null); monster = Character.Create(monsterFile, spawnPos, null, GameMain.Client != null);
monster.Enabled = false; monster.Enabled = false;
+3 -16
View File
@@ -63,29 +63,16 @@ namespace Barotrauma
{ {
if (disallowed) return; if (disallowed) return;
float minDist = Math.Max(Submarine.MainSub.Borders.Width, Submarine.MainSub.Borders.Height); Vector2 spawnPos = Level.Loaded.GetRandomInterestingPosition(true, spawnPosType, true);
//find a random spawnpos that isn't too close to the main sub
int tries = 0;
Vector2 spawnPos = Vector2.Zero;
do
{
spawnPos = Level.Loaded.GetRandomInterestingPosition(true, spawnPosType);
tries++;
} while (tries < 50 && Vector2.Distance(spawnPos, Submarine.MainSub.WorldPosition) < minDist);
int amount = Rand.Range(minAmount, maxAmount, false); int amount = Rand.Range(minAmount, maxAmount, false);
monsters = new Character[amount]; monsters = new Character[amount];
if (spawnDeep) spawnPos.Y -= Level.Loaded.Size.Y;
for (int i = 0; i < amount; i++) for (int i = 0; i < amount; i++)
{ {
if (spawnDeep)
{
spawnPos.Y -= Level.Loaded.Size.Y;
}
spawnPos.X += Rand.Range(-0.5f, 0.5f, false); spawnPos.X += Rand.Range(-0.5f, 0.5f, false);
spawnPos.Y += Rand.Range(-0.5f, 0.5f, false); spawnPos.Y += Rand.Range(-0.5f, 0.5f, false);
monsters[i] = Character.Create(characterFile, spawnPos, null, GameMain.Client != null); monsters[i] = Character.Create(characterFile, spawnPos, null, GameMain.Client != null);
+11 -1
View File
@@ -784,11 +784,21 @@ namespace Barotrauma
return position; return position;
} }
public Vector2 GetRandomInterestingPosition(bool useSyncedRand, PositionType positionType) public Vector2 GetRandomInterestingPosition(bool useSyncedRand, PositionType positionType, bool avoidSubs)
{ {
if (!positionsOfInterest.Any()) return Size * 0.5f; if (!positionsOfInterest.Any()) return Size * 0.5f;
var matchingPositions = positionsOfInterest.FindAll(p => positionType.HasFlag(p.PositionType)); var matchingPositions = positionsOfInterest.FindAll(p => positionType.HasFlag(p.PositionType));
if (avoidSubs)
{
foreach (Submarine sub in Submarine.Loaded)
{
float minDist = Math.Max(sub.Borders.Width, sub.Borders.Height);
matchingPositions.RemoveAll(p => Vector2.Distance(p.Position, sub.WorldPosition) < minDist);
}
}
if (!matchingPositions.Any()) if (!matchingPositions.Any())
{ {
return positionsOfInterest[Rand.Int(positionsOfInterest.Count, !useSyncedRand)].Position; return positionsOfInterest[Rand.Int(positionsOfInterest.Count, !useSyncedRand)].Position;