#if UNITY_EDITOR
using TMPro;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using Unity.Collections;
using System.Collections.Generic;
namespace GarmentButton.PathFinding
{
[BurstCompile]
public partial struct DebugPathFindSystem : ISystem
{
public bool Instance;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
var query = SystemAPI.QueryBuilder().WithAll<Grid, GridAdditionalData, DebugObject>().Build();
state.RequireForUpdate(query);
state.RequireForUpdate<DebugGrid>();
Instance = true;
}
public void OnUpdate(ref SystemState state)
{
var grid = SystemAPI.GetSingleton<Grid>().Value;
var entity = SystemAPI.GetSingletonEntity<Grid>();
var additional = SystemAPI.GetSingleton<GridAdditionalData>().Types;
var data = SystemAPI.GetSingleton<DebugObject>();
state.Dependency.Complete();
var list = new List<TextMeshPro>(grid.Value.Array.Length);
for (int i = 0; i < grid.Value.Array.Length; i++)
{
var finalPosition = new float3(grid.Value.Array[i].X, grid.Value.Array[i].Y, grid.Value.Array[i].Z);
var type = additional[i];
DrawDebugBox(finalPosition / 10, (float)Global.ElementSize, GetColor(type));
if (Instance)
continue;
if (data.GameObject == null)
continue;
var gameObject = GameObject.Instantiate(data.GameObject.Value, finalPosition / 10, data.GameObject.Value.transform.rotation);
var text = gameObject.GetComponent<TextMeshPro>();
text.text = $"{(float)grid.Value.Array[i].X / 10},{(float)grid.Value.Array[i].Z / 10}";
list.Add(text);
}
if (Instance)
return;
state.EntityManager.AddComponentData(entity, new DebugTextMesh
{
Text = list.ToArray()
});
Instance = true;
return;
Color GetColor(GroundType type)
{
switch (type)
{
case GroundType.Air:
return Color.green;
case GroundType.NearGround:
return Color.yellow;
case GroundType.Ground:
return Color.blue;
case GroundType.MoveAble:
return Color.yellow;
case GroundType.Blocked:
return Color.red;
default:
return Color.green;
}
}
}
void DrawDebugBox(float3 position, float size, Color color)
{
float halfSize = size / 2;
NativeArray<float3> vertices = new NativeArray<float3>(8, Allocator.Temp);
vertices[0] = position + new float3(-halfSize, -halfSize, -halfSize);
vertices[1] = position + new float3(halfSize, -halfSize, -halfSize);
vertices[2] = position + new float3(halfSize, -halfSize, halfSize);
vertices[3] = position + new float3(-halfSize, -halfSize, halfSize);
vertices[4] = position + new float3(-halfSize, halfSize, -halfSize);
vertices[5] = position + new float3(halfSize, halfSize, -halfSize);
vertices[6] = position + new float3(halfSize, halfSize, halfSize);
vertices[7] = position + new float3(-halfSize, halfSize, halfSize);
// Draw bottom face
Debug.DrawLine(vertices[0], vertices[1], color);
Debug.DrawLine(vertices[1], vertices[2], color);
Debug.DrawLine(vertices[2], vertices[3], color);
Debug.DrawLine(vertices[3], vertices[0], color);
// Draw top face
Debug.DrawLine(vertices[4], vertices[5], color);
Debug.DrawLine(vertices[5], vertices[6], color);
Debug.DrawLine(vertices[6], vertices[7], color);
Debug.DrawLine(vertices[7], vertices[4], color);
// Draw vertical lines
Debug.DrawLine(vertices[0], vertices[4], color);
Debug.DrawLine(vertices[1], vertices[5], color);
Debug.DrawLine(vertices[2], vertices[6], color);
Debug.DrawLine(vertices[3], vertices[7], color);
vertices.Dispose();
}
}
}
#endif