Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions Examples/Scenes/ExampleScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,29 @@ protected void HandleInput(float dt, Vector2 mousePosGame, Vector2 mousePosGameU
}

}
// var pausedState = GAMELOOP.InputActionPause.Consume();
// if (pausedState is { Consumed: false, Pressed: true })
// {
// GAMELOOP.Paused = !GAMELOOP.Paused;
// }


if (GameloopExamples.Instance.Paused) return;



var resetState = GameloopExamples.Instance.InputActionReset.Consume(out _);
if (resetState is { Consumed: false, Pressed: true })
{
Reset();
}

HandleZoom(dt);
// float zoomIncrement = 0.05f;
// var zoomInState = input.ConsumeAction(GameloopExamples.InputZoomInID);
// if (zoomInState is { Consumed: false, Pressed: true })
// {
// GAMELOOP.Camera.Zoom(-zoomIncrement);
// }
//
// var zoomOutState = input.ConsumeAction(GameloopExamples.InputZoomOutID);
// if (zoomOutState is { Consumed: false, Pressed: true })
// {
// GAMELOOP.Camera.Zoom(zoomIncrement);
// }
}

protected override void OnUpdate(GameTime time, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui)
protected override void OnHandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi)
{
HandleInput(time.Delta, game.MousePos, gameUi.MousePos, ui.MousePos);
HandleInput(time.Delta, mousePosGame, mousePosGameUi, mousePosUi);

if (GameloopExamples.Instance.Paused) return;
OnHandleInputExample(time.Delta, game.MousePos, gameUi.MousePos, ui.MousePos);
OnHandleInputExample(time.Delta, mousePosGame, mousePosGameUi, mousePosUi);
}

protected override void OnUpdate(GameTime time, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui)
{
if (GameloopExamples.Instance.Paused) return;
OnUpdateExample(time, game, gameUi, ui);
}
protected override void OnDrawGame(ScreenInfo game)
Expand Down
136 changes: 22 additions & 114 deletions ShapeEngine/Core/GameDef/GameCustomEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using ShapeEngine.Core.Structs;

namespace ShapeEngine.Core.GameDef;
Expand Down Expand Up @@ -42,90 +43,26 @@ protected CustomEvent(int order)
}

/// <summary>
/// Called before the fixed update step. Override to execute logic before fixed updates.
/// </summary>
/// <param name="fixedTime">The current fixed game time.</param>
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// Only called when <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c>!
/// Fixed Physics call order:
/// <list type="bullet">
/// <item><see cref="PreUpdate"/></item>
/// <item><see cref="PostUpdate"/></item>
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PreFixedUpdate(GameTime fixedTime, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }

/// <summary>
/// Called after the fixed update step. Override to execute logic after fixed updates.
/// </summary>
/// <param name="fixedTime">The current fixed game time.</param>
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// Only called when <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c>!
/// Fixed Physics call order:
/// <list type="bullet">
/// <item><see cref="PreUpdate"/></item>
/// <item><see cref="PostUpdate"/></item>
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PostFixedUpdate(GameTime fixedTime, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }

/// <summary>
/// Called before interpolating the fixed update. Override to execute logic before interpolation.
/// Called before input handling for the current frame or tick.
/// Override to perform input-related logic that should run prior to the game's <c>HandleInput</c> method.
/// </summary>
/// <param name="time">The current game time.</param>
/// <param name="alpha">Interpolation alpha value.</param>
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// Only called when <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c>!
/// Fixed Physics call order:
/// <list type="bullet">
/// <item><see cref="PreUpdate"/></item>
/// <item><see cref="PostUpdate"/></item>
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PreInterpolateFixedUpdate(GameTime time, float alpha, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }

/// <param name="mousePosGame">Mouse position in game world coordinates.</param>
/// <param name="mousePosGameUi">Mouse position transformed to the in-game UI coordinate space.</param>
/// <param name="mousePosUi">Mouse position in general UI/screen coordinates.</param>
/// <remarks>Default implementation performs no action.</remarks>
protected virtual void PreHandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi) { }

/// <summary>
/// Called after interpolating the fixed update. Override to execute logic after interpolation.
/// Called after input handling for the current frame or tick.
/// Override to perform input-related logic that should run after the game's <c>HandleInput</c> method.
/// </summary>
/// <param name="time">The current game time.</param>
/// <param name="alpha">Interpolation alpha value.</param>
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// Only called when <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c>!
/// Fixed Physics call order:
/// <list type="bullet">
/// <item><see cref="PreUpdate"/></item>
/// <item><see cref="PostUpdate"/></item>
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PostInterpolateFixedUpdate(GameTime time, float alpha, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }
/// <param name="mousePosGame">Mouse position in game world coordinates.</param>
/// <param name="mousePosGameUi">Mouse position transformed to the in-game UI coordinate space.</param>
/// <param name="mousePosUi">Mouse position in general UI/screen coordinates.</param>
/// <remarks>Default implementation performs no action.</remarks>
protected virtual void PostHandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi) { }

/// <summary>
/// Called before the update step. Override to execute logic before updates.
Expand All @@ -134,15 +71,6 @@ protected virtual void PostInterpolateFixedUpdate(GameTime time, float alpha, Sc
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// When <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c> this function will be called before:
/// <list type="bullet">
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PreUpdate(GameTime time, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }

/// <summary>
Expand All @@ -152,15 +80,6 @@ protected virtual void PreUpdate(GameTime time, ScreenInfo gameScreenInfo, Scree
/// <param name="gameScreenInfo">Screen info for the game view.</param>
/// <param name="gameUiScreenInfo">Screen info for the game UI.</param>
/// <param name="uiScreenInfo">Screen info for the general UI.</param>
/// <remarks>
/// When <see cref="Game.FixedPhysicsEnabled"/> is set to <c>true</c> this function will be called before:
/// <list type="bullet">
/// <item><see cref="PreFixedUpdate"/></item>
/// <item><see cref="PostFixedUpdate"/></item>
/// <item><see cref="PreInterpolateFixedUpdate"/></item>
/// <item><see cref="PostInterpolateFixedUpdate"/></item>
/// </list>
/// </remarks>
protected virtual void PostUpdate(GameTime time, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo) { }

/// <summary>
Expand Down Expand Up @@ -200,15 +119,11 @@ protected virtual void PreDrawUi(ScreenInfo info) { }
protected virtual void PostDrawUi(ScreenInfo info) { }


internal void TriggerOnFixedUpdate(bool pre, GameTime fixedTime, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)
{
if(pre) PreFixedUpdate(fixedTime, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
else PostFixedUpdate(fixedTime, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
}
internal void TriggerOnInterpolateFixedUpdate(bool pre, GameTime time, float alpha, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)

internal void TriggerOnHandleInput(bool pre, GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi)
{
if(pre) PreInterpolateFixedUpdate(time, alpha, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
else PostInterpolateFixedUpdate(time, alpha, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
if(pre) PreHandleInput(time, mousePosGame, mousePosGameUi, mousePosUi);
else PostHandleInput(time, mousePosGame, mousePosGameUi, mousePosUi);
}
internal void TriggerOnUpdate(bool pre, GameTime time, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)
{
Expand Down Expand Up @@ -291,18 +206,11 @@ public int CompareTo(CustomEvent? other)
/// </summary>
public void ClearCustomEvents() => customEvents.Clear();

private void TriggerCustomEventsOnFixedUpdate(bool pre, GameTime fixedTime, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)
{
foreach (var intervalEvent in customEvents)
{
intervalEvent.TriggerOnFixedUpdate(pre, fixedTime, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
}
}
private void TriggerCustomEventsOnInterpolateFixedUpdate(bool pre, GameTime time, float alpha, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)
private void TriggerCustomEventsOnHandleInput(bool pre, GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi)
{
foreach (var intervalEvent in customEvents)
{
intervalEvent.TriggerOnInterpolateFixedUpdate(pre, time, alpha, gameScreenInfo, gameUiScreenInfo, uiScreenInfo);
intervalEvent.TriggerOnHandleInput(pre, time, mousePosGame, mousePosGameUi, mousePosUi);
}
}
private void TriggerCustomEventsOnUpdate(bool pre, GameTime time, ScreenInfo gameScreenInfo, ScreenInfo gameUiScreenInfo, ScreenInfo uiScreenInfo)
Expand Down
2 changes: 2 additions & 0 deletions ShapeEngine/Core/GameDef/GameGameloop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ private void RunGameloop()
UpdateFlashes(dt);
}

ResolveHandleInput();

if (FixedFramerate > 0)//fixed update loop
{
fixedTimestepAccumulator += frameDelta;
Expand Down
16 changes: 8 additions & 8 deletions ShapeEngine/Core/GameDef/GameResolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ private void ResolveUpdate()
TriggerCustomEventsOnUpdate(false, UpdateTime, GameScreenInfo, GameUiScreenInfo, UIScreenInfo);
}

// private void ResolveInterpolateFixedUpdate(float f)
// {
// TriggerCustomEventsOnInterpolateFixedUpdate(true, Time, f, GameScreenInfo, GameUiScreenInfo, UIScreenInfo);
// InterpolateFixedUpdate(Time, GameScreenInfo, GameUiScreenInfo, UIScreenInfo, f);
// CurScene.ResolveInterpolateFixedUpdate(Time, GameScreenInfo, GameUiScreenInfo, UIScreenInfo, f);
// TriggerCustomEventsOnInterpolateFixedUpdate(false, Time, f, GameScreenInfo, GameUiScreenInfo, UIScreenInfo);
// }

private void ResolveHandleInput()
{
TriggerCustomEventsOnHandleInput(true, Time, GameScreenInfo.MousePos, GameUiScreenInfo.MousePos, UIScreenInfo.MousePos);
HandleInput(Time, GameScreenInfo.MousePos, GameUiScreenInfo.MousePos, UIScreenInfo.MousePos);
CurScene.ResolveHandleInput(Time, GameScreenInfo.MousePos, GameUiScreenInfo.MousePos, UIScreenInfo.MousePos);
TriggerCustomEventsOnHandleInput(false, Time, GameScreenInfo.MousePos, GameUiScreenInfo.MousePos, UIScreenInfo.MousePos);
}
private void ResolveOnGameTextureResized(int w, int h)
{
OnGameTextureResized(w, h);
Expand Down
16 changes: 15 additions & 1 deletion ShapeEngine/Core/GameDef/GameVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ protected virtual void LoadContent()
protected virtual void BeginRun()
{
}


/// <summary>
/// Processes input for the current frame.
/// Called each frame to handle and dispatch input events before the game update.
/// </summary>
/// <param name="time">Timing information for the current frame.</param>
/// <param name="mousePosGame">Mouse position in game world coordinates.</param>
/// <param name="mousePosGameUi">Mouse position in the game's UI coordinate space.</param>
/// <param name="mousePosUi">Mouse position in the global UI coordinate space.</param>
/// <remarks>
/// This method is synchronous with rendering and is not affected by fixed framerate settings.
/// Override to implement custom input handling and event dispatch.
/// </remarks>
protected virtual void HandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi) { }

/// <summary>
/// Updates game state when the fixed framerate is disabled.
/// This is the standard update method
Expand Down
31 changes: 26 additions & 5 deletions ShapeEngine/Core/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ public void Close()
RemovePathfinder();
OnClose();
}

internal void ResolveHandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi)
{
OnHandleInput(time, mousePosGame, mousePosGameUi, mousePosUi);
}
internal void ResolveUpdate(GameTime time, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui)
{
SpawnArea?.Update(time, game, gameUi, ui);
Expand Down Expand Up @@ -381,12 +386,28 @@ protected virtual void OnClose()
protected virtual void OnGameTextureResized(int w, int h) { }

/// <summary>
/// Called every frame. Called before FixedUpdate if fixed framerate is enabled.
/// Handle input for the scene. Override to process per-frame input using the provided timing
/// information and mouse coordinates in three coordinate spaces.
/// Default implementation does nothing.
/// </summary>
/// <param name="time"></param>
/// <param name="game"></param>
/// <param name="gameUi"></param>
/// <param name="ui"></param>
/// <param name="time">Frame timing and delta information.</param>
/// <param name="mousePosGame">Mouse position in game world coordinates (camera-transformed).</param>
/// <param name="mousePosGameUi">Mouse position in the game UI render target coordinates.</param>
/// <param name="mousePosUi">Mouse position in the main UI (screen) coordinates.</param>
protected virtual void OnHandleInput(GameTime time, Vector2 mousePosGame, Vector2 mousePosGameUi, Vector2 mousePosUi) { }

/// <summary>
/// Called once per frame to update the scene's logic. Override this method to perform
/// per-frame updates such as game object state updates, AI, timers, etc.
/// The default implementation does nothing.
/// </summary>
/// <param name="time">Frame timing and delta information.</param>
/// <param name="game">Screen information for the game render target (camera-transformed).</param>
/// <param name="gameUi">Screen information for the game UI render target (not affected by camera).</param>
/// <param name="ui">Screen information for the main UI (screen coordinates).</param>
/// <remarks>
/// This method is subject to fixed framerate settings and dynamic substepping, if enabled.
/// </remarks>
protected virtual void OnUpdate(GameTime time, ScreenInfo game, ScreenInfo gameUi, ScreenInfo ui) { }

/// <summary>
Expand Down
Loading