示例#1
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);
    }