Пример #1
0
    /// <summary>
    /// Sets up the outer walls and floor (background) of the game board.
    /// </summary>
    public IEnumerator DOBoardSetup()
    {
        boardReady = false;
        GameManager.ResetAll();

        //Set our map/maptiles array size.
        map      = new TType[rows + 1, columns + 1];
        mapTiles = new Tile[rows + 1, columns + 1];

        //Go over each spot on the array and make them all walls.
        for (int y = 0; y < map.GetLength(1); y++)
        {
            for (int x = 0; x < map.GetLength(0); x++)
            {
                map[x, y] = TType.Wall;
            }
        }

        //Go over the whole map and spawn tiles, and assign the tile values based on the map.
        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                Tile _tileref = testTile.Spawn();
                mapTiles[x, y]              = _tileref;
                _tileref.tilePosition       = new TilePosition(x, y);
                _tileref.transform.position = new Vector3((Mathf.Floor(rows / 2) * tileSize) - (x * tileSize), (Mathf.Floor(columns / 2) * tileSize) - (y * tileSize), 0);
                PaintTile(x, y);
                _tileref.gameObject.name = "Tile " + x + "/" + y;
                _tileref.transform.SetParent(transform);
            }
        }

        yield return(null);

        //Get a random position to start our tunneler.
        TilePosition centerPos = new TilePosition(Mathf.FloorToInt(map.GetLength(0) * 0.5f), Mathf.FloorToInt(map.GetLength(1) * 0.5f));

        List <TilePosition> aroundCenterPos = BoardMethod.GetTilesAroundTile(centerPos, 4, TType.Wall);

        TilePosition tunnelerPos = aroundCenterPos[Random.RandomRange(0, aroundCenterPos.Count)];

        int amountToDig = Mathf.FloorToInt((map.GetLength(0) - 2) * (map.GetLength(1) - 2) * digTilePct);

        yield return(null);

        Commands tunnelerCommand = (Commands)Random.Range(1, 5);

        while (amountToDig > 0)
        {
            yield return(null);

            if (BoardMethod.getTileType(tunnelerPos.x, tunnelerPos.y) == TType.Wall)
            {
                float roomChance = 0.05f;
                if (BoardMethod.CountTilesAlignedAroundTile(tunnelerPos, 3, TType.Floor) == 2)
                {
                    roomChance = 0.55f;
                }
                if (Random.value < roomChance)
                {
                    var tilesAllAround = BoardMethod.GetTilesAroundTile(tunnelerPos, 1, TType.Wall);
                    amountToDig -= 3;
                    foreach (TilePosition _tilePosition in tilesAllAround)
                    {
                        TurnToFloor(_tilePosition);
                    }
                }
                else
                {
                    amountToDig--;
                    TurnToFloor(tunnelerPos);
                }
            }
            TilePosition possibleTunnelerPos = BoardMethod.GetTileFromCommand(tunnelerPos, tunnelerCommand);
            if (BoardMethod.isPositionValid(possibleTunnelerPos))
            {
                tunnelerPos = possibleTunnelerPos;
            }
            else
            {
                tunnelerCommand = Random.value < 0.5
                    ? BoardMethod.GetNextCommand(tunnelerCommand)
                    : BoardMethod.GetPrevCommand(tunnelerCommand);
            }

            if (Random.value < 0.25f)
            {
                tunnelerCommand = Random.value < 0.5
                    ? BoardMethod.GetNextCommand(tunnelerCommand)
                    : BoardMethod.GetPrevCommand(tunnelerCommand);
            }
        }

        boardReady = true;

        for (int i = 0; i < 3; i++)
        {
            GameManager.get.SpawnEnemy();
        }
    }