Compare commits
23 Commits
net8
...
67d7f401b8
Author | SHA1 | Date | |
---|---|---|---|
67d7f401b8 | |||
9bf17cc191 | |||
bf8fbebae3 | |||
1b0f25e854 | |||
61a7f685c1 | |||
feb2a05aa3 | |||
cd30047e4a | |||
a3b03efd47 | |||
4213b3f8b5 | |||
d3fb612904 | |||
8f8558a262 | |||
2df41e1881 | |||
114fa82b9d | |||
bcce427376 | |||
6a750f8ce0 | |||
3e02ee7b6f | |||
6b9020bd24 | |||
832514ba7d | |||
877a004a13 | |||
b1970d93f9 | |||
e7bd924494 | |||
37b87f0f85 | |||
3b6a93d37a |
@@ -7,7 +7,7 @@ namespace Syntriax.Engine.Core;
|
|||||||
/// Provides mechanisms for tracking additions and removals, and notifies subscribers when such events occur on the assigned <see cref="IUniverse"/>.
|
/// Provides mechanisms for tracking additions and removals, and notifies subscribers when such events occur on the assigned <see cref="IUniverse"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of objects tracked by the collector.</typeparam>
|
/// <typeparam name="T">The type of objects tracked by the collector.</typeparam>
|
||||||
public interface IBehaviourCollector<T> : IHasUniverse, IEnumerable<T> where T : class
|
public interface IBehaviourCollector<T> : IHasUniverse where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event triggered when an object of type <typeparamref name="T"/> is added to the collector.
|
/// Event triggered when an object of type <typeparamref name="T"/> is added to the collector.
|
||||||
|
@@ -3,25 +3,10 @@ using System.Collections.Generic;
|
|||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public interface IBehaviourController : IEntity, IHasUniverseObject, IEnumerable<IBehaviour>
|
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>
|
/// <summary>
|
||||||
/// Event triggered when a <see cref="IBehaviour"/> is added to the <see cref="IBehaviourController"/>.
|
/// Event triggered when a <see cref="IBehaviour"/> is added to the <see cref="IBehaviourController"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -83,20 +68,6 @@ public interface IBehaviourController : IEntity, IHasUniverseObject, IEnumerable
|
|||||||
/// <param name="behaviour">The <see cref="IBehaviour"/> to remove.</param>
|
/// <param name="behaviour">The <see cref="IBehaviour"/> to remove.</param>
|
||||||
void RemoveBehaviour<T>(T behaviour) where T : class, IBehaviour;
|
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 BehaviourAddedEventHandler(IBehaviourController sender, IBehaviour behaviourAdded);
|
||||||
delegate void BehaviourRemovedEventHandler(IBehaviourController sender, IBehaviour behaviourRemoved);
|
delegate void BehaviourRemovedEventHandler(IBehaviourController sender, IBehaviour behaviourRemoved);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,24 @@ public interface IUniverse : IEntity, IEnumerable<IUniverseObject>
|
|||||||
event UpdateEventHandler? OnUpdate;
|
event UpdateEventHandler? OnUpdate;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </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>
|
/// <summary>
|
||||||
/// Event triggered when a <see cref="IUniverseObject"/> is registered to the <see cref="IUniverse"/>.
|
/// 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);
|
void Update(UniverseTime universeTime);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Performs operations that should be done before the draw calls.
|
/// Performs operations that should be done to the draw.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void PreDraw();
|
void Draw();
|
||||||
|
|
||||||
delegate void TimeScaleChangedEventHandler(IUniverse sender, float previousTimeScale);
|
delegate void TimeScaleChangedEventHandler(IUniverse sender, float previousTimeScale);
|
||||||
|
|
||||||
delegate void UpdateEventHandler(IUniverse sender, UniverseTime engineTime);
|
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 UniverseObjectRegisteredEventHandler(IUniverse sender, IUniverseObject universeObjectRegistered);
|
||||||
delegate void UniverseObjectUnRegisteredEventHandler(IUniverse sender, IUniverseObject universeObjectUnregistered);
|
delegate void UniverseObjectUnRegisteredEventHandler(IUniverse sender, IUniverseObject universeObjectUnregistered);
|
||||||
|
@@ -19,8 +19,6 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
|||||||
public IReadOnlyList<T> Behaviours => activeBehaviours;
|
public IReadOnlyList<T> Behaviours => activeBehaviours;
|
||||||
public IUniverse Universe { get; private set; } = null!;
|
public IUniverse Universe { get; private set; } = null!;
|
||||||
|
|
||||||
public T this[Index index] => activeBehaviours[index];
|
|
||||||
|
|
||||||
public ActiveBehaviourCollector() { }
|
public ActiveBehaviourCollector() { }
|
||||||
public ActiveBehaviourCollector(IUniverse universe) => Assign(universe);
|
public ActiveBehaviourCollector(IUniverse universe) => Assign(universe);
|
||||||
|
|
||||||
@@ -61,12 +59,12 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
|||||||
{
|
{
|
||||||
activeBehaviours.Add(behaviour);
|
activeBehaviours.Add(behaviour);
|
||||||
OnBehaviourAdd(behaviour);
|
OnBehaviourAdd(behaviour);
|
||||||
OnCollected?.InvokeSafe(this, behaviour);
|
OnCollected?.Invoke(this, behaviour);
|
||||||
}
|
}
|
||||||
else if (activeBehaviours.Remove(behaviour))
|
else if (activeBehaviours.Remove(behaviour))
|
||||||
{
|
{
|
||||||
OnBehaviourRemove(behaviour);
|
OnBehaviourRemove(behaviour);
|
||||||
OnRemoved?.InvokeSafe(this, behaviour);
|
OnRemoved?.Invoke(this, behaviour);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +81,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
|||||||
if (activeBehaviours.Remove(tBehaviour))
|
if (activeBehaviours.Remove(tBehaviour))
|
||||||
{
|
{
|
||||||
OnBehaviourRemove(tBehaviour);
|
OnBehaviourRemove(tBehaviour);
|
||||||
OnRemoved?.InvokeSafe(this, tBehaviour);
|
OnRemoved?.Invoke(this, tBehaviour);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +97,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
|||||||
universe.OnUniverseObjectUnRegistered += OnUniverseObjectUnregistered;
|
universe.OnUniverseObjectUnRegistered += OnUniverseObjectUnregistered;
|
||||||
|
|
||||||
Universe = universe;
|
Universe = universe;
|
||||||
OnUniverseAssigned?.InvokeSafe(this);
|
OnUniverseAssigned?.Invoke(this);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -116,10 +114,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
|
|||||||
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
|
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
|
||||||
|
|
||||||
Universe = null!;
|
Universe = null!;
|
||||||
OnUnassigned?.InvokeSafe(this);
|
OnUnassigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => activeBehaviours.GetEnumerator();
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => activeBehaviours.GetEnumerator();
|
|
||||||
}
|
}
|
||||||
|
@@ -33,7 +33,7 @@ public abstract class BaseEntity : IEntity
|
|||||||
string previousId = _id;
|
string previousId = _id;
|
||||||
|
|
||||||
_id = value;
|
_id = value;
|
||||||
OnIdChanged?.InvokeSafe(this, previousId);
|
OnIdChanged?.Invoke(this, previousId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +47,9 @@ public abstract class BaseEntity : IEntity
|
|||||||
|
|
||||||
_initialized = value;
|
_initialized = value;
|
||||||
if (value)
|
if (value)
|
||||||
OnInitialized?.InvokeSafe(this);
|
OnInitialized?.Invoke(this);
|
||||||
else
|
else
|
||||||
OnFinalized?.InvokeSafe(this);
|
OnFinalized?.Invoke(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ public abstract class BaseEntity : IEntity
|
|||||||
_stateEnable = stateEnable;
|
_stateEnable = stateEnable;
|
||||||
_stateEnable.Assign(this);
|
_stateEnable.Assign(this);
|
||||||
OnAssign(stateEnable);
|
OnAssign(stateEnable);
|
||||||
OnStateEnableAssigned?.InvokeSafe(this);
|
OnStateEnableAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ public abstract class BaseEntity : IEntity
|
|||||||
|
|
||||||
_stateEnable = null!;
|
_stateEnable = null!;
|
||||||
_stateEnable.Unassign();
|
_stateEnable.Unassign();
|
||||||
OnUnassigned?.InvokeSafe(this);
|
OnUnassigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
namespace Syntriax.Engine.Core;
|
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 IUniverse Universe => BehaviourController.UniverseObject.Universe;
|
||||||
protected IUniverseObject UniverseObject => BehaviourController.UniverseObject;
|
protected IUniverseObject UniverseObject => BehaviourController.UniverseObject;
|
||||||
|
|
||||||
@@ -20,11 +20,6 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
protected virtual void OnInitialize() { }
|
protected virtual void OnInitialize() { }
|
||||||
protected virtual void OnInitialize(IInitializable _)
|
protected virtual void OnInitialize(IInitializable _)
|
||||||
{
|
{
|
||||||
isInitializedThisFrame = true;
|
|
||||||
|
|
||||||
BehaviourController.OnPreUpdate += PreUpdate;
|
|
||||||
BehaviourController.OnPreDraw += PreDraw;
|
|
||||||
BehaviourController.OnUpdate += Update;
|
|
||||||
BehaviourController.UniverseObject.OnEnteredUniverse += EnteredUniverse;
|
BehaviourController.UniverseObject.OnEnteredUniverse += EnteredUniverse;
|
||||||
BehaviourController.UniverseObject.OnExitedUniverse += ExitedUniverse;
|
BehaviourController.UniverseObject.OnExitedUniverse += ExitedUniverse;
|
||||||
|
|
||||||
@@ -37,9 +32,6 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
protected virtual void OnFinalize() { }
|
protected virtual void OnFinalize() { }
|
||||||
protected virtual void OnFinalize(IInitializable _)
|
protected virtual void OnFinalize(IInitializable _)
|
||||||
{
|
{
|
||||||
BehaviourController.OnPreUpdate -= PreUpdate;
|
|
||||||
BehaviourController.OnPreDraw -= PreDraw;
|
|
||||||
BehaviourController.OnUpdate -= Update;
|
|
||||||
BehaviourController.UniverseObject.OnEnteredUniverse -= EnteredUniverse;
|
BehaviourController.UniverseObject.OnEnteredUniverse -= EnteredUniverse;
|
||||||
BehaviourController.UniverseObject.OnExitedUniverse -= ExitedUniverse;
|
BehaviourController.UniverseObject.OnExitedUniverse -= ExitedUniverse;
|
||||||
|
|
||||||
@@ -49,9 +41,16 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
ExitedUniverse(UniverseObject, Universe);
|
ExitedUniverse(UniverseObject, Universe);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void OnFirstActiveFrame() { }
|
||||||
|
void IFirstFrameUpdate.FirstActiveFrame()
|
||||||
|
{
|
||||||
|
Debug.Assert.AssertInitialized(this);
|
||||||
|
OnFirstActiveFrame();
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void OnPreUpdatePreActiveCheck() { }
|
protected virtual void OnPreUpdatePreActiveCheck() { }
|
||||||
protected virtual void OnPreUpdate() { }
|
protected virtual void OnPreUpdate() { }
|
||||||
protected virtual void PreUpdate(IBehaviourController _)
|
void IPreUpdate.PreUpdate()
|
||||||
{
|
{
|
||||||
Debug.Assert.AssertInitialized(this);
|
Debug.Assert.AssertInitialized(this);
|
||||||
|
|
||||||
@@ -60,22 +59,12 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
if (!IsActive)
|
if (!IsActive)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (isInitializedThisFrame)
|
|
||||||
FirstActiveFrame();
|
|
||||||
|
|
||||||
OnPreUpdate();
|
OnPreUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnFirstActiveFrame() { }
|
|
||||||
protected virtual void FirstActiveFrame()
|
|
||||||
{
|
|
||||||
OnFirstActiveFrame();
|
|
||||||
isInitializedThisFrame = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void OnUpdatePreActiveCheck() { }
|
protected virtual void OnUpdatePreActiveCheck() { }
|
||||||
protected virtual void OnUpdate() { }
|
protected virtual void OnUpdate() { }
|
||||||
protected virtual void Update(IBehaviourController _)
|
void IUpdate.Update()
|
||||||
{
|
{
|
||||||
Debug.Assert.AssertInitialized(this);
|
Debug.Assert.AssertInitialized(this);
|
||||||
|
|
||||||
@@ -87,9 +76,23 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
OnUpdate();
|
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 OnPreDrawPreActiveCheck() { }
|
||||||
protected virtual void OnPreDraw() { }
|
protected virtual void OnPreDraw() { }
|
||||||
protected virtual void PreDraw(IBehaviourController _)
|
void IPreDraw.PreDraw()
|
||||||
{
|
{
|
||||||
Debug.Assert.AssertInitialized(this);
|
Debug.Assert.AssertInitialized(this);
|
||||||
|
|
||||||
@@ -101,6 +104,34 @@ public abstract class Behaviour : BehaviourBase
|
|||||||
OnPreDraw();
|
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 OnEnteredUniverse(IUniverse universe) { }
|
||||||
protected virtual void EnteredUniverse(IUniverseObject sender, IUniverse universe) => OnEnteredUniverse(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 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 EnteredUniverse(IUniverseObject sender, IUniverse universe) => base.EnteredUniverse(sender, universe);
|
||||||
protected sealed override void ExitedUniverse(IUniverseObject sender, IUniverse universe) => base.ExitedUniverse(sender, universe);
|
protected sealed override void ExitedUniverse(IUniverseObject sender, IUniverse universe) => base.ExitedUniverse(sender, universe);
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
|
|||||||
|
|
||||||
int previousPriority = _priority;
|
int previousPriority = _priority;
|
||||||
_priority = value;
|
_priority = value;
|
||||||
OnPriorityChanged?.InvokeSafe(this, previousPriority);
|
OnPriorityChanged?.Invoke(this, previousPriority);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
|
|||||||
behaviourController.OnUniverseObjectAssigned += OnUniverseObjectAssigned;
|
behaviourController.OnUniverseObjectAssigned += OnUniverseObjectAssigned;
|
||||||
if (behaviourController.UniverseObject is not null)
|
if (behaviourController.UniverseObject is not null)
|
||||||
OnUniverseObjectAssigned(behaviourController);
|
OnUniverseObjectAssigned(behaviourController);
|
||||||
OnBehaviourControllerAssigned?.InvokeSafe(this);
|
OnBehaviourControllerAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +79,6 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
|
|||||||
_isActive = StateEnable.Enabled && _behaviourController.UniverseObject.IsActive;
|
_isActive = StateEnable.Enabled && _behaviourController.UniverseObject.IsActive;
|
||||||
|
|
||||||
if (previousActive != IsActive)
|
if (previousActive != IsActive)
|
||||||
OnActiveChanged?.InvokeSafe(this, previousActive);
|
OnActiveChanged?.Invoke(this, previousActive);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,8 +17,6 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
|
|||||||
public IReadOnlyList<T> Behaviours => behaviours;
|
public IReadOnlyList<T> Behaviours => behaviours;
|
||||||
public IUniverse Universe { get; private set; } = null!;
|
public IUniverse Universe { get; private set; } = null!;
|
||||||
|
|
||||||
public T this[Index index] => behaviours[index];
|
|
||||||
|
|
||||||
public BehaviourCollector() { }
|
public BehaviourCollector() { }
|
||||||
public BehaviourCollector(IUniverse universe) => Assign(universe);
|
public BehaviourCollector(IUniverse universe) => Assign(universe);
|
||||||
|
|
||||||
@@ -48,7 +46,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
|
|||||||
|
|
||||||
behaviours.Add(tBehaviour);
|
behaviours.Add(tBehaviour);
|
||||||
OnBehaviourAdd(behaviour);
|
OnBehaviourAdd(behaviour);
|
||||||
OnCollected?.InvokeSafe(this, tBehaviour);
|
OnCollected?.Invoke(this, tBehaviour);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnBehaviourRemove(IBehaviour behaviour) { }
|
protected virtual void OnBehaviourRemove(IBehaviour behaviour) { }
|
||||||
@@ -61,7 +59,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
OnBehaviourRemove(behaviour);
|
OnBehaviourRemove(behaviour);
|
||||||
OnRemoved?.InvokeSafe(this, tBehaviour);
|
OnRemoved?.Invoke(this, tBehaviour);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnAssign(IUniverse universe) { }
|
protected virtual void OnAssign(IUniverse universe) { }
|
||||||
@@ -78,7 +76,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
|
|||||||
|
|
||||||
Universe = universe;
|
Universe = universe;
|
||||||
OnAssign(universe);
|
OnAssign(universe);
|
||||||
OnUniverseAssigned?.InvokeSafe(this);
|
OnUniverseAssigned?.Invoke(this);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -95,10 +93,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
|
|||||||
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
|
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
|
||||||
|
|
||||||
Universe = null!;
|
Universe = null!;
|
||||||
OnUnassigned?.InvokeSafe(this);
|
OnUnassigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => behaviours.GetEnumerator();
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => behaviours.GetEnumerator();
|
|
||||||
}
|
}
|
||||||
|
@@ -8,10 +8,6 @@ namespace Syntriax.Engine.Core;
|
|||||||
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
|
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
|
||||||
public class BehaviourController : BaseEntity, IBehaviourController
|
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.BehaviourAddedEventHandler? OnBehaviourAdded = null;
|
||||||
public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
|
public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
|
||||||
public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
|
public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
|
||||||
@@ -31,12 +27,15 @@ public class BehaviourController : BaseEntity, IBehaviourController
|
|||||||
if (IsInitialized)
|
if (IsInitialized)
|
||||||
behaviour.Initialize();
|
behaviour.Initialize();
|
||||||
behaviour.OnPriorityChanged += OnPriorityChange;
|
behaviour.OnPriorityChanged += OnPriorityChange;
|
||||||
OnBehaviourAdded?.InvokeSafe(this, behaviour);
|
OnBehaviourAdded?.Invoke(this, behaviour);
|
||||||
return behaviour;
|
return behaviour;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T AddBehaviour<T>(params object?[]? args) where T : class, IBehaviour
|
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>()
|
public T? GetBehaviour<T>()
|
||||||
{
|
{
|
||||||
@@ -96,7 +95,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
|
|||||||
behaviour.OnPriorityChanged -= OnPriorityChange;
|
behaviour.OnPriorityChanged -= OnPriorityChange;
|
||||||
behaviour.Finalize();
|
behaviour.Finalize();
|
||||||
behaviours.Remove(behaviour);
|
behaviours.Remove(behaviour);
|
||||||
OnBehaviourRemoved?.InvokeSafe(this, behaviour);
|
OnBehaviourRemoved?.Invoke(this, behaviour);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnAssign(IUniverseObject universeObject) { }
|
protected virtual void OnAssign(IUniverseObject universeObject) { }
|
||||||
@@ -107,7 +106,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
|
|||||||
|
|
||||||
_universeObject = universeObject;
|
_universeObject = universeObject;
|
||||||
OnAssign(universeObject);
|
OnAssign(universeObject);
|
||||||
OnUniverseObjectAssigned?.InvokeSafe(this);
|
OnUniverseObjectAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,27 +124,6 @@ public class BehaviourController : BaseEntity, IBehaviourController
|
|||||||
behaviour.Finalize();
|
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() { }
|
||||||
public BehaviourController(IUniverseObject universeObject) => Assign(universeObject);
|
public BehaviourController(IUniverseObject universeObject) => Assign(universeObject);
|
||||||
|
|
||||||
|
@@ -1,9 +1,3 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core.Exceptions;
|
namespace Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
public class BehaviourNotFoundException(string? message) : Exception(message)
|
public class BehaviourNotFoundException(string? message) : NotFoundException(message);
|
||||||
{
|
|
||||||
public static NotAssignedException FromType<TBehaviour>()
|
|
||||||
=> new($"{typeof(TBehaviour).FullName} was not found");
|
|
||||||
}
|
|
||||||
|
9
Engine.Core/Exceptions/NotFoundException.cs
Normal file
9
Engine.Core/Exceptions/NotFoundException.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
|
public class NotFoundException(string? message) : Exception(message)
|
||||||
|
{
|
||||||
|
public static NotAssignedException FromType<T>()
|
||||||
|
=> new($"{typeof(T).FullName} was not found");
|
||||||
|
}
|
@@ -1,9 +1,3 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core.Exceptions;
|
namespace Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
public class UniverseObjectNotFoundException(string? message) : Exception(message)
|
public class UniverseObjectNotFoundException(string? message) : NotFoundException(message);
|
||||||
{
|
|
||||||
public static NotAssignedException FromType<TUniverseObject>()
|
|
||||||
=> new($"{typeof(TUniverseObject).FullName} was not found");
|
|
||||||
}
|
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
using Syntriax.Engine.Core.Exceptions;
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
@@ -38,6 +39,19 @@ public static class BehaviourControllerExtensions
|
|||||||
public static T GetOrAddBehaviour<T>(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour
|
public static T GetOrAddBehaviour<T>(this IBehaviourController behaviourController, params object?[]? args) where T : class, IBehaviour
|
||||||
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
=> behaviourController.GetBehaviour<T>() ?? behaviourController.AddBehaviour<T>(args);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an existing <see cref="IBehaviour"/> of the specified type, or adds and returns the fallback type if it doesn't exist.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TOriginal">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||||
|
/// <typeparam name="TFallback">The type of <see cref="IBehaviour"/> to add. It must be assignable from <typeparamref name="TOriginal"/></typeparam>
|
||||||
|
/// <param name="behaviourController">The <see cref="IBehaviourController"/> to search in.</param>
|
||||||
|
/// <param name="args">Optional arguments to pass to the constructor of the <see cref="IBehaviour"/> if a new one is added.</param>
|
||||||
|
/// <returns>The existing or newly added <see cref="IBehaviour"/> of the specified type.</returns>
|
||||||
|
public static TOriginal GetOrAddBehaviour<TOriginal, TFallback>(this IBehaviourController behaviourController, params object?[]? args)
|
||||||
|
where TOriginal : class
|
||||||
|
where TFallback : class, IBehaviour, TOriginal
|
||||||
|
=> behaviourController.GetBehaviour<TOriginal>() ?? behaviourController.AddBehaviour<TFallback>(args);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s parents recursively.
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s parents recursively.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -81,6 +95,27 @@ public static class BehaviourControllerExtensions
|
|||||||
public static T GetRequiredBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
public static T GetRequiredBehaviourInParent<T>(this IBehaviourController behaviourController) where T : class
|
||||||
=> behaviourController.GetBehaviourInParent<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any parent");
|
=> behaviourController.GetBehaviourInParent<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any parent");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all <see cref="IBehaviour"/>s of the specified type in it's <see cref="IUniverseObject"/>'s parents recursively and stores them in the provided list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam>
|
||||||
|
/// <param name="behavioursInParent">The list to store the <see cref="IBehaviour"/>s.</param>
|
||||||
|
public static void GetBehavioursInParent<T>(this IBehaviourController behaviourController, IList<T> behavioursInParent) where T : class
|
||||||
|
{
|
||||||
|
IBehaviourController? controller = behaviourController;
|
||||||
|
List<T> cache = [];
|
||||||
|
behavioursInParent.Clear();
|
||||||
|
|
||||||
|
while (controller is not null)
|
||||||
|
{
|
||||||
|
controller.GetBehaviours(cache);
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
behavioursInParent.Add(behaviour);
|
||||||
|
|
||||||
|
controller = controller.UniverseObject.Parent?.BehaviourController;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s children recursively.
|
/// Tries to get a <see cref="IBehaviour"/> of the specified type in it's <see cref="IUniverseObject"/>'s children recursively.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -120,4 +155,28 @@ public static class BehaviourControllerExtensions
|
|||||||
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
/// <returns>The <see cref="IBehaviour"/> of the specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
||||||
public static T GetRequiredBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
public static T GetRequiredBehaviourInChildren<T>(this IBehaviourController behaviourController) where T : class
|
||||||
=> behaviourController.GetBehaviourInChildren<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any children ");
|
=> behaviourController.GetBehaviourInChildren<T>() ?? throw new BehaviourNotFoundException($"{behaviourController.UniverseObject.Name}'s {nameof(IBehaviourController)} does not contain any {typeof(T).FullName} on any children ");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all <see cref="IBehaviour"/>s of the specified type in it's <see cref="IUniverseObject"/>'s children recursively and stores them in the provided list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/>s to get.</typeparam>
|
||||||
|
/// <param name="behavioursInChildren">The list to store the <see cref="IBehaviour"/>s.</param>
|
||||||
|
public static void GetBehavioursInChildren<T>(this IBehaviourController behaviourController, IList<T> behavioursInChildren) where T : class
|
||||||
|
{
|
||||||
|
List<T> cache = [];
|
||||||
|
behavioursInChildren.Clear();
|
||||||
|
|
||||||
|
TraverseChildrenForBehaviour(behaviourController.UniverseObject, behavioursInChildren, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TraverseChildrenForBehaviour<T>(IUniverseObject universeObject, IList<T> behaviours, IList<T> cache) where T : class
|
||||||
|
{
|
||||||
|
universeObject.BehaviourController.GetBehaviours(cache);
|
||||||
|
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
behaviours.Add(behaviour);
|
||||||
|
|
||||||
|
foreach (IUniverseObject child in universeObject)
|
||||||
|
TraverseChildrenForBehaviour(child, behaviours, cache);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,34 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
|
||||||
|
|
||||||
public static class BehaviourExtensions
|
|
||||||
{
|
|
||||||
public static T? FindBehaviour<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
|
||||||
{
|
|
||||||
foreach (IUniverseObject universeObject in universeObjects)
|
|
||||||
if (universeObject.BehaviourController.GetBehaviour<T>() is T behaviour)
|
|
||||||
return behaviour;
|
|
||||||
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool TryFindBehaviour<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
|
||||||
{
|
|
||||||
behaviour = FindBehaviour<T>(universeObjects);
|
|
||||||
return behaviour is not null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void FindBehaviours<T>(this IEnumerable<IUniverseObject> universeObjects, List<T> behaviours) where T : class
|
|
||||||
{
|
|
||||||
behaviours.Clear();
|
|
||||||
List<T> cache = [];
|
|
||||||
|
|
||||||
foreach (IUniverseObject universeObject in universeObjects)
|
|
||||||
{
|
|
||||||
universeObject.BehaviourController.GetBehaviours(cache);
|
|
||||||
behaviours.AddRange(cache);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -7,9 +7,30 @@ public static class UniverseExtensions
|
|||||||
public static IUniverseObject InstantiateUniverseObject(this IUniverse universe, params object?[]? args)
|
public static IUniverseObject InstantiateUniverseObject(this IUniverse universe, params object?[]? args)
|
||||||
=> universe.InstantiateUniverseObject<UniverseObject>(args);
|
=> universe.InstantiateUniverseObject<UniverseObject>(args);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through all <see cref="IUniverseObject"/>s to find the specified instance of the type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
|
||||||
|
/// <returns>The specified type if found; otherwise, throws <see cref="UniverseObjectNotFoundException"/>.</returns>
|
||||||
public static T GetRequiredUniverseObject<T>(this IUniverse universe) where T : class
|
public static T GetRequiredUniverseObject<T>(this IUniverse universe) where T : class
|
||||||
=> universe.GetUniverseObject<T>() ?? throw new UniverseObjectNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} object of type {typeof(T).FullName}");
|
=> universe.GetUniverseObject<T>() ?? throw new UniverseObjectNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} object of type {typeof(T).FullName}");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through all <see cref="IBehaviours"/>s to find the specified instance of the type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
|
||||||
|
/// <returns>The specified type if found; otherwise, throws <see cref="BehaviourNotFoundException"/>.</returns>
|
||||||
public static T FindRequiredBehaviour<T>(this IUniverse universe) where T : class
|
public static T FindRequiredBehaviour<T>(this IUniverse universe) where T : class
|
||||||
=> universe.FindBehaviour<T>() ?? throw new BehaviourNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} with {nameof(IBehaviour)} of type {typeof(T).FullName}");
|
=> universe.FindBehaviour<T>() ?? throw new BehaviourNotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} with {nameof(IBehaviour)} of type {typeof(T).FullName}");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through all <see cref="IUniverseObject"/>s and <see cref="IBehaviours"/>s to find the specified instance of the type.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// WARNING: This is more expensive compared to <see cref="GetRequiredUniverseObject{T}(IUniverse)"/> or <see cref="FindRequiredBehaviour{T}(IUniverse)"/> as it combines the two. If you know whether the type is either a type that gets implemented on an <see cref="IBehaviour"/> or <see cref="IUniverseObject"/> use the method appropriate for it for performance.
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="T">Type to be searched through the <see cref="IUniverse"/>.</typeparam>
|
||||||
|
/// <returns>The specified type if found; otherwise, throws <see cref="NotFoundException"/>.</returns>
|
||||||
|
public static T FindRequired<T>(this IUniverse universe) where T : class
|
||||||
|
=> universe.Find<T>() ?? throw new NotFoundException($"{universe.GetType().FullName}({universe.Id}) does not contain any {nameof(IUniverseObject)} or {nameof(IBehaviour)} of type {typeof(T).FullName}");
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
using Syntriax.Engine.Core.Exceptions;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
public static class UniverseObjectExtensions
|
public static class UniverseObjectExtensions
|
||||||
@@ -14,6 +16,13 @@ public static class UniverseObjectExtensions
|
|||||||
return universeObject;
|
return universeObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Universe Object Search
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IUniverseObject"/> of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObjects">The <see cref="IUniverseObject"/>s to search.</param>
|
||||||
|
/// <returns>The first found <see cref="IUniverseObject"/> of the specified type; otherwise, null.</returns>
|
||||||
public static T? GetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
public static T? GetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
||||||
{
|
{
|
||||||
foreach (IUniverseObject universeObject in universeObjects)
|
foreach (IUniverseObject universeObject in universeObjects)
|
||||||
@@ -23,17 +32,224 @@ public static class UniverseObjectExtensions
|
|||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool TryGetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IUniverseObject"/> of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObjects">The <see cref="IUniverseObject"/>s to search.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IUniverseObject"/> of the specified type was found in the universe objects; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryGetUniverseObject<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? universeObject) where T : class
|
||||||
{
|
{
|
||||||
behaviour = GetUniverseObject<T>(universeObjects);
|
universeObject = GetUniverseObject<T>(universeObjects);
|
||||||
|
return universeObject is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through the provided <see cref="IUniverseObject"/>s to collect a list of <see cref="IUniverseObject"/>s of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObject">The <see cref="IUniverseObject"/> to search.</param>
|
||||||
|
/// <returns>The found <see cref="IUniverseObject"/>s of the specified types</returns>
|
||||||
|
public static void GetUniverseObjects<T>(this IEnumerable<IUniverseObject> universeObjects, IList<T> foundUniverseObjects) where T : class
|
||||||
|
{
|
||||||
|
foundUniverseObjects.Clear();
|
||||||
|
|
||||||
|
foreach (IUniverseObject universeObject in universeObjects)
|
||||||
|
if (universeObject is T @object)
|
||||||
|
foundUniverseObjects.Add(@object);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Universe Object Search In Parent
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IUniverseObject"/> of the specified type in it's parents recursively.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IUniverseObject"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IUniverseObject"/> of the specified type was found in the parent universe objects; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryGetUniverseObjectInParent<T>(this IUniverseObject universeObject, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
|
{
|
||||||
|
behaviour = GetUniverseObjectInParent<T>(universeObject);
|
||||||
return behaviour is not null;
|
return behaviour is not null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void GetUniverseObjects<T>(this IEnumerable<IUniverseObject> universeObjects, List<T> behaviours) where T : class
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IUniverseObject"/> of the specified type in it's parents recursively.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObject">The <see cref="IUniverseObject"/> to start searching from.</param>
|
||||||
|
/// <returns>The <see cref="IUniverseObject"/> of the specified type if found; otherwise, null.</returns>
|
||||||
|
public static T? GetUniverseObjectInParent<T>(this IUniverseObject universeObject) where T : class
|
||||||
|
{
|
||||||
|
if (universeObject.GetUniverseObject<T>() is T localUniverseObject)
|
||||||
|
return localUniverseObject;
|
||||||
|
|
||||||
|
IUniverseObject? parent = universeObject;
|
||||||
|
|
||||||
|
while (parent is not null)
|
||||||
|
{
|
||||||
|
if (parent is T behaviour)
|
||||||
|
return behaviour;
|
||||||
|
|
||||||
|
parent = universeObject.Parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IUniverseObject"/> of the specified type in the parents recursively. Throws an error if not found.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObject">The <see cref="IUniverseObject"/> to start searching from.</param>
|
||||||
|
/// <returns>The <see cref="IUniverseObject"/> of the specified type if found; otherwise, throws <see cref="UniverseObjectNotFoundException"/>.</returns>
|
||||||
|
public static T GetRequiredUniverseObjectInParent<T>(this IUniverseObject universeObject) where T : class
|
||||||
|
=> universeObject.GetUniverseObjectInParent<T>() ?? throw new UniverseObjectNotFoundException($"{universeObject.Name}'s {nameof(IUniverseObject)} does not contain any {typeof(T).FullName} on any parent ");
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Universe Object Search In Children
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a <see cref="IUniverseObject"/> of the specified type in it's children recursively.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IUniverseObject"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IUniverseObject"/> of the specified type was found in the child universe objects; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryGetUniverseObjectInChildren<T>(this IUniverseObject universeObject, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
|
{
|
||||||
|
behaviour = GetUniverseObjectInChildren<T>(universeObject);
|
||||||
|
return behaviour is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IUniverseObject"/> of the specified type in it's children recursively.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObject">The <see cref="IUniverseObject"/> to start searching from.</param>
|
||||||
|
/// <returns>The <see cref="IUniverseObject"/> of the specified type if found; otherwise, null.</returns>
|
||||||
|
public static T? GetUniverseObjectInChildren<T>(this IUniverseObject universeObject) where T : class
|
||||||
|
{
|
||||||
|
if (universeObject.GetUniverseObject<T>() is T localUniverseObject)
|
||||||
|
return localUniverseObject;
|
||||||
|
|
||||||
|
foreach (IUniverseObject child in universeObject)
|
||||||
|
if (GetUniverseObjectInChildren<T>(child) is T behaviour)
|
||||||
|
return behaviour;
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a <see cref="IUniverseObject"/> of the specified type in the children recursively. Throws an error if not found.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IUniverseObject"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObject">The <see cref="IUniverseObject"/> to start searching from.</param>
|
||||||
|
/// <returns>The <see cref="IUniverseObject"/> of the specified type if found; otherwise, throws <see cref="UniverseObjectNotFoundException"/>.</returns>
|
||||||
|
public static T GetRequiredUniverseObjectInChildren<T>(this IUniverseObject universeObject) where T : class
|
||||||
|
=> universeObject.GetUniverseObjectInChildren<T>() ?? throw new UniverseObjectNotFoundException($"{universeObject.Name}'s {nameof(IUniverseObject)} does not contain any {typeof(T).FullName} on any children ");
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Behaviour Search
|
||||||
|
/// <summary>
|
||||||
|
/// Finds a <see cref="IBehaviour"/> of the specified type in the provided <see cref="IUniverseObject"/>s.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to find.</typeparam>
|
||||||
|
/// <returns>The first found <see cref="IBehaviour"/> of the specified type; otherwise, null.</returns>
|
||||||
|
public static T? FindBehaviour<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
||||||
|
{
|
||||||
|
foreach (IUniverseObject universeObject in universeObjects)
|
||||||
|
if (universeObject.BehaviourController.GetBehaviour<T>() is T behaviour)
|
||||||
|
return behaviour;
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to find a <see cref="IBehaviour"/> of the specified type in the provided <see cref="IUniverseObject"/>s.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to find.</typeparam>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IUniverseObject"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if a <see cref="IBehaviour"/> of the specified type was found in the provided <see cref="IUniverseObject"/>s; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryFindBehaviour<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
|
{
|
||||||
|
behaviour = FindBehaviour<T>(universeObjects);
|
||||||
|
return behaviour is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through the provided <see cref="IUniverseObject"/>s to collect a list of <see cref="IBehaviour"/>s of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||||
|
/// <param name="universeObjects">The <see cref="IUniverseObject"/>s to search.</param>
|
||||||
|
public static void FindBehaviours<T>(this IEnumerable<IUniverseObject> universeObjects, IList<T> behaviours) where T : class
|
||||||
{
|
{
|
||||||
behaviours.Clear();
|
behaviours.Clear();
|
||||||
|
List<T> cache = [];
|
||||||
|
|
||||||
foreach (IUniverseObject universeObject in universeObjects)
|
foreach (IUniverseObject universeObject in universeObjects)
|
||||||
if (universeObject is T @object)
|
{
|
||||||
behaviours.Add(@object);
|
universeObject.BehaviourController.GetBehaviours(cache);
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
behaviours.Add(behaviour);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region General Search
|
||||||
|
/// <summary>
|
||||||
|
/// Finds an object of the specified type in the provided <see cref="IUniverseObject"/>s and their <see cref="IBehaviour"/>s.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// WARNING: This is more expensive compared to <see cref="GetUniverseObject{T}(IEnumerable{IUniverseObject})"/> or <see cref="FindBehaviour{T}(IEnumerable{IUniverseObject})"/> as it combines the two. If you know whether the type is either a type that gets implemented on an <see cref="IBehaviour"/> or <see cref="IUniverseObject"/> use the method appropriate for it for performance.
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to find.</typeparam>
|
||||||
|
/// <returns>The first found instance of the specified type; otherwise, null.</returns>
|
||||||
|
public static T? Find<T>(this IEnumerable<IUniverseObject> universeObjects) where T : class
|
||||||
|
{
|
||||||
|
if (universeObjects.GetUniverseObject<T>() is T foundUniverseObject)
|
||||||
|
return foundUniverseObject;
|
||||||
|
|
||||||
|
if (universeObjects.FindBehaviour<T>() is T foundBehaviour)
|
||||||
|
return foundBehaviour;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to find an object of the specified type in the provided <see cref="IUniverseObject"/>s and their <see cref="IBehaviour"/>s.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// WARNING: This is more expensive compared to <see cref="TryGetUniverseObject{T}(IEnumerable{IUniverseObject}, out T?)"/> or <see cref="TryFindBehaviour{T}(IEnumerable{IUniverseObject}, out T?)"/> as it combines the two. If you know whether the type is either a type that gets implemented on an <see cref="IBehaviour"/> or <see cref="IUniverseObject"/> use the method appropriate for it for performance.
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to find.</typeparam>
|
||||||
|
/// <param name="behaviour">When this method returns, contains the <see cref="IUniverseObject"/> of the specified type, if found; otherwise, null.</param>
|
||||||
|
/// <returns><see cref="true"/> if an object of the specified type was found in the provided <see cref="IUniverseObject"/>s; otherwise, <see cref="false"/>.</returns>
|
||||||
|
public static bool TryFind<T>(this IEnumerable<IUniverseObject> universeObjects, [NotNullWhen(returnValue: true)] out T? behaviour) where T : class
|
||||||
|
{
|
||||||
|
behaviour = Find<T>(universeObjects);
|
||||||
|
return behaviour is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Searches through the provided <see cref="IUniverseObject"/>s and their <see cref="IBehaviour"/>s to collect a list of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// WARNING: This is more expensive compared to <see cref="GetUniverseObjects{T}(IEnumerable{IUniverseObject}, IList{T})"/> or <see cref="FindBehaviours{T}(IEnumerable{IUniverseObject}, IList{T})"/> as it combines the two. If you know whether the type is either a type that gets implemented on an <see cref="IBehaviour"/> or <see cref="IUniverseObject"/> use the method appropriate for it for performance.
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="T">The type of <see cref="IBehaviour"/> to get.</typeparam>
|
||||||
|
/// <param name="instances">List of objects found wit the specified type.</param>
|
||||||
|
/// <param name="universeObjects">The <see cref="IUniverseObject"/>s to search.</param>
|
||||||
|
public static void Find<T>(this IEnumerable<IUniverseObject> universeObjects, IList<T> instances) where T : class
|
||||||
|
{
|
||||||
|
instances.Clear();
|
||||||
|
List<T> cache = [];
|
||||||
|
|
||||||
|
foreach (IUniverseObject universeObject in universeObjects)
|
||||||
|
{
|
||||||
|
universeObject.Find(cache);
|
||||||
|
foreach (T behaviour in cache)
|
||||||
|
instances.Add(behaviour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,10 @@ namespace Syntriax.Engine.Core.Factory;
|
|||||||
|
|
||||||
public class BehaviourFactory
|
public class BehaviourFactory
|
||||||
{
|
{
|
||||||
public static T Instantiate<T>(IUniverseObject universeObject, params object?[]? args) where T : class, IBehaviour
|
public static T Instantiate<T>(params object?[]? args) where T : class, IBehaviour
|
||||||
=> Instantiate<T>(universeObject, stateEnable: null, args);
|
=> 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
|
where T : class, IBehaviour
|
||||||
{
|
{
|
||||||
T behaviour = TypeFactory.Get<T>(args);
|
T behaviour = TypeFactory.Get<T>(args);
|
||||||
@@ -18,8 +18,6 @@ public class BehaviourFactory
|
|||||||
|
|
||||||
if (!behaviour.Assign(stateEnable))
|
if (!behaviour.Assign(stateEnable))
|
||||||
throw AssignFailedException.From(behaviour, stateEnable);
|
throw AssignFailedException.From(behaviour, stateEnable);
|
||||||
if (!behaviour.Assign(universeObject.BehaviourController))
|
|
||||||
throw AssignFailedException.From(behaviour, universeObject.BehaviourController);
|
|
||||||
|
|
||||||
return behaviour;
|
return behaviour;
|
||||||
}
|
}
|
||||||
|
@@ -4,9 +4,10 @@ namespace Syntriax.Engine.Core;
|
|||||||
|
|
||||||
public static class DelegateExtensions
|
public static class DelegateExtensions
|
||||||
{
|
{
|
||||||
|
[Obsolete($"{nameof(InvokeSafe)} causes memory allocation, please use Invoke() instead.")]
|
||||||
public static void InvokeSafe(this Delegate @delegate, params object?[] args)
|
public static void InvokeSafe(this Delegate @delegate, params object?[] args)
|
||||||
{
|
{
|
||||||
foreach (Delegate invocation in @delegate.GetInvocationList())
|
foreach (Delegate invocation in Delegate.EnumerateInvocationList(@delegate))
|
||||||
try { invocation.DynamicInvoke(args); }
|
try { invocation.DynamicInvoke(args); }
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
@@ -19,10 +19,10 @@ public class ProgressionTracker : IProgressionTracker
|
|||||||
Progression = progression.Clamp(Progression, 1f);
|
Progression = progression.Clamp(Progression, 1f);
|
||||||
Status = status;
|
Status = status;
|
||||||
|
|
||||||
OnUpdated?.InvokeSafe(this, previousProgression, previousStatus);
|
OnUpdated?.Invoke(this, previousProgression, previousStatus);
|
||||||
|
|
||||||
if (progression >= 1f)
|
if (progression >= 1f)
|
||||||
OnEnded?.InvokeSafe(this);
|
OnEnded?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IProgressionTracker.Reset()
|
void IProgressionTracker.Reset()
|
||||||
|
@@ -35,7 +35,7 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
|||||||
foreach (Vector2D vertex in value)
|
foreach (Vector2D vertex in value)
|
||||||
_vertices.Add(vertex);
|
_vertices.Add(vertex);
|
||||||
|
|
||||||
OnShapeUpdated?.InvokeSafe(this);
|
OnShapeUpdated?.Invoke(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +229,7 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
|||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
to._vertices.Add(transform.Transform(from[i]));
|
to._vertices.Add(transform.Transform(from[i]));
|
||||||
|
|
||||||
to.OnShapeUpdated?.InvokeSafe(to);
|
to.OnShapeUpdated?.Invoke(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@@ -14,7 +14,7 @@ public class EntityRegistry
|
|||||||
public void Add(IEntity entity)
|
public void Add(IEntity entity)
|
||||||
{
|
{
|
||||||
if (registeredEntities.TryAdd(entity.Id, entity))
|
if (registeredEntities.TryAdd(entity.Id, entity))
|
||||||
OnEntityRegistered?.InvokeSafe(this, entity);
|
OnEntityRegistered?.Invoke(this, entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void QueueAssign(string id, Action<IEntity> setMethod)
|
public void QueueAssign(string id, Action<IEntity> setMethod)
|
||||||
@@ -26,7 +26,7 @@ public class EntityRegistry
|
|||||||
public void AssignAll()
|
public void AssignAll()
|
||||||
{
|
{
|
||||||
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
|
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
|
||||||
action?.InvokeSafe(registeredEntities[id]);
|
action?.Invoke(registeredEntities[id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
|
@@ -21,7 +21,7 @@ public class StateEnable : IStateEnable
|
|||||||
|
|
||||||
bool previousState = _enabled;
|
bool previousState = _enabled;
|
||||||
_enabled = value;
|
_enabled = value;
|
||||||
OnEnabledChanged?.InvokeSafe(this, previousState);
|
OnEnabledChanged?.Invoke(this, previousState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ public class StateEnable : IStateEnable
|
|||||||
|
|
||||||
_entity = entity;
|
_entity = entity;
|
||||||
OnAssign(entity);
|
OnAssign(entity);
|
||||||
OnEntityAssigned?.InvokeSafe(this);
|
OnEntityAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ public class StateEnable : IStateEnable
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
_entity = null!;
|
_entity = null!;
|
||||||
OnUnassigned?.InvokeSafe(this);
|
OnUnassigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
74
Engine.Core/Systems/UpdateManager.cs
Normal file
74
Engine.Core/Systems/UpdateManager.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
|
||||||
|
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 = new(32);
|
||||||
|
|
||||||
|
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();
|
||||||
|
toCallFirstFrameUpdates.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@@ -31,7 +31,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
_position = value;
|
_position = value;
|
||||||
|
|
||||||
UpdateLocalPosition();
|
UpdateLocalPosition();
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
_scale = value;
|
_scale = value;
|
||||||
|
|
||||||
UpdateLocalScale();
|
UpdateLocalScale();
|
||||||
OnScaleChanged?.InvokeSafe(this, previousScale);
|
OnScaleChanged?.Invoke(this, previousScale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
_rotation = value;
|
_rotation = value;
|
||||||
|
|
||||||
UpdateLocalRotation();
|
UpdateLocalRotation();
|
||||||
OnRotationChanged?.InvokeSafe(this, previousRotation);
|
OnRotationChanged?.Invoke(this, previousRotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
_localPosition = value;
|
_localPosition = value;
|
||||||
|
|
||||||
UpdatePosition();
|
UpdatePosition();
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,8 +97,8 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
|
|
||||||
UpdateScale();
|
UpdateScale();
|
||||||
UpdatePosition();
|
UpdatePosition();
|
||||||
OnScaleChanged?.InvokeSafe(this, previousScale);
|
OnScaleChanged?.Invoke(this, previousScale);
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
_localRotation = value;
|
_localRotation = value;
|
||||||
|
|
||||||
UpdateRotation();
|
UpdateRotation();
|
||||||
OnRotationChanged?.InvokeSafe(this, previousRotation);
|
OnRotationChanged?.Invoke(this, previousRotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
|
|
||||||
UpdatePosition();
|
UpdatePosition();
|
||||||
|
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RecalculateScale(ITransform2D _, Vector2D previousScale)
|
private void RecalculateScale(ITransform2D _, Vector2D previousScale)
|
||||||
@@ -138,8 +138,8 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
UpdateScale();
|
UpdateScale();
|
||||||
UpdatePosition();
|
UpdatePosition();
|
||||||
|
|
||||||
OnScaleChanged?.InvokeSafe(this, previousScale);
|
OnScaleChanged?.Invoke(this, previousScale);
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RecalculateRotation(ITransform2D _, float previousRotation)
|
private void RecalculateRotation(ITransform2D _, float previousRotation)
|
||||||
@@ -152,8 +152,8 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
UpdateRotation();
|
UpdateRotation();
|
||||||
UpdatePosition();
|
UpdatePosition();
|
||||||
|
|
||||||
OnRotationChanged?.InvokeSafe(this, previousRotation);
|
OnRotationChanged?.Invoke(this, previousRotation);
|
||||||
OnPositionChanged?.InvokeSafe(this, previousPosition);
|
OnPositionChanged?.Invoke(this, previousPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateLocalPosition()
|
private void UpdateLocalPosition()
|
||||||
@@ -247,9 +247,9 @@ public class Transform2D : Behaviour, ITransform2D
|
|||||||
UpdateLocalScale();
|
UpdateLocalScale();
|
||||||
UpdateLocalRotation();
|
UpdateLocalRotation();
|
||||||
|
|
||||||
OnPositionChanged?.InvokeSafe(this, Position);
|
OnPositionChanged?.Invoke(this, Position);
|
||||||
OnScaleChanged?.InvokeSafe(this, Scale);
|
OnScaleChanged?.Invoke(this, Scale);
|
||||||
OnRotationChanged?.InvokeSafe(this, Rotation);
|
OnRotationChanged?.Invoke(this, Rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded)
|
private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded)
|
||||||
|
@@ -9,7 +9,10 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
{
|
{
|
||||||
public event IUniverse.UpdateEventHandler? OnPreUpdate = null;
|
public event IUniverse.UpdateEventHandler? OnPreUpdate = null;
|
||||||
public event IUniverse.UpdateEventHandler? OnUpdate = 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.UniverseObjectRegisteredEventHandler? OnUniverseObjectRegistered = null;
|
||||||
public event IUniverse.UniverseObjectUnRegisteredEventHandler? OnUniverseObjectUnRegistered = null;
|
public event IUniverse.UniverseObjectUnRegisteredEventHandler? OnUniverseObjectUnRegistered = null;
|
||||||
@@ -34,7 +37,7 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
|
|
||||||
float previousTimeScale = _timeScale;
|
float previousTimeScale = _timeScale;
|
||||||
_timeScale = value;
|
_timeScale = value;
|
||||||
OnTimeScaleChanged?.InvokeSafe(this, previousTimeScale);
|
OnTimeScaleChanged?.Invoke(this, previousTimeScale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +60,7 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
if (!universeObject.EnterUniverse(this))
|
if (!universeObject.EnterUniverse(this))
|
||||||
throw new Exception($"{universeObject.Name} can't enter the universe");
|
throw new Exception($"{universeObject.Name} can't enter the universe");
|
||||||
|
|
||||||
OnUniverseObjectRegistered?.InvokeSafe(this, universeObject);
|
OnUniverseObjectRegistered?.Invoke(this, universeObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T InstantiateUniverseObject<T>(params object?[]? args) where T : class, IUniverseObject
|
public T InstantiateUniverseObject<T>(params object?[]? args) where T : class, IUniverseObject
|
||||||
@@ -92,7 +95,7 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
if (!universeObject.Finalize())
|
if (!universeObject.Finalize())
|
||||||
throw new Exception($"{universeObject.Name} can't be finalized");
|
throw new Exception($"{universeObject.Name} can't be finalized");
|
||||||
|
|
||||||
OnUniverseObjectUnRegistered?.InvokeSafe(this, universeObject);
|
OnUniverseObjectUnRegistered?.Invoke(this, universeObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void InitializeInternal()
|
protected override void InitializeInternal()
|
||||||
@@ -115,22 +118,18 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
UnscaledTime = engineTime;
|
UnscaledTime = engineTime;
|
||||||
Time = new(TimeSpan.FromTicks((long)(Time.TimeSinceStart.Ticks + engineTime.DeltaSpan.Ticks * TimeScale)), TimeSpan.FromTicks((long)(engineTime.DeltaSpan.Ticks * TimeScale)));
|
Time = new(TimeSpan.FromTicks((long)(Time.TimeSinceStart.Ticks + engineTime.DeltaSpan.Ticks * TimeScale)), TimeSpan.FromTicks((long)(engineTime.DeltaSpan.Ticks * TimeScale)));
|
||||||
|
|
||||||
OnPreUpdate?.InvokeSafe(this, Time);
|
OnPreUpdate?.Invoke(this, Time);
|
||||||
|
OnUpdate?.Invoke(this, Time);
|
||||||
for (int i = 0; i < UniverseObjects.Count; i++)
|
OnPostUpdate?.Invoke(this, Time);
|
||||||
UniverseObjects[i].BehaviourController.Update();
|
|
||||||
|
|
||||||
OnUpdate?.InvokeSafe(this, Time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PreDraw()
|
public void Draw()
|
||||||
{
|
{
|
||||||
Debug.Assert.AssertInitialized(this);
|
Debug.Assert.AssertInitialized(this);
|
||||||
|
|
||||||
for (int i = 0; i < UniverseObjects.Count; i++)
|
OnPreDraw?.Invoke(this);
|
||||||
UniverseObjects[i].BehaviourController.UpdatePreDraw();
|
OnDraw?.Invoke(this);
|
||||||
|
OnPostDraw?.Invoke(this);
|
||||||
OnPreDraw?.InvokeSafe(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnUniverseObjectFinalize(IInitializable initializable)
|
private void OnUniverseObjectFinalize(IInitializable initializable)
|
||||||
|
@@ -37,7 +37,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
|
|
||||||
string previousName = _name;
|
string previousName = _name;
|
||||||
_name = value;
|
_name = value;
|
||||||
OnNameChanged?.InvokeSafe(this, previousName);
|
OnNameChanged?.Invoke(this, previousName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
_universe = universe;
|
_universe = universe;
|
||||||
UpdateActive();
|
UpdateActive();
|
||||||
OnEnteringUniverse(universe);
|
OnEnteringUniverse(universe);
|
||||||
OnEnteredUniverse?.InvokeSafe(this, universe);
|
OnEnteredUniverse?.Invoke(this, universe);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
|
|
||||||
OnExitingUniverse(universe);
|
OnExitingUniverse(universe);
|
||||||
_universe = null!;
|
_universe = null!;
|
||||||
OnExitedUniverse?.InvokeSafe(this, universe);
|
OnExitedUniverse?.Invoke(this, universe);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
UpdateActive();
|
UpdateActive();
|
||||||
OnParentChanged?.InvokeSafe(this, previousParent, parent);
|
OnParentChanged?.Invoke(this, previousParent, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddChild(IUniverseObject parent)
|
public void AddChild(IUniverseObject parent)
|
||||||
@@ -103,7 +103,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
|
|
||||||
_children.Add(parent);
|
_children.Add(parent);
|
||||||
parent.SetParent(this);
|
parent.SetParent(this);
|
||||||
OnChildrenAdded?.InvokeSafe(this, parent);
|
OnChildrenAdded?.Invoke(this, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveChild(IUniverseObject child)
|
public void RemoveChild(IUniverseObject child)
|
||||||
@@ -112,7 +112,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
child.SetParent(null);
|
child.SetParent(null);
|
||||||
OnChildrenRemoved?.InvokeSafe(this, child);
|
OnChildrenRemoved?.Invoke(this, child);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnAssign(IBehaviourController behaviourController) { }
|
protected virtual void OnAssign(IBehaviourController behaviourController) { }
|
||||||
@@ -123,7 +123,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
|
|
||||||
_behaviourController = behaviourController;
|
_behaviourController = behaviourController;
|
||||||
OnAssign(behaviourController);
|
OnAssign(behaviourController);
|
||||||
OnBehaviourControllerAssigned?.InvokeSafe(this);
|
OnBehaviourControllerAssigned?.Invoke(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +143,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
_isActive = StateEnable.Enabled && (Parent?.IsActive ?? true);
|
_isActive = StateEnable.Enabled && (Parent?.IsActive ?? true);
|
||||||
|
|
||||||
if (previousActive != IsActive)
|
if (previousActive != IsActive)
|
||||||
OnActiveChanged?.InvokeSafe(this, previousActive);
|
OnActiveChanged?.Invoke(this, previousActive);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void UnassignInternal()
|
protected override void UnassignInternal()
|
||||||
|
@@ -26,6 +26,13 @@ public interface IPhysicsEngine2D
|
|||||||
/// <param name="deltaTime">The time step.</param>
|
/// <param name="deltaTime">The time step.</param>
|
||||||
void Step(float deltaTime);
|
void Step(float deltaTime);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Advances the physics simulation by the specified time on a single <see cref="IRigidBody2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="rigidBody">The <see cref="IRigidBody2D"/> to be progressed individually.</param>
|
||||||
|
/// <param name="deltaTime">The time step.</param>
|
||||||
|
void StepIndividual(IRigidBody2D rigidBody, float deltaTime);
|
||||||
|
|
||||||
delegate void PhysicsIterationEventHandler(IPhysicsEngine2D sender, float iterationDeltaTime);
|
delegate void PhysicsIterationEventHandler(IPhysicsEngine2D sender, float iterationDeltaTime);
|
||||||
delegate void PhysicsStepEventHandler(IPhysicsEngine2D sender, float stepDeltaTime);
|
delegate void PhysicsStepEventHandler(IPhysicsEngine2D sender, float stepDeltaTime);
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
@@ -69,7 +69,7 @@ public abstract class Collider2DBehaviourBase : Behaviour2D, ICollider2D
|
|||||||
Transform.OnRotationChanged -= SetNeedsRecalculationFromRotation;
|
Transform.OnRotationChanged -= SetNeedsRecalculationFromRotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.InvokeSafe(this, collisionDetectionInformation);
|
public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation);
|
||||||
public void Resolve(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionResolved?.InvokeSafe(this, collisionDetectionInformation);
|
public void Resolve(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionResolved?.Invoke(this, collisionDetectionInformation);
|
||||||
public void Trigger(ICollider2D initiator) => OnTriggered?.InvokeSafe(this, initiator);
|
public void Trigger(ICollider2D initiator) => OnTriggered?.Invoke(this, initiator);
|
||||||
}
|
}
|
||||||
|
@@ -14,9 +14,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
|
|||||||
protected readonly ICollisionDetector2D collisionDetector = null!;
|
protected readonly ICollisionDetector2D collisionDetector = null!;
|
||||||
protected readonly ICollisionResolver2D collisionResolver = 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<IRigidBody2D> rigidBodyCollector = new();
|
||||||
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
||||||
protected BehaviourCollector<IPhysicsUpdate> physicsUpdateCollector = new();
|
|
||||||
|
|
||||||
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
||||||
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
|
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
|
||||||
@@ -25,52 +28,123 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
|
|||||||
{
|
{
|
||||||
float intervalDeltaTime = deltaTime / IterationPerStep;
|
float intervalDeltaTime = deltaTime / IterationPerStep;
|
||||||
|
|
||||||
|
for (int i = physicsPreUpdateCollector.Behaviours.Count - 1; i >= 0; i--)
|
||||||
|
physicsPreUpdateCollector.Behaviours[i].PrePhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
for (int i = physicsUpdateCollector.Behaviours.Count - 1; i >= 0; i--)
|
||||||
|
physicsUpdateCollector.Behaviours[i].PhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
||||||
{
|
{
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
foreach (IRigidBody2D rigidBody in rigidBodyCollector)
|
for (int i = rigidBodyCollector.Behaviours.Count - 1; i >= 0; i--)
|
||||||
StepRigidBody(rigidBody, intervalDeltaTime);
|
StepRigidBody(rigidBodyCollector.Behaviours[i], intervalDeltaTime);
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
foreach (ICollider2D collider in colliderCollector)
|
for (int i = colliderCollector.Behaviours.Count - 1; i >= 0; i--)
|
||||||
collider.Recalculate();
|
colliderCollector.Behaviours[i].Recalculate();
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
for (int x = 0; x < colliderCollector.Behaviours.Count; x++)
|
for (int x = 0; x < colliderCollector.Behaviours.Count; x++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderX = colliderCollector.Behaviours[x];
|
ICollider2D? colliderX = colliderCollector.Behaviours[x];
|
||||||
if (!colliderX.IsActive)
|
if (!colliderX.IsActive)
|
||||||
return;
|
continue;
|
||||||
|
|
||||||
for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++)
|
for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderY = colliderCollector.Behaviours[y];
|
ICollider2D? colliderY = colliderCollector.Behaviours[y];
|
||||||
|
|
||||||
if (!colliderY.IsActive)
|
if (!colliderY.IsActive)
|
||||||
return;
|
|
||||||
|
|
||||||
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
ResolveColliders(colliderX, colliderY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OnPhysicsIteration?.Invoke(this, intervalDeltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = physicsPostUpdateCollector.Behaviours.Count - 1; i >= 0; i--)
|
||||||
|
physicsPostUpdateCollector.Behaviours[i].PostPhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
OnPhysicsStep?.Invoke(this, deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StepIndividual(IRigidBody2D rigidBody, float deltaTime)
|
||||||
|
{
|
||||||
|
float intervalDeltaTime = deltaTime / IterationPerStep;
|
||||||
|
|
||||||
|
System.Collections.Generic.List<ICollider2D> childColliders = [];
|
||||||
|
System.Collections.Generic.List<IPrePhysicsUpdate> physicsPreUpdates = [];
|
||||||
|
System.Collections.Generic.List<IPhysicsUpdate> physicsUpdates = [];
|
||||||
|
System.Collections.Generic.List<IPostPhysicsUpdate> physicsPostUpdates = [];
|
||||||
|
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(childColliders);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPreUpdates);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsUpdates);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPostUpdates);
|
||||||
|
|
||||||
|
foreach (IPrePhysicsUpdate physicsPreUpdate in physicsPreUpdates)
|
||||||
|
physicsPreUpdate.PrePhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
foreach (IPhysicsUpdate physicsUpdate in physicsUpdates)
|
||||||
|
physicsUpdate.PhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
||||||
|
{
|
||||||
|
StepRigidBody(rigidBody, intervalDeltaTime);
|
||||||
|
|
||||||
|
for (int i = childColliders.Count - 1; i >= 0; i--)
|
||||||
|
childColliders[i].Recalculate();
|
||||||
|
|
||||||
|
for (int x = 0; x < childColliders.Count; x++)
|
||||||
|
{
|
||||||
|
ICollider2D? colliderX = childColliders[x];
|
||||||
|
if (!colliderX.IsActive)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int y = 0; y < colliderCollector.Behaviours.Count; y++)
|
||||||
|
{
|
||||||
|
ICollider2D? colliderY = colliderCollector.Behaviours[y];
|
||||||
|
|
||||||
|
if (!colliderY.IsActive)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ResolveColliders(colliderX, colliderY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdates)
|
||||||
|
physicsPostUpdate.PostPhysicsUpdate(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResolveColliders(ICollider2D colliderX, ICollider2D colliderY)
|
||||||
|
{
|
||||||
|
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
||||||
|
return;
|
||||||
|
|
||||||
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
|
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
|
||||||
if (bothCollidersAreTriggers)
|
if (bothCollidersAreTriggers)
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
bool bothCollidersAreStatic = (colliderX.RigidBody2D?.IsStatic ?? true) && (colliderY.RigidBody2D?.IsStatic ?? true);
|
bool bothCollidersAreStatic = (colliderX.RigidBody2D?.IsStatic ?? true) && (colliderY.RigidBody2D?.IsStatic ?? true);
|
||||||
if (bothCollidersAreStatic)
|
if (bothCollidersAreStatic)
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
|
if (!collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
|
||||||
|
return;
|
||||||
|
|
||||||
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
|
|
||||||
{
|
|
||||||
if (colliderX.IsTrigger)
|
if (colliderX.IsTrigger)
|
||||||
{
|
{
|
||||||
colliderX.Trigger(colliderY);
|
colliderX.Trigger(colliderY);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
else if (colliderY.IsTrigger)
|
else if (colliderY.IsTrigger)
|
||||||
{
|
{
|
||||||
colliderY.Trigger(colliderX);
|
colliderY.Trigger(colliderX);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (information.Detector == colliderX)
|
if (information.Detector == colliderX)
|
||||||
@@ -86,17 +160,6 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
|
|||||||
|
|
||||||
collisionResolver?.Resolve(information);
|
collisionResolver?.Resolve(information);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
|
|
||||||
physicsUpdate.PhysicsUpdate(deltaTime);
|
|
||||||
|
|
||||||
OnPhysicsStep?.InvokeSafe(this, deltaTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
||||||
{
|
{
|
||||||
@@ -109,7 +172,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
|
|||||||
|
|
||||||
protected override void OnEnteringUniverse(IUniverse universe)
|
protected override void OnEnteringUniverse(IUniverse universe)
|
||||||
{
|
{
|
||||||
|
physicsPreUpdateCollector.Assign(universe);
|
||||||
physicsUpdateCollector.Assign(universe);
|
physicsUpdateCollector.Assign(universe);
|
||||||
|
physicsPostUpdateCollector.Assign(universe);
|
||||||
colliderCollector.Assign(universe);
|
colliderCollector.Assign(universe);
|
||||||
rigidBodyCollector.Assign(universe);
|
rigidBodyCollector.Assign(universe);
|
||||||
|
|
||||||
@@ -118,7 +183,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
|
|||||||
|
|
||||||
protected override void OnExitingUniverse(IUniverse universe)
|
protected override void OnExitingUniverse(IUniverse universe)
|
||||||
{
|
{
|
||||||
|
physicsPreUpdateCollector.Unassign();
|
||||||
physicsUpdateCollector.Unassign();
|
physicsUpdateCollector.Unassign();
|
||||||
|
physicsPostUpdateCollector.Unassign();
|
||||||
colliderCollector.Unassign();
|
colliderCollector.Unassign();
|
||||||
rigidBodyCollector.Unassign();
|
rigidBodyCollector.Unassign();
|
||||||
|
|
||||||
|
@@ -45,49 +45,108 @@ public class PhysicsEngine2DStandalone : IPhysicsEngine2D
|
|||||||
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
||||||
{
|
{
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
for (int i = 0; i < rigidBodies.Count; i++)
|
for (int i = rigidBodies.Count - 1; i >= 0; i--)
|
||||||
StepRigidBody(rigidBodies[i], intervalDeltaTime);
|
StepRigidBody(rigidBodies[i], intervalDeltaTime);
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
foreach (ICollider2D collider in colliders)
|
for (int i = colliders.Count - 1; i >= 0; i--)
|
||||||
collider.Recalculate();
|
colliders[i].Recalculate();
|
||||||
|
|
||||||
// Can Parallel
|
// Can Parallel
|
||||||
for (int x = 0; x < colliders.Count; x++)
|
for (int x = 0; x < colliders.Count; x++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderX = colliders[x];
|
ICollider2D? colliderX = colliders[x];
|
||||||
if (!colliderX.IsActive)
|
if (!colliderX.IsActive)
|
||||||
return;
|
continue;
|
||||||
|
|
||||||
for (int y = x + 1; y < colliders.Count; y++)
|
for (int y = x + 1; y < colliders.Count; y++)
|
||||||
{
|
{
|
||||||
ICollider2D? colliderY = colliders[y];
|
ICollider2D? colliderY = colliders[y];
|
||||||
|
|
||||||
if (!colliderY.IsActive)
|
if (!colliderY.IsActive)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
ResolveColliders(colliderX, colliderY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OnPhysicsIteration?.Invoke(this, intervalDeltaTime);
|
||||||
|
}
|
||||||
|
OnPhysicsStep?.Invoke(this, deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StepIndividual(IRigidBody2D rigidBody, float deltaTime)
|
||||||
|
{
|
||||||
|
float intervalDeltaTime = deltaTime / IterationPerStep;
|
||||||
|
|
||||||
|
List<ICollider2D> childColliders = [];
|
||||||
|
List<IPrePhysicsUpdate> physicsPreUpdates = [];
|
||||||
|
List<IPhysicsUpdate> physicsUpdates = [];
|
||||||
|
List<IPostPhysicsUpdate> physicsPostUpdates = [];
|
||||||
|
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(childColliders);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPreUpdates);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsUpdates);
|
||||||
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPostUpdates);
|
||||||
|
|
||||||
|
foreach (IPrePhysicsUpdate physicsPreUpdate in physicsPreUpdates)
|
||||||
|
physicsPreUpdate.PrePhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
foreach (IPhysicsUpdate physicsUpdate in physicsUpdates)
|
||||||
|
physicsUpdate.PhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
||||||
|
{
|
||||||
|
StepRigidBody(rigidBody, intervalDeltaTime);
|
||||||
|
|
||||||
|
foreach (ICollider2D collider in childColliders)
|
||||||
|
collider.Recalculate();
|
||||||
|
|
||||||
|
for (int x = 0; x < childColliders.Count; x++)
|
||||||
|
{
|
||||||
|
ICollider2D? colliderX = childColliders[x];
|
||||||
|
if (!colliderX.IsActive)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
for (int y = 0; y < colliders.Count; y++)
|
||||||
|
{
|
||||||
|
ICollider2D? colliderY = colliders[y];
|
||||||
|
|
||||||
|
if (!colliderY.IsActive)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ResolveColliders(colliderX, colliderY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdates)
|
||||||
|
physicsPostUpdate.PostPhysicsUpdate(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResolveColliders(ICollider2D colliderX, ICollider2D colliderY)
|
||||||
|
{
|
||||||
|
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
||||||
|
return;
|
||||||
|
|
||||||
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
|
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
|
||||||
if (bothCollidersAreTriggers)
|
if (bothCollidersAreTriggers)
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
bool bothCollidersAreStatic = (colliderX.RigidBody2D?.IsStatic ?? true) && (colliderY.RigidBody2D?.IsStatic ?? true);
|
bool bothCollidersAreStatic = (colliderX.RigidBody2D?.IsStatic ?? true) && (colliderY.RigidBody2D?.IsStatic ?? true);
|
||||||
if (bothCollidersAreStatic)
|
if (bothCollidersAreStatic)
|
||||||
continue;
|
return;
|
||||||
|
|
||||||
|
if (!collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
|
||||||
|
return;
|
||||||
|
|
||||||
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
|
|
||||||
{
|
|
||||||
if (colliderX.IsTrigger)
|
if (colliderX.IsTrigger)
|
||||||
{
|
{
|
||||||
colliderX.Trigger(colliderY);
|
colliderX.Trigger(colliderY);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
else if (colliderY.IsTrigger)
|
else if (colliderY.IsTrigger)
|
||||||
{
|
{
|
||||||
colliderY.Trigger(colliderX);
|
colliderY.Trigger(colliderX);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (information.Detector == colliderX)
|
if (information.Detector == colliderX)
|
||||||
@@ -103,12 +162,6 @@ public class PhysicsEngine2DStandalone : IPhysicsEngine2D
|
|||||||
|
|
||||||
collisionResolver?.Resolve(information);
|
collisionResolver?.Resolve(information);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
|
|
||||||
}
|
|
||||||
OnPhysicsStep?.InvokeSafe(this, deltaTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
||||||
{
|
{
|
||||||
|
@@ -53,12 +53,12 @@ public class State : BaseEntity, IState
|
|||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
if (GetNextState() is IState transitionState)
|
if (GetNextState() is IState transitionState)
|
||||||
OnStateTransitionReady?.InvokeSafe(this, transitionState);
|
OnStateTransitionReady?.Invoke(this, transitionState);
|
||||||
OnStateUpdate?.InvokeSafe(this);
|
OnStateUpdate?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TransitionTo(IState from) => OnStateTransitionedTo?.InvokeSafe(this, from);
|
public void TransitionTo(IState from) => OnStateTransitionedTo?.Invoke(this, from);
|
||||||
public void TransitionFrom(IState to) => OnStateTransitionedFrom?.InvokeSafe(this, to);
|
public void TransitionFrom(IState to) => OnStateTransitionedFrom?.Invoke(this, to);
|
||||||
|
|
||||||
public IState? GetNextState()
|
public IState? GetNextState()
|
||||||
{
|
{
|
||||||
|
@@ -30,21 +30,21 @@ public abstract class StateBehaviourBase : Behaviour, IState
|
|||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
OnUpdateState();
|
OnUpdateState();
|
||||||
OnStateUpdate?.InvokeSafe(this);
|
OnStateUpdate?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnTransitionedToState(IState from) { }
|
protected virtual void OnTransitionedToState(IState from) { }
|
||||||
public void TransitionTo(IState from)
|
public void TransitionTo(IState from)
|
||||||
{
|
{
|
||||||
OnTransitionedToState(from);
|
OnTransitionedToState(from);
|
||||||
OnStateTransitionedTo?.InvokeSafe(this, from);
|
OnStateTransitionedTo?.Invoke(this, from);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnTransitionedFromState(IState to) { }
|
protected virtual void OnTransitionedFromState(IState to) { }
|
||||||
public void TransitionFrom(IState to)
|
public void TransitionFrom(IState to)
|
||||||
{
|
{
|
||||||
OnTransitionedFromState(to);
|
OnTransitionedFromState(to);
|
||||||
OnStateTransitionedFrom?.InvokeSafe(this, to);
|
OnStateTransitionedFrom?.Invoke(this, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract IState? GetNextState();
|
public abstract IState? GetNextState();
|
||||||
|
@@ -23,7 +23,7 @@ public class StateMachine : Behaviour
|
|||||||
_state = value;
|
_state = value;
|
||||||
previousState.TransitionFrom(value);
|
previousState.TransitionFrom(value);
|
||||||
value.TransitionTo(_state);
|
value.TransitionTo(_state);
|
||||||
OnStateChanged?.InvokeSafe(this, previousState, value);
|
OnStateChanged?.Invoke(this, previousState, value);
|
||||||
|
|
||||||
value.OnStateTransitionReady += OnStateTransitionReady;
|
value.OnStateTransitionReady += OnStateTransitionReady;
|
||||||
}
|
}
|
||||||
|
@@ -35,7 +35,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
|||||||
shouldBeTicking = false;
|
shouldBeTicking = false;
|
||||||
|
|
||||||
State = TimerState.Stopped;
|
State = TimerState.Stopped;
|
||||||
OnStopped?.InvokeSafe(this);
|
OnStopped?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnUpdate()
|
protected override void OnUpdate()
|
||||||
@@ -46,7 +46,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
|||||||
double delta = Universe.Time.DeltaSpan.TotalSeconds;
|
double delta = Universe.Time.DeltaSpan.TotalSeconds;
|
||||||
|
|
||||||
Time += delta;
|
Time += delta;
|
||||||
OnDelta?.InvokeSafe(this, delta);
|
OnDelta?.Invoke(this, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnEnteredUniverse(IUniverse universe)
|
protected override void OnEnteredUniverse(IUniverse universe)
|
||||||
@@ -71,13 +71,13 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
|||||||
public virtual void Pause()
|
public virtual void Pause()
|
||||||
{
|
{
|
||||||
State = TimerState.Paused;
|
State = TimerState.Paused;
|
||||||
OnPaused?.InvokeSafe(this);
|
OnPaused?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Resume()
|
public virtual void Resume()
|
||||||
{
|
{
|
||||||
State = TimerState.Ticking;
|
State = TimerState.Ticking;
|
||||||
OnResumed?.InvokeSafe(this);
|
OnResumed?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartStopwatch()
|
private void StartStopwatch()
|
||||||
@@ -85,7 +85,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
|||||||
hasStartedTickingBefore = true;
|
hasStartedTickingBefore = true;
|
||||||
|
|
||||||
State = TimerState.Ticking;
|
State = TimerState.Ticking;
|
||||||
OnStarted?.InvokeSafe(this);
|
OnStarted?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnFinalize()
|
protected override void OnFinalize()
|
||||||
|
@@ -26,7 +26,7 @@ public class TickerBehaviour : StopwatchBehaviour, ITicker
|
|||||||
{
|
{
|
||||||
nextTick += Period;
|
nextTick += Period;
|
||||||
TickCounter++;
|
TickCounter++;
|
||||||
OnTick?.InvokeSafe(this);
|
OnTick?.Invoke(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -50,7 +50,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
|||||||
shouldBeTicking = false;
|
shouldBeTicking = false;
|
||||||
|
|
||||||
State = TimerState.Stopped;
|
State = TimerState.Stopped;
|
||||||
OnStopped?.InvokeSafe(this);
|
OnStopped?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnUpdate()
|
protected override void OnUpdate()
|
||||||
@@ -61,7 +61,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
|||||||
double delta = Universe.Time.DeltaSpan.TotalSeconds;
|
double delta = Universe.Time.DeltaSpan.TotalSeconds;
|
||||||
|
|
||||||
Remaining -= delta;
|
Remaining -= delta;
|
||||||
OnDelta?.InvokeSafe(this, delta);
|
OnDelta?.Invoke(this, delta);
|
||||||
|
|
||||||
if (Remaining <= .0f)
|
if (Remaining <= .0f)
|
||||||
Stop();
|
Stop();
|
||||||
@@ -89,13 +89,13 @@ public class TimerBehaviour : Behaviour, ITimer
|
|||||||
public virtual void Pause()
|
public virtual void Pause()
|
||||||
{
|
{
|
||||||
State = TimerState.Paused;
|
State = TimerState.Paused;
|
||||||
OnPaused?.InvokeSafe(this);
|
OnPaused?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Resume()
|
public virtual void Resume()
|
||||||
{
|
{
|
||||||
State = TimerState.Ticking;
|
State = TimerState.Ticking;
|
||||||
OnResumed?.InvokeSafe(this);
|
OnResumed?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void StartTimer()
|
private void StartTimer()
|
||||||
@@ -103,7 +103,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
|||||||
hasStartedTickingBefore = true;
|
hasStartedTickingBefore = true;
|
||||||
|
|
||||||
State = TimerState.Ticking;
|
State = TimerState.Ticking;
|
||||||
OnStarted?.InvokeSafe(this);
|
OnStarted?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnFinalize()
|
protected override void OnFinalize()
|
||||||
|
@@ -6,5 +6,5 @@ namespace Syntriax.Engine.Systems.Tween;
|
|||||||
public static class TweenAABBExtensions
|
public static class TweenAABBExtensions
|
||||||
{
|
{
|
||||||
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, Action<AABB> setMethod)
|
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, Action<AABB> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(new AABB(initialAABB.LowerBoundary.Lerp(targetAABB.LowerBoundary, t), initialAABB.UpperBoundary.Lerp(targetAABB.UpperBoundary, t))));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(initialAABB.LowerBoundary.Lerp(targetAABB.LowerBoundary, t), initialAABB.UpperBoundary.Lerp(targetAABB.UpperBoundary, t))));
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ public static class TweenCircleExtensions
|
|||||||
{
|
{
|
||||||
public static ITween TweenCircle(this Circle initialCircle, ITweenManager tweenManager, float duration, Circle targetCircle, System.Action<Circle> setMethod)
|
public static ITween TweenCircle(this Circle initialCircle, ITweenManager tweenManager, float duration, Circle targetCircle, System.Action<Circle> setMethod)
|
||||||
=> tweenManager.StartTween(duration,
|
=> tweenManager.StartTween(duration,
|
||||||
t => setMethod?.InvokeSafe(
|
t => setMethod?.Invoke(
|
||||||
new Circle(
|
new Circle(
|
||||||
initialCircle.Center.Lerp(targetCircle.Center, t),
|
initialCircle.Center.Lerp(targetCircle.Center, t),
|
||||||
initialCircle.Diameter.Lerp(targetCircle.Diameter, t)
|
initialCircle.Diameter.Lerp(targetCircle.Diameter, t)
|
||||||
|
@@ -5,11 +5,11 @@ namespace Syntriax.Engine.Systems.Tween;
|
|||||||
public static class TweenColorExtensions
|
public static class TweenColorExtensions
|
||||||
{
|
{
|
||||||
public static ITween TweenColor(this ColorRGB initialColorRGB, ITweenManager tweenManager, float duration, ColorRGB targetColorRGB, System.Action<ColorRGB> setMethod)
|
public static ITween TweenColor(this ColorRGB initialColorRGB, ITweenManager tweenManager, float duration, ColorRGB targetColorRGB, System.Action<ColorRGB> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialColorRGB.Lerp(targetColorRGB, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorRGB.Lerp(targetColorRGB, t)));
|
||||||
|
|
||||||
public static ITween TweenColor(this ColorRGBA initialColorRGBA, ITweenManager tweenManager, float duration, ColorRGBA targetColorRGBA, System.Action<ColorRGBA> setMethod)
|
public static ITween TweenColor(this ColorRGBA initialColorRGBA, ITweenManager tweenManager, float duration, ColorRGBA targetColorRGBA, System.Action<ColorRGBA> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialColorRGBA.Lerp(targetColorRGBA, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorRGBA.Lerp(targetColorRGBA, t)));
|
||||||
|
|
||||||
public static ITween TweenColor(this ColorHSV initialColorHSV, ITweenManager tweenManager, float duration, ColorHSV targetColorHSV, System.Action<ColorHSV> setMethod)
|
public static ITween TweenColor(this ColorHSV initialColorHSV, ITweenManager tweenManager, float duration, ColorHSV targetColorHSV, System.Action<ColorHSV> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialColorHSV.Lerp(targetColorHSV, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorHSV.Lerp(targetColorHSV, t)));
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ public static class TweenLine2DEquationExtensions
|
|||||||
{
|
{
|
||||||
public static ITween TweenLine2DEquation(this Line2DEquation initialLine2DEquation, ITweenManager tweenManager, float duration, Line2DEquation targetLine2DEquation, System.Action<Line2DEquation> setMethod)
|
public static ITween TweenLine2DEquation(this Line2DEquation initialLine2DEquation, ITweenManager tweenManager, float duration, Line2DEquation targetLine2DEquation, System.Action<Line2DEquation> setMethod)
|
||||||
=> tweenManager.StartTween(duration,
|
=> tweenManager.StartTween(duration,
|
||||||
t => setMethod?.InvokeSafe(
|
t => setMethod?.Invoke(
|
||||||
new Line2DEquation(
|
new Line2DEquation(
|
||||||
initialLine2DEquation.Slope.Lerp(targetLine2DEquation.Slope, t),
|
initialLine2DEquation.Slope.Lerp(targetLine2DEquation.Slope, t),
|
||||||
initialLine2DEquation.OffsetY.Lerp(targetLine2DEquation.OffsetY, t)
|
initialLine2DEquation.OffsetY.Lerp(targetLine2DEquation.OffsetY, t)
|
||||||
|
@@ -6,7 +6,7 @@ public static class TweenLine2DExtensions
|
|||||||
{
|
{
|
||||||
public static ITween TweenLine2D(this Line2D initialLine2D, ITweenManager tweenManager, float duration, Line2D targetLine2D, System.Action<Line2D> setMethod)
|
public static ITween TweenLine2D(this Line2D initialLine2D, ITweenManager tweenManager, float duration, Line2D targetLine2D, System.Action<Line2D> setMethod)
|
||||||
=> tweenManager.StartTween(duration,
|
=> tweenManager.StartTween(duration,
|
||||||
t => setMethod?.InvokeSafe(
|
t => setMethod?.Invoke(
|
||||||
new Line2D(
|
new Line2D(
|
||||||
initialLine2D.From.Lerp(targetLine2D.From, t),
|
initialLine2D.From.Lerp(targetLine2D.From, t),
|
||||||
initialLine2D.To.Lerp(targetLine2D.To, t)
|
initialLine2D.To.Lerp(targetLine2D.To, t)
|
||||||
|
@@ -7,7 +7,7 @@ public static class TweenProjection1DExtensions
|
|||||||
{
|
{
|
||||||
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, Action<Projection1D> setMethod)
|
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, Action<Projection1D> setMethod)
|
||||||
=> tweenManager.StartTween(duration,
|
=> tweenManager.StartTween(duration,
|
||||||
t => setMethod?.InvokeSafe(
|
t => setMethod?.Invoke(
|
||||||
new Projection1D(
|
new Projection1D(
|
||||||
initialProjection1D.Min.Lerp(targetProjection1D.Min, t),
|
initialProjection1D.Min.Lerp(targetProjection1D.Min, t),
|
||||||
initialProjection1D.Max.Lerp(targetProjection1D.Max, t)
|
initialProjection1D.Max.Lerp(targetProjection1D.Max, t)
|
||||||
|
@@ -6,5 +6,5 @@ namespace Syntriax.Engine.Systems.Tween;
|
|||||||
public static class TweenQuaternionExtensions
|
public static class TweenQuaternionExtensions
|
||||||
{
|
{
|
||||||
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, Action<Quaternion> setMethod)
|
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, Action<Quaternion> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialQuaternion.SLerp(targetQuaternion, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialQuaternion.SLerp(targetQuaternion, t)));
|
||||||
}
|
}
|
||||||
|
@@ -39,4 +39,69 @@ public static class TweenTransform2DExtensions
|
|||||||
float initialLocalRotation = transform2D.LocalRotation;
|
float initialLocalRotation = transform2D.LocalRotation;
|
||||||
return tweenManager.StartTween(duration, t => transform2D.LocalRotation = initialLocalRotation.Lerp(targetLocalRotation, t));
|
return tweenManager.StartTween(duration, t => transform2D.LocalRotation = initialLocalRotation.Lerp(targetLocalRotation, t));
|
||||||
}
|
}
|
||||||
|
public static ITween TweenPositionAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additivePosition)
|
||||||
|
{
|
||||||
|
Vector2D progressedPosition = Vector2D.Zero;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
Vector2D displacement = Vector2D.Zero.Lerp(additivePosition, t) - progressedPosition;
|
||||||
|
transform2D.Position += displacement;
|
||||||
|
progressedPosition += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITween TweenScaleAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additiveScale)
|
||||||
|
{
|
||||||
|
Vector2D progressedScale = Vector2D.Zero;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
Vector2D displacement = Vector2D.Zero.Lerp(additiveScale, t) - progressedScale;
|
||||||
|
transform2D.Scale += displacement;
|
||||||
|
progressedScale += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITween TweenRotationAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float additiveRotation)
|
||||||
|
{
|
||||||
|
float progressedRotation = 0f;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
float displacement = 0f.Lerp(additiveRotation, t) - progressedRotation;
|
||||||
|
transform2D.Rotation += displacement;
|
||||||
|
progressedRotation += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITween TweenLocalPositionAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additiveLocalPosition)
|
||||||
|
{
|
||||||
|
Vector2D progressedLocalPosition = Vector2D.Zero;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
Vector2D displacement = Vector2D.Zero.Lerp(additiveLocalPosition, t) - progressedLocalPosition;
|
||||||
|
transform2D.LocalPosition += displacement;
|
||||||
|
progressedLocalPosition += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITween TweenLocalScaleAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additiveLocalScale)
|
||||||
|
{
|
||||||
|
Vector2D progressedLocalScale = Vector2D.Zero;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
Vector2D displacement = Vector2D.Zero.Lerp(additiveLocalScale, t) - progressedLocalScale;
|
||||||
|
transform2D.LocalScale += displacement;
|
||||||
|
progressedLocalScale += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ITween TweenLocalRotationAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float additiveLocalRotation)
|
||||||
|
{
|
||||||
|
float progressedLocalRotation = 0f;
|
||||||
|
return tweenManager.StartTween(duration, t =>
|
||||||
|
{
|
||||||
|
float displacement = 0f.Lerp(additiveLocalRotation, t) - progressedLocalRotation;
|
||||||
|
transform2D.LocalRotation += displacement;
|
||||||
|
progressedLocalRotation += displacement;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,7 @@ public static class TweenTriangleExtensions
|
|||||||
{
|
{
|
||||||
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, Action<Triangle> setMethod)
|
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, Action<Triangle> setMethod)
|
||||||
=> tweenManager.StartTween(duration,
|
=> tweenManager.StartTween(duration,
|
||||||
t => setMethod?.InvokeSafe(
|
t => setMethod?.Invoke(
|
||||||
new Triangle(
|
new Triangle(
|
||||||
initialTriangle.A.Lerp(targetTriangle.A, t),
|
initialTriangle.A.Lerp(targetTriangle.A, t),
|
||||||
initialTriangle.B.Lerp(targetTriangle.B, t),
|
initialTriangle.B.Lerp(targetTriangle.B, t),
|
||||||
|
@@ -5,5 +5,5 @@ namespace Syntriax.Engine.Systems.Tween;
|
|||||||
public static class TweenVector2DExtensions
|
public static class TweenVector2DExtensions
|
||||||
{
|
{
|
||||||
public static ITween TweenVector2D(this Vector2D initialVector2D, ITweenManager tweenManager, float duration, Vector2D targetVector2D, System.Action<Vector2D> setMethod)
|
public static ITween TweenVector2D(this Vector2D initialVector2D, ITweenManager tweenManager, float duration, Vector2D targetVector2D, System.Action<Vector2D> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialVector2D.Lerp(targetVector2D, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector2D.Lerp(targetVector2D, t)));
|
||||||
}
|
}
|
||||||
|
@@ -5,5 +5,5 @@ namespace Syntriax.Engine.Systems.Tween;
|
|||||||
public static class TweenVector3DExtensions
|
public static class TweenVector3DExtensions
|
||||||
{
|
{
|
||||||
public static ITween TweenVector3D(this Vector3D initialVector3D, ITweenManager tweenManager, float duration, Vector3D targetVector3D, System.Action<Vector3D> setMethod)
|
public static ITween TweenVector3D(this Vector3D initialVector3D, ITweenManager tweenManager, float duration, Vector3D targetVector3D, System.Action<Vector3D> setMethod)
|
||||||
=> tweenManager.StartTween(duration, t => setMethod?.InvokeSafe(initialVector3D.Lerp(targetVector3D, t)));
|
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector3D.Lerp(targetVector3D, t)));
|
||||||
}
|
}
|
||||||
|
@@ -26,14 +26,14 @@ internal class Tween : ITween
|
|||||||
_state = value;
|
_state = value;
|
||||||
switch (value)
|
switch (value)
|
||||||
{
|
{
|
||||||
case TweenState.Completed: OnCompleted?.InvokeSafe(this); OnEnded?.InvokeSafe(this); break;
|
case TweenState.Completed: OnCompleted?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||||
case TweenState.Cancelled: OnCancelled?.InvokeSafe(this); OnEnded?.InvokeSafe(this); break;
|
case TweenState.Cancelled: OnCancelled?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||||
case TweenState.Paused: OnPaused?.InvokeSafe(this); break;
|
case TweenState.Paused: OnPaused?.Invoke(this); break;
|
||||||
case TweenState.Playing:
|
case TweenState.Playing:
|
||||||
if (previousState == TweenState.Idle)
|
if (previousState == TweenState.Idle)
|
||||||
OnStarted?.InvokeSafe(this);
|
OnStarted?.Invoke(this);
|
||||||
else
|
else
|
||||||
OnResumed?.InvokeSafe(this);
|
OnResumed?.Invoke(this);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -58,9 +58,9 @@ internal class Tween : ITween
|
|||||||
|
|
||||||
_counter = value.Min(Duration).Max(0f);
|
_counter = value.Min(Duration).Max(0f);
|
||||||
Progress = Counter / Duration;
|
Progress = Counter / Duration;
|
||||||
OnUpdated?.InvokeSafe(this);
|
OnUpdated?.Invoke(this);
|
||||||
|
|
||||||
OnDeltaUpdated?.InvokeSafe(this, Easing.Evaluate(Progress) - Easing.Evaluate(previousProgress));
|
OnDeltaUpdated?.Invoke(this, Easing.Evaluate(Progress) - Easing.Evaluate(previousProgress));
|
||||||
|
|
||||||
if (_counter >= Duration)
|
if (_counter >= Duration)
|
||||||
State = TweenState.Completed;
|
State = TweenState.Completed;
|
||||||
|
@@ -46,56 +46,56 @@ public static class TweenExtensions
|
|||||||
public static ITween OnStart(this ITween tween, Action callback)
|
public static ITween OnStart(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnStarted += _ => callback.InvokeSafe();
|
tweenConcrete.OnStarted += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnPause(this ITween tween, Action callback)
|
public static ITween OnPause(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnPaused += _ => callback.InvokeSafe();
|
tweenConcrete.OnPaused += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnResume(this ITween tween, Action callback)
|
public static ITween OnResume(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnResumed += _ => callback.InvokeSafe();
|
tweenConcrete.OnResumed += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnCancel(this ITween tween, Action callback)
|
public static ITween OnCancel(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnCancelled += _ => callback.InvokeSafe();
|
tweenConcrete.OnCancelled += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnComplete(this ITween tween, Action callback)
|
public static ITween OnComplete(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnCompleted += _ => callback.InvokeSafe();
|
tweenConcrete.OnCompleted += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnEnd(this ITween tween, Action callback)
|
public static ITween OnEnd(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnEnded += _ => callback.InvokeSafe();
|
tweenConcrete.OnEnded += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnUpdate(this ITween tween, Action callback)
|
public static ITween OnUpdate(this ITween tween, Action callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnUpdated += _ => callback.InvokeSafe();
|
tweenConcrete.OnUpdated += _ => callback.Invoke();
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ITween OnDeltaUpdate(this ITween tween, Action<float> callback)
|
public static ITween OnDeltaUpdate(this ITween tween, Action<float> callback)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
tweenConcrete.OnDeltaUpdated += (_, delta) => callback.InvokeSafe(delta);
|
tweenConcrete.OnDeltaUpdated += (_, delta) => callback.Invoke(delta);
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@ public class TweenManager : UniverseObject, ITweenManager
|
|||||||
public ITween StartTween(float duration, ITweenManager.TweenSetCallback? setCallback = null)
|
public ITween StartTween(float duration, ITweenManager.TweenSetCallback? setCallback = null)
|
||||||
{
|
{
|
||||||
Tween tween = new(duration);
|
Tween tween = new(duration);
|
||||||
tween.OnUpdated += tween => setCallback?.InvokeSafe(tween.Value);
|
tween.OnUpdated += tween => setCallback?.Invoke(tween.Value);
|
||||||
runningCoroutines.Add(tween, coroutineManager.StartCoroutine(RunTween(tween)));
|
runningCoroutines.Add(tween, coroutineManager.StartCoroutine(RunTween(tween)));
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user