Compare commits
	
		
			6 Commits
		
	
	
		
			net8
			...
			832514ba7d
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 832514ba7d | |||
| 877a004a13 | |||
| b1970d93f9 | |||
| e7bd924494 | |||
| 37b87f0f85 | |||
| 3b6a93d37a | 
@@ -3,25 +3,10 @@ using System.Collections.Generic;
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a controller for managing <see cref="IBehaviour"/>s and notify them accordingly about the engine's updates. Connected to an <see cref="IUniverseObject"/>.
 | 
			
		||||
/// Represents a controller for managing <see cref="IBehaviour"/>s. Connected to an <see cref="IUniverseObject"/>.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IBehaviourController : IEntity, IHasUniverseObject, IEnumerable<IBehaviour>
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered before the update of <see cref="IBehaviour"/>s.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event PreUpdateEventHandler? OnPreUpdate;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered during the update of <see cref="IBehaviour"/>s.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event UpdateEventHandler? OnUpdate;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered before the drawing phase.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event PreDrawEventHandler? OnPreDraw;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered when a <see cref="IBehaviour"/> is added to the <see cref="IBehaviourController"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
@@ -83,20 +68,6 @@ public interface IBehaviourController : IEntity, IHasUniverseObject, IEnumerable
 | 
			
		||||
    /// <param name="behaviour">The <see cref="IBehaviour"/> to remove.</param>
 | 
			
		||||
    void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates all <see cref="IBehaviour"/>s in the <see cref="IBehaviourController"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void Update();
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Performs pre-draw operations.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void UpdatePreDraw();
 | 
			
		||||
 | 
			
		||||
    delegate void PreUpdateEventHandler(IBehaviourController sender);
 | 
			
		||||
    delegate void UpdateEventHandler(IBehaviourController sender);
 | 
			
		||||
    delegate void PreDrawEventHandler(IBehaviourController sender);
 | 
			
		||||
    delegate void BehaviourAddedEventHandler(IBehaviourController sender, IBehaviour behaviourAdded);
 | 
			
		||||
    delegate void BehaviourRemovedEventHandler(IBehaviourController sender, IBehaviour behaviourRemoved);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,9 +18,24 @@ public interface IUniverse : IEntity, IEnumerable<IUniverseObject>
 | 
			
		||||
    event UpdateEventHandler? OnUpdate;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered when <see cref="PreDraw"/> is called on the <see cref="IUniverse"/>.
 | 
			
		||||
    /// Event triggered after <see cref="Update(UniverseTime)"/> is called on the <see cref="IUniverse"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event PreDrawEventHandler? OnPreDraw;
 | 
			
		||||
    event UpdateEventHandler? OnPostUpdate;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered when <see cref="Draw"/> is about to be called called on the <see cref="IUniverse"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event DrawEventHandler? OnPreDraw;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered when <see cref="Draw"/> is called on the <see cref="IUniverse"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event DrawEventHandler? OnDraw;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered after <see cref="Draw"/> is called on the <see cref="IUniverse"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    event DrawEventHandler? OnPostDraw;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Event triggered when a <see cref="IUniverseObject"/> is registered to the <see cref="IUniverse"/>.
 | 
			
		||||
@@ -84,14 +99,14 @@ public interface IUniverse : IEntity, IEnumerable<IUniverseObject>
 | 
			
		||||
    void Update(UniverseTime universeTime);
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Performs operations that should be done before the draw calls.
 | 
			
		||||
    /// Performs operations that should be done to the draw.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void PreDraw();
 | 
			
		||||
    void Draw();
 | 
			
		||||
 | 
			
		||||
    delegate void TimeScaleChangedEventHandler(IUniverse sender, float previousTimeScale);
 | 
			
		||||
 | 
			
		||||
    delegate void UpdateEventHandler(IUniverse sender, UniverseTime engineTime);
 | 
			
		||||
    delegate void PreDrawEventHandler(IUniverse sender);
 | 
			
		||||
    delegate void DrawEventHandler(IUniverse sender);
 | 
			
		||||
 | 
			
		||||
    delegate void UniverseObjectRegisteredEventHandler(IUniverse sender, IUniverseObject universeObjectRegistered);
 | 
			
		||||
    delegate void UniverseObjectUnRegisteredEventHandler(IUniverse sender, IUniverseObject universeObjectUnregistered);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,9 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
public abstract class Behaviour : BehaviourBase
 | 
			
		||||
public abstract class Behaviour : BehaviourBase, IFirstFrameUpdate,
 | 
			
		||||
    IPreUpdate, IUpdate, IPostUpdate,
 | 
			
		||||
    IPreDraw, IDraw, IPostDraw
 | 
			
		||||
{
 | 
			
		||||
    private bool isInitializedThisFrame = false;
 | 
			
		||||
 | 
			
		||||
    protected IUniverse Universe => BehaviourController.UniverseObject.Universe;
 | 
			
		||||
    protected IUniverseObject UniverseObject => BehaviourController.UniverseObject;
 | 
			
		||||
 | 
			
		||||
@@ -20,11 +20,6 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
    protected virtual void OnInitialize() { }
 | 
			
		||||
    protected virtual void OnInitialize(IInitializable _)
 | 
			
		||||
    {
 | 
			
		||||
        isInitializedThisFrame = true;
 | 
			
		||||
 | 
			
		||||
        BehaviourController.OnPreUpdate += PreUpdate;
 | 
			
		||||
        BehaviourController.OnPreDraw += PreDraw;
 | 
			
		||||
        BehaviourController.OnUpdate += Update;
 | 
			
		||||
        BehaviourController.UniverseObject.OnEnteredUniverse += EnteredUniverse;
 | 
			
		||||
        BehaviourController.UniverseObject.OnExitedUniverse += ExitedUniverse;
 | 
			
		||||
 | 
			
		||||
@@ -37,9 +32,6 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
    protected virtual void OnFinalize() { }
 | 
			
		||||
    protected virtual void OnFinalize(IInitializable _)
 | 
			
		||||
    {
 | 
			
		||||
        BehaviourController.OnPreUpdate -= PreUpdate;
 | 
			
		||||
        BehaviourController.OnPreDraw -= PreDraw;
 | 
			
		||||
        BehaviourController.OnUpdate -= Update;
 | 
			
		||||
        BehaviourController.UniverseObject.OnEnteredUniverse -= EnteredUniverse;
 | 
			
		||||
        BehaviourController.UniverseObject.OnExitedUniverse -= ExitedUniverse;
 | 
			
		||||
 | 
			
		||||
@@ -49,9 +41,16 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
            ExitedUniverse(UniverseObject, Universe);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnFirstActiveFrame() { }
 | 
			
		||||
    void IFirstFrameUpdate.FirstActiveFrame()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
        OnFirstActiveFrame();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnPreUpdatePreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnPreUpdate() { }
 | 
			
		||||
    protected virtual void PreUpdate(IBehaviourController _)
 | 
			
		||||
    void IPreUpdate.PreUpdate()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
@@ -60,22 +59,12 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
        if (!IsActive)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        if (isInitializedThisFrame)
 | 
			
		||||
            FirstActiveFrame();
 | 
			
		||||
 | 
			
		||||
        OnPreUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnFirstActiveFrame() { }
 | 
			
		||||
    protected virtual void FirstActiveFrame()
 | 
			
		||||
    {
 | 
			
		||||
        OnFirstActiveFrame();
 | 
			
		||||
        isInitializedThisFrame = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnUpdatePreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnUpdate() { }
 | 
			
		||||
    protected virtual void Update(IBehaviourController _)
 | 
			
		||||
    void IUpdate.Update()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
@@ -87,9 +76,23 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
        OnUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnPostUpdatePreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnPostUpdate() { }
 | 
			
		||||
    void IPostUpdate.PostUpdate()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        OnPostUpdatePreActiveCheck();
 | 
			
		||||
 | 
			
		||||
        if (!StateEnable.Enabled)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        OnPostUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnPreDrawPreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnPreDraw() { }
 | 
			
		||||
    protected virtual void PreDraw(IBehaviourController _)
 | 
			
		||||
    void IPreDraw.PreDraw()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
@@ -101,6 +104,34 @@ public abstract class Behaviour : BehaviourBase
 | 
			
		||||
        OnPreDraw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnDrawPreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnDraw() { }
 | 
			
		||||
    void IDraw.Draw()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        OnDrawPreActiveCheck();
 | 
			
		||||
 | 
			
		||||
        if (!StateEnable.Enabled)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        OnDraw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnPostDrawPreActiveCheck() { }
 | 
			
		||||
    protected virtual void OnPostDraw() { }
 | 
			
		||||
    void IPostDraw.PostDraw()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        OnPostDrawPreActiveCheck();
 | 
			
		||||
 | 
			
		||||
        if (!StateEnable.Enabled)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        OnPostDraw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected virtual void OnEnteredUniverse(IUniverse universe) { }
 | 
			
		||||
    protected virtual void EnteredUniverse(IUniverseObject sender, IUniverse universe) => OnEnteredUniverse(universe);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,10 +17,6 @@ public abstract class Behaviour2D : Behaviour, IBehaviour2D
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected sealed override void OnUnassign(IAssignable assignable) => base.OnUnassign(assignable);
 | 
			
		||||
    protected sealed override void PreUpdate(IBehaviourController behaviourController) => base.PreUpdate(behaviourController);
 | 
			
		||||
    protected sealed override void FirstActiveFrame() => base.FirstActiveFrame();
 | 
			
		||||
    protected sealed override void Update(IBehaviourController behaviourController) => base.Update(behaviourController);
 | 
			
		||||
    protected sealed override void PreDraw(IBehaviourController behaviourController) => base.PreDraw(behaviourController);
 | 
			
		||||
    protected sealed override void EnteredUniverse(IUniverseObject sender, IUniverse universe) => base.EnteredUniverse(sender, universe);
 | 
			
		||||
    protected sealed override void ExitedUniverse(IUniverseObject sender, IUniverse universe) => base.ExitedUniverse(sender, universe);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,6 @@ namespace Syntriax.Engine.Core;
 | 
			
		||||
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
 | 
			
		||||
public class BehaviourController : BaseEntity, IBehaviourController
 | 
			
		||||
{
 | 
			
		||||
    public event IBehaviourController.PreUpdateEventHandler? OnPreUpdate = null;
 | 
			
		||||
    public event IBehaviourController.UpdateEventHandler? OnUpdate = null;
 | 
			
		||||
    public event IBehaviourController.PreDrawEventHandler? OnPreDraw = null;
 | 
			
		||||
 | 
			
		||||
    public event IBehaviourController.BehaviourAddedEventHandler? OnBehaviourAdded = null;
 | 
			
		||||
    public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
 | 
			
		||||
    public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
 | 
			
		||||
@@ -36,7 +32,10 @@ public class BehaviourController : BaseEntity, IBehaviourController
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public T AddBehaviour<T>(params object?[]? args) where T : class, IBehaviour
 | 
			
		||||
        => AddBehaviour(Factory.BehaviourFactory.Instantiate<T>(_universeObject, args));
 | 
			
		||||
    {
 | 
			
		||||
        T behaviour = Factory.BehaviourFactory.Instantiate<T>(args);
 | 
			
		||||
        return AddBehaviour(behaviour);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public T? GetBehaviour<T>()
 | 
			
		||||
    {
 | 
			
		||||
@@ -125,27 +124,6 @@ public class BehaviourController : BaseEntity, IBehaviourController
 | 
			
		||||
            behaviour.Finalize();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Update()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        if (!UniverseObject.StateEnable.Enabled || !StateEnable.Enabled)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        OnPreUpdate?.InvokeSafe(this);
 | 
			
		||||
        OnUpdate?.InvokeSafe(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void UpdatePreDraw()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        if (!UniverseObject.StateEnable.Enabled || !StateEnable.Enabled)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        OnPreDraw?.InvokeSafe(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public BehaviourController() { }
 | 
			
		||||
    public BehaviourController(IUniverseObject universeObject) => Assign(universeObject);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,10 @@ namespace Syntriax.Engine.Core.Factory;
 | 
			
		||||
 | 
			
		||||
public class BehaviourFactory
 | 
			
		||||
{
 | 
			
		||||
    public static T Instantiate<T>(IUniverseObject universeObject, params object?[]? args) where T : class, IBehaviour
 | 
			
		||||
        => Instantiate<T>(universeObject, stateEnable: null, args);
 | 
			
		||||
    public static T Instantiate<T>(params object?[]? args) where T : class, IBehaviour
 | 
			
		||||
        => Instantiate<T>(stateEnable: null, args);
 | 
			
		||||
 | 
			
		||||
    public static T Instantiate<T>(IUniverseObject universeObject, IStateEnable? stateEnable, params object?[]? args)
 | 
			
		||||
    public static T Instantiate<T>(IStateEnable? stateEnable, params object?[]? args)
 | 
			
		||||
        where T : class, IBehaviour
 | 
			
		||||
    {
 | 
			
		||||
        T behaviour = TypeFactory.Get<T>(args);
 | 
			
		||||
@@ -18,8 +18,6 @@ public class BehaviourFactory
 | 
			
		||||
 | 
			
		||||
        if (!behaviour.Assign(stateEnable))
 | 
			
		||||
            throw AssignFailedException.From(behaviour, stateEnable);
 | 
			
		||||
        if (!behaviour.Assign(universeObject.BehaviourController))
 | 
			
		||||
            throw AssignFailedException.From(behaviour, universeObject.BehaviourController);
 | 
			
		||||
 | 
			
		||||
        return behaviour;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IDraw.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IDraw.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified when the draw phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IDraw : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Calls draw logic for the <see cref="IBehaviour"/> to be displayed visually.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void Draw();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										6
									
								
								Engine.Core/Systems/Abstract/IFirstFrameUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Engine.Core/Systems/Abstract/IFirstFrameUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
public interface IFirstFrameUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    void FirstActiveFrame();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IPostDraw.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IPostDraw.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified after the draw phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPostDraw : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates the state of the <see cref="IBehaviour"/> after the main draw phase happens.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void PostDraw();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IPostUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IPostUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified after the update phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPostUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates the state of the <see cref="IBehaviour"/> after the main update phase happens.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void PostUpdate();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IPreDraw.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IPreDraw.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified before the draw phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPreDraw : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates the state of the <see cref="IBehaviour"/> before the main draw phase happens.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void PreDraw();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IPreUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IPreUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified before the update phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPreUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates the state of the <see cref="IBehaviour"/> before the main update phase happens.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void PreUpdate();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Engine.Core/Systems/Abstract/IUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Engine.Core/Systems/Abstract/IUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> to be notified when the update phase of the <see cref="IUniverse"/> occurs.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Updates the state of the <see cref="IBehaviour"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    void Update();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								Engine.Core/Systems/DrawManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								Engine.Core/Systems/DrawManager.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
public class DrawManager : UniverseObject
 | 
			
		||||
{
 | 
			
		||||
    private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
 | 
			
		||||
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IPreDraw> preDrawEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IDraw> drawEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IPostDraw> postDrawEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
 | 
			
		||||
    private void OnPreDraw(IUniverse sender)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = preDrawEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            preDrawEntities.Behaviours[i].PreDraw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnDraw(IUniverse sender)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = drawEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            drawEntities.Behaviours[i].Draw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnPostDraw(IUniverse sender)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = postDrawEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            postDrawEntities.Behaviours[i].PostDraw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnEnteringUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        preDrawEntities.Assign(universe);
 | 
			
		||||
        drawEntities.Assign(universe);
 | 
			
		||||
        postDrawEntities.Assign(universe);
 | 
			
		||||
 | 
			
		||||
        universe.OnPreDraw += OnPreDraw;
 | 
			
		||||
        universe.OnDraw += OnDraw;
 | 
			
		||||
        universe.OnPostDraw += OnPostDraw;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnExitingUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        preDrawEntities.Unassign();
 | 
			
		||||
        drawEntities.Unassign();
 | 
			
		||||
        postDrawEntities.Unassign();
 | 
			
		||||
 | 
			
		||||
        universe.OnPreDraw -= OnPreDraw;
 | 
			
		||||
        universe.OnDraw -= OnDraw;
 | 
			
		||||
        universe.OnPostDraw -= OnPostDraw;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										71
									
								
								Engine.Core/Systems/UpdateManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								Engine.Core/Systems/UpdateManager.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
			
		||||
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
public class UpdateManager : UniverseObject
 | 
			
		||||
{
 | 
			
		||||
    private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
 | 
			
		||||
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IFirstFrameUpdate> firstFrameUpdates = new() { SortBy = SortByPriority() };
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IPreUpdate> preUpdateEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IUpdate> updateEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
    private readonly ActiveBehaviourCollectorSorted<IPostUpdate> postUpdateEntities = new() { SortBy = SortByPriority() };
 | 
			
		||||
 | 
			
		||||
    private readonly List<IFirstFrameUpdate> toCallFirstFrameUpdates = [];
 | 
			
		||||
 | 
			
		||||
    protected override void OnEnteringUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        firstFrameUpdates.Assign(universe);
 | 
			
		||||
        preUpdateEntities.Assign(universe);
 | 
			
		||||
        updateEntities.Assign(universe);
 | 
			
		||||
        postUpdateEntities.Assign(universe);
 | 
			
		||||
 | 
			
		||||
        universe.OnPreUpdate += OnPreUpdate;
 | 
			
		||||
        universe.OnUpdate += OnUpdate;
 | 
			
		||||
        universe.OnPostUpdate += OnPostUpdate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnExitingUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        firstFrameUpdates.Unassign();
 | 
			
		||||
        preUpdateEntities.Unassign();
 | 
			
		||||
        updateEntities.Unassign();
 | 
			
		||||
        postUpdateEntities.Unassign();
 | 
			
		||||
 | 
			
		||||
        universe.OnPreUpdate -= OnPreUpdate;
 | 
			
		||||
        universe.OnUpdate -= OnUpdate;
 | 
			
		||||
        universe.OnPostUpdate -= OnPostUpdate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnPreUpdate(IUniverse sender, UniverseTime engineTime)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = toCallFirstFrameUpdates.Count - 1; i >= 0; i--)
 | 
			
		||||
            toCallFirstFrameUpdates[i].FirstActiveFrame();
 | 
			
		||||
 | 
			
		||||
        for (int i = preUpdateEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            preUpdateEntities.Behaviours[i].PreUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnUpdate(IUniverse sender, UniverseTime engineTime)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = updateEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            updateEntities.Behaviours[i].Update();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnPostUpdate(IUniverse sender, UniverseTime engineTime)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = postUpdateEntities.Behaviours.Count - 1; i >= 0; i--)
 | 
			
		||||
            postUpdateEntities.Behaviours[i].PostUpdate();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnFirstFrameCollected(IBehaviourCollector<IFirstFrameUpdate> sender, IFirstFrameUpdate behaviourCollected)
 | 
			
		||||
    {
 | 
			
		||||
        toCallFirstFrameUpdates.Add(behaviourCollected);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UpdateManager()
 | 
			
		||||
    {
 | 
			
		||||
        firstFrameUpdates.OnCollected += OnFirstFrameCollected;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,7 +9,10 @@ public class Universe : BaseEntity, IUniverse
 | 
			
		||||
{
 | 
			
		||||
    public event IUniverse.UpdateEventHandler? OnPreUpdate = null;
 | 
			
		||||
    public event IUniverse.UpdateEventHandler? OnUpdate = null;
 | 
			
		||||
    public event IUniverse.PreDrawEventHandler? OnPreDraw = null;
 | 
			
		||||
    public event IUniverse.UpdateEventHandler? OnPostUpdate = null;
 | 
			
		||||
    public event IUniverse.DrawEventHandler? OnPreDraw = null;
 | 
			
		||||
    public event IUniverse.DrawEventHandler? OnDraw = null;
 | 
			
		||||
    public event IUniverse.DrawEventHandler? OnPostDraw = null;
 | 
			
		||||
 | 
			
		||||
    public event IUniverse.UniverseObjectRegisteredEventHandler? OnUniverseObjectRegistered = null;
 | 
			
		||||
    public event IUniverse.UniverseObjectUnRegisteredEventHandler? OnUniverseObjectUnRegistered = null;
 | 
			
		||||
@@ -116,21 +119,17 @@ public class Universe : BaseEntity, IUniverse
 | 
			
		||||
        Time = new(TimeSpan.FromTicks((long)(Time.TimeSinceStart.Ticks + engineTime.DeltaSpan.Ticks * TimeScale)), TimeSpan.FromTicks((long)(engineTime.DeltaSpan.Ticks * TimeScale)));
 | 
			
		||||
 | 
			
		||||
        OnPreUpdate?.InvokeSafe(this, Time);
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < UniverseObjects.Count; i++)
 | 
			
		||||
            UniverseObjects[i].BehaviourController.Update();
 | 
			
		||||
 | 
			
		||||
        OnUpdate?.InvokeSafe(this, Time);
 | 
			
		||||
        OnPostUpdate?.InvokeSafe(this, Time);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void PreDraw()
 | 
			
		||||
    public void Draw()
 | 
			
		||||
    {
 | 
			
		||||
        Debug.Assert.AssertInitialized(this);
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < UniverseObjects.Count; i++)
 | 
			
		||||
            UniverseObjects[i].BehaviourController.UpdatePreDraw();
 | 
			
		||||
 | 
			
		||||
        OnPreDraw?.InvokeSafe(this);
 | 
			
		||||
        OnDraw?.InvokeSafe(this);
 | 
			
		||||
        OnPostDraw?.InvokeSafe(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnUniverseObjectFinalize(IInitializable initializable)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								Engine.Physics2D/Abstract/Updates/IPostPhysicsUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Engine.Physics2D/Abstract/Updates/IPostPhysicsUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> that listens to the phase after the physics simulation phase.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPostPhysicsUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Execute logic that should occur after the physics simulation has been updated.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
 | 
			
		||||
    void PostPhysicsUpdate(float delta);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								Engine.Physics2D/Abstract/Updates/IPrePhysicsUpdate.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Engine.Physics2D/Abstract/Updates/IPrePhysicsUpdate.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Represents a <see cref="IBehaviour"/> that listens to the phase before the physics simulation phase.
 | 
			
		||||
/// </summary>
 | 
			
		||||
public interface IPrePhysicsUpdate : IBehaviour
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Execute logic that should occur before the physics simulation is updated.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
 | 
			
		||||
    void PrePhysicsUpdate(float delta);
 | 
			
		||||
}
 | 
			
		||||
@@ -14,9 +14,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
 | 
			
		||||
    protected readonly ICollisionDetector2D collisionDetector = null!;
 | 
			
		||||
    protected readonly ICollisionResolver2D collisionResolver = null!;
 | 
			
		||||
 | 
			
		||||
    private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
 | 
			
		||||
    protected ActiveBehaviourCollectorSorted<IPrePhysicsUpdate> physicsPreUpdateCollector = new() { SortBy = SortByPriority() };
 | 
			
		||||
    protected ActiveBehaviourCollectorSorted<IPhysicsUpdate> physicsUpdateCollector = new() { SortBy = SortByPriority() };
 | 
			
		||||
    protected ActiveBehaviourCollectorSorted<IPostPhysicsUpdate> physicsPostUpdateCollector = new() { SortBy = SortByPriority() };
 | 
			
		||||
    protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
 | 
			
		||||
    protected BehaviourCollector<ICollider2D> colliderCollector = new();
 | 
			
		||||
    protected BehaviourCollector<IPhysicsUpdate> physicsUpdateCollector = new();
 | 
			
		||||
 | 
			
		||||
    public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
 | 
			
		||||
    public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
 | 
			
		||||
@@ -25,6 +28,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
 | 
			
		||||
    {
 | 
			
		||||
        float intervalDeltaTime = deltaTime / IterationPerStep;
 | 
			
		||||
 | 
			
		||||
        foreach (IPrePhysicsUpdate physicsPreUpdate in physicsPreUpdateCollector)
 | 
			
		||||
            physicsPreUpdate.PrePhysicsUpdate(deltaTime);
 | 
			
		||||
 | 
			
		||||
        foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
 | 
			
		||||
            physicsUpdate.PhysicsUpdate(deltaTime);
 | 
			
		||||
 | 
			
		||||
        for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
 | 
			
		||||
        {
 | 
			
		||||
            // Can Parallel
 | 
			
		||||
@@ -92,8 +101,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
 | 
			
		||||
            OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
 | 
			
		||||
            physicsUpdate.PhysicsUpdate(deltaTime);
 | 
			
		||||
        foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdateCollector)
 | 
			
		||||
            physicsPostUpdate.PostPhysicsUpdate(deltaTime);
 | 
			
		||||
 | 
			
		||||
        OnPhysicsStep?.InvokeSafe(this, deltaTime);
 | 
			
		||||
    }
 | 
			
		||||
@@ -109,7 +118,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
 | 
			
		||||
 | 
			
		||||
    protected override void OnEnteringUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        physicsPreUpdateCollector.Assign(universe);
 | 
			
		||||
        physicsUpdateCollector.Assign(universe);
 | 
			
		||||
        physicsPostUpdateCollector.Assign(universe);
 | 
			
		||||
        colliderCollector.Assign(universe);
 | 
			
		||||
        rigidBodyCollector.Assign(universe);
 | 
			
		||||
 | 
			
		||||
@@ -118,7 +129,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
 | 
			
		||||
 | 
			
		||||
    protected override void OnExitingUniverse(IUniverse universe)
 | 
			
		||||
    {
 | 
			
		||||
        physicsPreUpdateCollector.Unassign();
 | 
			
		||||
        physicsUpdateCollector.Unassign();
 | 
			
		||||
        physicsPostUpdateCollector.Unassign();
 | 
			
		||||
        colliderCollector.Unassign();
 | 
			
		||||
        rigidBodyCollector.Unassign();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user