Skip to content

Commit

Permalink
attempt to stop double loading
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 12, 2024
1 parent eb1c41f commit 7d49652
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void LoadAsteroidState()
if (!MyAPIGateway.Session.IsServer)
return;

_asteroids.Clear(); // Clear existing asteroids to avoid double loading

if (MyAPIGateway.Utilities.FileExistsInLocalStorage("asteroid_states.dat", typeof(AsteroidSpawner)))
{
byte[] stateBytes;
Expand All @@ -80,8 +82,17 @@ private void LoadAsteroidsInRange(Vector3D playerPosition)
foreach (var state in _despawnedAsteroids.ToArray())
{
double distanceSquared = Vector3D.DistanceSquared(state.Position, playerPosition);

if (distanceSquared < AsteroidSettings.AsteroidSpawnRadius * AsteroidSettings.AsteroidSpawnRadius)
{
bool tooClose = _asteroids.Any(a => Vector3D.DistanceSquared(a.PositionComp.GetPosition(), state.Position) < AsteroidSettings.MinDistanceFromPlayer * AsteroidSettings.MinDistanceFromPlayer);

if (tooClose)
{
Log.Info($"Skipping respawn of asteroid at {state.Position} due to proximity to other asteroids");
continue;
}

Log.Info($"Respawning asteroid at {state.Position} due to player re-entering range");
var asteroid = AsteroidEntity.CreateAsteroid(state.Position, state.Size, Vector3D.Zero, state.Type);
_asteroids.Add(asteroid);
Expand All @@ -95,7 +106,6 @@ private void LoadAsteroidsInRange(Vector3D playerPosition)
}
}


public void Close()
{
if (!MyAPIGateway.Session.IsServer)
Expand Down Expand Up @@ -237,4 +247,3 @@ private Vector3D RandVector()
}

}

0 comments on commit 7d49652

Please sign in to comment.