2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.8 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Game
{
public enum EShapeType
{
Dot,
Circle,
Sector,
Box,
}
public class Shape
{
public EShapeType Type;
public Vector3 Offset;
public float Height;
public float SizeX;
public float SizeY;
public float Radius;
public float Angle;
public static Shape NewDot(Vector3 offset)
{
var shape = new Shape
{
Type = EShapeType.Dot,
Offset = offset,
};
return shape;
}
public static Shape NewCircle(Vector3 offset, float height, float radius)
{
var shape = new Shape
{
Type = EShapeType.Circle,
Offset = offset,
Height = height,
Radius = radius
};
return shape;
}
public static Shape NewSector(Vector3 offset, float height, float radius, float angle)
{
var shape = new Shape
{
Type = EShapeType.Sector,
Offset = offset,
Height = height,
Radius = radius,
Angle = angle
};
return shape;
}
public static Shape NewBox(Vector3 offset, float height, float sizeX, float sizeY)
{
Util.Print("offset", offset);
var shape = new Shape
{
Type = EShapeType.Box,
Offset = offset,
Height = height,
SizeX = sizeX,
SizeY = sizeY
};
return shape;
}
public Vector4 GetAABB(Vector3 direction)
{
var aabb = Vector4.zero;
switch (Type)
{
case EShapeType.Circle:
case EShapeType.Sector:
aabb = new Vector4(-Radius, Radius, -Radius, Radius);
break;
case EShapeType.Box:
ForeachVertices(direction, (vertex) =>
{
if (vertex.x < aabb.x) aabb.x = vertex.x;
if (vertex.x > aabb.y) aabb.y = vertex.x;
if (vertex.z < aabb.z) aabb.z = vertex.z;
if (vertex.z > aabb.w) aabb.w = vertex.z;
});
break;
}
return aabb;
}
public void ForeachVertices(Vector3 direction, Action<Vector3> action)
{
var (halfX, halfY) = (SizeX / 2, SizeY / 2);
var up = new Vector3(direction.z, 0, direction.x);
var right = new Vector3(direction.x, 0, -direction.z);
var topLeft = right * halfX - up * halfY;
var topRight = right * halfX + up * halfY;
var bottomLeft = -right * halfX - up * halfY;
var bottomRight = -right * halfX + up * halfY;
action(topLeft);
action(topRight);
action(bottomLeft);
action(bottomRight);
}
public void ForeachVerticesPair(Vector3 direction, Action<Vector3, Vector3> action)
{
var (halfX, halfY) = (SizeX / 2, SizeY / 2);
var up = new Vector3(direction.z, 0, direction.x);
var right = new Vector3(direction.x, 0, -direction.z);
var topLeft = right * halfX - up * halfY;
var topRight = right * halfX + up * halfY;
var bottomLeft = -right * halfX - up * halfY;
var bottomRight = -right * halfX + up * halfY;
action(topLeft, topRight);
action(topRight, bottomRight);
action(bottomRight, bottomLeft);
action(bottomLeft, topLeft);
}
}
}