Protfolio-Emanuel-Polsky / Assets / _Project / Code / PathFinding / Systems / CreationGridPathfindingSystem.cs
CreationGridPathfindingSystem.cs
Raw
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;


namespace GarmentButton.PathFinding
{
    [BurstCompile]
    public partial struct CreationGridPathfindingSystem : ISystem
    {
        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            var query = SystemAPI.QueryBuilder().WithAll<Grid>().WithNone<GridAdditionalData>().Build();
            state.RequireForUpdate(query);
        }

        public void OnDestroy(ref SystemState state)
        {
            if (!SystemAPI.TryGetSingletonRW<GridAdditionalData>(out var data))
                return;

            data.ValueRW.Types.Dispose();
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            var entity = SystemAPI.GetSingletonEntity<Grid>();
            var grid = SystemAPI.GetComponentRO<Grid>(entity);
            var lenght = grid.ValueRO.Value.Value.Array.Length;
            var additionData = new NativeArray<GroundType>(lenght, Allocator.Persistent);
            state.EntityManager.AddComponentData(entity, new GridAdditionalData
            {
                Types = additionData,
            });

            var updatePoints = new NativeArray<ushort>(lenght, Allocator.Persistent);
            for (var i = 0; i < updatePoints.Length; i++)
                updatePoints[i] = (ushort)i;

            state.EntityManager.AddComponentData(entity, new UpdatePoints
            {
                Indexes = updatePoints,
            });

            state.EntityManager.AddComponent<CastTag>(entity);
        }
    }
}