3 Commits

12 changed files with 188 additions and 39 deletions

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Syntriax.Engine.Core;
public class CoroutineManager : UniverseObject
public class CoroutineManager : Behaviour
{
private readonly List<IEnumerator> enumerators = [];
@@ -18,12 +18,12 @@ public class CoroutineManager : UniverseObject
enumerators.Remove(enumerator);
}
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
universe.OnUpdate.AddListener(OnUpdate);
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
universe.OnUpdate.RemoveListener(OnUpdate);
}

View File

@@ -5,11 +5,14 @@ namespace Syntriax.Engine.Core;
public class Event
{
private readonly List<EventHandler> listeners = new(8);
private readonly List<EventHandler> listeners = new(4);
private readonly List<EventHandler> onceListeners = new(2);
public void AddListener(EventHandler listener) => listeners.Add(listener);
public void AddOnceListener(EventHandler listener) => onceListeners.Add(listener);
public void RemoveListener(EventHandler listener) => listeners.Remove(listener);
public void Clear() => listeners.Clear();
public void RemoveOnceListener(EventHandler listener) => onceListeners.Remove(listener);
public void Clear() { listeners.Clear(); onceListeners.Clear(); }
public void Invoke()
{
for (int i = 0; i < listeners.Count; i++)
@@ -19,6 +22,17 @@ public class Event
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
{
try { onceListeners[i].Invoke(); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Method.DeclaringType?.FullName}.{onceListeners[i].Method.Name}()";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
onceListeners.RemoveAt(i);
}
}
public delegate void EventHandler();
@@ -26,20 +40,34 @@ public class Event
public class Event<TSender>
{
private readonly List<EventHandler> listeners = new(8);
private readonly List<EventHandler> listeners = new(4);
private readonly List<EventHandler> onceListeners = new(2);
public void AddListener(EventHandler listener) => listeners.Add(listener);
public void AddOnceListener(EventHandler listener) => onceListeners.Add(listener);
public void RemoveListener(EventHandler listener) => listeners.Remove(listener);
public void Clear() => listeners.Clear();
public void Invoke(TSender argument)
public void RemoveOnceListener(EventHandler listener) => onceListeners.Remove(listener);
public void Clear() { listeners.Clear(); onceListeners.Clear(); }
public void Invoke(TSender sender)
{
for (int i = 0; i < listeners.Count; i++)
try { listeners[i].Invoke(argument); }
try { listeners[i].Invoke(sender); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({argument})";
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({sender})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
{
try { onceListeners[i].Invoke(sender); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Method.DeclaringType?.FullName}.{onceListeners[i].Method.Name}({sender})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
onceListeners.RemoveAt(i);
}
}
public delegate void EventHandler(TSender sender);
@@ -47,11 +75,14 @@ public class Event<TSender>
public class Event<TSender, TArguments>
{
private readonly List<EventHandler> listeners = new(8);
private readonly List<EventHandler> listeners = new(4);
private readonly List<EventHandler> onceListeners = new(2);
public void AddListener(EventHandler listener) => listeners.Add(listener);
public void AddOnceListener(EventHandler listener) => onceListeners.Add(listener);
public void RemoveListener(EventHandler listener) => listeners.Remove(listener);
public void Clear() => listeners.Clear();
public void RemoveOnceListener(EventHandler listener) => onceListeners.Remove(listener);
public void Clear() { listeners.Clear(); onceListeners.Clear(); }
public void Invoke(TSender sender, TArguments args)
{
for (int i = 0; i < listeners.Count; i++)
@@ -61,6 +92,17 @@ public class Event<TSender, TArguments>
string methodCallRepresentation = $"{listeners[i].Method.DeclaringType?.FullName}.{listeners[i].Method.Name}({string.Join(", ", sender, args)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
{
try { onceListeners[i].Invoke(sender, args); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Method.DeclaringType?.FullName}.{onceListeners[i].Method.Name}({string.Join(", ", sender, args)})";
Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}");
}
onceListeners.RemoveAt(i);
}
}
public delegate void EventHandler(TSender sender, TArguments args);

View File

@@ -2,7 +2,7 @@ using System.Collections.Generic;
namespace Syntriax.Engine.Core;
public class DrawManager : UniverseObject
public class DrawManager : Behaviour
{
// We use Descending order because draw calls are running from last to first
private static Comparer<IBehaviour> SortByDescendingPriority() => Comparer<IBehaviour>.Create((x, y) => y.Priority.CompareTo(x.Priority));
@@ -29,7 +29,7 @@ public class DrawManager : UniverseObject
postDrawEntities[i].PostDraw();
}
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
preDrawEntities.Assign(universe);
drawEntities.Assign(universe);
@@ -40,7 +40,7 @@ public class DrawManager : UniverseObject
universe.OnPostDraw.AddListener(OnPostDraw);
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
preDrawEntities.Unassign();
drawEntities.Unassign();

View File

@@ -2,7 +2,7 @@ using System.Collections.Generic;
namespace Syntriax.Engine.Core;
public class UpdateManager : UniverseObject
public class UpdateManager : Behaviour
{
// We use Ascending order because draw calls are running from last to first
private static Comparer<IBehaviour> SortByAscendingPriority() => Comparer<IBehaviour>.Create((x, y) => x.Priority.CompareTo(y.Priority));
@@ -14,7 +14,7 @@ public class UpdateManager : UniverseObject
private readonly List<IFirstFrameUpdate> toCallFirstFrameUpdates = new(32);
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
firstFrameUpdates.Assign(universe);
preUpdateEntities.Assign(universe);
@@ -26,7 +26,7 @@ public class UpdateManager : UniverseObject
universe.OnPostUpdate.AddListener(OnPostUpdate);
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
firstFrameUpdates.Unassign();
preUpdateEntities.Unassign();

View File

@@ -1,10 +1,8 @@
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Integration.MonoGame;
public interface IDrawableSprite : IBehaviour
{
void Draw(SpriteBatch spriteBatch);
void Draw(ISpriteBatch spriteBatch);
}

View File

@@ -0,0 +1,29 @@
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Integration.MonoGame;
public interface ISpriteBatch
{
void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color);
void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color);
void Draw(Texture2D texture, Vector2D position, Color color);
void Draw(Texture2D texture, AABB destinationAABB, Color color);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl);
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color);
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl);
void End();
}

View File

@@ -0,0 +1,64 @@
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Integration.MonoGame;
public class SpriteBatchWrapper(GraphicsDevice graphicsDevice) : ISpriteBatch
{
private readonly SpriteBatch spriteBatch = new(graphicsDevice);
public void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null)
=> spriteBatch.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, Vector2D position, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), color);
public void Draw(Texture2D texture, AABB destinationAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), color);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth, rtl);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth, rtl);
public void End()
=> spriteBatch.End();
}

View File

@@ -1,7 +1,5 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Integration.MonoGame;
@@ -9,8 +7,10 @@ namespace Syntriax.Engine.Integration.MonoGame;
public class SpriteBatcher : BehaviourBase, IFirstFrameUpdate, IDraw
{
private static Comparer<IBehaviour> SortByPriority() => Comparer<IBehaviour>.Create((x, y) => y.Priority.CompareTo(x.Priority));
private SpriteBatch spriteBatch = null!;
private ISpriteBatch spriteBatch = null!;
private MonoGameCamera2DBehaviour camera2D = null!;
private readonly ActiveBehaviourCollectorSorted<IDrawableSprite> drawableSprites = new() { SortBy = SortByPriority() };
public void FirstActiveFrame()
@@ -18,7 +18,7 @@ public class SpriteBatcher : BehaviourBase, IFirstFrameUpdate, IDraw
MonoGameWindowContainer windowContainer = Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = Universe.FindRequiredBehaviour<MonoGameCamera2DBehaviour>();
spriteBatch = new(windowContainer.Window.GraphicsDevice);
spriteBatch = new SpriteBatchWrapper(windowContainer.Window.GraphicsDevice);
drawableSprites.Unassign();
drawableSprites.Assign(Universe);
}

View File

@@ -13,18 +13,25 @@ public static class EngineConverterExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UniverseTime ToEngineTime(this GameTime gameTime) => new(gameTime.TotalGameTime, gameTime.ElapsedGameTime);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2D ToVector2D(this Vector2 vector) => new(vector.X, vector.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color ToColor(this ColorRGBA rgba) => new(rgba.R, rgba.G, rgba.B, rgba.A);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2D ToVector2D(this Point point) => new(point.X, point.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToVector2(this Vector2D vector) => new(vector.X, vector.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToDisplayVector2(this Vector2D vector) => vector.Scale(screenScale).ToVector2();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2D ApplyDisplayScale(this Vector2D vector) => vector.Scale(screenScale);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToDisplayRectangle(this Rectangle rectangle, DisplayMode displayMode) => new()
{
@@ -33,4 +40,13 @@ public static class EngineConverterExtensions
Width = rectangle.Width,
Height = rectangle.Height
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToRectangle(this AABB aabb) => new()
{
X = (int)(aabb.LowerBoundary.X * screenScale.X),
Y = (int)(aabb.LowerBoundary.Y * screenScale.Y),
Width = (int)(aabb.UpperBoundary.X - aabb.LowerBoundary.X),
Height = (int)(aabb.UpperBoundary.Y - aabb.LowerBoundary.Y)
};
}

View File

@@ -5,7 +5,7 @@ using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
public class PhysicsCoroutineManager : UniverseObject
public class PhysicsCoroutineManager : Behaviour
{
private readonly Event<IUniverse, IUniverse.UpdateArguments>.EventHandler delegateOnUpdate = null!;
@@ -23,9 +23,9 @@ public class PhysicsCoroutineManager : UniverseObject
enumerators.Remove(enumerator);
}
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
physicsEngine = universe.GetUniverseObject<IPhysicsEngine2D>();
physicsEngine = universe.FindRequiredBehaviour<IPhysicsEngine2D>();
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
foundPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
else
@@ -44,11 +44,11 @@ public class PhysicsCoroutineManager : UniverseObject
}
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
if (physicsEngine is IPhysicsEngine2D existingPhysicsEngine)
existingPhysicsEngine.OnPhysicsStep.RemoveListener(OnPhysicsStep);
universe.OnUpdate.RemoveListener(OnUpdate);
universe.OnUpdate.RemoveListener(delegateOnUpdate);
}
private void OnUpdate(IUniverse sender, IUniverse.UpdateArguments args)
@@ -56,11 +56,11 @@ public class PhysicsCoroutineManager : UniverseObject
if (Universe is not IUniverse universe)
return;
physicsEngine = universe.GetUniverseObject<IPhysicsEngine2D>();
physicsEngine = universe.FindRequiredBehaviour<IPhysicsEngine2D>();
if (physicsEngine is IPhysicsEngine2D foundPhysicsEngine)
{
foundPhysicsEngine.OnPhysicsStep.AddListener(OnPhysicsStep);
universe.OnUpdate.RemoveListener(OnUpdate);
universe.OnUpdate.RemoveListener(delegateOnUpdate);
}
}

View File

@@ -4,7 +4,7 @@ using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
public class PhysicsEngine2D : Behaviour, IPhysicsEngine2D
{
public Event<IPhysicsEngine2D, float> OnPhysicsIteration { get; } = new();
public Event<IPhysicsEngine2D, float> OnPhysicsStep { get; } = new();
@@ -174,7 +174,7 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
}
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Assign(universe);
physicsUpdateCollector.Assign(universe);
@@ -185,7 +185,7 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
universe.OnPreUpdate.AddListener(OnEnginePreUpdate);
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Unassign();
physicsUpdateCollector.Unassign();

View File

@@ -5,7 +5,7 @@ using Syntriax.Engine.Core;
namespace Syntriax.Engine.Systems.Tween;
public class TweenManager : UniverseObject, ITweenManager
public class TweenManager : Behaviour, ITweenManager
{
private CoroutineManager coroutineManager = null!;
@@ -73,12 +73,12 @@ public class TweenManager : UniverseObject, ITweenManager
Return((Tween)tween);
}
protected override void OnEnteringUniverse(IUniverse universe)
protected override void OnEnteredUniverse(IUniverse universe)
{
coroutineManager = universe.GetRequiredUniverseObject<CoroutineManager>();
coroutineManager = universe.FindRequiredBehaviour<CoroutineManager>();
}
protected override void OnExitingUniverse(IUniverse universe)
protected override void OnExitedUniverse(IUniverse universe)
{
coroutineManager = null!;
}