Exemple #1
0
    public void Initialize(int cx, int cz, int idx, int idz)
    {
        PosX = cx;
        PosZ = cz;

        // 1D array containing all the block id's for the world
        NativeArray <int> blockIndices = new NativeArray <int>(ChunkWidth * ChunkHeight * ChunkWidth, Allocator.Temp);
        // make the terrain, but return number of solid blocks in the world for this chunk
        int solidBlocks = alg.setBlocks(blockIndices, PosX, PosZ);
        // now make an array with just the real blocks and not air
        // also need to keep positions of those blocks and if they're near air so
        // that they should be drawn
        NativeArray <int> solidBlockIndices = new NativeArray <int>(solidBlocks, Allocator.Temp);

        Vector3[] positions = new Vector3[solidBlocks];
        byte[]    nearAir   = new byte[solidBlocks];

        // calculate what should be drawn from the solid block list
        // and put all the solid blocks into the same array
        solidBlocks = 0;
        int width  = ChunkWidth;
        int height = ChunkHeight;

        for (int i = 0; i < blockIndices.Length; i++)
        {
            if (blockIndices[i] != (int)BlockTypes.Air)
            {
                int x = i / (height * width);
                int y = (i - x * height * width) / width;
                int z = i - x * height * width - y * width;

                solidBlockIndices[solidBlocks] = blockIndices[i];
                positions[solidBlocks]         = new Vector3(x, y, z);
                nearAir[solidBlocks]           = checkAir(blockIndices, positions[solidBlocks], idx, idz);                 solidBlocks++;
            }
        }

        int countEntities = 0;
        var entityManager = World.Active.GetOrCreateManager <EntityManager>();
        int totalCount    = solidBlockIndices.Length;

        for (int index = 0; index < totalCount; index++)
        {
            if (nearAir[index] == 1)
            {
                countEntities++;

                float x = positions[index].x;
                float y = positions[index].y;
                float z = positions[index].z;

                // create entity and add position and texture to it
                Entity blockEntity = entityManager.CreateEntity(GameManager.blockArchetype);
                entityManager.SetComponentData(blockEntity, new Position {
                    Value = new float3(x + PosX, y, z + PosZ)
                });
                entityManager.AddSharedComponentData(blockEntity, renderers[solidBlockIndices[index]]);
            }
        }

        //GameManager.Log("number of entities created is: " + countEntities);

        blockIndices.Dispose();
        // we would keep the blocks if we were doing something other than
        // just chucking them into the display system
        solidBlockIndices.Dispose();
    }