Compare commits
20 Commits
499f875903
...
fe0173b091
| Author | SHA1 | Date | |
|---|---|---|---|
| fe0173b091 | |||
| 0707665481 | |||
| 734649955e | |||
| 105b87da3a | |||
| 51534606c8 | |||
| 1418927c32 | |||
| 35c7eb9578 | |||
| 6ca3f22b17 | |||
| 7ae8b4feb0 | |||
| e84c6edce1 | |||
| 4326d5615e | |||
| fa514531bf | |||
| 1d3dd8b046 | |||
| 785fee9b6b | |||
| 9f54f89f6d | |||
| aadc87d78a | |||
| d653774357 | |||
| 45bd505da7 | |||
| 3b1c291588 | |||
| 32a7e9be24 |
@@ -5,6 +5,11 @@ namespace Engine.Core;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICamera
|
public interface ICamera
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The viewport of the <see cref="ICamera"/>.
|
||||||
|
/// </summary>
|
||||||
|
Vector2D Viewport { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
|
/// View <see cref="Matrix4x4"/> of the <see cref="ICamera"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
50
Engine.Core/Config/BasicConfiguration.cs
Normal file
50
Engine.Core/Config/BasicConfiguration.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
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 = default)
|
||||||
|
{
|
||||||
|
if (!values.TryGetValue(key, out object? value))
|
||||||
|
return defaultValue;
|
||||||
|
|
||||||
|
if (value is T castedObject)
|
||||||
|
return castedObject;
|
||||||
|
|
||||||
|
try { return (T?)System.Convert.ChangeType(value, typeof(T)); } catch { }
|
||||||
|
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Engine.Core/Config/ConfigurationExtensions.cs
Normal file
8
Engine.Core/Config/ConfigurationExtensions.cs
Normal 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}");
|
||||||
|
}
|
||||||
23
Engine.Core/Config/IConfiguration.cs
Normal file
23
Engine.Core/Config/IConfiguration.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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 = default);
|
||||||
|
void Set<T>(string key, T value);
|
||||||
|
void Remove<T>(string key);
|
||||||
|
|
||||||
|
readonly record struct ConfigUpdateArguments(string Key);
|
||||||
|
}
|
||||||
11
Engine.Core/Config/SystemConfiguration.cs
Normal file
11
Engine.Core/Config/SystemConfiguration.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Engine.Core/Extensions/IdentifiableExtensions.cs
Normal file
12
Engine.Core/Extensions/IdentifiableExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -436,4 +436,9 @@ public static class Matrix4x4Extensions
|
|||||||
|
|
||||||
/// <inheritdoc cref="Matrix4x4.ToRightHanded(Matrix4x4) />
|
/// <inheritdoc cref="Matrix4x4.ToRightHanded(Matrix4x4) />
|
||||||
public static Matrix4x4 ToRightHanded(this Matrix4x4 matrix) => Matrix4x4.ToRightHanded(matrix);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ namespace Engine.Core.Serialization;
|
|||||||
|
|
||||||
public interface ISerializer
|
public interface ISerializer
|
||||||
{
|
{
|
||||||
object Deserialize(string configuration);
|
object Deserialize(string content);
|
||||||
object Deserialize(string configuration, Type type);
|
object Deserialize(string content, Type type);
|
||||||
T Deserialize<T>(string configuration);
|
T Deserialize<T>(string content);
|
||||||
|
|
||||||
string Serialize(object instance);
|
string Serialize(object instance);
|
||||||
|
|
||||||
ProgressiveTask<object> DeserializeAsync(string configuration);
|
ProgressiveTask<object> DeserializeAsync(string content);
|
||||||
ProgressiveTask<object> DeserializeAsync(string configuration, Type type);
|
ProgressiveTask<object> DeserializeAsync(string content, Type type);
|
||||||
ProgressiveTask<T> DeserializeAsync<T>(string configuration);
|
ProgressiveTask<T> DeserializeAsync<T>(string content);
|
||||||
|
|
||||||
ProgressiveTask<string> SerializeAsync(object instance);
|
ProgressiveTask<string> SerializeAsync(object instance);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,17 @@ public class UpdateManager : Behaviour, IEnterUniverse, IExitUniverse
|
|||||||
universe.OnPostUpdate.RemoveListener(OnPostUpdate);
|
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)
|
private void OnFirstUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
||||||
{
|
{
|
||||||
for (int i = toCallFirstFrameUpdates.Count - 1; i >= 0; i--)
|
for (int i = toCallFirstFrameUpdates.Count - 1; i >= 0; i--)
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Engine.Core;
|
|
||||||
|
|
||||||
public class WaitForSecondsYield(float seconds) : ICoroutineYield
|
|
||||||
{
|
|
||||||
private readonly DateTime triggerTime = DateTime.UtcNow.AddSeconds(seconds);
|
|
||||||
|
|
||||||
public bool Yield() => DateTime.UtcNow < triggerTime;
|
|
||||||
}
|
|
||||||
35
Engine.Core/Systems/Yields/WaitForTaskYield.cs
Normal file
35
Engine.Core/Systems/Yields/WaitForTaskYield.cs
Normal 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.Either) : ICoroutineYield
|
||||||
|
{
|
||||||
|
public bool Yield()
|
||||||
|
{
|
||||||
|
switch (completionStatus)
|
||||||
|
{
|
||||||
|
case TaskCompletionStatus.Successful:
|
||||||
|
if (task.IsCanceled)
|
||||||
|
throw new("Task has been canceled.");
|
||||||
|
if (task.IsFaulted)
|
||||||
|
throw new("Task has faulted.");
|
||||||
|
return task.IsCompletedSuccessfully;
|
||||||
|
|
||||||
|
case TaskCompletionStatus.Failed:
|
||||||
|
if (task.IsCompletedSuccessfully)
|
||||||
|
throw new("Task was completed successfully.");
|
||||||
|
return task.IsFaulted;
|
||||||
|
}
|
||||||
|
|
||||||
|
return task.IsCompleted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum TaskCompletionStatus
|
||||||
|
{
|
||||||
|
Either,
|
||||||
|
Successful,
|
||||||
|
Failed
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Engine.Core/Systems/Yields/WaitForTimeYield.cs
Normal file
14
Engine.Core/Systems/Yields/WaitForTimeYield.cs
Normal 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;
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
|
||||||
|
|
||||||
using Engine.Core;
|
using Engine.Core;
|
||||||
|
|
||||||
@@ -41,7 +40,7 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
}
|
}
|
||||||
} = Matrix4x4.Identity;
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Viewport Viewport
|
public Vector2D Viewport
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set
|
set
|
||||||
@@ -72,8 +71,8 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
|
// TODO This causes delay since OnPreDraw calls assuming this is called in in Update
|
||||||
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
|
public Vector2D ScreenToWorldPosition(Vector2D screenPosition)
|
||||||
{
|
{
|
||||||
float x = 2f * screenPosition.X / Viewport.Width - 1f;
|
float x = 2f * screenPosition.X / Viewport.X - 1f;
|
||||||
float y = 1f - 2f * screenPosition.Y / Viewport.Height;
|
float y = 1f - 2f * screenPosition.Y / Viewport.Y;
|
||||||
Vector4D normalizedCoordinates = new(x, y, 0f, 1f);
|
Vector4D normalizedCoordinates = new(x, y, 0f, 1f);
|
||||||
|
|
||||||
Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse;
|
Matrix4x4 invertedViewProjectionMatrix = (ProjectionMatrix * ViewMatrix).Inverse;
|
||||||
@@ -96,8 +95,8 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
if (clip.W != 0f)
|
if (clip.W != 0f)
|
||||||
clip /= clip.W;
|
clip /= clip.W;
|
||||||
|
|
||||||
float screenX = (clip.X + 1f) * .5f * Viewport.Width;
|
float screenX = (clip.X + 1f) * .5f * Viewport.X;
|
||||||
float screenY = (1f - clip.Y) * .5f * Viewport.Height;
|
float screenY = (1f - clip.Y) * .5f * Viewport.Y;
|
||||||
|
|
||||||
return new(screenX, screenY);
|
return new(screenX, screenY);
|
||||||
}
|
}
|
||||||
@@ -106,17 +105,17 @@ public class MonoGameCamera2D : Behaviour, ICamera2D, IFirstFrameUpdate, ILastFr
|
|||||||
public void FirstActiveFrame()
|
public void FirstActiveFrame()
|
||||||
{
|
{
|
||||||
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
|
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>();
|
Transform = BehaviourController.GetRequiredBehaviour<ITransform2D>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PreDraw()
|
public void PreDraw()
|
||||||
{
|
{
|
||||||
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.Width, Viewport.Height);
|
ProjectionMatrix = Matrix4x4.CreateOrthographicViewCentered(Viewport.X, Viewport.Y);
|
||||||
ViewMatrix = Matrix4x4.Identity
|
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(Transform.Scale.X.Max(Transform.Scale.Y))
|
||||||
.ApplyScale(Zoom)
|
.ApplyScale(Zoom);
|
||||||
.ApplyRotationZ(-Transform.Rotation * Math.DegreeToRadian)
|
|
||||||
.ApplyTranslation(new Vector3D(-Transform.Position.X, -Transform.Position.Y, 0f));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
}
|
}
|
||||||
} = Matrix4x4.Identity;
|
} = Matrix4x4.Identity;
|
||||||
|
|
||||||
public Viewport Viewport
|
public Vector2D Viewport
|
||||||
{
|
{
|
||||||
get;
|
get;
|
||||||
set
|
set
|
||||||
@@ -57,7 +57,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
if (field.Equals(value))
|
if (field.Equals(value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Viewport previousViewport = field;
|
Vector2D previousViewport = field;
|
||||||
field = value;
|
field = value;
|
||||||
SetForRecalculation();
|
SetForRecalculation();
|
||||||
OnViewportChanged.Invoke(this, new(previousViewport));
|
OnViewportChanged.Invoke(this, new(previousViewport));
|
||||||
@@ -114,21 +114,21 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
Matrix projection = ProjectionMatrix.ToXnaMatrix();
|
Matrix projection = ProjectionMatrix.ToXnaMatrix();
|
||||||
Matrix view = ViewMatrix.ToXnaMatrix();
|
Matrix view = ViewMatrix.ToXnaMatrix();
|
||||||
|
|
||||||
Vector3 worldNear = Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
|
Vector3 worldNear = Graphics.GraphicsDevice.Viewport.Unproject(nearPoint, projection, view, Matrix.Identity);
|
||||||
Vector3 worldFar = Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
|
Vector3 worldFar = Graphics.GraphicsDevice.Viewport.Unproject(farPoint, projection, view, Matrix.Identity);
|
||||||
|
|
||||||
Vector3 direction = Vector3.Normalize(worldFar - worldNear);
|
Vector3 direction = Vector3.Normalize(worldFar - worldNear);
|
||||||
return new(worldNear.ToVector3D(), direction.ToVector3D());
|
return new(worldNear.ToVector3D(), direction.ToVector3D());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2D WorldToScreenPosition(Vector3D worldPosition) => Viewport.Project(worldPosition.ToVector3(), ProjectionMatrix.ToXnaMatrix(), ViewMatrix.ToXnaMatrix(), 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 LastActiveFrame() => Transform.OnTransformUpdated.RemoveListener(SetDirtyOnTransformUpdate);
|
||||||
public void FirstActiveFrame()
|
public void FirstActiveFrame()
|
||||||
{
|
{
|
||||||
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
|
Transform = BehaviourController.GetRequiredBehaviour<ITransform3D>();
|
||||||
Graphics = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.Graphics;
|
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);
|
Transform.OnTransformUpdated.AddListener(SetDirtyOnTransformUpdate);
|
||||||
}
|
}
|
||||||
@@ -167,7 +167,7 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
private void CalculateProjection()
|
private void CalculateProjection()
|
||||||
{
|
{
|
||||||
float yScale = 1f / (float)Math.Tan(fieldOfView / 2f);
|
float yScale = 1f / (float)Math.Tan(fieldOfView / 2f);
|
||||||
float xScale = yScale / Viewport.AspectRatio;
|
float xScale = yScale / (Viewport.X / Viewport.Y);
|
||||||
|
|
||||||
ProjectionMatrix = new Matrix4x4(
|
ProjectionMatrix = new Matrix4x4(
|
||||||
xScale, 0, 0, 0,
|
xScale, 0, 0, 0,
|
||||||
@@ -179,5 +179,5 @@ public class MonoGameCamera3D : Behaviour, ICamera3D, IFirstFrameUpdate, ILastFr
|
|||||||
|
|
||||||
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
|
public readonly record struct ViewChangedArguments(Matrix4x4 PreviousView);
|
||||||
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
|
public readonly record struct ProjectionChangedArguments(Matrix4x4 PreviousProjection);
|
||||||
public readonly record struct ViewportChangedArguments(Viewport PreviousViewport);
|
public readonly record struct ViewportChangedArguments(Vector2D PreviousViewport);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
using Engine.Core;
|
using Engine.Core;
|
||||||
|
|
||||||
namespace Engine.Integration.MonoGame;
|
namespace Engine.Integration.MonoGame;
|
||||||
@@ -12,6 +14,7 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
|
|||||||
private ISpriteBatch spriteBatch = null!;
|
private ISpriteBatch spriteBatch = null!;
|
||||||
private MonoGameCamera2D camera2D = null!;
|
private MonoGameCamera2D camera2D = null!;
|
||||||
|
|
||||||
|
private readonly RasterizerState rasterizerState = new() { CullMode = CullMode.CullClockwiseFace };
|
||||||
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableSprite> drawableSprites = new(GetPriority(), SortByPriority());
|
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableSprite> drawableSprites = new(GetPriority(), SortByPriority());
|
||||||
|
|
||||||
public void FirstActiveFrame()
|
public void FirstActiveFrame()
|
||||||
@@ -26,7 +29,13 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
|
|||||||
|
|
||||||
public void Draw()
|
public void Draw()
|
||||||
{
|
{
|
||||||
spriteBatch.Begin(transformMatrix: camera2D.ViewMatrix.ToXnaMatrix());
|
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++)
|
for (int i = 0; i < drawableSprites.Count; i++)
|
||||||
drawableSprites[i].Draw(spriteBatch);
|
drawableSprites[i].Draw(spriteBatch);
|
||||||
spriteBatch.End();
|
spriteBatch.End();
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class BehaviourControllerConverter : EngineTypeYamlSerializerBase<IBehaviourController>
|
public class BehaviourControllerConverter : EngineTypeYamlConverterBase<IBehaviourController>
|
||||||
{
|
{
|
||||||
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";
|
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class BehaviourConverter : EngineTypeYamlSerializerBase<IBehaviour>
|
public class BehaviourConverter : EngineTypeYamlConverterBase<IBehaviour>
|
||||||
{
|
{
|
||||||
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public abstract class EngineTypeYamlSerializerBase<T> : IEngineTypeYamlConverter
|
public abstract class EngineTypeYamlConverterBase<T> : IEngineTypeYamlConverter
|
||||||
{
|
{
|
||||||
protected const string SERIALIZED_SCALAR_NAME = "Properties";
|
protected const string SERIALIZED_SCALAR_NAME = "Properties";
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class AABB2DConverter : EngineTypeYamlSerializerBase<AABB2D>
|
public class AABB2DConverter : EngineTypeYamlConverterBase<AABB2D>
|
||||||
{
|
{
|
||||||
public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class AABB3DConverter : EngineTypeYamlSerializerBase<AABB3D>
|
public class AABB3DConverter : EngineTypeYamlConverterBase<AABB3D>
|
||||||
{
|
{
|
||||||
public override AABB3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override AABB3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class CircleConverter : EngineTypeYamlSerializerBase<Circle>
|
public class CircleConverter : EngineTypeYamlConverterBase<Circle>
|
||||||
{
|
{
|
||||||
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSVA).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSV).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGBA).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGB).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Line2DConverter : EngineTypeYamlSerializerBase<Line2D>
|
public class Line2DConverter : EngineTypeYamlConverterBase<Line2D>
|
||||||
{
|
{
|
||||||
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Line2DEquationConverter : EngineTypeYamlSerializerBase<Line2DEquation>
|
public class Line2DEquationConverter : EngineTypeYamlConverterBase<Line2DEquation>
|
||||||
{
|
{
|
||||||
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Line3DConverter : EngineTypeYamlSerializerBase<Line3D>
|
public class Line3DConverter : EngineTypeYamlConverterBase<Line3D>
|
||||||
{
|
{
|
||||||
public override Line3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Line3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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})"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Projection1DConverter : EngineTypeYamlSerializerBase<Projection1D>
|
public class Projection1DConverter : EngineTypeYamlConverterBase<Projection1D>
|
||||||
{
|
{
|
||||||
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Quaternion).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Ray2DConverter : EngineTypeYamlSerializerBase<Ray2D>
|
public class Ray2DConverter : EngineTypeYamlConverterBase<Ray2D>
|
||||||
{
|
{
|
||||||
public override Ray2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Ray2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Ray3DConverter : EngineTypeYamlSerializerBase<Ray3D>
|
public class Ray3DConverter : EngineTypeYamlConverterBase<Ray3D>
|
||||||
{
|
{
|
||||||
public override Ray3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Ray3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Shape2DConverter : EngineTypeYamlSerializerBase<Shape2D>
|
public class Shape2DConverter : EngineTypeYamlConverterBase<Shape2D>
|
||||||
{
|
{
|
||||||
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class Sphere3DConverter : EngineTypeYamlSerializerBase<Sphere3D>
|
public class Sphere3DConverter : EngineTypeYamlConverterBase<Sphere3D>
|
||||||
{
|
{
|
||||||
public override Sphere3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Sphere3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class TriangleConverter : EngineTypeYamlSerializerBase<Triangle>
|
public class TriangleConverter : EngineTypeYamlConverterBase<Triangle>
|
||||||
{
|
{
|
||||||
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2D).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2DInt).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3D).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3DInt).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
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;
|
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector4D).Length + 1;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class SerializedClassConverter : EngineTypeYamlSerializerBase<SerializedClass>
|
public class SerializedClassConverter : EngineTypeYamlConverterBase<SerializedClass>
|
||||||
{
|
{
|
||||||
public override SerializedClass? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override SerializedClass? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class StateEnableConverter : EngineTypeYamlSerializerBase<IStateEnable>
|
public class StateEnableConverter : EngineTypeYamlConverterBase<IStateEnable>
|
||||||
{
|
{
|
||||||
public override IStateEnable? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override IStateEnable? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class TypeContainerConverter : EngineTypeYamlSerializerBase<TypeContainer>
|
public class TypeContainerConverter : EngineTypeYamlConverterBase<TypeContainer>
|
||||||
{
|
{
|
||||||
public override TypeContainer Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override TypeContainer Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class UniverseConverter : EngineTypeYamlSerializerBase<IUniverse>
|
public class UniverseConverter : EngineTypeYamlConverterBase<IUniverse>
|
||||||
{
|
{
|
||||||
public override IUniverse? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override IUniverse? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
|||||||
|
|
||||||
namespace Engine.Serializers.Yaml;
|
namespace Engine.Serializers.Yaml;
|
||||||
|
|
||||||
public class UniverseObjectSerializer : EngineTypeYamlSerializerBase<IUniverseObject>
|
public class UniverseObjectConverter : EngineTypeYamlConverterBase<IUniverseObject>
|
||||||
{
|
{
|
||||||
public override IUniverseObject? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
public override IUniverseObject? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,65 +62,65 @@ public class YamlSerializer : Core.Serialization.ISerializer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public object Deserialize(string configuration)
|
public object Deserialize(string content)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
identifiableRegistry.Reset();
|
identifiableRegistry.Reset();
|
||||||
object result = deserializer.Deserialize(configuration)!;
|
object result = deserializer.Deserialize(content)!;
|
||||||
identifiableRegistry.AssignAll();
|
identifiableRegistry.AssignAll();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public object Deserialize(string configuration, Type type)
|
public object Deserialize(string content, Type type)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
identifiableRegistry.Reset();
|
identifiableRegistry.Reset();
|
||||||
object result = deserializer.Deserialize(configuration, type)!;
|
object result = deserializer.Deserialize(content, type)!;
|
||||||
identifiableRegistry.AssignAll();
|
identifiableRegistry.AssignAll();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Deserialize<T>(string configuration)
|
public T Deserialize<T>(string content)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
identifiableRegistry.Reset();
|
identifiableRegistry.Reset();
|
||||||
T result = deserializer.Deserialize<T>(configuration);
|
T result = deserializer.Deserialize<T>(content);
|
||||||
identifiableRegistry.AssignAll();
|
identifiableRegistry.AssignAll();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProgressiveTask<object> DeserializeAsync(string configuration)
|
public ProgressiveTask<object> DeserializeAsync(string content)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
progressionTracker.Reset();
|
progressionTracker.Reset();
|
||||||
Task<object> task = Task.Run(() => Deserialize(configuration));
|
Task<object> task = Task.Run(() => Deserialize(content));
|
||||||
return new ProgressiveTask<object>(progressionTracker, task);
|
return new ProgressiveTask<object>(progressionTracker, task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProgressiveTask<object> DeserializeAsync(string configuration, Type type)
|
public ProgressiveTask<object> DeserializeAsync(string content, Type type)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
progressionTracker.Reset();
|
progressionTracker.Reset();
|
||||||
Task<object> task = Task.Run(() => Deserialize(configuration, type));
|
Task<object> task = Task.Run(() => Deserialize(content, type));
|
||||||
return new ProgressiveTask<object>(progressionTracker, task);
|
return new ProgressiveTask<object>(progressionTracker, task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProgressiveTask<T> DeserializeAsync<T>(string configuration)
|
public ProgressiveTask<T> DeserializeAsync<T>(string content)
|
||||||
{
|
{
|
||||||
lock (Lock)
|
lock (Lock)
|
||||||
{
|
{
|
||||||
progressionTracker.Reset();
|
progressionTracker.Reset();
|
||||||
Task<T> task = Task.Run(() => Deserialize<T>(configuration));
|
Task<T> task = Task.Run(() => Deserialize<T>(content));
|
||||||
return new ProgressiveTask<T>(progressionTracker, task);
|
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)!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
Engine.Systems/Network/CommonNetworkBehaviour.cs
Normal file
40
Engine.Systems/Network/CommonNetworkBehaviour.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using Engine.Core;
|
||||||
|
|
||||||
|
namespace Engine.Systems.Network;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Basic network behaviour that supports both client & server behaviour. Finds both
|
||||||
|
/// the <see cref="INetworkCommunicatorClient"/> and the <see cref="INetworkCommunicatorServer"/>
|
||||||
|
/// in the universe in it's first active frame. Recommended to use <see cref="ClientBehaviour"/> or <see cref="ServerBehaviour"/>
|
||||||
|
/// <br/>
|
||||||
|
/// Disclaimer: It implements <see cref="IFirstFrameUpdate"/> and <see cref="ILastFrameUpdate"/> in virtual methods.
|
||||||
|
/// </summary>
|
||||||
|
public class CommonNetworkBehaviour : Behaviour, IFirstFrameUpdate, ILastFrameUpdate
|
||||||
|
{
|
||||||
|
protected INetworkCommunicatorServer? server = null!;
|
||||||
|
protected INetworkCommunicatorClient? client = null!;
|
||||||
|
|
||||||
|
protected bool IsServer { get; private set; } = false;
|
||||||
|
protected bool IsClient { get; private set; } = false;
|
||||||
|
|
||||||
|
public INetworkCommunicatorServer Server => server ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorServer)}");
|
||||||
|
public INetworkCommunicatorClient Client => client ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorClient)}");
|
||||||
|
|
||||||
|
public virtual void FirstActiveFrame()
|
||||||
|
{
|
||||||
|
client = Universe.FindBehaviour<INetworkCommunicatorClient>();
|
||||||
|
server = Universe.FindBehaviour<INetworkCommunicatorServer>();
|
||||||
|
|
||||||
|
IsServer = server is not null;
|
||||||
|
IsClient = client is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void LastActiveFrame()
|
||||||
|
{
|
||||||
|
client = null!;
|
||||||
|
server = null!;
|
||||||
|
|
||||||
|
IsServer = false;
|
||||||
|
IsClient = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Engine.Systems/Network/Extensions/CommunicatorExtensions.cs
Normal file
13
Engine.Systems/Network/Extensions/CommunicatorExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Engine.Systems.Network;
|
||||||
|
|
||||||
|
public static class CommunicatorExtensions
|
||||||
|
{
|
||||||
|
public static void SendToClients<T>(this INetworkCommunicatorServer server, IEnumerable<IConnection> connections, T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new()
|
||||||
|
{
|
||||||
|
foreach (IConnection connection in connections)
|
||||||
|
server.SendToClient(connection, packet, packetDelivery);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,8 +27,16 @@ internal class Tween : ITween
|
|||||||
field = value;
|
field = value;
|
||||||
switch (value)
|
switch (value)
|
||||||
{
|
{
|
||||||
case TweenState.Completed: OnCompleted?.Invoke(this); OnEnded?.Invoke(this); break;
|
case TweenState.Completed:
|
||||||
case TweenState.Cancelled: OnCancelled?.Invoke(this); OnEnded?.Invoke(this); break;
|
OnCompleted?.Invoke(this);
|
||||||
|
if (State == TweenState.Completed)
|
||||||
|
OnEnded?.Invoke(this);
|
||||||
|
break;
|
||||||
|
case TweenState.Cancelled:
|
||||||
|
OnCancelled?.Invoke(this);
|
||||||
|
if (State == TweenState.Cancelled)
|
||||||
|
OnEnded?.Invoke(this);
|
||||||
|
break;
|
||||||
case TweenState.Paused: OnPaused?.Invoke(this); break;
|
case TweenState.Paused: OnPaused?.Invoke(this); break;
|
||||||
case TweenState.Playing:
|
case TweenState.Playing:
|
||||||
if (previousState == TweenState.Idle)
|
if (previousState == TweenState.Idle)
|
||||||
|
|||||||
@@ -4,35 +4,48 @@ namespace Engine.Systems.Tween;
|
|||||||
|
|
||||||
public static class TweenExtensions
|
public static class TweenExtensions
|
||||||
{
|
{
|
||||||
|
private static readonly System.Collections.Generic.Dictionary<ITween, int> loopDictionary = [];
|
||||||
|
|
||||||
public static ITween Loop(this ITween tween, int count)
|
public static ITween Loop(this ITween tween, int count)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
if (!loopDictionary.TryAdd(tween, count))
|
||||||
int counter = count;
|
throw new($"Tween already has a loop in progress.");
|
||||||
|
|
||||||
tweenConcrete.OnCompleted.AddListener(_ =>
|
tween.OnCompleted.AddListener(looperDelegate);
|
||||||
{
|
tween.OnEnded.AddListener(looperEndDelegate);
|
||||||
if (counter-- <= 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
tweenConcrete.Reset();
|
|
||||||
tweenConcrete.State = TweenState.Playing;
|
|
||||||
});
|
|
||||||
|
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly Core.Event<ITween>.EventHandler looperEndDelegate = sender => loopDictionary.Remove(sender);
|
||||||
|
private static readonly Core.Event<ITween>.EventHandler looperDelegate = sender =>
|
||||||
|
{
|
||||||
|
int counter = loopDictionary[sender] = loopDictionary[sender] - 1;
|
||||||
|
|
||||||
|
if (counter <= 0)
|
||||||
|
{
|
||||||
|
loopDictionary.Remove(sender);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tween tweenConcrete = (Tween)sender;
|
||||||
|
tweenConcrete.Reset();
|
||||||
|
tweenConcrete.State = TweenState.Playing;
|
||||||
|
};
|
||||||
|
|
||||||
public static ITween LoopInfinitely(this ITween tween)
|
public static ITween LoopInfinitely(this ITween tween)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
tween.OnCompleted.AddListener(repeaterDelegate);
|
||||||
tweenConcrete.OnCompleted.AddListener(_ =>
|
|
||||||
{
|
|
||||||
tweenConcrete.Reset();
|
|
||||||
tweenConcrete.State = TweenState.Playing;
|
|
||||||
});
|
|
||||||
|
|
||||||
return tween;
|
return tween;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly Core.Event<ITween>.EventHandler repeaterDelegate = sender =>
|
||||||
|
{
|
||||||
|
Tween tweenConcrete = (Tween)sender;
|
||||||
|
tweenConcrete.Reset();
|
||||||
|
tweenConcrete.State = TweenState.Playing;
|
||||||
|
};
|
||||||
|
|
||||||
public static ITween Ease(this ITween tween, IEasing easing)
|
public static ITween Ease(this ITween tween, IEasing easing)
|
||||||
{
|
{
|
||||||
Tween tweenConcrete = (Tween)tween;
|
Tween tweenConcrete = (Tween)tween;
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ using Engine.Core;
|
|||||||
|
|
||||||
namespace Engine.Systems.Tween;
|
namespace Engine.Systems.Tween;
|
||||||
|
|
||||||
public class WaitWhileTweenActiveCoroutineYield(ITween tween) : WaitUntilYield(() => tween.State.CheckFlag(TweenState.Completed | TweenState.Cancelled));
|
public class WaitForTweenDoneCoroutineYield(ITween tween) : WaitUntilYield(() => tween.State.CheckFlag(TweenState.Completed | TweenState.Cancelled));
|
||||||
|
|||||||
Reference in New Issue
Block a user