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.

124 lines
4.7 KiB
C#

2 years ago
using System.Collections;
using UnityEngine;
namespace Game
{
public abstract partial class Util
{
public static void Draw(Vector3 start, Vector3 end, Color color, float duration = 0)
{
if (!GameSetting.IsDebugDraw)
{
return;
}
2 years ago
Debug.DrawLine(start, end, color, duration);
}
public static void DrawBox(Vector3 center, Vector3 halfExtents, Vector3 direction, Color color,
float duration = 0)
2 years ago
{
if (!GameSetting.IsDebugDraw)
{
return;
}
var rot = Quaternion.FromToRotation(Vector3.right, direction);
2 years ago
var boxPoints = new Vector3[8];
boxPoints[0] = center + rot * new Vector3(halfExtents.x, halfExtents.y, halfExtents.z);
boxPoints[1] = center + rot * new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z);
boxPoints[2] = center + rot * new Vector3(halfExtents.x, -halfExtents.y, halfExtents.z);
boxPoints[3] = center + rot * new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z);
boxPoints[4] = center + rot * new Vector3(-halfExtents.x, halfExtents.y, halfExtents.z);
boxPoints[5] = center + rot * new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z);
boxPoints[6] = center + rot * new Vector3(-halfExtents.x, -halfExtents.y, halfExtents.z);
boxPoints[7] = center + rot * new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z);
for (int i = 0; i < 8; i++)
{
for (int j = i; j < 8; j++)
{
var p1 = boxPoints[i];
var p2 = boxPoints[j];
int defCount = 0;
defCount += (p1.x == p2.x ? 1 : 0);
defCount += (p1.y == p2.y ? 1 : 0);
defCount += (p1.z == p2.z ? 1 : 0);
Debug.DrawLine(boxPoints[i], boxPoints[j], color, duration);
}
}
}
public static void DrawShape(Shape shape, Vector3 pos, Vector3 direction, Color color)
{
var posReal = pos + shape.Offset;
var posRealTop = posReal + Vector3.up * shape.Height;
switch (shape.Type)
{
case EShapeType.Dot:
Debug.DrawLine(posReal + Vector3.left, posReal + Vector3.right, color);
Debug.DrawLine(posReal + Vector3.down, posReal + Vector3.up, color);
Debug.DrawLine(posReal + Vector3.back, posReal + Vector3.forward, color);
break;
case EShapeType.Box:
shape.ForeachVerticesPair(direction, (v1, v2) =>
{
Debug.DrawLine(posReal + v1, posReal + v2, color);
Debug.DrawLine(posRealTop + v1, posRealTop + v2, color);
});
shape.ForeachVertices(direction, v => { Debug.DrawLine(posReal + v, posRealTop + v, color); });
break;
case EShapeType.Circle:
var r = shape.Radius;
var h = shape.Height;
Debug.DrawLine(posReal + Vector3.left * r, posReal + Vector3.right * r, color);
Debug.DrawLine(posReal + Vector3.back * r, posReal + Vector3.forward * r, color);
Debug.DrawLine(posRealTop + Vector3.left * r, posRealTop + Vector3.right * r, color);
Debug.DrawLine(posRealTop + Vector3.back * r, posRealTop + Vector3.forward * r, color);
Debug.DrawLine(posReal, posRealTop, color);
break;
}
}
2 years ago
public static void Print(params object[] values)
{
string text = "";
foreach (object obj in values)
{
text += Str(obj) + " ";
}
2 years ago
Debug.Log(text);
}
2 years ago
private static string Str(object obj)
{
string ret = "";
if (obj is null)
{
ret = $"null";
}
else if (obj is string)
{
ret = $"\"{obj}\"";
}
else if (obj is IEnumerable)
{
ret += "List[";
foreach (object subobj in (IEnumerable)obj)
{
ret += Str(subobj) + " ";
}
2 years ago
ret += "]";
}
else
{
ret = obj.ToString();
}
2 years ago
return ret;
}
}
}