using GarmentButton.PhysicFillters;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using UnityEngine;
namespace GarmentButton.PathFinding.Editor
{
[DisableAutoCreation]
[BurstCompile]
public partial struct GetPointsHeightsSystemEditor : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<PhysicsWorldSingleton>();
state.RequireForUpdate<WorldIndexs>();
}
public void OnDestroy(ref SystemState state)
{
if (SystemAPI.TryGetSingletonRW<WorldIndexs>(out var index))
if (index.ValueRW.Value.IsCreated)
index.ValueRW.Value.Dispose();
if (SystemAPI.TryGetSingletonRW<WorldHeights>(out var height))
if (height.ValueRW.Value.IsCreated)
height.ValueRW.Value.Dispose();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var physic = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
var entity = SystemAPI.GetSingletonEntity<WorldIndexs>();
var worldIndex = SystemAPI.GetSingletonRW<WorldIndexs>();
var lenght = worldIndex.ValueRO.Value.Length;
var heights = new NativeArray<float>(lenght, Allocator.Persistent);
var Direction = new NativeArray<float2>(8, Allocator.Persistent);
Direction[0] = new float2(0, 0.1f);
Direction[1] = new float2(0.1f, 0.1f);
Direction[2] = new float2(0, 0.1f);
Direction[3] = new float2(-0.1f, 0.1f);
Direction[4] = new float2(-0.1f, 0);
Direction[5] = new float2(-0.1f, -0.1f);
Direction[6] = new float2(0, -0.1f);
Direction[7] = new float2(0.1f, -0.1f);
for (int i = 0; i < lenght; i++)
{
var position = worldIndex.ValueRO.Value[i];
var filler = CollisionFilter.Default;
filler.CollidesWith = (uint)CollisionLayer.Ground | (uint)(CollisionLayer.AIGround);
float height = float.MinValue;
for (global::System.Int32 d = 0; d < Direction.Length; d++)
{
var offest = Direction[d];
var ray = new RaycastInput
{
Start = new float3(position.x + offest.x, 100, position.y + offest.y),
End = new float3(position.x + offest.x, -100, position.y + offest.y),
Filter = filler,
};
var raycast = physic.CastRay(ray, out var hit);
if (raycast && hit.Position.y > height)
{
height = hit.Position.y;
Debug.DrawLine(hit.Position, hit.Position + new float3(0, - 5 , 0),Color.red,10);
}
}
if(height == float.MinValue)
height = 0;
heights[i] = height;
}
worldIndex.ValueRW.Value.Dispose();
state.EntityManager.RemoveComponent<WorldIndexs>(entity);
state.EntityManager.AddComponentData(entity, new WorldHeights
{
Value = heights
});
}
}
public struct WorldIndexs : IComponentData
{
public NativeArray<float2> Value;
}
public struct WorldHeights : IComponentData
{
public NativeArray<float> Value;
}
}