Skip to content

Commit

Permalink
Update AsteroidSpawner.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 17, 2024
1 parent 46d012d commit 2b93552
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ public class AsteroidSpawner
private List<AsteroidState> _despawnedAsteroids = new List<AsteroidState>();
private List<AsteroidNetworkMessage> _networkMessages = new List<AsteroidNetworkMessage>();
private Dictionary<long, AsteroidZone> playerZones = new Dictionary<long, AsteroidZone>();

private Dictionary<long, PlayerMovementData> playerMovementData = new Dictionary<long, PlayerMovementData>();
private Queue<AsteroidEntity> gravityCheckQueue = new Queue<AsteroidEntity>();
private const int GravityChecksPerTick = 10;

private Queue<AsteroidEntity> _updateQueue = new Queue<AsteroidEntity>();
private const int UpdatesPerTick = 50; // Adjust this number based on performance needs


private class PlayerMovementData
{
Expand Down Expand Up @@ -111,6 +116,9 @@ public void LoadAsteroidState()
asteroid.EntityId = state.EntityId;
_asteroids.Add(asteroid);
MyEntities.Add(asteroid);

// Add to gravity check queue
gravityCheckQueue.Enqueue(asteroid);
}
}
}
Expand Down Expand Up @@ -147,6 +155,9 @@ private void LoadAsteroidsInRange(Vector3D playerPosition, AsteroidZone zone)
MyAPIGateway.Multiplayer.SendMessageToOthers(32000, messageBytes);

_despawnedAsteroids.Remove(state);

// Add to gravity check queue
gravityCheckQueue.Enqueue(asteroid);
}
}

Expand Down Expand Up @@ -358,6 +369,8 @@ public void UpdateTick()
}
}

ProcessGravityCheckQueue();

if (AsteroidSettings.EnableLogging)
MyAPIGateway.Utilities.ShowNotification($"Active Asteroids: {_asteroids.Count}", 1000 / 60);
}
Expand All @@ -367,6 +380,23 @@ public void UpdateTick()
}
}

private void ProcessGravityCheckQueue()
{
for (int i = 0; i < GravityChecksPerTick && gravityCheckQueue.Count > 0; i++)
{
var asteroid = gravityCheckQueue.Dequeue();
if (IsInNaturalGravity(asteroid.PositionComp.GetPosition()))
{
RemoveAsteroid(asteroid);
}
else
{
// Re-enqueue if still valid
gravityCheckQueue.Enqueue(asteroid);
}
}
}

private void UpdateAsteroids(List<AsteroidZone> zones)
{
Log.Info($"Updating asteroids. Total asteroids: {_asteroids.Count}, Total zones: {zones.Count}");
Expand Down Expand Up @@ -404,6 +434,9 @@ private void UpdateAsteroids(List<AsteroidZone> zones)
}
currentZone.AsteroidCount++;
}

// Add to gravity check queue
gravityCheckQueue.Enqueue(asteroid);
}

Log.Info($"Update complete. Removed asteroids: {removedCount}, Remaining asteroids: {_asteroids.Count}");
Expand Down Expand Up @@ -509,6 +542,9 @@ public void SpawnAsteroids(List<AsteroidZone> zones)
var message = new AsteroidNetworkMessage(newPosition, size, newVelocity, Vector3D.Zero, type, false, asteroid.EntityId, false, true, rotation);
_networkMessages.Add(message);
asteroidsSpawned++;

// Add to gravity check queue
gravityCheckQueue.Enqueue(asteroid);
}
}

Expand Down

0 comments on commit 2b93552

Please sign in to comment.