From d330e341863816d30868f4dce8b7cb14a2783ebf Mon Sep 17 00:00:00 2001 From: InvalidArgument3 Date: Sun, 16 Jun 2024 21:29:22 -0500 Subject: [PATCH] attempt at queueing asteroid updates --- .../AsteroidEntities/AsteroidSpawner.cs | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs index 1d951a0e..7517f4b7 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs @@ -40,12 +40,11 @@ public class AsteroidSpawner private Dictionary playerZones = new Dictionary(); private Dictionary playerMovementData = new Dictionary(); private Queue gravityCheckQueue = new Queue(); - private const int GravityChecksPerTick = 10; + private const int GravityChecksPerTick = 1; private Queue _updateQueue = new Queue(); private const int UpdatesPerTick = 50; // Adjust this number based on performance needs - private class PlayerMovementData { public Vector3D LastPosition { get; set; } @@ -63,6 +62,12 @@ public void Init(int seed) _worldLoadTime = DateTime.UtcNow; rand = new Random(seed); AsteroidSettings.Seed = seed; + + // Add all asteroids to the update queue + foreach (var asteroid in _asteroids) + { + _updateQueue.Enqueue(asteroid); + } } public void SaveAsteroidState() @@ -85,6 +90,13 @@ public void SaveAsteroidState() { writer.Write(stateBytes, 0, stateBytes.Length); } + + // Ensure the update queue is saved as well + _updateQueue.Clear(); + foreach (var asteroid in _asteroids) + { + _updateQueue.Enqueue(asteroid); + } } public void LoadAsteroidState() @@ -119,6 +131,9 @@ public void LoadAsteroidState() // Add to gravity check queue gravityCheckQueue.Enqueue(asteroid); + + // Add to update queue + _updateQueue.Enqueue(asteroid); } } } @@ -345,7 +360,7 @@ public void UpdateTick() } else { - UpdateAsteroids(playerZones.Values.ToList()); + ProcessAsteroidUpdates(); _updateIntervalTimer = AsteroidSettings.UpdateInterval; } @@ -380,6 +395,29 @@ public void UpdateTick() } } + private void ProcessAsteroidUpdates() + { + int updatesProcessed = 0; + + while (updatesProcessed < UpdatesPerTick && _updateQueue.Count > 0) + { + var asteroid = _updateQueue.Dequeue(); + + // Perform the update logic for the asteroid here + UpdateAsteroid(asteroid); + + // Re-enqueue the asteroid for future updates + _updateQueue.Enqueue(asteroid); + + updatesProcessed++; + } + } + + private void UpdateAsteroid(AsteroidEntity asteroid) + { + // Implement the actual update logic for an individual asteroid here + } + private void ProcessGravityCheckQueue() { for (int i = 0; i < GravityChecksPerTick && gravityCheckQueue.Count > 0; i++)