71 lines
1.8 KiB
C#
71 lines
1.8 KiB
C#
using Unity.Mathematics;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public struct TileRect
|
|
{
|
|
public static readonly TileRect Empty = new(int2.zero, int2.zero);
|
|
|
|
public static readonly TileRect Everything = new(int.MinValue, int.MaxValue);
|
|
|
|
private int2 _min;
|
|
|
|
private int2 _max;
|
|
|
|
public int2 Min
|
|
{
|
|
readonly get => _min;
|
|
set
|
|
{
|
|
_min = value;
|
|
_max = math.max(_min, _max);
|
|
}
|
|
}
|
|
|
|
public int2 Max
|
|
{
|
|
readonly get => _max;
|
|
set
|
|
{
|
|
_max = value;
|
|
_min = math.min(_min, _max);
|
|
}
|
|
}
|
|
|
|
public int Width => _max.x - _min.x + 1;
|
|
|
|
public int Height => _max.y - _min.y + 1;
|
|
|
|
public int2 Center => (_min + _max) / 2;
|
|
|
|
public TileRect(int2 min, int2 max)
|
|
{
|
|
_min = math.min(min, max);
|
|
_max = math.max(min, max);
|
|
}
|
|
|
|
public TileRect(int2 min, int width, int height) : this(min, min + new int2(width - 1, height - 1))
|
|
{
|
|
}
|
|
|
|
public readonly bool Contains(int2 tile)
|
|
{
|
|
return tile.x >= Min.x && tile.x <= Max.x && tile.y >= Min.y && tile.y <= Max.y;
|
|
}
|
|
|
|
public readonly bool Intersects(TileRect other)
|
|
{
|
|
return !(other.Max.x < Min.x || other.Min.x > Max.x || other.Max.y < Min.y || other.Min.y > Max.y);
|
|
}
|
|
|
|
public readonly TileRect Inflate(int amount)
|
|
{
|
|
return amount == int.MaxValue ? Everything : new TileRect(Min - amount, Max + amount);
|
|
}
|
|
|
|
public static TileRect OneTile(int2 tile)
|
|
{
|
|
return new TileRect(tile, 1, 1);
|
|
}
|
|
}
|
|
} |