Skip to content

Commit

Permalink
Fixed devil square wave timings
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Aug 28, 2023
1 parent 9a4445a commit a6c6549
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/GameLogic/MiniGames/MiniGameContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MUnique.OpenMU.GameLogic.MiniGames;

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using MUnique.OpenMU.DataModel.Statistics;
using MUnique.OpenMU.GameLogic.Attributes;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class MiniGameContext : AsyncDisposable, IEventStateProvider
private readonly HashSet<byte> _currentSpawnWaves = new();
private readonly List<ChangeEventContext> _remainingEvents = new();

private Stopwatch? _elapsedTimeSinceStart;

/// <summary>
/// Initializes a new instance of the <see cref="MiniGameContext"/> class.
/// </summary>
Expand Down Expand Up @@ -250,6 +253,9 @@ protected async ValueTask ForEachPlayerAsync(Func<Player, Task> playerAction)
/// <param name="players">The player which started with the game.</param>
protected virtual async ValueTask OnGameStartAsync(ICollection<Player> players)
{
this._elapsedTimeSinceStart = new Stopwatch();
this._elapsedTimeSinceStart.Start();

var startEvents = this.Definition.ChangeEvents
.OrderBy(e => e.Index)
.TakeWhile(e => e is { Index: <= 0, NumberOfKills: 0 })
Expand Down Expand Up @@ -609,9 +615,10 @@ private async ValueTask RunSpawnWaveAsync(MiniGameSpawnWave spawnWave, Cancellat
return;
}

if (spawnWave.StartTime > TimeSpan.Zero)
var requiredDelay = spawnWave.StartTime - this._elapsedTimeSinceStart?.Elapsed;
if (requiredDelay > TimeSpan.Zero)
{
await Task.Delay(spawnWave.StartTime, cancellationToken).ConfigureAwait(false);
await Task.Delay(requiredDelay.Value, cancellationToken).ConfigureAwait(false);
}

this.Logger.LogDebug("{context}: Starting next wave: {wave}", this, spawnWave.Description);
Expand Down

0 comments on commit a6c6549

Please sign in to comment.