Engine-Pong/Game/Behaviours/ShapeBehaviour.cs

50 lines
1.7 KiB
C#
Raw Normal View History

2024-01-24 12:17:21 +03:00
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Primitives;
namespace Pong.Behaviours;
public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
2024-01-24 18:40:12 +03:00
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
2024-01-24 12:17:21 +03:00
public ShapeBehaviour(Shape Shape) { this.Shape = Shape; }
public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; }
public ShapeBehaviour(Shape Shape, Color color) { this.Shape = Shape; Color = color; }
public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; Color = color; }
public Shape Shape { get; } = default!;
public Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f;
public void Draw(ShapeBatch shapeBatch)
{
Shape.TransformShape(GameObject.Transform, vectors);
2024-01-24 18:40:12 +03:00
for (int i = 0; i < vectors.Count; i++)
vectors[i] = vectors[i].Scale(screenScale);
2024-01-24 12:17:21 +03:00
for (int i = 0; i < vectors.Count - 1; i++)
2024-01-24 18:40:12 +03:00
shapeBatch.DrawLine(vectors[i].ToVector2(), vectors[i + 1].ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[0].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color);
2024-01-24 12:17:21 +03:00
}
}
public static class ShapeTransform
{
public static void TransformShape(this Shape shape, ITransform transform, List<Vector2D> vectors)
{
vectors.Clear();
int count = shape.Vertices.Count;
for (int i = 0; i < count; i++)
2024-01-24 18:40:12 +03:00
vectors.Add(transform.TransformVector2D(shape[i]));
2024-01-24 12:17:21 +03:00
}
}