private void VisualizeUsingPrefabs(MapGrid grid, MapData data)
    {
        for (int col = 0; col < grid.Width; col++)
        {
            for (int row = 0; row < grid.Length; row++)
            {
                var cell     = grid.GetCell(col, row);
                var position = new Vector3(cell.X, cell.Y, 0);

                var index = grid.CalculateIndexFromCoordinates(position.x, position.y);
                if (data.resourcesArray[index] && !grid.GetCell(cell.X, cell.Y).IsTaken)
                {
                    cell.ObjectType = CellObjectType.Resource;
                }

                switch (cell.ObjectType)
                {
                case CellObjectType.Environment:
                    int randomIndex = Random.Range(0, environmentObjects.Length);
                    CreateIndicator(position, environmentObjects[randomIndex]);
                    break;

                case CellObjectType.Resource:
                    GameObject resourceToSpawn = SelectRandomResourceNode();
                    CreateIndicator(position, resourceToSpawn);
                    break;

                default:
                    break;
                }
            }
        }
    }
        private static VertexPosition[] FindNeighboursFor(VertexPosition currentVertex, MapGrid grid, bool[] obstaclesArray)
        {
            VertexPosition[] arrayOfNeighbours = new VertexPosition[4];

            int arrayIndex = 0;

            foreach (var possibleNeihgbour in VertexPosition.possibleNeighbours)
            {
                Vector3 position = new Vector3(currentVertex.X + possibleNeihgbour.x, 0, currentVertex.Z + possibleNeihgbour.y);
                if (grid.IsCellValid(position.x, position.z))
                {
                    int index = grid.CalculateIndexFromCoordinates(position.x, position.z);
                    arrayOfNeighbours[arrayIndex] = new VertexPosition(position, obstaclesArray[index]);
                    arrayIndex++;
                }
            }
            return(arrayOfNeighbours);
        }
Esempio n. 3
0
    private void VisualizeUsingPrefabs(MapGrid grid, MapData data)
    {
        for (int i = 0; i < data.path.Count; i++)
        {
            var position = data.path[i];
            if (position != data.exitPosition)
            {
                grid.SetCell(position.x, position.z, CellObjectType.Road);
            }
        }


        int           environmentIndex = Random.Range(0, environmentArray.Count);
        EnvironmentSO environment      = environmentArray[environmentIndex];

        int enemyCount       = data.enemyCount;
        int actualEnemyCount = 0;

        for (int col = 0; col < grid.Width; col++)
        {
            for (int row = 0; row < grid.Length; row++)
            {
                var cell     = grid.GetCell(col, row);
                var position = new Vector3(cell.X, 0, cell.Z);

                var index = grid.CalculateIndexFromCoordinates(position.x, position.z);
                if (data.obstacleArray[index] && cell.IsTaken == false)
                {
                    cell.ObjectType = CellObjectType.Obstacle;
                }

                switch (cell.ObjectType)
                {
                case CellObjectType.Empty:
                    int decDice = Random.Range(0, 2);
                    if (decDice == 1)
                    {
                        CreateCharacter(position, environment.decorations); // Decorations
                    }
                    CreateIndicator(position, environment.empty);           // Base
                    break;

                case CellObjectType.Road:
                    CreateIndicator(position, environment.empty);        // Base
                    break;

                case CellObjectType.Obstacle:
                    bool createCharacter = false;
                    if (enemyCount > 0)
                    {
                        int dice = Random.Range(0, 2);
                        if (dice == 1)
                        {
                            createCharacter = true;
                        }
                    }
                    if (createCharacter || actualEnemyCount == 0)
                    {
                        GameObject enemy;
                        if (actualEnemyCount == 0 && (PlayerPrefs.GetInt("Level") == PlayerPrefs.GetInt("MaxLevel")))
                        {
                            // Enemy
                            int bossIndex = Random.Range(0, bossPrefabs.Count);
                            enemy = CreateCharacter(position, bossPrefabs[bossIndex]);
                        }
                        else
                        {
                            // Enemy
                            int enemyIndex = Random.Range(0, enemyPrefabs.Count);
                            enemy = CreateCharacter(position, enemyPrefabs[enemyIndex]);
                        }
                        enemy.transform.SetParent(enemies.transform);

                        // Base
                        CreateIndicator(position, environment.empty);

                        // UI following enemy
                        var healthBar = Instantiate(enemyHealthBar, Vector3.zero, new Quaternion());
                        healthBar.transform.SetParent(followCanvas.transform);
                        healthBar.GetComponent <UiFollowWorld>().lookAt = enemy.transform;
                        enemy.GetComponent <EnemyUiController>().Initialization(healthBar);

                        enemyAnimators.Add(enemy.GetComponent <Animator>());

                        // Enemy count
                        actualEnemyCount++;
                        enemyCount--;
                    }
                    else
                    {
                        CreateIndicator(position, environment.empty);           // Base
                        CreateCharacter(position, environment.obstacles);       // Obstacle
                    }
                    break;

                case CellObjectType.Start:
                    CreateCharacter(new Vector3(position.x, position.y + 0.5f, position.z), environment.startSymbol); // Symbol
                    CreateIndicator(position, environment.start);                                                     // Base

                    GameObject.FindGameObjectWithTag("Player").transform.position = new Vector3(position.x, position.y + 1f, position.z);
                    break;

                case CellObjectType.Exit:
                    CreateCharacter(new Vector3(position.x, position.y + 0.5f, position.z), environment.exitSymbol); // Symbol

                    var exit = CreateIndicator(position, environment.exit);                                          // Player
                    exit.AddComponent <ExitPointController>();
                    break;

                default:
                    break;
                }
            }
        }

        GameObject.FindGameObjectWithTag("LevelController").GetComponent <LevelController>().ActualEnemyCount = actualEnemyCount;
    }
    private void VisualizeUsingPrefabs(MapGrid grid, MapData data)
    {
        for (int i = 0; i < data.path.Count; i++)
        {
            var position = data.path[i];
            if (position != data.exitPosition)
            {
                grid.SetCell(position.x, position.z, CellObjectType.Road);
            }
        }
        for (int col = 0; col < grid.Width; col++)
        {
            for (int row = 0; row < grid.Length; row++)
            {
                var cell     = grid.GetCell(col, row);
                var position = new Vector3(cell.X, 0, cell.Z);

                var index = grid.CalculateIndexFromCoordinates(position.x, position.z);
                if (data.obstacleArray[index] && cell.IsTaken == false)
                {
                    cell.ObjectType = CellObjectType.Obstacle;
                }
                Direction previousDirection = Direction.None;
                Direction nextDirection     = Direction.None;
                switch (cell.ObjectType)
                {
                case CellObjectType.Empty:
                    CreateIndicator(position, tileEmpty);
                    break;

                case CellObjectType.Road:
                    if (data.path.Count > 0)
                    {
                        previousDirection = GetDirectionOfPreviousCell(position, data);
                        nextDirection     = GetDirectionOfNextCell(position, data);
                    }
                    if (previousDirection == Direction.Up && nextDirection == Direction.Right || previousDirection == Direction.Right && nextDirection == Direction.Up)
                    {
                        CreateIndicator(position, roadTileCorner, Quaternion.Euler(0, 90, 0));
                    }
                    else if (previousDirection == Direction.Right && nextDirection == Direction.Down || previousDirection == Direction.Down && nextDirection == Direction.Right)
                    {
                        CreateIndicator(position, roadTileCorner, Quaternion.Euler(0, 180, 0));
                    }
                    else if (previousDirection == Direction.Down && nextDirection == Direction.Left || previousDirection == Direction.Left && nextDirection == Direction.Down)
                    {
                        CreateIndicator(position, roadTileCorner, Quaternion.Euler(0, -90, 0));
                    }
                    else if (previousDirection == Direction.Left && nextDirection == Direction.Up || previousDirection == Direction.Up && nextDirection == Direction.Left)
                    {
                        CreateIndicator(position, roadTileCorner);
                    }
                    else if (previousDirection == Direction.Right && nextDirection == Direction.Left || previousDirection == Direction.Left && nextDirection == Direction.Right)
                    {
                        CreateIndicator(position, roadStraight, Quaternion.Euler(0, 90, 0));
                    }
                    else
                    {
                        CreateIndicator(position, roadStraight);
                    }
                    break;

                case CellObjectType.Obstacle:
                    int randomIndex = Random.Range(0, environmentTiles.Length);
                    CreateIndicator(position, environmentTiles[randomIndex]);
                    break;

                case CellObjectType.Start:
                    if (data.path.Count > 0)
                    {
                        nextDirection = GetDirectionFromVectors(data.path[0], position);
                    }
                    if (nextDirection == Direction.Right || nextDirection == Direction.Left)
                    {
                        CreateIndicator(position, startTile, Quaternion.Euler(0, 90, 0));
                    }
                    else
                    {
                        CreateIndicator(position, startTile);
                    }
                    break;

                case CellObjectType.Exit:
                    if (data.path.Count > 0)
                    {
                        previousDirection = GetDirectionOfPreviousCell(position, data);
                    }
                    switch (previousDirection)
                    {
                    case Direction.Right:
                        CreateIndicator(position, exitTile, Quaternion.Euler(0, 90, 0));
                        break;

                    case Direction.Left:
                        CreateIndicator(position, exitTile, Quaternion.Euler(0, -90, 0));
                        break;

                    case Direction.Down:
                        CreateIndicator(position, exitTile, Quaternion.Euler(0, 180, 0));
                        break;

                    default:
                        CreateIndicator(position, exitTile);
                        break;
                    }

                    break;

                default:
                    break;
                }
            }
        }
    }