Example #1
0
    private void Awake()
    {
        if (numberOfMaps > MAX_COLS * MAX_ROWS)
        {
            numberOfMaps = MAX_COLS * MAX_ROWS;
        }

        if (numberOfMaps < 1)
        {
            numberOfMaps = 1;
        }

        battleMapList_ = new BattleMap[numberOfMaps];
        cameraList_    = new Camera[numberOfMaps];

        int j = 0, i = 0;

        for (int k = 0; k < battleMapList_.Length; k++)
        {
            battleMapList_[k] = Instantiate(battleMapPrefab, this.transform).GetComponent <BattleMap>();

            battleMapList_[k].OffsetCol          = HorizOffset * i;
            battleMapList_[k].OffsetRow          = VertOffset * j;
            battleMapList_[k].transform.position = HexCalculator.Position((HorizOffset * i), (VertOffset * j)) + new Vector3(0, 0, -1);

            cameraList_[k]         = battleMapList_[k].GetComponentInChildren <Camera>();
            cameraList_[k].enabled = false;

            battleMapList_[k].StartMap();

            if (j >= MAX_COLS - 1)
            {
                j = 0;

                if (i >= MAX_ROWS)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }
            else
            {
                j++;
            }
        }

        cameraList_[currentCameraIndex].enabled = true;
    }
Example #2
0
    /// <summary>
    /// Initializes Map Tiles and Spawn Positions based on a Map File.
    /// Chosen file range depends on the training phase the environment is in
    /// </summary>
    void InitializeMap(int mapMax)
    {
        if (mapMax > mapFiles.Count)
        {
            mapMax = mapFiles.Count;
        }

        string[] mapData = mapFiles[UnityEngine.Random.Range(0, mapMax)].text.Split(' ', '\n');

        int  maxRow = int.MinValue, maxCol = int.MinValue, minRow = int.MaxValue, minCol = int.MaxValue;
        int  row, col;
        bool isSpawn = false;
        int  faction = 0;

        Vector2Int position;
        GameObject tile;

        for (int j = 0; j < 4; j++)
        {
            spawnableTiles_.Add(new List <Vector2Int>());
        }

        int i = 0;

        while (i < mapData.Length)
        {
            for (int j = 0; j < spawnMarkers.Count; j++)
            {
                if (mapData[i].Equals(spawnMarkers[j]))
                {
                    isSpawn = true;
                    faction = j;
                    i++;
                    break;
                }
            }

            row = int.Parse(mapData[i]) + OffsetRow;
            col = int.Parse(mapData[i + 1]) + OffsetCol;

            position = new Vector2Int(row, col);
            tile     = Instantiate(hexTilePrefab, HexCalculator.Position(position.y, position.x), Quaternion.identity, this.transform);

            tile.GetComponent <HexTile>().Position = position;
            mapTiles.Add(position, tile.GetComponent <HexTile>());

            // If this is a spawn tile => Add its position into the spawnable tiles list
            if (isSpawn)
            {
                spawnableTiles_[faction].Add(position);
            }

            // map limits calculation
            if (maxRow < row)
            {
                maxRow = row;
            }
            if (maxCol < col)
            {
                maxCol = col;
            }
            if (minRow > row)
            {
                minRow = row;
            }
            if (minCol > col)
            {
                minCol = col;
            }

            isSpawn = false;
            i      += 2;
        }

        // Now, fill blanks and surroundings with obstacle tiles
        for (int x = minRow - 2; x <= maxRow + 2; x++)
        {
            for (int y = minCol - 2; y <= maxCol + 2; y++)
            {
                position = new Vector2Int(x, y);

                if (!mapTiles.ContainsKey(position))
                {
                    tile = Instantiate(obstaclePrefab, HexCalculator.Position(position.y, position.x), Quaternion.identity, this.transform);
                }
            }
        }

        // For each tile, set its neighbors in the HexTile component
        HexCalculator.SetNeighborsInMap(mapTiles);
    }