using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Mathematics; using UnityEngine; namespace GarmentButton.PathFinding { [BurstCompile] public class PathGridCreationMono : MonoBehaviour { public Vector2Int Size; public float[] HieghtsPoints; [Header("All Things That Should Be turn On When Baking The Road")] public GameObject[] AssistPlatform; [Header("All Things That Should Be turn off When Baking The Road")] public GameObject[] DisablePlatforms; [Header("Debug")] public GameObject gameObjectText; public bool DebugPath; public bool DebugGrid; public bool WithMouseSelection; private void OnDrawGizmosSelected() { var totalSize = new float2 { x = Size.x * (Global.ElementSize), y = Size.y * (Global.ElementSize), }; var newPosition = transform.position; var halfGridSize = new float3(totalSize.x / 2, 0, totalSize.y / 2); var min = (float3)newPosition - halfGridSize; var max = (float3)newPosition + halfGridSize; DrawBoundingBox(min, max, Color.green); void DrawBoundingBox(Vector3 min, Vector3 max, Color boxColor) { Gizmos.color = boxColor; // Bottom edges Gizmos.DrawLine(new Vector3(min.x, min.y, min.z), new Vector3(max.x, min.y, min.z)); Gizmos.DrawLine(new Vector3(min.x, min.y, min.z), new Vector3(min.x, min.y, max.z)); Gizmos.DrawLine(new Vector3(max.x, min.y, min.z), new Vector3(max.x, min.y, max.z)); Gizmos.DrawLine(new Vector3(min.x, min.y, max.z), new Vector3(max.x, min.y, max.z)); // Top edges Gizmos.DrawLine(new Vector3(min.x, max.y, min.z), new Vector3(max.x, max.y, min.z)); Gizmos.DrawLine(new Vector3(min.x, max.y, min.z), new Vector3(min.x, max.y, max.z)); Gizmos.DrawLine(new Vector3(max.x, max.y, min.z), new Vector3(max.x, max.y, max.z)); Gizmos.DrawLine(new Vector3(min.x, max.y, max.z), new Vector3(max.x, max.y, max.z)); // Vertical edges Gizmos.DrawLine(new Vector3(min.x, min.y, min.z), new Vector3(min.x, max.y, min.z)); Gizmos.DrawLine(new Vector3(min.x, min.y, max.z), new Vector3(min.x, max.y, max.z)); Gizmos.DrawLine(new Vector3(max.x, min.y, min.z), new Vector3(max.x, max.y, min.z)); Gizmos.DrawLine(new Vector3(max.x, min.y, max.z), new Vector3(max.x, max.y, max.z)); //// } } } public class PathGridCreationBaker : Baker { public const int ILIMINATE_FLOAT = 10; public override void Bake(PathGridCreationMono authoring) { //Create entity CreateWithBlobAsset(authoring); } private void CreateWithBlobAsset(PathGridCreationMono authoring) { var expectedLenght = authoring.Size.x * authoring.Size.y; if (expectedLenght != authoring.HieghtsPoints.Length) return; var entity = GetEntity(TransformUsageFlags.None); var totalSize = new float2 { x = authoring.Size.x * Global.ElementSize, y = authoring.Size.y * Global.ElementSize, }; var center = authoring.transform.position; var halfGridSize = new float3(totalSize.x / 2, 0, totalSize.y / 2); var halfCellSize = new float3(Global.ElementSize / 2, 0, Global.ElementSize / 2); NativeList grid = new NativeList(expectedLenght, Allocator.Temp); for (int y = 0; y < authoring.Size.y; y++) { for (int x = 0; x < authoring.Size.x; x++) { var position = new float3(x * (Global.ElementSize), 0, y * (Global.ElementSize)); position -= new float3(halfGridSize); position += new float3(halfCellSize); position += new float3(center.x, 0, center.z); position *= ILIMINATE_FLOAT; grid.Add(new Point { X = (short)position.x, Y = (short)position.y, Z = (short)position.z, }); } } using var blobBuilder = new BlobBuilder(Allocator.Temp); ref var blobArray = ref blobBuilder.ConstructRoot(); var points = blobBuilder.Allocate(ref blobArray.Array, grid.Length); for (int i = 0; i < grid.Length; i++) { var point = grid[i]; point.Y = (short)(authoring.HieghtsPoints[i] * ILIMINATE_FLOAT); points[i] = point; } AddComponent(entity, new Grid { Value = blobBuilder.CreateBlobAssetReference(Allocator.Persistent), }); AddComponent(entity, new GridData { GridSize = new int2(authoring.Size.x, authoring.Size.y), CellSize = Global.ElementSize, Center = new float2(center.x, center.z), }); AddComponent(entity); if (authoring.DebugPath) { AddComponent(entity, new DebugPath()); } if (authoring.DebugGrid) { AddComponent(entity, new DebugObject { GameObject = authoring.gameObjectText, }); AddComponent(entity, new DebugGrid()); } if (authoring.WithMouseSelection) { AddComponent(entity); } } } }