Skip to content

Commit

Permalink
feat: fixed time step
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Oct 13, 2023
1 parent 3b39073 commit 3b1a1fc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static void RegisterTransformComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(_ =>
{
return new TransformComponent { Position = new(16, 20) };
return new TransformComponent { Position = new(16, 6) };
}, "Player");

serviceRegistry.RegisterSingleton(_ =>
Expand Down
5 changes: 4 additions & 1 deletion src/Apps/GameDesktop/CompositionRoots/GameCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ namespace GameDesktop.CompositionRoots;

internal class GameCompositionRoot : ICompositionRoot
{
private const float TargetFramesPerSecond = 120.0f;
private const bool IsMouseVisible = true;

public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register(factory =>
{
Game game = new(factory.GetInstance<ILogger>(), factory.GetInstance<IServiceContainer>())
Game game = new(factory.GetInstance<ILogger>(),
factory.GetInstance<IServiceContainer>(),
TargetFramesPerSecond)
{
IsMouseVisible = IsMouseVisible, Content = { RootDirectory = AppVariable.ContentRootDirectory, }
};
Expand Down
21 changes: 17 additions & 4 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Features;
using System;
using Features;
using GameDesktop.CompositionRoots.Features;
using LightInject;
using Microsoft.Xna.Framework;
Expand All @@ -9,17 +10,21 @@ namespace GameDesktop;

public class Game : Microsoft.Xna.Framework.Game
{
private readonly float _targetTimeStep;

private readonly ILogger _logger;
private readonly IServiceContainer _container;

private Entitas.Extended.Feature _rootFeature;
private SpriteBatch _spriteBatch;
private float _accumulatedTime;


public Game(ILogger logger, IServiceContainer container)
public Game(ILogger logger, IServiceContainer container, float targetFramesPerSecond)
{
_logger = logger;
_container = container;
_targetTimeStep = 1 / targetFramesPerSecond;

_logger.ForContext<Game>().Verbose("ctor");
}
Expand Down Expand Up @@ -75,11 +80,19 @@ protected override void EndRun()
_logger.ForContext<Game>().Verbose("Ended");
}

private void FixedUpdate(GameTime fixedGameTime) => _rootFeature.FixedExecute(fixedGameTime);
// maybe fixed update is incorrect. needs review in the future
private void FixedUpdate(GameTime gameTime) => _rootFeature.FixedExecute(gameTime);

protected override void Update(GameTime gameTime)
{
FixedUpdate(gameTime);
_accumulatedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

while (_accumulatedTime >= _targetTimeStep)
{
FixedUpdate(gameTime);

_accumulatedTime -= _targetTimeStep;
}

base.Update(gameTime);
_rootFeature.Execute(gameTime);
Expand Down
2 changes: 2 additions & 0 deletions src/Libs/Systems/CollisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Systems;

// Input & Collision systems both have to be fixed execute systems,
// otherwise it'll lead to the desynchronized behaviour.
public class CollisionSystem : IFixedExecuteSystem
{
private readonly IGroup<GameEntity> _group;
Expand Down
8 changes: 5 additions & 3 deletions src/Libs/Systems/InputSystem.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Entitas;
using Entitas.Extended;
using Microsoft.Xna.Framework;
using Serilog;
using Services.Input;
using IExecuteSystem = Entitas.Extended.IExecuteSystem;

namespace Systems;

// Input & Collision systems both have to be fixed execute systems,
// otherwise it'll lead to the desynchronized behaviour.
[Input]
public class InputSystem : IExecuteSystem
public class InputSystem : IFixedExecuteSystem
{
private readonly IInputScanner _inputScanner;
private readonly IGroup<GameEntity> _group;
Expand All @@ -20,7 +22,7 @@ public InputSystem(IInputScanner inputScanner, IGroup<GameEntity> group, ILogger
_logger = logger;
}

public void Execute(GameTime gameTime)
public void FixedExecute(GameTime gameTime)
{
try
{
Expand Down
5 changes: 2 additions & 3 deletions src/Libs/Systems/MovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace Systems;

// TODO: correct fixed execution in the game loop
public class MovementSystem : IFixedExecuteSystem
public class MovementSystem : IExecuteSystem
{
private readonly IGroup<GameEntity> _group;
private readonly IMovement _movement;
Expand All @@ -21,7 +20,7 @@ public MovementSystem(IMovement movement, IGroup<GameEntity> group, ILogger logg
_logger = logger;
}

public void FixedExecute(GameTime fixedGameTime)
public void Execute(GameTime fixedGameTime)
{
try
{
Expand Down

0 comments on commit 3b1a1fc

Please sign in to comment.