private void _CreateTerrains()
    {
        EntityManager   entityManager = World.Active.EntityManager;
        EntityArchetype obsticleType  = entityManager.CreateArchetype(
            typeof(TerrainComponent),
            typeof(Translation),
            typeof(RenderMesh),
            typeof(LocalToWorld)
            );
        EntityArchetype groundType = entityManager.CreateArchetype(
            typeof(Translation),
            typeof(RenderMesh),
            typeof(LocalToWorld)
            );

        for (int x = 0; x < 100; x++)
        {
            for (int y = 0; y < 100; y++)
            {
                Enum.TerrainType type   = Enum.TerrainType.None;
                float            height = 1;

                var random = Random.Range(0, 100);
                if (random > 98)
                {
                    type   = Enum.TerrainType.Tree;
                    height = Random.Range(3f, 4f);
                }
                else if (random > 96)
                {
                    type   = Enum.TerrainType.Rock;
                    height = Random.Range(2f, 3f);
                }

                for (int i = 0; i < 3; i++)
                {
                    var    thisType = i > 0 ? Enum.TerrainType.None : type;
                    Entity entity   = entityManager.CreateEntity(thisType == Enum.TerrainType.None ? groundType : obsticleType);
                    entityManager.SetComponentData(entity, new Translation {
                        Value = new float3(x, -4.5f + height + i, y)
                    });
                    Material material;
                    if (type == Enum.TerrainType.Rock)
                    {
                        material = GroundMaterials[3];
                    }
                    else if (type == Enum.TerrainType.Tree)
                    {
                        material = GroundMaterials[4];
                    }
                    else
                    {
                        material = GroundMaterials[Mathf.FloorToInt(height + i - 1)];
                    }

                    entityManager.SetSharedComponentData(entity, new RenderMesh {
                        mesh = TerrainMesh, material = material
                    });
                    if (thisType != Enum.TerrainType.None)
                    {
                        entityManager.SetComponentData(entity, new TerrainComponent
                        {
                            Type   = type,
                            Height = height,
                        });
                    }
                }
            }
        }
    }
Beispiel #2
0
    private void CreateMaze(Vector3 playerPosition)
    {
        // Clear old Walls
        Entities.ForEach((Entity wall, ref TerrainComponent terrainComponent) =>
        {
            PostUpdateCommands.DestroyEntity(wall);
        });

        bool isPassed = false;

        MazeGeneration.Maze mazeData = null;
        while (!isPassed)
        {
            // Create Maze
            mazeData = new MazeGeneration.Maze(25, 25);
            mazeData.WallToSquare();

            int playerPositionX = Mathf.RoundToInt(playerPosition.x);
            int playerPositionY = Mathf.RoundToInt(playerPosition.z);

            isPassed = !mazeData.squaredCells[playerPositionX, playerPositionY];

            playerPosition = new Vector3(playerPositionX, playerPosition.y, playerPositionY);
        }

        // Create Entity
        EntityManager   entityManager = World.Active.EntityManager;
        EntityArchetype obsticleType  = entityManager.CreateArchetype(
            typeof(TerrainComponent),
            typeof(Translation),
            typeof(RenderMesh),
            typeof(LocalToWorld)
            );

        List <Vector2> moveablePosition = new List <Vector2>();

        for (int x = 0; x < 51; x++)
        {
            for (int y = 0; y < 51; y++)
            {
                Enum.TerrainType type = Enum.TerrainType.None;

                if (!mazeData.squaredCells[x, y])
                {
                    if (x % 2 == 1 && y % 2 == 1)
                    {
                        moveablePosition.Add(new Vector2(x, y));
                    }
                    continue;
                }

                Entity entity = entityManager.CreateEntity(obsticleType);
                entityManager.SetComponentData(entity, new Translation {
                    Value = new float3(x, 0, y)
                });
                Material material = GameManager.GetInstance().GroundMaterials[1];

                entityManager.SetSharedComponentData(entity, new RenderMesh {
                    mesh = GameManager.GetInstance().TerrainMesh, material = material
                });
            }
        }

        // Create new treasure
        Entity treasure = entityManager.CreateEntity(entityManager.CreateArchetype(
                                                         typeof(TreasureComponent),
                                                         typeof(Translation),
                                                         typeof(RenderMesh),
                                                         typeof(LocalToWorld)
                                                         ));

        var treasurePosition   = moveablePosition[Random.Range(0, moveablePosition.Count)];
        var treasurePositionV3 = new float3(treasurePosition.x, 0, treasurePosition.y);

        entityManager.SetComponentData(treasure, new Translation {
            Value = treasurePositionV3
        });
        Material material2 = GameManager.GetInstance().GroundMaterials[2];

        entityManager.SetSharedComponentData(treasure,
                                             new RenderMesh {
            mesh = GameManager.GetInstance().PlayerMesh, material = material2
        });

        GameManager.GetInstance().CreateNavMeshObstacles(mazeData);
        GameManager.GetInstance().NavMeshAgent.transform.position = playerPosition;
        GameManager.GetInstance().NavMeshAgent.SetDestination(treasurePositionV3);
    }