Permalink
Browse files

fixed bug of not processing lockstep frames properly when processing …

…a big delta time since it was overriding Update()

method instead of considering the fixed time updates so lockstep logic wasnt processing any frames in
between the current frame and the ending frame of all fixed time updates. That was the reason replays with different speeds
were not validating the gamestate properly.
  • Loading branch information...
1 parent 32b032e commit 7639bf1cabfa1715e6f658dcf25febbab3193135 @acoppes committed Sep 24, 2016
@@ -48,9 +48,18 @@ public void Init()
// fixed time step based on http://gafferongames.com/game-physics/fix-your-timestep/
- const float maxAllowedFrameTime = 0.25f;
+ float maxAllowedFrameTime = 0.25f;
- public virtual void Update(float dt)
+ public float MaxAllowedFrameTime {
+ get {
+ return maxAllowedFrameTime;
+ }
+ set {
+ maxAllowedFrameTime = value;
+ }
+ }
+
+ public void Update(float dt)
{
if (dt > maxAllowedFrameTime)
dt = maxAllowedFrameTime;
@@ -59,11 +68,16 @@ public virtual void Update(float dt)
_gameTime += dt;
while (_accumulatedTime >= _fixedStepTime) {
- if (_gameLogic != null)
- _gameLogic.GameUpdate (_fixedStepTime, _currentGameFrame);
- _currentGameFrame++;
+ FixedTimeUpdate ();
_accumulatedTime -= _fixedStepTime;
}
}
+
+ protected virtual void FixedTimeUpdate()
+ {
+ if (_gameLogic != null)
+ _gameLogic.GameUpdate (_fixedStepTime, _currentGameFrame);
+ _currentGameFrame++;
+ }
}
}
@@ -23,7 +23,7 @@ public LockstepFixedUpdate(LockstepLogic lockstepLogic)
set;
}
- public override void Update (float dt)
+ protected override void FixedTimeUpdate ()
{
if (IsLockstepTurn ()) {
@@ -36,7 +36,7 @@ public override void Update (float dt)
}
// performs basic update logic...
- base.Update (dt);
+ base.FixedTimeUpdate ();
}
bool IsReady()
@@ -2,7 +2,6 @@
using NSubstitute;
using Gemserk.Lockstep;
-
public class TestLockstepLogic {
[Test]
@@ -81,11 +80,19 @@ public class TestLockstepLogic {
lockstepGameLogic.Update (0.1f);
lockstepGameLogic.Update (0.1f);
+ // didnt process lockstep turn yet
+ lockstepLogic.DidNotReceive ().IsReady (Arg.Any<int> ());
+ lockstepLogic.DidNotReceive ().Process (Arg.Any<int> ());
+
Assert.That (lockstepGameLogic.IsLockstepTurn(), Is.True);
+ lockstepLogic.ClearReceivedCalls ();
+
+ // if update to low, then even if it is lockstep turn, it didnt process it
lockstepGameLogic.Update (0.002f);
- Assert.That (lockstepGameLogic.IsLockstepTurn(), Is.False);
+ lockstepLogic.DidNotReceive ().IsReady (Arg.Any<int> ());
+ lockstepLogic.DidNotReceive ().Process (Arg.Any<int> ());
}
[Test]
@@ -106,6 +113,26 @@ public class TestLockstepLogic {
Assert.That (lockstepGameLogic.IsLastFrameForNextLockstep (8), Is.EqualTo (false));
}
+ [Test]
+ public void TestProcessCallsWhenBigUpdateTime(){
+
+ var lockstepLogic = NSubstitute.Substitute.For<LockstepLogic> ();
+
+ LockstepFixedUpdate lockstepGameLogic = new LockstepFixedUpdate (lockstepLogic);
+
+ lockstepGameLogic.FixedStepTime = 0.1f;
+ lockstepGameLogic.MaxAllowedFrameTime = 100.0f;
+ lockstepGameLogic.GameFramesPerLockstep = 5;
+
+ lockstepLogic.IsReady (Arg.Any<int> ()).Returns (true);
+
+ lockstepGameLogic.Update (1.2f);
+
+ lockstepLogic.Received (2).Process(Arg.Any<int>());
+
+// lockstepLogic.Recei
+ }
+
}

0 comments on commit 7639bf1

Please sign in to comment.