Skip to content

Commit

Permalink
attempt at queueing asteroid updates
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 17, 2024
1 parent 2b93552 commit d330e34
Showing 1 changed file with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ public class AsteroidSpawner
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 const int GravityChecksPerTick = 1;

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


private class PlayerMovementData
{
public Vector3D LastPosition { get; set; }
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -119,6 +131,9 @@ public void LoadAsteroidState()

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

// Add to update queue
_updateQueue.Enqueue(asteroid);
}
}
}
Expand Down Expand Up @@ -345,7 +360,7 @@ public void UpdateTick()
}
else
{
UpdateAsteroids(playerZones.Values.ToList());
ProcessAsteroidUpdates();
_updateIntervalTimer = AsteroidSettings.UpdateInterval;
}

Expand Down Expand Up @@ -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++)
Expand Down

0 comments on commit d330e34

Please sign in to comment.