diff --git a/Engine.Core/Primitives/AABB.cs b/Engine.Core/Primitives/AABB.cs deleted file mode 100644 index 15a2767..0000000 --- a/Engine.Core/Primitives/AABB.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Engine.Core; - -/// -/// Represents an Axis-Aligned Bounding Box (AABB) in 2D space. -/// -/// The lower boundary of the . -/// The upper boundary of the . -/// -/// Initializes a new instance of the struct with the specified lower and upper boundaries. -/// -[System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")] -public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) : IEquatable -{ - /// - /// The lower boundary of the . - /// - public readonly Vector2D LowerBoundary = lowerBoundary; - - /// - /// The upper boundary of the . - /// - public readonly Vector2D UpperBoundary = upperBoundary; - - /// - /// Gets the center point of the . - /// - public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f; - - /// - /// Gets the size of the . - /// - public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs(); - - /// - /// Gets half the size of the . - /// - public readonly Vector2D SizeHalf => Size * .5f; - - public static bool operator ==(AABB left, AABB right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary; - public static bool operator !=(AABB left, AABB right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary; - - /// - /// Creates an from a collection of s. - /// - /// The collection of s. - /// An that bounds all the s. - public static AABB FromVectors(IEnumerable vectors) - { - int counter = 0; - - Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue); - Vector2D upperBoundary = new(float.MinValue, float.MinValue); - - foreach (Vector2D vector in vectors) - { - lowerBoundary = Vector2D.Min(lowerBoundary, vector); - upperBoundary = Vector2D.Max(upperBoundary, vector); - counter++; - } - - if (counter < 2) - throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items."); - - return new(lowerBoundary, upperBoundary); - } - - /// - /// Checks if two s are approximately equal. - /// - /// The first . - /// The second . - /// The epsilon range. - /// if the s are approximately equal; otherwise, . - public static bool ApproximatelyEquals(AABB left, AABB right, float epsilon = float.Epsilon) - => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon); - - /// - /// Determines whether the specified object is equal to the current . - /// - /// The object to compare with the current . - /// if the specified object is equal to the current ; otherwise, . - public override bool Equals(object? obj) => obj is AABB aabb && this == aabb; - public bool Equals(AABB other) => this == other; - - /// - /// Generates a hash code for the . - /// - /// A hash code for the . - public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary); - - /// - /// Converts the to its string representation. - /// - /// A string representation of the . - public override string ToString() => $"{nameof(AABB)}({LowerBoundary}, {UpperBoundary})"; -} - -/// -/// Provides extension methods for the struct. -/// -public static class AABBExtensions -{ - /// - public static AABB ToAABB(this IEnumerable vectors) => AABB.FromVectors(vectors); - - /// - public static bool ApproximatelyEquals(this AABB left, AABB right, float epsilon = float.Epsilon) => AABB.ApproximatelyEquals(left, right, epsilon); -} diff --git a/Engine.Core/Primitives/AABB2D.cs b/Engine.Core/Primitives/AABB2D.cs new file mode 100644 index 0000000..cddfa74 --- /dev/null +++ b/Engine.Core/Primitives/AABB2D.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; + +namespace Engine.Core; + +/// +/// Represents an Axis-Aligned Bounding Box (AABB) in 2D space. +/// +/// The lower boundary of the . +/// The upper boundary of the . +/// +/// Initializes a new instance of the struct with the specified lower and upper boundaries. +/// +[System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")] +public readonly struct AABB2D(Vector2D lowerBoundary, Vector2D upperBoundary) : IEquatable +{ + /// + /// The lower boundary of the . + /// + public readonly Vector2D LowerBoundary = lowerBoundary; + + /// + /// The upper boundary of the . + /// + public readonly Vector2D UpperBoundary = upperBoundary; + + /// + /// Gets the center point of the . + /// + public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f; + + /// + /// Gets the size of the . + /// + public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs(); + + /// + /// Gets half the size of the . + /// + public readonly Vector2D SizeHalf => Size * .5f; + + public static bool operator ==(AABB2D left, AABB2D right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary; + public static bool operator !=(AABB2D left, AABB2D right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary; + + /// + /// Creates an from a collection of s. + /// + /// The collection of s. + /// An that bounds all the s. + public static AABB2D FromVectors(IEnumerable vectors) + { + int counter = 0; + + Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue); + Vector2D upperBoundary = new(float.MinValue, float.MinValue); + + foreach (Vector2D vector in vectors) + { + lowerBoundary = Vector2D.Min(lowerBoundary, vector); + upperBoundary = Vector2D.Max(upperBoundary, vector); + counter++; + } + + if (counter < 2) + throw new ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items."); + + return new(lowerBoundary, upperBoundary); + } + + /// + /// Checks if two s are approximately equal. + /// + /// The first . + /// The second . + /// The epsilon range. + /// if the s are approximately equal; otherwise, . + public static bool ApproximatelyEquals(AABB2D left, AABB2D right, float epsilon = float.Epsilon) + => left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon); + + /// + /// Determines whether the specified object is equal to the current . + /// + /// The object to compare with the current . + /// if the specified object is equal to the current ; otherwise, . + public override bool Equals(object? obj) => obj is AABB2D aabb && this == aabb; + public bool Equals(AABB2D other) => this == other; + + /// + /// Generates a hash code for the . + /// + /// A hash code for the . + public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary); + + /// + /// Converts the to its string representation. + /// + /// A string representation of the . + public override string ToString() => $"{nameof(AABB2D)}({LowerBoundary}, {UpperBoundary})"; +} + +/// +/// Provides extension methods for the struct. +/// +public static class AABBExtensions +{ + /// + public static AABB2D ToAABB(this IEnumerable vectors) => AABB2D.FromVectors(vectors); + + /// + public static bool ApproximatelyEquals(this AABB2D left, AABB2D right, float epsilon = float.Epsilon) => AABB2D.ApproximatelyEquals(left, right, epsilon); +} diff --git a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs index 7691103..12113e0 100644 --- a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs +++ b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs @@ -143,7 +143,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private void SetupEnginePackets() { // I know, ugly af. I need to find a better way - netPacketProcessor.RegisterNestedType(AABBNetPacker.Write, AABBNetPacker.Read); + netPacketProcessor.RegisterNestedType(AABB2DNetPacker.Write, AABB2DNetPacker.Read); netPacketProcessor.RegisterNestedType(CircleNetPacker.Write, CircleNetPacker.Read); netPacketProcessor.RegisterNestedType(ColorHSVNetPacker.Write, ColorHSVNetPacker.Read); netPacketProcessor.RegisterNestedType(ColorRGBANetPacker.Write, ColorRGBANetPacker.Read); diff --git a/Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABBNetPacker.cs b/Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABB2DNetPacker.cs similarity index 62% rename from Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABBNetPacker.cs rename to Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABB2DNetPacker.cs index 0af57aa..ef3297f 100644 --- a/Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABBNetPacker.cs +++ b/Engine.Integration/Engine.Integration.LiteNetLib/Packers/AABB2DNetPacker.cs @@ -4,19 +4,19 @@ using Engine.Core; namespace Engine.Systems.Network; -internal static class AABBNetPacker +internal static class AABB2DNetPacker { - internal static void Write(NetDataWriter writer, AABB data) + internal static void Write(NetDataWriter writer, AABB2D data) { Vector2DNetPacker.Write(writer, data.LowerBoundary); Vector2DNetPacker.Write(writer, data.UpperBoundary); } - internal static AABB Read(NetDataReader reader) + internal static AABB2D Read(NetDataReader reader) { Vector2D lowerBoundary = Vector2DNetPacker.Read(reader); Vector2D upperBoundary = Vector2DNetPacker.Read(reader); - return new AABB(lowerBoundary, upperBoundary); + return new AABB2D(lowerBoundary, upperBoundary); } } diff --git a/Engine.Integration/Engine.Integration.MonoGame/Abstract/ISpriteBatch.cs b/Engine.Integration/Engine.Integration.MonoGame/Abstract/ISpriteBatch.cs index 0939141..f79ad46 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Abstract/ISpriteBatch.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/Abstract/ISpriteBatch.cs @@ -10,13 +10,13 @@ namespace Engine.Integration.MonoGame; public interface ISpriteBatch { void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null); - void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth); - void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth); - void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth); - void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color); - void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color); + void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth); + void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth); + void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth); + void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color); + void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color); void Draw(Texture2D texture, Vector2D position, Color color); - void Draw(Texture2D texture, AABB destinationAABB, Color color); + void Draw(Texture2D texture, AABB2D destinationAABB, Color color); void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color); void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth); void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth); diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatchWrapper.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatchWrapper.cs index 7e40376..8940127 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatchWrapper.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatchWrapper.cs @@ -14,25 +14,25 @@ public class SpriteBatchWrapper(GraphicsDevice graphicsDevice) : ISpriteBatch public void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null) => spriteBatch.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); - public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth) + 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); - public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float 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); - public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float 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); - public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color) + public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color) => spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color); - public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color 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); - public void Draw(Texture2D texture, AABB destinationAABB, Color 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) diff --git a/Engine.Integration/Engine.Integration.MonoGame/EngineConverter.cs b/Engine.Integration/Engine.Integration.MonoGame/EngineConverter.cs index 8a3e140..32e48d1 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/EngineConverter.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/EngineConverter.cs @@ -60,7 +60,7 @@ public static class EngineConverterExtensions }; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Rectangle ToRectangle(this AABB aabb) => new() + public static Rectangle ToRectangle(this AABB2D aabb) => new() { X = (int)(aabb.LowerBoundary.X * screenScale.X), Y = (int)(aabb.LowerBoundary.Y * screenScale.Y), diff --git a/Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABBConverter.cs b/Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABB2DConverter.cs similarity index 56% rename from Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABBConverter.cs rename to Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABB2DConverter.cs index d09150d..1c2a967 100644 --- a/Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABBConverter.cs +++ b/Engine.Integration/Engine.Integration.Yaml/Converters/Primitives/AABB2DConverter.cs @@ -8,33 +8,33 @@ using YamlDotNet.Serialization; namespace Engine.Serializers.Yaml; -public class AABBConverter : EngineTypeYamlSerializerBase +public class AABB2DConverter : EngineTypeYamlSerializerBase { - public override AABB Read(IParser parser, Type type, ObjectDeserializer rootDeserializer) + public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer) { parser.Consume(); - if (parser.Consume().Value.CompareTo(nameof(AABB.LowerBoundary)) != 0) - throw new ArgumentException($"{nameof(AABB)} mapping must start with {nameof(AABB.LowerBoundary)}"); + if (parser.Consume().Value.CompareTo(nameof(AABB2D.LowerBoundary)) != 0) + throw new ArgumentException($"{nameof(AABB2D)} mapping must start with {nameof(AABB2D.LowerBoundary)}"); Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!; - if (parser.Consume().Value.CompareTo(nameof(AABB.UpperBoundary)) != 0) - throw new ArgumentException($"{nameof(AABB)} mapping must end with {nameof(AABB.UpperBoundary)}"); + if (parser.Consume().Value.CompareTo(nameof(AABB2D.UpperBoundary)) != 0) + throw new ArgumentException($"{nameof(AABB2D)} mapping must end with {nameof(AABB2D.UpperBoundary)}"); Vector2D upperBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!; parser.Consume(); - return new AABB(lowerBoundary, upperBoundary); + return new AABB2D(lowerBoundary, upperBoundary); } public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) { - AABB aabb = (AABB)value!; + AABB2D aabb = (AABB2D)value!; emitter.Emit(new MappingStart()); - emitter.Emit(new Scalar(nameof(AABB.LowerBoundary))); + emitter.Emit(new Scalar(nameof(AABB2D.LowerBoundary))); serializer(aabb.LowerBoundary, typeof(Vector2D)); - emitter.Emit(new Scalar(nameof(AABB.UpperBoundary))); + emitter.Emit(new Scalar(nameof(AABB2D.UpperBoundary))); serializer(aabb.UpperBoundary, typeof(Vector2D)); emitter.Emit(new MappingEnd()); } diff --git a/Engine.Physics2D/Physics2D.cs b/Engine.Physics2D/Physics2D.cs index 1dd6b0b..3908f66 100644 --- a/Engine.Physics2D/Physics2D.cs +++ b/Engine.Physics2D/Physics2D.cs @@ -68,11 +68,11 @@ public static class Physics2D return isOverlapping; } - public static bool Overlaps(this AABB aabb, Vector2D point) + public static bool Overlaps(this AABB2D aabb, Vector2D point) => point.X >= aabb.LowerBoundary.X && point.X <= aabb.UpperBoundary.X && point.Y >= aabb.LowerBoundary.Y && point.Y <= aabb.UpperBoundary.Y; - public static bool Overlaps(this AABB left, AABB right) + public static bool Overlaps(this AABB2D left, AABB2D right) => left.LowerBoundary.X <= right.UpperBoundary.X && left.UpperBoundary.X >= right.LowerBoundary.X && left.LowerBoundary.Y <= right.UpperBoundary.Y && left.UpperBoundary.Y >= right.LowerBoundary.Y; diff --git a/Engine.Systems/Tween/EngineExtensions/TweenAABB2DExtensions.cs b/Engine.Systems/Tween/EngineExtensions/TweenAABB2DExtensions.cs new file mode 100644 index 0000000..df983fb --- /dev/null +++ b/Engine.Systems/Tween/EngineExtensions/TweenAABB2DExtensions.cs @@ -0,0 +1,24 @@ +using Engine.Core; + +namespace Engine.Systems.Tween; + +public static class TweenAABB2DExtensions +{ + private static readonly BoxedPool boxedAABBPool = new(2); + + public static ITween TweenAABB(this AABB2D initialAABB, ITweenManager tweenManager, float duration, AABB2D targetAABB, System.Action setMethod) + { + Boxed boxedInitial = boxedAABBPool.Get(initialAABB); + Boxed boxedTarget = boxedAABBPool.Get(targetAABB); + + ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB2D(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t)))); + + tween.OnComplete(() => + { + boxedAABBPool.Return(boxedInitial); + boxedAABBPool.Return(boxedTarget); + }); + + return tween; + } +} diff --git a/Engine.Systems/Tween/EngineExtensions/TweenAABBExtensions.cs b/Engine.Systems/Tween/EngineExtensions/TweenAABBExtensions.cs deleted file mode 100644 index bfedd4f..0000000 --- a/Engine.Systems/Tween/EngineExtensions/TweenAABBExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Engine.Core; - -namespace Engine.Systems.Tween; - -public static class TweenAABBExtensions -{ - private static readonly BoxedPool boxedAABBPool = new(2); - - public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, System.Action setMethod) - { - Boxed boxedInitial = boxedAABBPool.Get(initialAABB); - Boxed boxedTarget = boxedAABBPool.Get(targetAABB); - - ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t)))); - - tween.OnComplete(() => - { - boxedAABBPool.Return(boxedInitial); - boxedAABBPool.Return(boxedTarget); - }); - - return tween; - } -}