68 Commits

Author SHA1 Message Date
e9d4c3eb64 fix: coroutine managers now handle exceptions 2026-04-10 11:22:40 +03:00
50794d44ba fix: replace LoggerBase FilterLevel from Trace to Info 2026-04-09 14:01:30 +03:00
5cd3b938f7 fix: yaml type containers are not being recognized while reading back 2026-04-08 15:21:37 +03:00
f81bd61aa1 fix: yaml serialized messing up string fields 2026-04-08 14:24:40 +03:00
6d8ba5c80c feat: added a wip nested coroutine manager
This coroutine manager allows for nested IEnumerators, however it is experimental at the moment.
2026-04-07 18:53:02 +03:00
b713fe4f12 feat: task.ToYield extension method added 2026-04-07 16:19:04 +03:00
4b3a0fdde0 chore: renamed task completion status names 2026-04-07 16:18:35 +03:00
44ff916afe fix: WaitForTaskYield getting stuck 2026-04-06 17:07:00 +03:00
497eedab72 chore: added throwing task exception when faulted on WaitForTaskYield 2026-04-06 15:01:05 +03:00
3893a1d249 chore: forgotten test physics material 2026-04-04 21:21:57 +03:00
4255cc4893 fix: some bad idea 2026-04-04 20:39:29 +03:00
37f4f56cd6 feat(physics): IRigidbody Intertia and Inverse Mass & Intertia fields added 2026-04-04 19:38:29 +03:00
629d758dbc feat: ICollider.OnRecalculated event added 2026-04-04 19:02:25 +03:00
7fb6821a83 feat(math): Math.OneOver and Invert methods added 2026-04-04 18:52:05 +03:00
af2eed2200 feat(physics): added area & inertia calculations for the shape and circles 2026-04-04 18:51:55 +03:00
6db427f39b chore: converted PhysicsMaterial2D's to classes and renamed Default to be ReadOnly 2026-04-04 14:07:37 +03:00
53b342da46 feat: added CollisionDetectionInformation.ContactPoint 2026-04-04 00:13:30 +03:00
4c13578125 feat: basic impulse resolving for 2D collisions 2026-04-03 21:41:32 +03:00
a47f8e4944 fix: color lerping not working properly 2026-03-31 23:11:42 +03:00
24046cf60c fix: triangle batch not drawing transparent colors properly 2026-03-31 22:51:59 +03:00
2266f69927 fix: color lerping not working from bright to darker 2026-03-31 21:01:10 +03:00
c11aced8d2 feat: added default valueless Get to IConfiguration for nullables 2026-03-31 17:21:18 +03:00
fe0173b091 fix: SpriteBatcher not drawing correctly 2026-03-28 22:35:47 +03:00
0707665481 feat: added a basic Viewport field to ICamera 2026-03-28 22:35:01 +03:00
734649955e feat: added .ApplyMatrix extension method for Matrix4x4 2026-03-28 22:32:51 +03:00
105b87da3a fix: file & type name mismatches fixed 2026-03-08 15:12:16 +03:00
51534606c8 feat: added WaitForTask yield 2026-03-08 12:55:22 +03:00
1418927c32 refactor: renamed WaitForSeconds to a more general WaitForTime class 2026-03-08 12:53:02 +03:00
35c7eb9578 refactor: renamed NetworkBehaviour to CommonNetworkBehaviour 2026-03-06 11:55:10 +03:00
6ca3f22b17 feat: added UpdateManager.CallFirstActiveFrameImmediately method for early calls of behaviours 2026-03-06 11:51:15 +03:00
7ae8b4feb0 feat: added NetworkBehaviour for oth client & server communication 2026-03-05 23:48:57 +03:00
e84c6edce1 refactor: removed the IComparable from IIdentifiable and implemented in extension method 2026-03-04 20:16:07 +03:00
4326d5615e feat: INetworkCommunicatorServer.SendToClients extension method added 2026-03-04 12:32:58 +03:00
fa514531bf feat: added IIdentifiable comparison interface & IsIdentical extension method 2026-03-03 23:30:56 +03:00
1d3dd8b046 chore: fixed IIdentifiable file name and class name being different 2026-03-03 23:14:37 +03:00
785fee9b6b perf: memory allocation improvements on ITween.Loop method 2026-02-20 11:22:46 +03:00
9f54f89f6d fix: ITween.OnEnded getting multiple calls and getting unnecessary calls on repeats fixed 2026-02-20 11:22:00 +03:00
aadc87d78a perf: memory allocation improvements on ITween.LoopIndefinitely method 2026-02-20 11:00:49 +03:00
d653774357 fix: forgotten Save method for YamlConfiguration 2026-02-09 13:23:22 +03:00
45bd505da7 chore: renamed parameter names for ISerializer methods 2026-02-09 13:22:37 +03:00
3b1c291588 feat: IConfiguration for system and other configurations 2026-02-09 13:17:10 +03:00
32a7e9be24 fix: forgotten yaml converter for Matrix4x4 2026-02-09 13:14:45 +03:00
499f875903 chore: removed unused piece of code 2026-02-02 14:42:34 +03:00
b2cfb2a590 docs: added NetworkManager comments 2026-02-01 13:34:02 +03:00
1d6b9d2421 feat: added WaitForSeconds and WaitWhile yields 2026-01-31 13:08:59 +03:00
882f9e8b29 feat: added new yields 2026-01-31 13:08:13 +03:00
913af2a4a4 fix: added dynamic index updates during event invocation so there are no missing/duplicate invocations
This also makes the events very not ideal for multithreaded applications at the moment.
2026-01-31 00:49:00 +03:00
4e9fda3d7c refactor: auto property on FastListOrdered 2026-01-30 18:33:07 +03:00
7675f9acac chore: added missing Matrix4x4NetPacker 2026-01-30 18:02:34 +03:00
72f86478f2 feat!: added broadcast & routing support for NetworkManager
It used to only route or broadcast, now the same packet can be both routed to it's own behaviour AND at the same time the broadcast listeners can get alerted of the same package such as universe-wide managers
2026-01-30 13:43:48 +03:00
64e7321f0f fix: rotating file logger deleting from the wrong order 2026-01-30 10:54:56 +03:00
c355c666e0 refactor: fixed LiteNetLibServer using events to subscribe to PostUpdate instead of the interface 2026-01-29 22:29:19 +03:00
b9f3227f73 refactor: removed parameters on triangle batch calls on TriangleBatcher for multi window support 2026-01-28 22:19:04 +03:00
c68de39c83 fix: MonoGame view matrix calculation issues 2026-01-28 12:58:25 +03:00
ad444decbb perf: removed old position flipping on MonoGame 2026-01-28 11:16:47 +03:00
297e0eb790 fix: typos in Matrix4x4 methods 2026-01-28 11:14:31 +03:00
efa4da4398 fix: ScreenToWorldPosition & vice versa methods on MonoGameCamera2D fixed 2026-01-28 11:06:15 +03:00
e30280f1f8 fix: missing implicit conversion operators on Vectors 2026-01-28 11:02:07 +03:00
d4437edfbf feat: added Matrix4x4 x Vector4D multiplication 2026-01-28 10:22:36 +03:00
08f32f96e4 BREAKING CHANGE: 4x4 matrices are now column major 2026-01-27 23:45:50 +03:00
9294df8a19 feat: added support for multiple batches 2026-01-27 21:28:31 +03:00
50a0269798 feat: added matrix 4x4 transpose & orthographic view matrix methods 2026-01-27 20:48:39 +03:00
985f898327 refactor: moved drawable triangles into systems project for platform agnosticism 2026-01-27 13:04:04 +03:00
7c9973a5e7 refactor: ICamera interface added with view and projection matrices fields 2026-01-26 23:48:16 +03:00
af6dde84fd fix: universe entrance issues where entering behaviours adding more behaviours causing duplicate/missing calls 2026-01-26 23:23:49 +03:00
e2820670c6 fix: classname x filename inconsistency fixed 2026-01-26 22:01:36 +03:00
5ce5e4eb0b feat: added inverse & float scale methods to 4x4 matrices 2026-01-26 21:48:15 +03:00
ee58e60ef1 fix: universe entrance issues caused by wrong logic 2026-01-26 12:15:42 +03:00
108 changed files with 1814 additions and 498 deletions

View File

@@ -0,0 +1,22 @@
namespace Engine.Core;
/// <summary>
/// Represents a camera with view and projections matrices in the engine.
/// </summary>
public interface ICamera
{
/// <summary>
/// The viewport of the <see cref="ICamera"/>.
/// </summary>
Vector2D Viewport { get; }
/// <summary>
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
/// </summary>
Matrix4x4 ViewMatrix { get; }
/// <summary>
/// Projection <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
/// </summary>
Matrix4x4 ProjectionMatrix { get; }
}

View File

@@ -3,7 +3,7 @@ namespace Engine.Core;
/// <summary>
/// Represents a 2D camera in the engine.
/// </summary>
public interface ICamera2D : IBehaviour2D
public interface ICamera2D : ICamera, IBehaviour2D
{
/// <summary>
/// The zoom level of the camera.

View File

@@ -3,7 +3,7 @@ namespace Engine.Core;
/// <summary>
/// Represents a 3D camera in the engine.
/// </summary>
public interface ICamera3D : IBehaviour3D
public interface ICamera3D : ICamera, IBehaviour3D
{
/// <summary>
/// Event triggered when the near plane of the <see cref="ICamera3D"/> changes.

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
namespace Engine.Core.Config;
public class BasicConfiguration : IConfiguration
{
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnAdded { get; } = new();
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnSet { get; } = new();
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnRemoved { get; } = new();
private readonly Dictionary<string, object?> values = [];
public IReadOnlyDictionary<string, object?> Values => values;
public T Get<T>(string key, T defaultValue) => Get<T>(key) ?? defaultValue;
public T? Get<T>(string key)
{
if (!values.TryGetValue(key, out object? value))
return default;
if (value is T castedObject)
return castedObject;
try { return (T?)System.Convert.ChangeType(value, typeof(T)); } catch { }
return default;
}
public object? Get(string key)
{
values.TryGetValue(key, out object? value);
return value;
}
public bool Has(string key) => values.ContainsKey(key);
public void Remove<T>(string key)
{
if (values.Remove(key))
OnRemoved.Invoke(this, new(key));
}
public void Set<T>(string key, T value)
{
if (!values.TryAdd(key, value))
values[key] = value;
else
OnAdded.Invoke(this, new(key));
OnSet.Invoke(this, new(key));
}
}

View File

@@ -0,0 +1,8 @@
using Engine.Core.Exceptions;
namespace Engine.Core.Config;
public static class ConfigurationExtensions
{
public static T GetRequired<T>(this IConfiguration configuration, string key) => configuration.Get<T>(key) ?? throw new NotFoundException($"Type of {typeof(T).FullName} with the key {key} was not present in the {configuration.GetType().FullName}");
}

View File

@@ -0,0 +1,24 @@
using System.Collections.Generic;
namespace Engine.Core.Config;
public interface IConfiguration
{
static IConfiguration System { get; set; } = new SystemConfiguration();
static IConfiguration Shared { get; set; } = new BasicConfiguration();
Event<IConfiguration, ConfigUpdateArguments> OnAdded { get; }
Event<IConfiguration, ConfigUpdateArguments> OnSet { get; }
Event<IConfiguration, ConfigUpdateArguments> OnRemoved { get; }
IReadOnlyDictionary<string, object?> Values { get; }
bool Has(string key);
object? Get(string key);
T Get<T>(string key, T defaultValue);
T? Get<T>(string key);
void Set<T>(string key, T value);
void Remove<T>(string key);
readonly record struct ConfigUpdateArguments(string Key);
}

View File

@@ -0,0 +1,11 @@
namespace Engine.Core.Config;
public class SystemConfiguration : BasicConfiguration, IConfiguration
{
public SystemConfiguration()
{
foreach (System.Collections.DictionaryEntry entry in System.Environment.GetEnvironmentVariables())
if (entry is { Key: string key, Value: not null })
Set(key, entry.Value);
}
}

View File

@@ -4,7 +4,7 @@ namespace Engine.Core.Debug;
public abstract class LoggerBase : ILogger
{
public ILogger.Level FilterLevel { get; set; } = ILogger.Level.Trace;
public ILogger.Level FilterLevel { get; set; } = ILogger.Level.Info;
public void Log(string message, ILogger.Level level = ILogger.Level.Info, bool force = false)
{

View File

@@ -55,7 +55,7 @@ public class RotatingFileLogger : ILogger
private static void RotateLastLogs(string directory, string prefix, int rotateLength)
{
IOrderedEnumerable<string> logs = System.IO.Directory.GetFiles(directory, $"{prefix}*.log")
.OrderBy(File.GetCreationTime);
.OrderByDescending(File.GetCreationTime);
foreach (string file in logs.Skip(rotateLength))
try

View File

@@ -0,0 +1,12 @@
namespace Engine.Core;
public static class IdentifiableExtensions
{
public static bool IsIdentical(this IIdentifiable? left, IIdentifiable? right)
{
if (left == null || right == null)
return false;
return left?.Id?.CompareTo(right?.Id) == 0;
}
}

View File

@@ -58,6 +58,9 @@ public class Event
public ILogger Logger { get; set => field = value ?? ILogger.Shared; } = ILogger.Shared;
private int currentOnceCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private int currentCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@@ -74,6 +77,9 @@ public class Event
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentCallIndex)
currentCallIndex++;
listeners.Insert(insertIndex, listenerData);
}
@@ -90,6 +96,9 @@ public class Event
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentOnceCallIndex)
currentOnceCallIndex++;
onceListeners.Insert(insertIndex, listenerData);
}
@@ -103,6 +112,8 @@ public class Event
if (listeners[i].Callback == listener)
{
listeners.RemoveAt(i);
if (i < currentCallIndex)
currentCallIndex--;
return;
}
}
@@ -117,6 +128,8 @@ public class Event
if (onceListeners[i].Callback == listener)
{
onceListeners.RemoveAt(i);
if (i < currentOnceCallIndex)
currentOnceCallIndex--;
return;
}
}
@@ -131,23 +144,23 @@ public class Event
/// </summary>
public void Invoke()
{
for (int i = listeners.Count - 1; i >= 0; i--)
try { listeners[i].Callback.Invoke(); }
for (currentCallIndex = listeners.Count - 1; currentCallIndex >= 0; currentCallIndex--)
try { listeners[currentCallIndex].Callback.Invoke(); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}()";
EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{listeners[currentCallIndex].Callback.Method.DeclaringType?.FullName}.{listeners[currentCallIndex].Callback.Method.Name}()";
EventHelpers.LogInvocationException(listeners[currentCallIndex].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
for (currentOnceCallIndex = onceListeners.Count - 1; currentOnceCallIndex >= 0; currentOnceCallIndex--)
{
try { onceListeners[i].Callback.Invoke(); }
try { onceListeners[currentOnceCallIndex].Callback.Invoke(); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}()";
EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{onceListeners[currentOnceCallIndex].Callback.Method.DeclaringType?.FullName}.{onceListeners[currentOnceCallIndex].Callback.Method.Name}()";
EventHelpers.LogInvocationException(onceListeners[currentOnceCallIndex].Callback.Target ?? this, Logger, exception, methodCallRepresentation);
}
onceListeners.RemoveAt(i);
onceListeners.RemoveAt(currentOnceCallIndex);
}
}
@@ -216,6 +229,9 @@ public class Event<TSender> where TSender : class
public ILogger Logger { get; set => field = value ?? ILogger.Shared; } = ILogger.Shared;
private int currentOnceCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private int currentCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@@ -232,6 +248,9 @@ public class Event<TSender> where TSender : class
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentCallIndex)
currentCallIndex++;
listeners.Insert(insertIndex, listenerData);
}
@@ -248,6 +267,9 @@ public class Event<TSender> where TSender : class
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentOnceCallIndex)
currentOnceCallIndex++;
onceListeners.Insert(insertIndex, listenerData);
}
@@ -261,6 +283,8 @@ public class Event<TSender> where TSender : class
if (listeners[i].Callback == listener)
{
listeners.RemoveAt(i);
if (i < currentCallIndex)
currentCallIndex--;
return;
}
}
@@ -275,6 +299,8 @@ public class Event<TSender> where TSender : class
if (onceListeners[i].Callback == listener)
{
onceListeners.RemoveAt(i);
if (i < currentOnceCallIndex)
currentOnceCallIndex--;
return;
}
}
@@ -290,23 +316,23 @@ public class Event<TSender> where TSender : class
/// <param name="sender">The caller that's triggering this event.</param>
public void Invoke(TSender sender)
{
for (int i = listeners.Count - 1; i >= 0; i--)
try { listeners[i].Callback.Invoke(sender); }
for (currentCallIndex = listeners.Count - 1; currentCallIndex >= 0; currentCallIndex--)
try { listeners[currentCallIndex].Callback.Invoke(sender); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender})";
EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{listeners[currentCallIndex].Callback.Method.DeclaringType?.FullName}.{listeners[currentCallIndex].Callback.Method.Name}({sender})";
EventHelpers.LogInvocationException(listeners[currentCallIndex].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
for (currentOnceCallIndex = onceListeners.Count - 1; currentOnceCallIndex >= 0; currentOnceCallIndex--)
{
try { onceListeners[i].Callback.Invoke(sender); }
try { onceListeners[currentOnceCallIndex].Callback.Invoke(sender); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender})";
EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{onceListeners[currentOnceCallIndex].Callback.Method.DeclaringType?.FullName}.{onceListeners[currentOnceCallIndex].Callback.Method.Name}({sender})";
EventHelpers.LogInvocationException(onceListeners[currentOnceCallIndex].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
}
onceListeners.RemoveAt(i);
onceListeners.RemoveAt(currentOnceCallIndex);
}
}
@@ -382,6 +408,9 @@ public class Event<TSender, TArguments> where TSender : class
public ILogger Logger { get; set => field = value ?? ILogger.Shared; } = ILogger.Shared;
private int currentOnceCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private int currentCallIndex = -1; // These are for the purpose of if a listener is added/removed during invocation the index is dynamically updated so no missing/duplicate invocations happen.
private readonly List<ListenerData> listeners = null!;
private readonly List<ListenerData> onceListeners = null!;
@@ -398,6 +427,9 @@ public class Event<TSender, TArguments> where TSender : class
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentCallIndex)
currentCallIndex++;
listeners.Insert(insertIndex, listenerData);
}
@@ -414,6 +446,9 @@ public class Event<TSender, TArguments> where TSender : class
if (insertIndex < 0)
insertIndex = ~insertIndex;
if (insertIndex < currentOnceCallIndex)
currentOnceCallIndex++;
onceListeners.Insert(insertIndex, listenerData);
}
@@ -427,6 +462,8 @@ public class Event<TSender, TArguments> where TSender : class
if (listeners[i].Callback == listener)
{
listeners.RemoveAt(i);
if (i < currentCallIndex)
currentCallIndex--;
return;
}
}
@@ -441,6 +478,8 @@ public class Event<TSender, TArguments> where TSender : class
if (onceListeners[i].Callback == listener)
{
onceListeners.RemoveAt(i);
if (i < currentOnceCallIndex)
currentOnceCallIndex--;
return;
}
}
@@ -457,23 +496,23 @@ public class Event<TSender, TArguments> where TSender : class
/// <param name="args">The arguments provided for this event.</param>
public void Invoke(TSender sender, TArguments args)
{
for (int i = listeners.Count - 1; i >= 0; i--)
try { listeners[i].Callback.Invoke(sender, args); }
for (currentCallIndex = listeners.Count - 1; currentCallIndex >= 0; currentCallIndex--)
try { listeners[currentCallIndex].Callback.Invoke(sender, args); }
catch (Exception exception)
{
string methodCallRepresentation = $"{listeners[i].Callback.Method.DeclaringType?.FullName}.{listeners[i].Callback.Method.Name}({sender}, {args})";
EventHelpers.LogInvocationException(listeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{listeners[currentCallIndex].Callback.Method.DeclaringType?.FullName}.{listeners[currentCallIndex].Callback.Method.Name}({sender}, {args})";
EventHelpers.LogInvocationException(listeners[currentCallIndex].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
}
for (int i = onceListeners.Count - 1; i >= 0; i--)
for (currentOnceCallIndex = onceListeners.Count - 1; currentOnceCallIndex >= 0; currentOnceCallIndex--)
{
try { onceListeners[i].Callback.Invoke(sender, args); }
try { onceListeners[currentOnceCallIndex].Callback.Invoke(sender, args); }
catch (Exception exception)
{
string methodCallRepresentation = $"{onceListeners[i].Callback.Method.DeclaringType?.FullName}.{onceListeners[i].Callback.Method.Name}({sender}, {args})";
EventHelpers.LogInvocationException(onceListeners[i].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
string methodCallRepresentation = $"{onceListeners[currentOnceCallIndex].Callback.Method.DeclaringType?.FullName}.{onceListeners[currentOnceCallIndex].Callback.Method.Name}({sender}, {args})";
EventHelpers.LogInvocationException(onceListeners[currentOnceCallIndex].Callback.Target ?? sender, Logger, exception, methodCallRepresentation);
}
onceListeners.RemoveAt(i);
onceListeners.RemoveAt(currentOnceCallIndex);
}
}

View File

@@ -16,8 +16,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
private readonly Func<TItem, TIndex> getIndexFunc = null!;
private readonly IComparer<TIndex> sortBy = null!;
private int count = 0;
public int Count => count;
public int Count { get; private set; } = 0;
public bool IsReadOnly { get; set; } = false;
@@ -35,10 +34,10 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
private (TIndex TIndex, int i) GetAt(Index index)
{
int actualIndex = index.IsFromEnd
? count - index.Value
? Count - index.Value
: index.Value;
if (actualIndex < 0 || actualIndex >= count)
if (actualIndex < 0 || actualIndex >= Count)
throw new IndexOutOfRangeException();
int leftIndex = actualIndex;
@@ -75,7 +74,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
items[key] = list = [];
list.Add(item);
count++;
Count++;
}
public void Insert(int index, TItem item)
@@ -88,7 +87,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
items[tIndex] = list = [];
list.Insert(index, item);
count++;
Count++;
}
public bool Remove(TItem item)
@@ -103,7 +102,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
if (!list.Remove(item))
return false;
count--;
Count--;
return true;
}
@@ -114,7 +113,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
(TIndex tIndex, int i) = GetAt(index);
items[tIndex].RemoveAt(i);
count--;
Count--;
}
public void Clear()
@@ -125,7 +124,7 @@ public class FastListOrdered<TIndex, TItem> : IList<TItem>, IReadOnlyList<TItem>
foreach ((TIndex index, FastList<TItem> list) in items)
list.Clear();
count = 0;
Count = 0;
}
public bool Contains(TItem item)

View File

@@ -30,6 +30,16 @@ public static class Math
/// </summary>
public const float DegreeToRadian = Pi / 180f;
/// <inheritdoc cref="Invert{T}(T)" />
public static T OneOver<T>(T value) where T : INumber<T> => T.One / value;
/// <summary>
/// Gets 1 / value of given <see cref="T"/>.
/// </summary>
/// <param name="value">The value <see cref="T"/>.</param>
/// <returns>1 / value of given <see cref="T"/>.</returns>
public static T Invert<T>(T value) where T : INumber<T> => T.One / value;
/// <summary>
/// Gets one minus of given <see cref="T"/>.
/// </summary>

View File

@@ -8,6 +8,12 @@ public static class MathExtensions
/// <inheritdoc cref="Math.OneMinus{T}(T)" />
public static T OneMinus<T>(this T value) where T : INumber<T> => Math.OneMinus(value);
/// <inheritdoc cref="Math.OneOver{T}(T)" />
public static T OneOver<T>(this T value) where T : INumber<T> => Math.OneOver(value);
/// <inheritdoc cref="Math.Invert{T}(T)" />
public static T Invert<T>(this T value) where T : INumber<T> => Math.OneMinus(value);
/// <inheritdoc cref="Math.Add{T}(T, T)" />
public static T Add<T>(this T left, T value) where T : INumber<T> => Math.Add(left, value);

View File

@@ -34,6 +34,16 @@ public readonly struct Circle(Vector2D center, float radius) : IEquatable<Circle
/// </summary>
public readonly float Diameter => 2f * Radius;
/// <summary>
/// Gets the area of the <see cref="Circle"/>.
/// </summary>
public readonly float Area => Math.Pi * RadiusSquared;
/// <summary>
/// Gets the geometric interia of the <see cref="Circle"/>.
/// </summary>
public readonly float GeometricInertia => .5f * RadiusSquared;
/// <summary>
/// A predefined unit <see cref="Circle"/> with a center at the origin and a radius of 1.
/// </summary>

View File

@@ -95,7 +95,11 @@ public readonly struct ColorRGB(byte r, byte g, byte b) : IEquatable<ColorRGB>
int greenDiff = to.G - from.G;
int blueDiff = to.B - from.B;
return from + new ColorRGB((byte)(redDiff * t), (byte)(greenDiff * t), (byte)(blueDiff * t));
return new(
(byte)(from.R + redDiff * t),
(byte)(from.G + greenDiff * t),
(byte)(from.B + blueDiff * t)
);
}
/// <summary>

View File

@@ -1,4 +1,5 @@
using System;
using Engine.Core.Debug;
namespace Engine.Core;
@@ -125,7 +126,12 @@ public readonly struct ColorRGBA(byte r, byte g, byte b, byte a = 255) : IEquata
int blueDiff = to.B - from.B;
int alphaDiff = to.A - from.A;
return from + new ColorRGBA((byte)(redDiff * t), (byte)(greenDiff * t), (byte)(blueDiff * t), (byte)(alphaDiff * t));
return new(
(byte)(from.R + redDiff * t),
(byte)(from.G + greenDiff * t),
(byte)(from.B + blueDiff * t),
(byte)(from.A + alphaDiff * t)
);
}
/// <summary>

View File

@@ -1,12 +1,11 @@
using System;
using System.Numerics;
namespace Engine.Core;
// TODO Comments
/// <summary>
/// Represents a 4D left handed space matrix.
/// Represents a 4D left handed space matrix in a Column Major convention.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="Matrix4x4"/> struct with the specified values.
@@ -62,6 +61,16 @@ public readonly struct Matrix4x4(
0f, 0f, 0f, 1f
);
/// <summary>
/// Represents the inverted version of this <see cref="Matrix4x4"/>.
/// </summary>
public Matrix4x4 Inverse => Invert(this);
/// <summary>
/// Represents the transposed version of this <see cref="Matrix4x4"/>.
/// </summary>
public Matrix4x4 Transposed => Transpose(this);
public static Matrix4x4 operator *(Matrix4x4 a, Matrix4x4 b) => new(
a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31 + a.M14 * b.M41,
a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32 + a.M14 * b.M42,
@@ -84,6 +93,13 @@ public readonly struct Matrix4x4(
a.M41 * b.M14 + a.M42 * b.M24 + a.M43 * b.M34 + a.M44 * b.M44
);
public static Vector4D operator *(Matrix4x4 m, Vector4D v) => new(
m.M11 * v.X + m.M12 * v.Y + m.M13 * v.Z + m.M14 * v.W,
m.M21 * v.X + m.M22 * v.Y + m.M23 * v.Z + m.M24 * v.W,
m.M31 * v.X + m.M32 * v.Y + m.M33 * v.Z + m.M34 * v.W,
m.M41 * v.X + m.M42 * v.Y + m.M43 * v.Z + m.M44 * v.W
);
public static bool operator ==(Matrix4x4 left, Matrix4x4 right) =>
left.M11 == right.M11 && left.M12 == right.M12 && left.M13 == right.M13 && left.M14 == right.M14 &&
left.M21 == right.M21 && left.M22 == right.M22 && left.M23 == right.M23 && left.M24 == right.M24 &&
@@ -129,11 +145,98 @@ public readonly struct Matrix4x4(
m.M13 * m.M21 * m.M32 * m.M44 - m.M11 * m.M23 * m.M32 * m.M44 -
m.M12 * m.M21 * m.M33 * m.M44 + m.M11 * m.M22 * m.M33 * m.M44;
/// <summary>
/// Inverts the given <see cref="Matrix4x4"/>.
/// </summary>
/// <param name="m">The <see cref="Matrix4x4"/>.</param>
/// <returns>The inverted <see cref="Matrix4x4"/> of the given <see cref="Matrix4x4"/>.</returns>
public static Matrix4x4 Invert(Matrix4x4 m)
{
float m1 = m.M11, m2 = m.M12, m3 = m.M13, m4 = m.M14;
float m5 = m.M21, m6 = m.M22, m7 = m.M23, m8 = m.M24;
float m9 = m.M31, m10 = m.M32, m11 = m.M33, m12 = m.M34;
float m13 = m.M41, m14 = m.M42, m15 = m.M43, m16 = m.M44;
float num = m11 * m16 - m12 * m15;
float num2 = m10 * m16 - m12 * m14;
float num3 = m10 * m15 - m11 * m14;
float num4 = m9 * m16 - m12 * m13;
float num5 = m9 * m15 - m11 * m13;
float num6 = m9 * m14 - m10 * m13;
float num7 = m6 * num - m7 * num2 + m8 * num3;
float num8 = -(m5 * num - m7 * num4 + m8 * num5);
float num9 = m5 * num2 - m6 * num4 + m8 * num6;
float num10 = -(m5 * num3 - m6 * num5 + m7 * num6);
float invDet = 1f / (m1 * num7 + m2 * num8 + m3 * num9 + m4 * num10);
float r11 = num7 * invDet;
float r21 = num8 * invDet;
float r31 = num9 * invDet;
float r41 = num10 * invDet;
float r12 = (-(m2 * num - m3 * num2 + m4 * num3)) * invDet;
float r22 = (m1 * num - m3 * num4 + m4 * num5) * invDet;
float r32 = (-(m1 * num2 - m2 * num4 + m4 * num6)) * invDet;
float r42 = (m1 * num3 - m2 * num5 + m3 * num6) * invDet;
float num12 = m7 * m16 - m8 * m15;
float num13 = m6 * m16 - m8 * m14;
float num14 = m6 * m15 - m7 * m14;
float num15 = m5 * m16 - m8 * m13;
float num16 = m5 * m15 - m7 * m13;
float num17 = m5 * m14 - m6 * m13;
float r13 = (m2 * num12 - m3 * num13 + m4 * num14) * invDet;
float r23 = (-(m1 * num12 - m3 * num15 + m4 * num16)) * invDet;
float r33 = (m1 * num13 - m2 * num15 + m4 * num17) * invDet;
float r43 = (-(m1 * num14 - m2 * num16 + m3 * num17)) * invDet;
float num18 = m7 * m12 - m8 * m11;
float num19 = m6 * m12 - m8 * m10;
float num20 = m6 * m11 - m7 * m10;
float num21 = m5 * m12 - m8 * m9;
float num22 = m5 * m11 - m7 * m9;
float num23 = m5 * m10 - m6 * m9;
float r14 = (-(m2 * num18 - m3 * num19 + m4 * num20)) * invDet;
float r24 = (m1 * num18 - m3 * num21 + m4 * num22) * invDet;
float r34 = (-(m1 * num19 - m2 * num21 + m4 * num23)) * invDet;
float r44 = (m1 * num20 - m2 * num22 + m3 * num23) * invDet;
return new(
r11, r12, r13, r14,
r21, r22, r23, r24,
r31, r32, r33, r34,
r41, r42, r43, r44
);
}
/// <summary>
/// Transposes the given <see cref="Matrix4x4"/>.
/// </summary>
/// <param name="m">The <see cref="Matrix4x4"/>.</param>
/// <returns>The transposed <see cref="Matrix4x4"/> of the given <see cref="Matrix4x4"/>.</returns>
public static Matrix4x4 Transpose(Matrix4x4 m) => new(
m.M11, m.M21, m.M31, m.M41,
m.M12, m.M22, m.M32, m.M42,
m.M13, m.M23, m.M33, m.M43,
m.M14, m.M24, m.M34, m.M44
);
public static Matrix4x4 CreateTranslation(Vector3D position) => new(
1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
position.X, position.Y, position.Z, 1
1f, 0f, 0f, position.X,
0f, 1f, 0f, position.Y,
0f, 0f, 1f, position.Z,
0f, 0f, 0f, 1f
);
public static Matrix4x4 CreateScale(float scale) => new(
scale, 0f, 0f, 0f,
0f, scale, 0f, 0f,
0f, 0f, scale, 0f,
0f, 0f, 0f, 1f
);
public static Matrix4x4 CreateScale(Vector3D scale) => new(
@@ -148,10 +251,10 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
return new(
1f, 0f, 0f, 0f,
0f, c, s, 0f,
0f, -s, c, 0f,
0f, c, -s, 0f,
0f, s, c, 0f,
0f, 0f, 0f, 1f
);
}
@@ -161,10 +264,10 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
c, 0f, -s, 0f,
return new(
c, 0f, s, 0f,
0f, 1f, 0f, 0f,
s, 0f, c, 0f,
-s, 0f, c, 0f,
0f, 0f, 0f, 1f
);
}
@@ -174,9 +277,9 @@ public readonly struct Matrix4x4(
float c = Math.Cos(radians);
float s = Math.Sin(radians);
return new Matrix4x4(
c, s, 0f, 0f,
-s, c, 0f, 0f,
return new(
c, -s, 0f, 0f,
s, c, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f
);
@@ -195,7 +298,7 @@ public readonly struct Matrix4x4(
Vector3D x = up.Cross(z).Normalized;
Vector3D y = z.Cross(x);
return new Matrix4x4(
return new(
x.X, y.X, z.X, 0f,
x.Y, y.Y, z.Y, 0f,
x.Z, y.Z, z.Z, 0f,
@@ -205,28 +308,53 @@ public readonly struct Matrix4x4(
public static Matrix4x4 CreateLookMatrix(Vector3D position, Vector3D target, Vector3D up)
{
Vector3D z = position.FromTo(target).Normalized;
Vector3D x = up.Cross(z).Normalized;
Vector3D y = z.Cross(x);
Vector3D f = (target - position).Normalized;
Vector3D s = f.Cross(up).Normalized;
Vector3D u = s.Cross(f);
return new Matrix4x4(
x.X, y.X, z.X, 0f,
x.Y, y.Y, z.Y, 0f,
x.Z, y.Z, z.Z, 0f,
-x.Dot(position), -y.Dot(position), -z.Dot(position), 1f
return new(
s.X, u.X, -f.X, 0f,
s.Y, u.Y, -f.Y, 0f,
s.Z, u.Z, -f.Z, 0f,
-s.Dot(position), -u.Dot(position), f.Dot(position), 1f
);
}
public static Matrix4x4 CreatePerspectiveFieldOfView(float fieldOfViewInRadians, float aspectRatio, float nearPlane, float farPlane)
public static Matrix4x4 CreateOrthographicView(float width, float height, float nearPlane = -1f, float farPlane = 1f)
{
float yScale = 1f / Math.Tan(fieldOfViewInRadians / 2f);
float xScale = yScale / aspectRatio;
float invDepth = 1f / (farPlane - nearPlane);
return new Matrix4x4(
return new(
2f / width, 0f, 0f, 0f,
0f, -2f / height, 0f, 0f,
0f, 0f, 1f * invDepth, 0f,
-1f, 1f, -nearPlane * invDepth, 1f
);
}
public static Matrix4x4 CreateOrthographicViewCentered(float width, float height, float nearPlane = -1f, float farPlane = 1f)
{
float invDepth = 1f / (farPlane - nearPlane);
return new(
2f / width, 0f, 0f, 0f,
0f, 2f / height, 0f, 0f,
0f, 0f, 1f * invDepth, 0f,
0f, 0f, -nearPlane * invDepth, 1f
);
}
public static Matrix4x4 CreatePerspectiveFieldOfView(float fovRadians, float aspectRatio, float nearPlane, float farPlane)
{
float yScale = 1f / Math.Tan(fovRadians / 2f);
float xScale = yScale / aspectRatio;
float zRange = farPlane - nearPlane;
return new(
xScale, 0f, 0f, 0f,
0f, yScale, 0f, 0f,
0f, 0f, farPlane / (farPlane - nearPlane), 1f,
0f, 0f, -nearPlane * farPlane / (farPlane - nearPlane), 0f
0f, 0f, -(farPlane + nearPlane) / zRange, -1f,
0f, 0f, -(2f * nearPlane * farPlane) / zRange, 0f
);
}
@@ -261,11 +389,20 @@ public static class Matrix4x4Extensions
/// <inheritdoc cref="Matrix4x4.Determinant(Matrix4x4)" />
public static float Determinant(this Matrix4x4 matrix) => Matrix4x4.Determinant(matrix);
/// <inheritdoc cref="Matrix4x4.Invert(Matrix4x4)" />
public static Matrix4x4 Invert(this Matrix4x4 matrix) => Matrix4x4.Invert(matrix);
/// <inheritdoc cref="Matrix4x4.Transpose(Matrix4x4)" />
public static Matrix4x4 Transpose(this Matrix4x4 matrix) => Matrix4x4.Transpose(matrix);
/// <inheritdoc cref="Matrix4x4.CreateTranslation(Vector3D)" />
public static Matrix4x4 ApplyTranslation(this Matrix4x4 matrix, Vector3D translation) => matrix * Matrix4x4.CreateTranslation(translation);
/// <inheritdoc cref="Matrix4x4.CreateScale(Vector3D)" />
public static Matrix4x4 ApplyScale(this Matrix4x4 matrix, Vector3 scale) => matrix * Matrix4x4.CreateScale(scale);
public static Matrix4x4 ApplyScale(this Matrix4x4 matrix, Vector3D scale) => matrix * Matrix4x4.CreateScale(scale);
/// <inheritdoc cref="Matrix4x4.CreateScale(float)" />
public static Matrix4x4 ApplyScale(this Matrix4x4 matrix, float scale) => matrix * Matrix4x4.CreateScale(scale);
/// <inheritdoc cref="Matrix4x4.CreateRotationZ(float)" />
public static Matrix4x4 ApplyRotationX(this Matrix4x4 matrix, float radians) => matrix * Matrix4x4.CreateRotationX(radians);
@@ -280,15 +417,28 @@ public static class Matrix4x4Extensions
public static Matrix4x4 ApplyRotation(this Matrix4x4 matrix, Quaternion quaternion) => matrix * Matrix4x4.CreateRotation(quaternion);
/// <inheritdoc cref="Matrix4x4.CreateLookMatrix( Vector3D, Vector3D)" />
public static Matrix4x4 ApplyLookRotationTo(this Matrix4x4 matrix, Vector3D forward, Vector3 up) => matrix * Matrix4x4.CreateLookMatrix(forward, up);
public static Matrix4x4 ApplyLookRotationTo(this Matrix4x4 matrix, Vector3D forward, Vector3D up) => matrix * Matrix4x4.CreateLookMatrix(forward, up);
/// <inheritdoc cref="Matrix4x4.CreateLookMatrix(Vector3D, Vector3D, Vector3D)" />
public static Matrix4x4 CreateLookMatrixTo(this Vector3D from, Vector3D to, Vector3 up) => Matrix4x4.CreateLookMatrix(from, to, up);
public static Matrix4x4 CreateLookMatrixTo(this Vector3D from, Vector3D to, Vector3D up) => Matrix4x4.CreateLookMatrix(from, to, up);
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveFieldOfView(float, float, float, float)" />
public static Matrix4x4 ApplyPerspectiveFieldOfView(this Matrix4x4 matrix, float fieldOfViewInRadians, float aspectRatio, float nearPlane, float farPlane)
=> matrix * Matrix4x4.CreatePerspectiveFieldOfView(fieldOfViewInRadians, aspectRatio, nearPlane, farPlane);
/// <inheritdoc cref="Matrix4x4.CreateOrthographicView(float, float, float, float)" />
public static Matrix4x4 ApplyOrthographicView(this Matrix4x4 matrix, float width, float height, float nearPlane = -1f, float farPlane = 1f)
=> matrix * Matrix4x4.CreateOrthographicView(width, height, nearPlane, farPlane);
/// <inheritdoc cref="Matrix4x4.CreateOrthographicViewCentered(float, float, float, float)" />
public static Matrix4x4 ApplyOrthographicViewCentered(this Matrix4x4 matrix, float width, float height, float nearPlane = -1f, float farPlane = 1f)
=> matrix * Matrix4x4.CreateOrthographicViewCentered(width, height, nearPlane, farPlane);
/// <inheritdoc cref="Matrix4x4.ToRightHanded(Matrix4x4) />
public static Matrix4x4 ToRightHanded(this Matrix4x4 matrix) => Matrix4x4.ToRightHanded(matrix);
/// <summary>
/// Multiplies two <see cref="Matrix4x4"/>'s.
/// </summary>
public static Matrix4x4 ApplyMatrix(this Matrix4x4 left, Matrix4x4 right) => left * right;
}

View File

@@ -22,6 +22,31 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
private readonly List<Vector2D> _vertices = vertices;
/// <summary>
/// Gets the area of the <see cref="Shape2D"/>.
/// </summary>
public float Area
{
get
{
float area = 0f;
for (int i = 0; i < _vertices.Count; i++)
{
Vector2D a = _vertices[i];
Vector2D b = _vertices[(i + 1) % _vertices.Count];
area += a.Cross(b);
}
return area.Abs() * .5f;
}
}
/// <summary>
/// Gets the geometric interia of the <see cref="Shape2D"/>.
/// </summary>
public float GeometricInertia => GetGeometricInertia(this, Vector2D.Zero);
/// <summary>
/// Gets the vertices of the <see cref="Shape2D"/>.
/// </summary>
@@ -112,6 +137,40 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
return new Triangle(p1, p2, p3);
}
/// <summary>
/// Gets the geometric interia of the <see cref="Shape2D"/>.
/// </summary>
/// <param name="shape">The shape to get the geometrical interia of.</param>
/// <param name="centerOfMass">The point in space to calculate the geometrical interia from.</param>
/// <returns>The geometrical interia of the <see cref="Shape2D"/>.</returns>
public static float GetGeometricInertia(Shape2D shape, Vector2D centerOfMass)
{
float geometricInertia = 0f;
for (int i = 0; i < shape._vertices.Count; i++)
{
Vector2D p1 = centerOfMass.FromTo(shape._vertices[i]);
Vector2D p2 = centerOfMass.FromTo(shape._vertices[(i + 1) % shape._vertices.Count]);
float cross = p1.Cross(p2);
float dot = p1.Dot(p1) + p1.Dot(p2) + p2.Dot(p2);
geometricInertia += cross * dot;
}
return geometricInertia.Abs() / 12f;
}
/// <summary>
/// Gets the interia of the <see cref="Shape2D"/>.
/// </summary>
/// <param name="shape">The shape to get the interia of.</param>
/// <param name="centerOfMass">The point in space to calculate the geometrical interia from.</param>
/// <param name="mass">Mass of the shape.</param>
/// <returns>The interia of the <see cref="Shape2D"/>.</returns>
public static float GetInertia(Shape2D shape, Vector2D centerOfMass, float mass)
=> GetGeometricInertia(shape, centerOfMass) * mass;
/// <summary>
/// Triangulates the given convex <see cref="Shape2D"/>.
/// </summary>
@@ -297,6 +356,12 @@ public static class Shape2DExtensions
/// <inheritdoc cref="Shape2D.GetSuperTriangle(Shape2D)" />
public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape);
/// <inheritdoc cref="Shape2D.GetGeometricInertia(Shape2D, Vector2D)" />
public static float GetGeometricInertia(this Shape2D shape, Vector2D centerOfMass) => Shape2D.GetGeometricInertia(shape, centerOfMass);
/// <inheritdoc cref="Shape2D.GetInertia(Shape2D, Vector2D, float)" />
public static float GetInertia(this Shape2D shape, Vector2D centerOfMass, float mass) => Shape2D.GetInertia(shape, centerOfMass, mass);
/// <inheritdoc cref="Shape2D.TriangulateConvex(Shape2D, IList{Triangle})" />
public static void ToTrianglesConvex(this Shape2D shape, IList<Triangle> triangles) => Shape2D.TriangulateConvex(shape, triangles);

View File

@@ -82,11 +82,19 @@ public readonly struct Vector2D(float x, float y) : IEquatable<Vector2D>
public static bool operator ==(Vector2D left, Vector2D right) => left.X == right.X && left.Y == right.Y;
public static bool operator !=(Vector2D left, Vector2D right) => left.X != right.X || left.Y != right.Y;
public static implicit operator System.Numerics.Vector2(Vector2D vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(System.Numerics.Vector2 vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(Vector2DInt vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(Vector3D vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(Vector3DInt vector) => new(vector.X, vector.Y);
public static implicit operator System.Numerics.Vector2(Vector2D vector) => new(vector.X, vector.Y);
public static implicit operator System.Numerics.Vector3(Vector2D vector) => new(vector.X, vector.Y, 0f);
public static implicit operator System.Numerics.Vector4(Vector2D vector) => new(vector.X, vector.Y, 0f, 0f);
public static implicit operator Vector2D(System.Numerics.Vector2 vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(System.Numerics.Vector3 vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(System.Numerics.Vector4 vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(Vector3D vector) => new(vector.X, vector.Y);
public static implicit operator Vector2D(Vector4D vector) => new(vector.X, vector.Y);
/// <summary>
/// Calculates the length of the <see cref="Vector2D"/>.

View File

@@ -74,9 +74,12 @@ public readonly struct Vector2DInt(int x, int y) : IEquatable<Vector2DInt>
public static bool operator ==(Vector2DInt left, Vector2DInt right) => left.X == right.X && left.Y == right.Y;
public static bool operator !=(Vector2DInt left, Vector2DInt right) => left.X != right.X || left.Y != right.Y;
public static implicit operator Vector2DInt(Vector2D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt());
public static implicit operator Vector2DInt(Vector3DInt vector) => new(vector.X, vector.Y);
public static implicit operator Vector2DInt(Vector2D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt());
public static implicit operator Vector2DInt(Vector3D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt());
public static implicit operator Vector2DInt(Vector4D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt());
/// <summary>
/// Calculates the length of the <see cref="Vector2DInt"/>.
/// </summary>

View File

@@ -92,11 +92,19 @@ public readonly struct Vector3D(float x, float y, float z) : IEquatable<Vector3D
public static bool operator ==(Vector3D left, Vector3D right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator !=(Vector3D left, Vector3D right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public static implicit operator System.Numerics.Vector3(Vector3D vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator Vector3D(System.Numerics.Vector3 vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator Vector3D(Vector2DInt vector) => new(vector.X, vector.Y, 0f);
public static implicit operator Vector3D(Vector3DInt vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator Vector3D(Vector2D vector) => new(vector.X, vector.Y, 0f);
public static implicit operator System.Numerics.Vector2(Vector3D vector) => new(vector.X, vector.Y);
public static implicit operator System.Numerics.Vector3(Vector3D vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator System.Numerics.Vector4(Vector3D vector) => new(vector.X, vector.Y, vector.Z, 0f);
public static implicit operator Vector3D(System.Numerics.Vector2 vector) => new(vector.X, vector.Y, 0f);
public static implicit operator Vector3D(System.Numerics.Vector3 vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator Vector3D(System.Numerics.Vector4 vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator Vector3D(Vector2D vector) => new(vector.X, vector.Y, 0f);
public static implicit operator Vector3D(Vector4D vector) => new(vector.X, vector.Y, vector.Z);
/// <summary>
/// Calculates the length of the <see cref="Vector3D"/>.

View File

@@ -84,9 +84,12 @@ public readonly struct Vector3DInt(int x, int y, int z) : IEquatable<Vector3DInt
public static bool operator ==(Vector3DInt left, Vector3DInt right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
public static bool operator !=(Vector3DInt left, Vector3DInt right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
public static implicit operator Vector3DInt(Vector3D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt(), vector.Z.RoundToInt());
public static implicit operator Vector3DInt(Vector2DInt vector) => new(vector.X, vector.Y, 0);
public static implicit operator Vector3DInt(Vector2D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt(), 0);
public static implicit operator Vector3DInt(Vector3D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt(), vector.Z.RoundToInt());
public static implicit operator Vector3DInt(Vector4D vector) => new(vector.X.RoundToInt(), vector.Y.RoundToInt(), vector.Z.RoundToInt());
/// <summary>
/// Calculates the length of the <see cref="Vector3DInt"/>.
/// </summary>

View File

@@ -90,9 +90,20 @@ public readonly struct Vector4D(float x, float y, float z, float w) : IEquatable
public static bool operator ==(Vector4D left, Vector4D right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z && left.W == right.W;
public static bool operator !=(Vector4D left, Vector4D right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z || left.W != right.W;
public static implicit operator Vector4D(Vector2DInt vector) => new(vector.X, vector.Y, 0f, 0f);
public static implicit operator Vector4D(Vector3DInt vector) => new(vector.X, vector.Y, vector.Z, 0f);
public static implicit operator System.Numerics.Vector2(Vector4D vector) => new(vector.X, vector.Y);
public static implicit operator System.Numerics.Vector3(Vector4D vector) => new(vector.X, vector.Y, vector.Z);
public static implicit operator System.Numerics.Vector4(Vector4D vector) => new(vector.X, vector.Y, vector.Z, vector.W);
public static implicit operator Vector4D(System.Numerics.Vector2 vector) => new(vector.X, vector.Y, 0f, 0f);
public static implicit operator Vector4D(System.Numerics.Vector3 vector) => new(vector.X, vector.Y, vector.Z, 0f);
public static implicit operator Vector4D(System.Numerics.Vector4 vector) => new(vector.X, vector.Y, vector.Z, vector.W);
public static implicit operator Vector4D(Vector2D vector) => new(vector.X, vector.Y, 0f, 0f);
public static implicit operator Vector4D(Vector3D vector) => new(vector.X, vector.Y, vector.Z, 0f);
/// <summary>
/// Calculates the length of the <see cref="Vector4D"/>.
/// </summary>

View File

@@ -4,15 +4,15 @@ namespace Engine.Core.Serialization;
public interface ISerializer
{
object Deserialize(string configuration);
object Deserialize(string configuration, Type type);
T Deserialize<T>(string configuration);
object Deserialize(string content);
object Deserialize(string content, Type type);
T Deserialize<T>(string content);
string Serialize(object instance);
ProgressiveTask<object> DeserializeAsync(string configuration);
ProgressiveTask<object> DeserializeAsync(string configuration, Type type);
ProgressiveTask<T> DeserializeAsync<T>(string configuration);
ProgressiveTask<object> DeserializeAsync(string content);
ProgressiveTask<object> DeserializeAsync(string content, Type type);
ProgressiveTask<T> DeserializeAsync<T>(string content);
ProgressiveTask<string> SerializeAsync(object instance);
}

View File

@@ -0,0 +1,9 @@
using System.Collections;
namespace Engine.Core;
public interface ICoroutineManager
{
IEnumerator StartCoroutine(IEnumerator enumerator);
void StopCoroutine(IEnumerator enumerator);
}

View File

@@ -1,6 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using Engine.Core.Debug;
namespace Engine.Core;
public class CoroutineManager : Behaviour, IUpdate
@@ -21,6 +23,8 @@ public class CoroutineManager : Behaviour, IUpdate
void IUpdate.Update()
{
for (int i = enumerators.Count - 1; i >= 0; i--)
{
try
{
if (enumerators[i].Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
continue;
@@ -28,6 +32,14 @@ public class CoroutineManager : Behaviour, IUpdate
if (!enumerators[i].MoveNext())
enumerators.RemoveAt(i);
}
catch (System.Exception exception)
{
ILogger.Shared.LogError(this, $"Coroutine failed, removing from execution.");
ILogger.Shared.LogException(this, exception);
ILogger.Shared.LogTrace(exception.StackTrace);
enumerators.RemoveAt(i);
}
}
}
public CoroutineManager() => Priority = int.MinValue;

View File

@@ -0,0 +1,92 @@
using System.Collections;
using System.Collections.Generic;
using Engine.Core.Debug;
namespace Engine.Core;
public class NestedCoroutineManager : Behaviour, IUpdate, ICoroutineManager
{
private readonly List<CoroutineStack> stacks = [];
private readonly Pool<CoroutineStack> pool = new(() => new());
public IEnumerator StartCoroutine(IEnumerator enumerator)
{
CoroutineStack stack = pool.Get();
stack.EntryPoint = enumerator;
stack.Stack.Push(enumerator);
stacks.Add(stack);
return enumerator;
}
public void StopCoroutine(IEnumerator enumerator)
{
for (int i = 0; i < stacks.Count; i++)
if (stacks[i].EntryPoint == enumerator)
{
RemoveCoroutineAt(i);
return;
}
}
private void RemoveCoroutineAt(int i)
{
stacks[i].Reset();
stacks.RemoveAt(i);
}
void IUpdate.Update()
{
for (int i = stacks.Count - 1; i >= 0; i--)
{
Stack<IEnumerator> stack = stacks[i].Stack;
if (stack.Count == 0)
{
RemoveCoroutineAt(i);
continue;
}
try
{
IEnumerator top = stack.Peek();
if (top.Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
continue;
if (top.Current is IEnumerator nested)
{
stack.Push(nested);
continue;
}
if (!top.MoveNext())
{
stack.Pop();
if (stack.Count != 0)
stack.Peek().MoveNext();
continue;
}
}
catch (System.Exception exception)
{
ILogger.Shared.LogError(this, $"Coroutine failed, removing from execution.");
ILogger.Shared.LogException(this, exception);
ILogger.Shared.LogTrace(exception.StackTrace);
RemoveCoroutineAt(i);
}
}
}
private class CoroutineStack
{
public IEnumerator EntryPoint = null!;
public Stack<IEnumerator> Stack = new();
public void Reset() { EntryPoint = null!; Stack.Clear(); }
}
public NestedCoroutineManager() => Priority = int.MinValue;
}

View File

@@ -9,65 +9,56 @@ public class UniverseEntranceManager : Internal.BehaviourIndependent
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private readonly ActiveBehaviourCollectorOrdered<int, IEnterUniverse> enterUniverses = new(GetPriority(), SortByAscendingPriority());
private readonly ActiveBehaviourCollectorOrdered<int, IExitUniverse> exitUniverses = new(GetPriority(), SortByAscendingPriority());
private readonly List<IEnterUniverse> toCallEnterUniverses = new(32);
private readonly List<IExitUniverse> toCallExitUniverses = new(32);
private bool isInitialCollectionDone = false;
private readonly FastListOrdered<int, IEnterUniverse> toCallEnterUniverses = new(GetPriority(), SortByAscendingPriority());
protected override void OnEnteredUniverse(IUniverse universe)
{
// FIXME: This causes an issue when the UniverseEntranceManager is already attached to a UniverseObject then registered into a Universe,
// the enter/exit universe collectors call OnUniverseObjectRegistered internally on Assign, but since the Universe calls the OnUniverseObjectRegistered
// event it tries to call OnUniverseObjectRegistered again on the same object, causing a duplicate entry error.
Debug.Assert.AssertTrue(BehaviourController.Count == 1, $"{nameof(UniverseEntranceManager)} must be in it's own {nameof(IUniverseObject)} with no other {nameof(IBehaviour)}s attached at the moment. Failing to do so might cause instantiation or serialization issues.");
exitUniverses.Assign(universe);
// FIXME: the isInitialCollectionDone is for the sole reason of some behaviours
// adding more behaviours during entrance calls and the internal workings of
// behaviour collector not being able to tell which behaviour was already called
// (because it just runs a for loop with the behaviour count, and priority ordering doesn't help as well)
// so it sometimes double processes or misses behaviours. A more elegant way of
// handling this would be nice but for now it works good enough.
//
// SIDE NOTE: This same issue has the potential to occur on exitUniverses as well, but I've yet to run
// into an instance of it actually happening so... I'm not gonna touch it until the edge case happens.
isInitialCollectionDone = false;
enterUniverses.Assign(universe);
isInitialCollectionDone = true;
foreach (IUniverseObject universeObject in universe)
OnUniverseObjectRegistered(universe, new(universeObject));
universe.OnUniverseObjectRegistered.AddListener(OnUniverseObjectRegistered);
universe.OnUniverseObjectUnRegistered.AddListener(OnUniverseObjectUnRegistered);
for (int i = toCallEnterUniverses.Count - 1; i >= 0; i--)
toCallEnterUniverses[i].EnterUniverse(universe);
toCallEnterUniverses.Clear();
}
protected override void OnExitedUniverse(IUniverse universe)
{
enterUniverses.Unassign();
foreach (IUniverseObject universeObject in universe)
OnUniverseObjectUnRegistered(universe, new(universeObject));
universe.OnUniverseObjectRegistered.RemoveListener(OnUniverseObjectRegistered);
universe.OnUniverseObjectUnRegistered.RemoveListener(OnUniverseObjectUnRegistered);
}
private void OnUniverseObjectUnRegistered(IUniverse sender, IUniverse.UniverseObjectUnRegisteredArguments args)
{
args.UniverseObjectUnregistered.BehaviourController.GetBehavioursInChildren(toCallExitUniverses);
for (int i = toCallExitUniverses.Count - 1; i >= 0; i--)
{
IExitUniverse exitUniverse = toCallExitUniverses[i];
toCallExitUniverses.RemoveAt(i);
exitUniverse.ExitUniverse(Universe);
}
}
private void OnUniverseObjectRegistered(IUniverse sender, IUniverse.UniverseObjectRegisteredArguments args)
{
for (int i = toCallEnterUniverses.Count - 1; i >= 0; i--)
{
IEnterUniverse enterUniverse = toCallEnterUniverses[i];
toCallEnterUniverses.RemoveAt(i);
enterUniverse.EnterUniverse(Universe);
}
exitUniverses.Unassign();
}
private void OnEnterUniverseCollected(IBehaviourCollector<IEnterUniverse> sender, IBehaviourCollector<IEnterUniverse>.BehaviourCollectedArguments args)
{
if (!isInitialCollectionDone)
{
toCallEnterUniverses.Add(args.BehaviourCollected);
return;
}
args.BehaviourCollected.EnterUniverse(Universe);
}
private void OnExitUniverseRemoved(IBehaviourCollector<IExitUniverse> sender, IBehaviourCollector<IExitUniverse>.BehaviourRemovedArguments args)
=> args.BehaviourRemoved.ExitUniverse(Universe);
public UniverseEntranceManager()
{
enterUniverses.OnCollected.AddListener(OnEnterUniverseCollected);
exitUniverses.OnRemoved.AddListener(OnExitUniverseRemoved);
}
}

View File

@@ -44,6 +44,17 @@ public class UpdateManager : Behaviour, IEnterUniverse, IExitUniverse
universe.OnPostUpdate.RemoveListener(OnPostUpdate);
}
/// <summary>
/// Call the <see cref="IFirstFrameUpdate"/> early if it's in queue to be called by this the <see cref="UpdateManager"/>.
/// It will not be called in the next natural cycle.
/// </summary>
/// <param name="instance">The instance that will be called now rather than later.</param>
public void CallFirstActiveFrameImmediately(IFirstFrameUpdate instance)
{
if (toCallFirstFrameUpdates.Remove(instance))
instance.FirstActiveFrame();
}
private void OnFirstUpdate(IUniverse sender, IUniverse.UpdateArguments args)
{
for (int i = toCallFirstFrameUpdates.Count - 1; i >= 0; i--)

View File

@@ -0,0 +1,35 @@
using System.Threading.Tasks;
using static Engine.Core.WaitForTaskYield;
namespace Engine.Core;
public class WaitForTaskYield(Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Any) : ICoroutineYield
{
public bool Yield()
{
switch (completionStatus)
{
case TaskCompletionStatus.Success:
if (task.IsCanceled)
throw new("Task has been canceled.");
if (task.IsFaulted)
throw task.Exception ?? new("Task has faulted.");
return !task.IsCompletedSuccessfully;
case TaskCompletionStatus.Fail:
if (task.IsCompletedSuccessfully)
throw new("Task was completed successfully.");
return !task.IsFaulted;
}
return !task.IsCompleted;
}
public enum TaskCompletionStatus
{
Any,
Success,
Fail
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace Engine.Core;
public class WaitForTimeYield(float seconds = 0f, float milliseconds = 0f, float minutes = 0f, float hours = 0f) : ICoroutineYield
{
private readonly DateTime triggerTime = DateTime.UtcNow
.AddHours(hours)
.AddMinutes(minutes)
.AddSeconds(seconds)
.AddMilliseconds(milliseconds);
public bool Yield() => DateTime.UtcNow < triggerTime;
}

View File

@@ -0,0 +1,10 @@
using System;
namespace Engine.Core;
public class WaitUntilYield(Func<bool> condition) : ICoroutineYield
{
private readonly Func<bool> condition = condition;
public bool Yield() => !condition.Invoke();
}

View File

@@ -2,7 +2,7 @@ using System;
namespace Engine.Core;
public class CoroutineYield(Func<bool> condition) : ICoroutineYield
public class WaitWhileYield(Func<bool> condition) : ICoroutineYield
{
private readonly Func<bool> condition = condition;

View File

@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using static Engine.Core.WaitForTaskYield;
namespace Engine.Core;
public static class YieldExtensions
{
public static WaitForTaskYield ToYield(this Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Any) => new(task, completionStatus);
}

View File

@@ -178,6 +178,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
netPacketProcessor.RegisterNestedType(Vector3DNetPacker.Write, Vector3DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Vector3DIntNetPacker.Write, Vector3DIntNetPacker.Read);
netPacketProcessor.RegisterNestedType(Vector4DNetPacker.Write, Vector4DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Matrix4x4NetPacker.Write, Matrix4x4NetPacker.Read);
}
public INetworkCommunicator SubscribeToPackets<T>(Event<IConnection, T>.EventHandler callback)

View File

@@ -6,7 +6,7 @@ using Engine.Core.Debug;
namespace Engine.Systems.Network;
public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicatorServer
public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicatorServer, IPostUpdate
{
public string Password { get; private set; } = string.Empty;
public int MaxConnectionCount { get; private set; } = 2;
@@ -104,17 +104,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
}
}
private void PollEvents(IUniverse sender, IUniverse.UpdateArguments args) => Manager.PollEvents();
public override void EnterUniverse(IUniverse universe)
public void PostUpdate()
{
base.EnterUniverse(universe);
universe.OnPostUpdate.AddListener(PollEvents);
}
public override void ExitUniverse(IUniverse universe)
{
base.ExitUniverse(universe);
universe.OnPostUpdate.RemoveListener(PollEvents);
Manager.PollEvents();
}
}

View File

@@ -0,0 +1,61 @@
using LiteNetLib.Utils;
using Engine.Core;
namespace Engine.Systems.Network.Packers;
internal static class Matrix4x4NetPacker
{
internal static void Write(NetDataWriter writer, Matrix4x4 data)
{
writer.Put(data.M11);
writer.Put(data.M12);
writer.Put(data.M13);
writer.Put(data.M14);
writer.Put(data.M21);
writer.Put(data.M22);
writer.Put(data.M23);
writer.Put(data.M24);
writer.Put(data.M31);
writer.Put(data.M32);
writer.Put(data.M33);
writer.Put(data.M34);
writer.Put(data.M41);
writer.Put(data.M42);
writer.Put(data.M43);
writer.Put(data.M44);
}
internal static Matrix4x4 Read(NetDataReader reader)
{
float m11 = reader.GetFloat();
float m12 = reader.GetFloat();
float m13 = reader.GetFloat();
float m14 = reader.GetFloat();
float m21 = reader.GetFloat();
float m22 = reader.GetFloat();
float m23 = reader.GetFloat();
float m24 = reader.GetFloat();
float m31 = reader.GetFloat();
float m32 = reader.GetFloat();
float m33 = reader.GetFloat();
float m34 = reader.GetFloat();
float m41 = reader.GetFloat();
float m42 = reader.GetFloat();
float m43 = reader.GetFloat();
float m44 = reader.GetFloat();
return new Matrix4x4(
m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44
);
}
}

View File

@@ -1,12 +0,0 @@
using Microsoft.Xna.Framework;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public interface ITriangleBatch
{
void Begin(Matrix? view = null, Matrix? projection = null);
void Draw(Triangle triangle, ColorRGBA colorRGBA);
void End();
}

View File

@@ -1,5 +1,4 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core;
@@ -7,33 +6,41 @@ namespace Engine.Integration.MonoGame;
public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFrameUpdate, IPreDraw
{
public Event<MonoGameCamera2D> OnMatrixTransformChanged { get; } = new();
public Event<MonoGameCamera2D> OnViewMatrixChanged { get; } = new();
public Event<MonoGameCamera2D> OnProjectionMatrixChanged { get; } = new();
public Event<MonoGameCamera2D> OnViewportChanged { get; } = new();
public Event<MonoGameCamera2D> OnZoomChanged { get; } = new();
public GraphicsDeviceManager Graphics { get; private set; } = null!;
public ITransform2D Transform { get; private set; } = null!;
public Matrix MatrixTransform
public Matrix4x4 ProjectionMatrix
{
get;
set
private set
{
if (field == value)
return;
field = value;
OnMatrixTransformChanged.Invoke(this);
OnProjectionMatrixChanged.Invoke(this);
}
} = Matrix.Identity;
} = Matrix4x4.Identity;
public Vector2D Position
public Matrix4x4 ViewMatrix
{
get => Transform.Position;
set => Transform.Position = value;
}
get;
private set
{
if (field == value)
return;
public Viewport Viewport
field = value;
OnViewMatrixChanged.Invoke(this);
}
} = Matrix4x4.Identity;
public Vector2D Viewport
{
get;
set
@@ -61,39 +68,54 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
}
} = 1f;
public float Rotation
{
get => Transform.Rotation;
set => Transform.Rotation = value;
}
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
{
Vector2D worldPosition = Vector2.Transform(screenPosition.ToVector2(), Matrix.Invert(MatrixTransform)).ToVector2D();
return worldPosition.Scale(EngineConverterExtensions.screenScale);
float x = 2f * screenPosition.X / Viewport.X - 1f;
float y = 1f - 2f * screenPosition.Y / Viewport.Y;
Vector4D normalizedCoordinates = new(x, y, 0f, 1f);
Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse;
Vector4D worldPosition = invertedViewProjectionMatrix * normalizedCoordinates;
if (worldPosition.W != 0f)
worldPosition /= worldPosition.W;
return new(worldPosition.X, worldPosition.Y);
}
public Vector2D WorldToScreenPosition(Vector2D worldPosition)
{
Vector2D screenPosition = Vector2.Transform(worldPosition.ToVector2(), MatrixTransform).ToVector2D();
return screenPosition.Scale(EngineConverterExtensions.screenScale);
Vector4D worldPosition4D = new(worldPosition.X, worldPosition.Y, 0f, 1f);
Matrix4x4 viewProjection = ProjectionMatrix * ViewMatrix;
Vector4D clip = viewProjection * worldPosition4D;
if (clip.W != 0f)
clip /= clip.W;
float screenX = (clip.X + 1f) * .5f * Viewport.X;
float screenY = (1f - clip.Y) * .5f * Viewport.Y;
return new(screenX, screenY);
}
public void LastActiveFrame() => Transform = null!;
public void FirstActiveFrame()
{
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
Viewport = Graphics.GraphicsDevice.Viewport;
Viewport = new(Graphics.GraphicsDevice.Viewport.Width, Graphics.GraphicsDevice.Viewport.Height);
Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
}
public void PreDraw()
{
MatrixTransform =
Matrix.CreateTranslation(new Vector3(-Position.X, Position.Y, 0f)) *
Matrix.CreateRotationZ(Rotation * Math.DegreeToRadian) *
Matrix.CreateScale(Transform.Scale.X.Max(Transform.Scale.Y)) *
Matrix.CreateScale(Zoom) *
Matrix.CreateTranslation(new Vector3(Viewport.Width * .5f, Viewport.Height * .5f, 0f));
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.X, Viewport.Y);
ViewMatrix = Matrix4x4.Identity
.ApplyTranslation(new Vector3D(-Transform.Position.X, -Transform.Position.Y, 0f))
.ApplyRotationZ(Transform.Rotation * Math.DegreeToRadian)
.ApplyScale(Transform.Scale.X.Max(Transform.Scale.Y))
.ApplyScale(Zoom);
}
}

View File

@@ -21,7 +21,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
public GraphicsDeviceManager Graphics { get; private set; } = null!;
public ITransform3D Transform { get; private set; } = null!;
public Matrix View
public Matrix4x4 ViewMatrix
{
get;
set
@@ -29,13 +29,13 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
if (field == value)
return;
Matrix previousView = field;
Matrix4x4 previousView = field;
field = value;
OnViewChanged.Invoke(this, new(previousView));
}
} = Matrix.Identity;
} = Matrix4x4.Identity;
public Matrix Projection
public Matrix4x4 ProjectionMatrix
{
get;
set
@@ -43,13 +43,13 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
if (field == value)
return;
Matrix previousProjection = field;
Matrix4x4 previousProjection = field;
field = value;
OnProjectionChanged.Invoke(this, new(previousProjection));
}
} = Matrix.Identity;
} = Matrix4x4.Identity;
public Viewport Viewport
public Vector2D Viewport
{
get;
set
@@ -57,7 +57,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
if (field.Equals(value))
return;
Viewport previousViewport = field;
Vector2D previousViewport = field;
field = value;
SetForRecalculation();
OnViewportChanged.Invoke(this, new(previousViewport));
@@ -111,21 +111,24 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
Vector3 nearPoint = new(screenPosition.X, screenPosition.Y, 0f);
Vector3 farPoint = new(screenPosition.X, screenPosition.Y, 1f);
Vector3 worldNear = Viewport.Unproject(nearPoint, Projection, View, Matrix.Identity);
Vector3 worldFar = Viewport.Unproject(farPoint, Projection, View, Matrix.Identity);
Matrix projection = ProjectionMatrix.ToXnaMatrix();
Matrix view = ViewMatrix.ToXnaMatrix();
Vector3 worldNear = Graphics.GraphicsDevice.Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
Vector3 worldFar = Graphics.GraphicsDevice.Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
Vector3 direction = Vector3.Normalize(worldFar - worldNear);
return new(worldNear.ToVector3D(), direction.ToVector3D());
}
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Viewport.Project(worldPosition.ToVector3(), Projection, View, Matrix.Identity).ToVector3D();
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Graphics.GraphicsDevice.Viewport.Project(worldPosition.ToVector3(), ProjectionMatrix.ToXnaMatrix(), ViewMatrix.ToXnaMatrix(), Matrix.Identity).ToVector3D();
public void LastActiveFrame() => Transform.OnTransformUpdated.RemoveListener(SetDirtyOnTransformUpdate);
public void FirstActiveFrame()
{
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
Viewport = Graphics.GraphicsDevice.Viewport;
Viewport = new(Graphics.GraphicsDevice.Viewport.Width, Graphics.GraphicsDevice.Viewport.Height);
Transform.OnTransformUpdated.AddListener(SetDirtyOnTransformUpdate);
}
@@ -150,7 +153,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
Vector3 right = Vector3.Normalize(Transform.Right.ToVector3());
Vector3 position = Transform.Position.ToVector3();
View = new Matrix(
ViewMatrix = new Matrix4x4(
right.X, up.X, forward.X, 0,
right.Y, up.Y, forward.Y, 0,
right.Z, up.Z, forward.Z, 0,
@@ -164,9 +167,9 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
private void CalculateProjection()
{
float yScale = 1f / (float)Math.Tan(fieldOfView / 2f);
float xScale = yScale / Viewport.AspectRatio;
float xScale = yScale / (Viewport.X / Viewport.Y);
Projection = new Matrix(
ProjectionMatrix = new Matrix4x4(
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, FarPlane / (FarPlane - NearPlane), 1,
@@ -174,7 +177,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
);
}
public readonly record struct ViewChangedArguments(Matrix PreviousView);
public readonly record struct ProjectionChangedArguments(Matrix PreviousProjection);
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
public readonly record struct ViewportChangedArguments(Vector2D PreviousViewport);
}

View File

@@ -15,49 +15,49 @@ public class SpriteBatchWrapper(GraphicsDevice graphicsDevice) : ISpriteBatch
=> spriteBatch.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
=> spriteBatch.Draw(texture, position.ToVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToVector2(), scale.ToVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
=> spriteBatch.Draw(texture, position.ToVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToVector2(), scale, effects, layerDepth);
public void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), effects, layerDepth);
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color, rotation, origin.ToVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color);
=> spriteBatch.Draw(texture, position.ToVector2(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, Vector2D position, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), color);
=> spriteBatch.Draw(texture, position.ToVector2(), color);
public void Draw(Texture2D texture, AABB2D destinationAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), color);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale, effects, layerDepth);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale.ToVector2(), effects, layerDepth);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth, rtl);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale.ToVector2(), effects, layerDepth, rtl);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale, effects, layerDepth);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale.ToVector2(), effects, layerDepth);
public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth, bool rtl)
=> spriteBatch.DrawString(spriteFont, text, position.ToDisplayVector2(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth, rtl);
=> spriteBatch.DrawString(spriteFont, text, position.ToVector2(), color, rotation, origin.ToVector2(), scale.ToVector2(), effects, layerDepth, rtl);
public void End()
=> spriteBatch.End();

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core;
namespace Engine.Integration.MonoGame;
@@ -12,6 +14,7 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
private ISpriteBatch spriteBatch = null!;
private MonoGameCamera2D camera2D = null!;
private readonly RasterizerState rasterizerState = new() { CullMode = CullMode.CullClockwiseFace };
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableSprite> drawableSprites = new(GetPriority(), SortByPriority());
public void FirstActiveFrame()
@@ -26,7 +29,13 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
public void Draw()
{
spriteBatch.Begin(transformMatrix: camera2D.MatrixTransform);
Matrix4x4 transformMatrix = Matrix4x4.Identity
.ApplyTranslation(new Vector2D(camera2D.Viewport.X, camera2D.Viewport.Y) * .5f)
.ApplyScale(new Vector3D(1f, -1f, 1f))
.ApplyMatrix(camera2D.ViewMatrix)
.Transposed;
spriteBatch.Begin(transformMatrix: transformMatrix.ToXnaMatrix(), rasterizerState: rasterizerState);
for (int i = 0; i < drawableSprites.Count; i++)
drawableSprites[i].Draw(spriteBatch);
spriteBatch.End();

View File

@@ -1,40 +0,0 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public class TriangleBatcher : Behaviour, ITriangleBatch, IFirstFrameUpdate, IDraw
{
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private TriangleBatch triangleBatch = null!;
private MonoGameCamera2D camera2D = null!;
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
public void FirstActiveFrame()
{
MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameCamera2D>();
triangleBatch = new(windowContainer.Window.GraphicsDevice);
drawableShapes.Unassign();
drawableShapes.Assign(BehaviourController.UniverseObject.Universe);
}
public void Draw()
{
triangleBatch.Begin(camera2D.MatrixTransform);
for (int i = 0; i < drawableShapes.Count; i++)
drawableShapes[i].Draw(triangleBatch);
triangleBatch.End();
}
public void Begin(Matrix? view = null, Matrix? projection = null) => triangleBatch.Begin(view, projection);
public void Draw(Triangle triangle, ColorRGBA colorRGBA) => triangleBatch.Draw(triangle, colorRGBA);
public void End() => triangleBatch.End();
}

View File

@@ -9,8 +9,6 @@ namespace Engine.Integration.MonoGame;
public static class EngineConverterExtensions
{
public readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UniverseTime ToEngineTime(this GameTime gameTime) => new(gameTime.TotalGameTime, gameTime.ElapsedGameTime);
@@ -49,15 +47,17 @@ public static class EngineConverterExtensions
m.M41, m.M42, m.M43, m.M44
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4x4 FromXnaMatrix(this Matrix m) => new(
m.M11, m.M12, m.M13, m.M14,
m.M21, m.M22, m.M23, m.M24,
m.M31, m.M32, m.M33, m.M34,
m.M41, m.M42, m.M43, m.M44
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Microsoft.Xna.Framework.Quaternion ToXnaQuaternion(this Core.Quaternion quaternion) => new(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToDisplayVector2(this Vector2D vector) => vector.Scale(screenScale).ToVector2();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2D ApplyDisplayScale(this Vector2D vector) => vector.Scale(screenScale);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToDisplayRectangle(this Rectangle rectangle, DisplayMode displayMode) => new()
{
@@ -70,8 +70,8 @@ public static class EngineConverterExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToRectangle(this AABB2D aabb) => new()
{
X = (int)(aabb.LowerBoundary.X * screenScale.X),
Y = (int)(aabb.LowerBoundary.Y * screenScale.Y),
X = (int)aabb.LowerBoundary.X,
Y = (int)aabb.LowerBoundary.Y,
Width = (int)(aabb.UpperBoundary.X - aabb.LowerBoundary.X),
Height = (int)(aabb.UpperBoundary.Y - aabb.LowerBoundary.Y)
};

View File

@@ -2,24 +2,29 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core;
using Engine.Systems.Graphics;
namespace Engine.Integration.MonoGame;
public class TriangleBatch : ITriangleBatch
public class MonoGameTriangleBatch : Behaviour, ITriangleBatch, IFirstFrameUpdate
{
private readonly GraphicsDevice graphicsDevice;
private readonly VertexBuffer vertexBuffer = default!;
private GraphicsDevice graphicsDevice = null!;
private VertexBuffer vertexBuffer = default!;
private readonly VertexPositionColor[] vertices = new VertexPositionColor[1024];
private int verticesIndex = 0;
private Matrix view = Matrix.Identity;
private Matrix projection = Matrix.Identity;
private readonly BasicEffect basicEffect = null!;
private BasicEffect basicEffect = null!;
private readonly RasterizerState rasterizerState = new() { CullMode = CullMode.None };
public TriangleBatch(GraphicsDevice graphicsDevice)
private ICamera camera = null!;
public void FirstActiveFrame()
{
camera = Universe.FindRequiredBehaviour<ICamera>();
GraphicsDevice graphicsDevice = Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.GraphicsDevice;
this.graphicsDevice = graphicsDevice;
basicEffect = new(graphicsDevice);
basicEffect.VertexColorEnabled = true;
@@ -31,30 +36,20 @@ public class TriangleBatch : ITriangleBatch
if (verticesIndex + 3 >= vertices.Length)
Flush();
Vector2 A = triangle.A.ToDisplayVector2();
Vector2 B = triangle.B.ToDisplayVector2();
Vector2 C = triangle.C.ToDisplayVector2();
Color color = colorRGBA.ToColor();
Vector2 A = triangle.A.ToVector2();
Vector2 B = triangle.B.ToVector2();
Vector2 C = triangle.C.ToVector2();
Color color = colorRGBA.ToPreMultipliedColor();
vertices[verticesIndex++] = new(new(A.X, A.Y, 0f), color);
vertices[verticesIndex++] = new(new(B.X, B.Y, 0f), color);
vertices[verticesIndex++] = new(new(C.X, C.Y, 0f), color);
}
public void Begin(Matrix? view = null, Matrix? projection = null)
public void Begin(Matrix4x4? view = null, Matrix4x4? projection = null)
{
if (view != null)
this.view = view.Value;
else
this.view = Matrix.Identity;
if (projection != null)
this.projection = projection.Value;
else
{
Viewport viewport = graphicsDevice.Viewport;
this.projection = Matrix.CreateOrthographicOffCenter(viewport.X, viewport.Width, viewport.Height, viewport.Y, 0, 1);
}
this.view = (view ?? camera.ViewMatrix).Transposed.ToXnaMatrix();
this.projection = (projection ?? camera.ProjectionMatrix).Transposed.ToXnaMatrix();
}
public void End() => Flush();
@@ -65,6 +60,8 @@ public class TriangleBatch : ITriangleBatch
return;
graphicsDevice.RasterizerState = rasterizerState;
graphicsDevice.BlendState = BlendState.AlphaBlend;
basicEffect.Projection = projection;
basicEffect.View = view;
vertexBuffer.SetData(vertices);
@@ -72,8 +69,10 @@ public class TriangleBatch : ITriangleBatch
graphicsDevice.SetVertexBuffer(vertexBuffer);
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, verticesIndex / 3);
}
verticesIndex = 0;
}

View File

@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class BehaviourControllerConverter : EngineTypeYamlSerializerBase<IBehaviourController>
public class BehaviourControllerConverter : EngineTypeYamlConverterBase<IBehaviourController>
{
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";

View File

@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class BehaviourConverter : EngineTypeYamlSerializerBase<IBehaviour>
public class BehaviourConverter : EngineTypeYamlConverterBase<IBehaviour>
{
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public abstract class EngineTypeYamlSerializerBase<T> : IEngineTypeYamlConverter
public abstract class EngineTypeYamlConverterBase<T> : IEngineTypeYamlConverter
{
protected const string SERIALIZED_SCALAR_NAME = "Properties";

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class AABB2DConverter : EngineTypeYamlSerializerBase<AABB2D>
public class AABB2DConverter : EngineTypeYamlConverterBase<AABB2D>
{
public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class AABB3DConverter : EngineTypeYamlSerializerBase<AABB3D>
public class AABB3DConverter : EngineTypeYamlConverterBase<AABB3D>
{
public override AABB3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class CircleConverter : EngineTypeYamlSerializerBase<Circle>
public class CircleConverter : EngineTypeYamlConverterBase<Circle>
{
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorHSVAConverter : EngineTypeYamlSerializerBase<ColorHSVA>
public class ColorHSVAConverter : EngineTypeYamlConverterBase<ColorHSVA>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSVA).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorHSVConverter : EngineTypeYamlSerializerBase<ColorHSV>
public class ColorHSVConverter : EngineTypeYamlConverterBase<ColorHSV>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSV).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorRGBAConverter : EngineTypeYamlSerializerBase<ColorRGBA>
public class ColorRGBAConverter : EngineTypeYamlConverterBase<ColorRGBA>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGBA).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class ColorRGBConverter : EngineTypeYamlSerializerBase<ColorRGB>
public class ColorRGBConverter : EngineTypeYamlConverterBase<ColorRGB>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGB).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Line2DConverter : EngineTypeYamlSerializerBase<Line2D>
public class Line2DConverter : EngineTypeYamlConverterBase<Line2D>
{
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Line2DEquationConverter : EngineTypeYamlSerializerBase<Line2DEquation>
public class Line2DEquationConverter : EngineTypeYamlConverterBase<Line2DEquation>
{
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Line3DConverter : EngineTypeYamlSerializerBase<Line3D>
public class Line3DConverter : EngineTypeYamlConverterBase<Line3D>
{
public override Line3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -0,0 +1,33 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Matrix4x4Converter : EngineTypeYamlConverterBase<Matrix4x4>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Matrix4x4).Length + 1;
public override Matrix4x4 Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
string value = parser.Consume<Scalar>().Value;
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
string[] values = insideParenthesis.Split(", ");
return new Matrix4x4(
float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3]),
float.Parse(values[4]), float.Parse(values[5]), float.Parse(values[6]), float.Parse(values[7]),
float.Parse(values[8]), float.Parse(values[9]), float.Parse(values[10]), float.Parse(values[11]),
float.Parse(values[12]), float.Parse(values[13]), float.Parse(values[14]), float.Parse(values[15])
);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Matrix4x4 m = (Matrix4x4)value!;
emitter.Emit(new Scalar($"{nameof(Matrix4x4)}({m.M11}, {m.M12}, {m.M13}, {m.M14},{m.M21}, {m.M22}, {m.M23}, {m.M24},{m.M31}, {m.M32}, {m.M33}, {m.M34},{m.M41}, {m.M42}, {m.M43}, {m.M44})"));
}
}

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Projection1DConverter : EngineTypeYamlSerializerBase<Projection1D>
public class Projection1DConverter : EngineTypeYamlConverterBase<Projection1D>
{
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class QuaternionConverter : EngineTypeYamlSerializerBase<Quaternion>
public class QuaternionConverter : EngineTypeYamlConverterBase<Quaternion>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Quaternion).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Ray2DConverter : EngineTypeYamlSerializerBase<Ray2D>
public class Ray2DConverter : EngineTypeYamlConverterBase<Ray2D>
{
public override Ray2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Ray3DConverter : EngineTypeYamlSerializerBase<Ray3D>
public class Ray3DConverter : EngineTypeYamlConverterBase<Ray3D>
{
public override Ray3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Shape2DConverter : EngineTypeYamlSerializerBase<Shape2D>
public class Shape2DConverter : EngineTypeYamlConverterBase<Shape2D>
{
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Sphere3DConverter : EngineTypeYamlSerializerBase<Sphere3D>
public class Sphere3DConverter : EngineTypeYamlConverterBase<Sphere3D>
{
public override Sphere3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class TriangleConverter : EngineTypeYamlSerializerBase<Triangle>
public class TriangleConverter : EngineTypeYamlConverterBase<Triangle>
{
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector2DConverter : EngineTypeYamlSerializerBase<Vector2D>
public class Vector2DConverter : EngineTypeYamlConverterBase<Vector2D>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2D).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector2DIntConverter : EngineTypeYamlSerializerBase<Vector2DInt>
public class Vector2DIntConverter : EngineTypeYamlConverterBase<Vector2DInt>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2DInt).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector3DConverter : EngineTypeYamlSerializerBase<Vector3D>
public class Vector3DConverter : EngineTypeYamlConverterBase<Vector3D>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3D).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector3DIntConverter : EngineTypeYamlSerializerBase<Vector3DInt>
public class Vector3DIntConverter : EngineTypeYamlConverterBase<Vector3DInt>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3DInt).Length + 1;

View File

@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Vector4DConverter : EngineTypeYamlSerializerBase<Vector4D>
public class Vector4DConverter : EngineTypeYamlConverterBase<Vector4D>
{
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector4D).Length + 1;

View File

@@ -12,7 +12,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class SerializedClassConverter : EngineTypeYamlSerializerBase<SerializedClass>
public class SerializedClassConverter : EngineTypeYamlConverterBase<SerializedClass>
{
public override SerializedClass? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
@@ -37,17 +37,25 @@ public class SerializedClassConverter : EngineTypeYamlSerializerBase<SerializedC
}
}
foreach ((string key, object @object) in publicDictionary)
if (@object is IDictionary<object, object> dictionary && dictionary.TryGetValue(nameof(TypeContainer.Type), out object? typeField) && dictionary.TryGetValue(nameof(TypeContainer.Value), out object? valueField))
publicDictionary[key] = new TypeContainer() { Type = typeField!.ToString()!, Value = valueField };
foreach ((string key, object @object) in privateDictionary)
if (@object is IDictionary<object, object> dictionary && dictionary.TryGetValue(nameof(TypeContainer.Type), out object? typeField) && dictionary.TryGetValue(nameof(TypeContainer.Value), out object? valueField))
privateDictionary[key] = new TypeContainer() { Type = typeField!.ToString()!, Value = valueField };
Type classType = TypeFactory.GetType(serializedClass.Type);
foreach ((string key, object @object) in publicDictionary)
if (@object is TypeContainer typeContainer)
serializedClass.Public.Add(key, Serializer.InternalDeserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
serializedClass.Public.Add(key, Serializer.InternalDeserialize(Serializer.Serialize(typeContainer.Value!), TypeFactory.GetType(typeContainer.Type)));
else
serializedClass.Public.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.FieldType)!));
foreach ((string key, object @object) in privateDictionary)
if (@object is TypeContainer typeContainer)
serializedClass.Private.Add(key, Serializer.InternalDeserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
serializedClass.Private.Add(key, Serializer.InternalDeserialize(Serializer.Serialize(typeContainer.Value!), TypeFactory.GetType(typeContainer.Type)));
else
serializedClass.Private.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.FieldType)!));
@@ -65,14 +73,14 @@ public class SerializedClassConverter : EngineTypeYamlSerializerBase<SerializedC
Dictionary<string, object> publics = [];
Dictionary<string, object> privates = [];
foreach ((string key, object? @object) in serializedClass.Public.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
if (@object?.GetType().IsClass == false)
foreach ((string key, object? @object) in serializedClass.Public)
if (@object?.GetType().IsClass == false || @object is string)
publics.Add(key, @object!);
else
publics.Add(key, new TypeContainer(@object));
foreach ((string key, object? @object) in serializedClass.Private.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
if (@object?.GetType().IsClass == false)
foreach ((string key, object? @object) in serializedClass.Private)
if (@object?.GetType().IsClass == false || @object is string)
privates.Add(key, @object!);
else
privates.Add(key, new TypeContainer(@object));

View File

@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class StateEnableConverter : EngineTypeYamlSerializerBase<IStateEnable>
public class StateEnableConverter : EngineTypeYamlConverterBase<IStateEnable>
{
public override IStateEnable? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class TypeContainerConverter : EngineTypeYamlSerializerBase<TypeContainer>
public class TypeContainerConverter : EngineTypeYamlConverterBase<TypeContainer>
{
public override TypeContainer Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class UniverseConverter : EngineTypeYamlSerializerBase<IUniverse>
public class UniverseConverter : EngineTypeYamlConverterBase<IUniverse>
{
public override IUniverse? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class UniverseObjectSerializer : EngineTypeYamlSerializerBase<IUniverseObject>
public class UniverseObjectConverter : EngineTypeYamlConverterBase<IUniverseObject>
{
public override IUniverseObject? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using Engine.Core.Config;
namespace Engine.Serializers.Yaml;
public class YamlConfiguration : BasicConfiguration
{
public readonly string FilePath;
private readonly YamlSerializer yamlSerializer = new();
public YamlConfiguration(string filePath)
{
if (!filePath.EndsWith(".yaml"))
filePath += ".yaml";
FilePath = filePath;
bool isRelativePath = Path.GetFullPath(filePath).CompareTo(filePath) != 0;
if (isRelativePath)
FilePath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath));
if (Path.GetDirectoryName(FilePath) is string directoryPath)
Directory.CreateDirectory(directoryPath);
if (!File.Exists(FilePath))
return;
string yamlFileText = File.ReadAllText(FilePath);
Dictionary<string, string> valuePairs = yamlSerializer.Deserialize<Dictionary<string, string>>(yamlFileText);
foreach ((string key, string value) in valuePairs)
Set(key, value);
}
public void Save()
{
File.WriteAllText(FilePath, yamlSerializer.Serialize(Values));
}
}

View File

@@ -62,65 +62,65 @@ public class YamlSerializer : Core.Serialization.ISerializer
}
}
public object Deserialize(string configuration)
public object Deserialize(string content)
{
lock (Lock)
{
identifiableRegistry.Reset();
object result = deserializer.Deserialize(configuration)!;
object result = deserializer.Deserialize(content)!;
identifiableRegistry.AssignAll();
return result;
}
}
public object Deserialize(string configuration, Type type)
public object Deserialize(string content, Type type)
{
lock (Lock)
{
identifiableRegistry.Reset();
object result = deserializer.Deserialize(configuration, type)!;
object result = deserializer.Deserialize(content, type)!;
identifiableRegistry.AssignAll();
return result;
}
}
public T Deserialize<T>(string configuration)
public T Deserialize<T>(string content)
{
lock (Lock)
{
identifiableRegistry.Reset();
T result = deserializer.Deserialize<T>(configuration);
T result = deserializer.Deserialize<T>(content);
identifiableRegistry.AssignAll();
return result;
}
}
public ProgressiveTask<object> DeserializeAsync(string configuration)
public ProgressiveTask<object> DeserializeAsync(string content)
{
lock (Lock)
{
progressionTracker.Reset();
Task<object> task = Task.Run(() => Deserialize(configuration));
Task<object> task = Task.Run(() => Deserialize(content));
return new ProgressiveTask<object>(progressionTracker, task);
}
}
public ProgressiveTask<object> DeserializeAsync(string configuration, Type type)
public ProgressiveTask<object> DeserializeAsync(string content, Type type)
{
lock (Lock)
{
progressionTracker.Reset();
Task<object> task = Task.Run(() => Deserialize(configuration, type));
Task<object> task = Task.Run(() => Deserialize(content, type));
return new ProgressiveTask<object>(progressionTracker, task);
}
}
public ProgressiveTask<T> DeserializeAsync<T>(string configuration)
public ProgressiveTask<T> DeserializeAsync<T>(string content)
{
lock (Lock)
{
progressionTracker.Reset();
Task<T> task = Task.Run(() => Deserialize<T>(configuration));
Task<T> task = Task.Run(() => Deserialize<T>(content));
return new ProgressiveTask<T>(progressionTracker, task);
}
}
@@ -135,8 +135,8 @@ public class YamlSerializer : Core.Serialization.ISerializer
}
}
internal object InternalDeserialize(string configuration, Type type)
internal object InternalDeserialize(string content, Type type)
{
return deserializer.Deserialize(configuration, type)!;
return deserializer.Deserialize(content, type)!;
}
}

View File

@@ -7,6 +7,11 @@ namespace Engine.Physics2D;
/// </summary>
public interface ICollider2D : IBehaviour
{
/// <summary>
/// Event triggered when the collider is recalculated.
/// </summary>
Event<ICollider2D> OnRecalculated { get; }
/// <summary>
/// Event triggered when a collision is detected.
/// </summary>
@@ -30,6 +35,16 @@ public interface ICollider2D : IBehaviour
/// </summary>
IRigidBody2D? RigidBody2D { get; }
/// <summary>
/// The area of the <see cref="ICollider2D"/>.
/// </summary>
float Area { get; }
/// <summary>
/// The geometric inertia of the <see cref="ICollider2D"/>.
/// </summary>
float GeometricInertia { get; }
/// <summary>
/// The value indicating whether the <see cref="ICollider2D"/> is a trigger.
/// </summary>

View File

@@ -27,6 +27,21 @@ public interface IRigidBody2D : IBehaviour2D
/// </summary>
float Mass { get; set; }
/// <summary>
/// The inverse mass (1 / Mass) of the <see cref="IRigidBody2D"/>.
/// </summary>
float InverseMass { get; }
/// <summary>
/// The Invertia of the <see cref="IRigidBody2D"/>.
/// </summary>
float Inertia { get; }
/// <summary>
/// The inverse Invertia (1 / Invertia) of the <see cref="IRigidBody2D"/>.
/// </summary>
float InverseInertia { get; }
/// <summary>
/// The value indicating whether the <see cref="IRigidBody2D"/> is static/immovable.
/// </summary>

View File

@@ -4,6 +4,7 @@ namespace Engine.Physics2D;
public abstract class Collider2DBase : Behaviour2D, ICollider2D
{
public Event<ICollider2D> OnRecalculated { get; } = new();
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionDetected { get; } = new();
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionResolved { get; } = new();
public Event<ICollider2D, ICollider2D> OnTriggered { get; } = new();
@@ -20,6 +21,9 @@ public abstract class Collider2DBase : Behaviour2D, ICollider2D
public IRigidBody2D? RigidBody2D { get; protected set; } = null;
public bool IsTrigger { get; set; } = false;
public abstract float Area { get; }
public abstract float GeometricInertia { get; }
public void Recalculate()
{
if (!NeedsRecalculation)
@@ -27,6 +31,7 @@ public abstract class Collider2DBase : Behaviour2D, ICollider2D
CalculateCollider();
NeedsRecalculation = false;
OnRecalculated.Invoke(this);
}
public abstract void CalculateCollider();

View File

@@ -15,7 +15,15 @@ public class Collider2DCircle : Collider2DBase, ICircleCollider2D
}
} = Circle.UnitCircle;
public override void CalculateCollider() => CircleWorld = Transform.Transform(CircleLocal);
private float area = 0f; public override float Area => area;
private float geometricInertia = 0f; public override float GeometricInertia => geometricInertia;
public override void CalculateCollider()
{
CircleWorld = Transform.Transform(CircleLocal);
area = CircleWorld.Area;
geometricInertia = CircleWorld.GeometricInertia;
}
public Collider2DCircle() { }
public Collider2DCircle(Circle circle) => CircleLocal = circle;

View File

@@ -15,7 +15,16 @@ public class Collider2DShape : Collider2DBase, IShapeCollider2D
}
} = Shape2D.Square;
public override void CalculateCollider() => ShapeLocal.Transform(Transform, ShapeWorld);
private float area = 0f; public override float Area => area;
private float geometricInertia = 0f; public override float GeometricInertia => geometricInertia;
public override void CalculateCollider()
{
ShapeLocal.Transform(Transform, ShapeWorld);
area = ShapeWorld.Area;
geometricInertia = ShapeWorld.GetGeometricInertia(Transform.Position);
}
public Collider2DShape() { }
public Collider2DShape(Shape2D shape) { ShapeLocal = shape; }

View File

@@ -7,14 +7,16 @@ public readonly struct CollisionDetectionInformation
(
ICollider2D Detector,
ICollider2D Detected,
Vector2D ContactPoint,
Vector2D Normal,
float Penetration
)
{
public ICollider2D Detector { get; init; } = Detector;
public ICollider2D Detected { get; init; } = Detected;
public Vector2D ContactPoint { get; init; } = ContactPoint;
public Vector2D Normal { get; init; } = Normal;
public float Penetration { get; init; } = Penetration;
public CollisionDetectionInformation Reverse() => new(Detected, Detector, -Normal, Penetration);
public CollisionDetectionInformation Reverse() => new(Detected, Detector, ContactPoint, -Normal, Penetration);
}

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
using Engine.Core;
using Engine.Physics2D;
@@ -41,12 +43,13 @@ public class CollisionDetector2D : ICollisionDetector2D
private static bool DetectShapeShapeOneWay(IShapeCollider2D left, IShapeCollider2D right, ref CollisionDetectionInformation collisionInformation)
{
System.Collections.Generic.IReadOnlyList<Vector2D> vertices = left.ShapeWorld.Vertices;
IReadOnlyList<Vector2D> vertices = left.ShapeWorld.Vertices;
int count = vertices.Count;
for (int indexProjection = 0; indexProjection < count; indexProjection++)
{
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
Vector2D leftEdge = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]);
Vector2D projectionVector = leftEdge.Perpendicular().Normalized;
Projection1D leftProjection = left.ShapeWorld.ToProjection(projectionVector);
Projection1D rightProjection = right.ShapeWorld.ToProjection(projectionVector);
@@ -55,17 +58,61 @@ public class CollisionDetector2D : ICollisionDetector2D
return false;
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
collisionInformation = new(left, right, projectionVector, depth);
{
Vector2D contactPoint = FindShapeToShapeContactPoint(left, right, projectionVector);
collisionInformation = new(left, right, contactPoint, projectionVector, depth);
}
}
return true;
}
private static Vector2D FindShapeToShapeContactPoint(IShapeCollider2D left, IShapeCollider2D right, Vector2D contactProjectionVector)
{
IReadOnlyList<Vector2D> leftVertices = left.ShapeWorld.Vertices;
IReadOnlyList<Vector2D> rightVertices = right.ShapeWorld.Vertices;
Line2D leftSupportLine = GetSupportLine(leftVertices, contactProjectionVector);
Line2D rightSupportLine = GetSupportLine(rightVertices, -contactProjectionVector);
if (leftSupportLine.Direction.Dot(rightSupportLine.Direction).Abs() > .99f)
return (leftSupportLine.From + leftSupportLine.To + rightSupportLine.From + rightSupportLine.To) / 4f;
return leftSupportLine.IntersectionPoint(rightSupportLine);
}
private static Line2D GetSupportLine(IReadOnlyList<Vector2D> vertices, Vector2D contactProjectionVector)
{
System.Span<Vector2D> points = stackalloc Vector2D[2];
System.Span<float> distances = stackalloc float[2] { float.MaxValue, float.MaxValue };
for (int i = 0; i < vertices.Count; i++)
{
Vector2D point = vertices[i];
float distance = contactProjectionVector.Dot(point);
if (distance < distances[0])
{
points[1] = points[0];
distances[1] = distances[0];
points[0] = point;
distances[0] = distance;
}
else if (distance < distances[1])
{
points[1] = point;
distances[1] = distance;
}
}
return new(points[0], points[1]);
}
private static bool DetectShapeCircle(IShapeCollider2D shapeCollider, ICircleCollider2D circleCollider, out CollisionDetectionInformation collisionInformation)
{
collisionInformation = default;
System.Collections.Generic.IReadOnlyList<Vector2D> vertices = shapeCollider.ShapeWorld.Vertices;
IReadOnlyList<Vector2D> vertices = shapeCollider.ShapeWorld.Vertices;
int count = vertices.Count;
for (int indexProjection = 0; indexProjection < count; indexProjection++)
@@ -78,8 +125,10 @@ public class CollisionDetector2D : ICollisionDetector2D
if (!shapeProjection.Overlaps(circleProjection, out float depth))
return false;
Vector2D contactPoint = circleCollider.CircleWorld.Center + projectionVector * circleCollider.CircleWorld.Radius;
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
collisionInformation = new(shapeCollider, circleCollider, projectionVector, depth);
collisionInformation = new(shapeCollider, circleCollider, contactPoint, projectionVector, depth);
}
{
@@ -91,8 +140,10 @@ public class CollisionDetector2D : ICollisionDetector2D
if (!shapeProjection.Overlaps(circleProjection, out float depth))
return false;
Vector2D contactPoint = circleCollider.CircleWorld.Center + shapeToCircleProjectionVector * circleCollider.CircleWorld.Radius;
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
collisionInformation = new(shapeCollider, circleCollider, shapeToCircleProjectionVector, depth);
collisionInformation = new(shapeCollider, circleCollider, contactPoint, shapeToCircleProjectionVector, depth);
}
return true;
@@ -110,7 +161,10 @@ public class CollisionDetector2D : ICollisionDetector2D
bool collision = leftProjection.Overlaps(rightProjection, out float depth);
if (collision)
collisionInformation = new(left, right, leftToRightCenterProjectionVector, depth);
{
Vector2D contactPoint = left.CircleWorld.Center + leftToRightCenterProjectionVector * left.CircleWorld.Radius;
collisionInformation = new(left, right, contactPoint, leftToRightCenterProjectionVector, depth);
}
return collision;
}

View File

@@ -6,8 +6,6 @@ public class CollisionResolver2D : ICollisionResolver2D
{
public void Resolve(CollisionDetectionInformation collisionInformation)
{
Vector2D displacementVector = collisionInformation.Normal * collisionInformation.Penetration;
ICollider2D left = collisionInformation.Detector;
ICollider2D right = collisionInformation.Detected;
@@ -17,6 +15,20 @@ public class CollisionResolver2D : ICollisionResolver2D
if (isLeftStatic && isRightStatic)
return;
Displace(collisionInformation, left, right, isLeftStatic, isRightStatic);
Bounce(collisionInformation, left, right, isLeftStatic, isRightStatic);
left.Recalculate();
right.Recalculate();
left.Resolve(collisionInformation);
right.Resolve(collisionInformation);
}
private static void Displace(CollisionDetectionInformation collisionInformation, ICollider2D left, ICollider2D right, bool isLeftStatic, bool isRightStatic)
{
Vector2D displacementVector = collisionInformation.Normal * collisionInformation.Penetration;
if (isLeftStatic)
right.Transform.Position += displacementVector;
else if (isRightStatic)
@@ -33,11 +45,32 @@ public class CollisionResolver2D : ICollisionResolver2D
right.Transform.Position += leftMomentumPercentage * displacementVector;
left.Transform.Position -= rightMomentumPercentage * displacementVector;
}
}
left.Recalculate();
right.Recalculate();
private static void Bounce(CollisionDetectionInformation collisionInformation, ICollider2D left, ICollider2D right, bool isLeftStatic, bool isRightStatic)
{
Vector2D leftVelocity = left.RigidBody2D?.Velocity ?? Vector2D.Zero;
Vector2D rightVelocity = right.RigidBody2D?.Velocity ?? Vector2D.Zero;
left.Resolve(collisionInformation);
right.Resolve(collisionInformation);
Vector2D relativeVelocity = leftVelocity - rightVelocity;
float velocityAlongNormal = Vector2D.Dot(relativeVelocity, collisionInformation.Normal);
if (velocityAlongNormal > 0)
{
collisionInformation = collisionInformation.Reverse();
velocityAlongNormal = -velocityAlongNormal;
}
float e = (left.RigidBody2D?.Material.Restitution ?? 0f).Add(right.RigidBody2D?.Material.Restitution ?? 0f).Divide(2f);
float leftMassEffective = isLeftStatic ? float.PositiveInfinity : left.RigidBody2D?.Mass ?? float.Epsilon;
float rightMassEffective = isRightStatic ? float.PositiveInfinity : right.RigidBody2D?.Mass ?? float.Epsilon;
float impulse = -(1f + e) * velocityAlongNormal / ((1f / leftMassEffective) + (1f / rightMassEffective));
if (!isLeftStatic)
left.RigidBody2D?.Velocity += impulse / leftMassEffective * collisionInformation.Normal;
if (!isRightStatic)
right.RigidBody2D?.Velocity -= impulse / rightMassEffective * collisionInformation.Normal;
}
}

View File

@@ -1,7 +1,7 @@
namespace Engine.Physics2D;
public readonly struct PhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
public class PhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
{
public readonly float Friction { get; init; } = Friction;
public readonly float Restitution { get; init; } = Restitution;
public float Friction { get; set; } = Friction;
public float Restitution { get; set; } = Restitution;
}

View File

@@ -1,8 +0,0 @@
namespace Engine.Physics2D;
public readonly struct PhysicsMaterial2DDefault : IPhysicsMaterial2D
{
public readonly float Friction => .1f;
public readonly float Restitution => .1f;
}

View File

@@ -0,0 +1,9 @@
namespace Engine.Physics2D;
public readonly struct ReadOnlyPhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
{
public readonly float Friction { get; } = Friction;
public readonly float Restitution { get; } = Restitution;
public readonly static ReadOnlyPhysicsMaterial2D Default = new(.1f, .1f);
}

View File

@@ -1,16 +1,75 @@
using System.Collections.Generic;
using Engine.Core;
namespace Engine.Physics2D;
public class RigidBody2D : Behaviour2D, IRigidBody2D
public class RigidBody2D : Behaviour2D, IRigidBody2D, IFirstFrameUpdate, ILastFrameUpdate
{
private const float LOWEST_ALLOWED_MASS = 0.00001f;
public IPhysicsMaterial2D Material { get; set; } = new PhysicsMaterial2DDefault();
public IPhysicsMaterial2D Material { get; set; } = ReadOnlyPhysicsMaterial2D.Default;
public Vector2D Velocity { get; set; } = Vector2D.Zero;
public float AngularVelocity { get; set; } = 0f;
public bool IsStatic { get; set; } = false;
public float Mass { get; set => field = Math.Max(value, LOWEST_ALLOWED_MASS); } = 1f;
public float Mass { get; set { field = Math.Max(value, LOWEST_ALLOWED_MASS); } } = 1f;
public float InverseMass { get; protected set; } = 1f;
public float Inertia { get; protected set; } = 1f;
public float InverseInertia { get; protected set; } = 1f;
private readonly List<ICollider2D> childColliders = [];
public void LastActiveFrame() => DisconnectColliders();
public void FirstActiveFrame()
{
ReconnectColliders();
UpdateValues();
}
private void ReconnectColliders()
{
DisconnectColliders();
BehaviourController.GetBehavioursInChildren(childColliders);
foreach (ICollider2D collider in childColliders)
collider.OnRecalculated.AddListener(RecalculateCallback);
}
private void DisconnectColliders()
{
foreach (ICollider2D collider in childColliders)
collider.OnRecalculated.RemoveListener(RecalculateCallback);
}
private void RecalculateCallback(ICollider2D _) => UpdateValues();
private void UpdateValues()
{
InverseMass = Mass.OneOver();
Vector2D center = Transform.Position;
Inertia = 0f;
float totalColliderArea = 0f;
foreach (ICollider2D collider in childColliders)
totalColliderArea += collider.Area;
foreach (ICollider2D collider in childColliders)
{
float colliderMass = Mass * (collider.Area / totalColliderArea);
float colliderInertia = collider.GeometricInertia * colliderMass;
float distanceSquared = center.FromTo(collider.Transform.Position).MagnitudeSquared;
Inertia += colliderInertia + colliderMass * distanceSquared;
}
if (childColliders.Count == 0)
Inertia = 1f;
InverseInertia = Inertia.OneOver();
}
}

View File

@@ -2,9 +2,9 @@ using System.Collections.Generic;
using Engine.Core;
namespace Engine.Integration.MonoGame;
namespace Engine.Systems.Graphics;
public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
public class DrawableShape2D : Behaviour2D, IDrawableTriangle, IPreDraw
{
private readonly Shape2D shape = new([]);
private readonly List<Triangle> worldTriangles = [];
@@ -23,7 +23,7 @@ public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
protected void UpdateWorldShape() => shape.Transform(Transform, worldShape);
public DrawableShape() => shape = Shape2D.Triangle;
public DrawableShape(Shape2D shape) => this.shape = shape;
public DrawableShape(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
public DrawableShape2D() => shape = Shape2D.Triangle;
public DrawableShape2D(Shape2D shape) => this.shape = shape;
public DrawableShape2D(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
}

View File

@@ -1,6 +1,6 @@
using Engine.Core;
namespace Engine.Integration.MonoGame;
namespace Engine.Systems.Graphics;
public interface IDrawableTriangle : IBehaviour
{

View File

@@ -0,0 +1,10 @@
using Engine.Core;
namespace Engine.Systems.Graphics;
public interface ITriangleBatch
{
void Begin(Matrix4x4? view = null, Matrix4x4? projection = null);
void Draw(Triangle triangle, ColorRGBA colorRGBA);
void End();
}

View File

@@ -0,0 +1,38 @@
using System.Collections.Generic;
using Engine.Core;
namespace Engine.Systems.Graphics;
public class TriangleBatcher : Behaviour, IFirstFrameUpdate, ILastFrameUpdate, IDraw
{
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private readonly BehaviourCollector<ITriangleBatch> triangleBatches = new();
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
public void FirstActiveFrame()
{
drawableShapes.Assign(Universe);
triangleBatches.Assign(Universe);
}
public void Draw()
{
for (int i = 0; i < triangleBatches.Count; i++)
{
ITriangleBatch triangleBatch = triangleBatches[i];
triangleBatch.Begin();
for (int j = 0; j < drawableShapes.Count; j++)
drawableShapes[j].Draw(triangleBatch);
triangleBatch.End();
}
}
public void LastActiveFrame()
{
triangleBatches.Unassign();
drawableShapes.Unassign();
}
}

View File

@@ -0,0 +1,6 @@
namespace Engine.Systems.Network;
public interface IPacketListenerClientEntity<T> : INetworkEntity where T : IEntityNetworkPacket
{
void OnEntityClientPacketArrived(IConnection sender, T packet);
}

View File

@@ -0,0 +1,6 @@
namespace Engine.Systems.Network;
public interface IPacketListenerServerEntity<T> : INetworkEntity where T : IEntityNetworkPacket
{
void OnEntityServerPacketArrived(IConnection sender, T packet);
}

Some files were not shown because too many files have changed in this diff Show More