experiments-monogame/Inky/Inky/Skeletron/Shape.cs

70 lines
2.0 KiB
C#

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Nez;
namespace Inky.Skeletron
{
public enum Edge
{
Top,
Bottom,
Left,
Right
}
public class EdgeAttachment
{
public Edge OnEdge;
public float XOffset;
public float YOffset;
public Shape Shape;
}
public abstract class Shape
{
public abstract int HalfHeight { get; }
public abstract int HalfWidth { get; }
private List<EdgeAttachment> _attachments;
protected Shape()
{
_attachments = new List<EdgeAttachment>();
}
public abstract void Draw(Batcher batcher, float x, float y, Color color);
public EdgeAttachment Attach(Edge on, Shape shape, float xOffset = 0, float yOffset = 0)
{
var ea = new EdgeAttachment() { OnEdge = on, Shape = shape, XOffset = xOffset, YOffset = yOffset};
_attachments.Add(ea);
return ea;
}
protected void DrawChildren(Batcher batcher, float x, float y, Color color)
{
foreach (var att in _attachments)
{
var childX = x;
var childY = y;
switch (att.OnEdge)
{
case Edge.Top:
childY -= att.Shape.HalfHeight + HalfHeight;
break;
case Edge.Bottom:
childY += att.Shape.HalfHeight + HalfHeight;
break;
case Edge.Left:
childX -= att.Shape.HalfWidth + HalfWidth;
break;
case Edge.Right:
childX += att.Shape.HalfWidth + HalfWidth;
break;
}
att.Shape.Draw(batcher, childX + att.XOffset * HalfWidth, childY + att.YOffset * HalfHeight, color);
}
}
}
}