MoonTools.Bonk/Bonk/MultiShape2D.cs

83 lines
2.6 KiB
C#
Raw Permalink Normal View History

2020-01-05 00:13:07 +00:00
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
2020-02-21 08:03:47 +00:00
using MoonTools.Structs;
2020-01-05 00:13:07 +00:00
2020-02-21 08:03:47 +00:00
namespace MoonTools.Bonk
2020-01-05 00:13:07 +00:00
{
2020-09-07 02:38:51 +00:00
public struct MultiShape2D : IHasAABB2D
2020-01-05 00:13:07 +00:00
{
public ImmutableArray<(IShape2D, Transform2D)> ShapeTransformPairs { get; }
2020-09-07 02:38:51 +00:00
public AABB2D AABB { get; }
2020-01-05 00:13:07 +00:00
2020-09-07 02:38:51 +00:00
public MultiShape2D(ImmutableArray<(IShape2D, Transform2D)> shapeTransformPairs)
2020-01-05 00:13:07 +00:00
{
ShapeTransformPairs = shapeTransformPairs;
AABB = AABBFromShapes(shapeTransformPairs);
}
2020-09-07 02:38:51 +00:00
public AABB2D TransformedAABB(Transform2D transform)
2020-01-05 00:13:07 +00:00
{
2020-09-07 02:38:51 +00:00
return AABB2D.Transformed(AABB, transform);
2020-01-05 00:13:07 +00:00
}
/// <summary>
/// Moves the shapes by pivoting with an offset transform.
/// </summary>
/// <param name="offsetTransform"></param>
/// <returns></returns>
public IEnumerable<(IShape2D, Transform2D)> TransformShapesUsingOffset(Transform2D offsetTransform)
2020-01-05 00:13:07 +00:00
{
foreach (var (shape, shapeTransform) in ShapeTransformPairs)
{
var newTransform = new Transform2D(Vector2.Transform(shapeTransform.Position, offsetTransform.TransformMatrix), offsetTransform.Rotation, offsetTransform.Scale);
yield return (shape, newTransform);
2020-01-05 00:13:07 +00:00
}
}
2020-01-05 21:10:52 +00:00
public bool IsSingleShape<T>() where T : struct, IShape2D
{
return ShapeTransformPairs.Length == 1 && ShapeTransformPairs[0].Item1 is T;
}
public (T, Transform2D) ShapeTransformPair<T>() where T : struct, IShape2D
{
return ((T, Transform2D))ShapeTransformPairs[0];
}
2020-09-07 02:38:51 +00:00
private static AABB2D AABBFromShapes(IEnumerable<(IShape2D, Transform2D)> shapeTransforms)
2020-01-05 00:13:07 +00:00
{
var minX = float.MaxValue;
var minY = float.MaxValue;
var maxX = float.MinValue;
var maxY = float.MinValue;
foreach (var (shape, transform) in shapeTransforms)
{
var aabb = shape.TransformedAABB(transform);
if (aabb.Min.X < minX)
{
minX = aabb.Min.X;
}
if (aabb.Min.Y < minY)
{
minY = aabb.Min.Y;
}
if (aabb.Max.X > maxX)
{
maxX = aabb.Max.X;
}
if (aabb.Max.Y > maxY)
{
maxY = aabb.Max.Y;
}
}
2020-09-07 02:38:51 +00:00
return new AABB2D(minX, minY, maxX, maxY);
2020-01-05 00:13:07 +00:00
}
}
}