Assets/Scripts/LevelManager.cs
/// <summary>
/// Sets up the scene based on a text-file tilemap
/// </summary>
/// <param name="levelTileMap">The tilemap to use in assembling the level</param>
Sets up the scene based on a text-file tilemap
Sets up the scene based on a text - file tilemap
public void SetupScene(TextAsset levelTileMap)
    {
        for (int i = 0; i < level.GetLength(0); i++)
        {
            for (int j = 0; j < level.GetLength(1); j++)
            {
                if (placedTiles[i, j] != null)
                    Destroy(placedTiles[i, j]);
            }
        }
        var levelRows = levelTileMap.text.Split('\n').Where(n => n != null && n != "").ToList();
        var rowCount = levelRows.Count;
        var columnCount = levelRows.First().Split(' ').Where(n => n != null && n != "").ToList().Count;
        level = new int[columnCount, rowCount];
        placedTiles = new GameObject[columnCount, rowCount];
        height = rowCount;
        width = columnCount;
        InitializeEmptyLevel();
        int row = rowCount - 1;
        foreach(var levelRow in levelRows)
        {
            var rowItems = levelRow.Split(' ').ToList();
            for(var col = 0; col < columnCount; col++)
            {
                var tile = int.Parse(rowItems[col]);
                if (tile == CELL_BLOCK)
                {
                    SetTileAt(col, row, CELL_BLOCK);
                }
                else if (tile == CELL_EXIT)
                {
                    SetTileAt(col, row, CELL_EXIT);
                }
                else
                {
                    SetTileAt(col, row, CELL_OPEN);
                }
                if(tile == CELL_ENEMY)
                {
                    levelItems.Add(Instantiate(enemy, new Vector3(col, row, 0f), Quaternion.identity));
                }
                else if(tile == CELL_DOOR)
                {
                    levelItems.Add(Instantiate(door, new Vector3(col, row, 0f), Quaternion.identity));
                }
                else if(tile == CELL_KEY)
                {
                    levelItems.Add(Instantiate(key, new Vector3(col, row, 0f), Quaternion.identity));
                }
                else if(tile == CELL_TREASURE)
                {
                    levelItems.Add(Instantiate(treasure, new Vector3(col, row, 0f), Quaternion.identity));
                }
                else if(tile == CELL_EXIT)
                {
                    levelItems.Add(Instantiate(exit, new Vector3(col, row, 0f), Quaternion.identity));
                }
            }
            row--;
        }
    }
public void SetupScene ( TextAsset levelTileMap ) { for ( int i = 0 ; i < level . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < level . GetLength ( 1 ) ; j ++ ) { if ( placedTiles [ i , j ] != null ) Destroy ( placedTiles [ i , j ] ) ; } } var levelRows = levelTileMap . text . Split ( ' \n ' ) . Where ( n => n != null && n != " " ) . ToList ( ) ; var rowCount = levelRows . Count ; var columnCount = levelRows . First ( ) . Split ( ' ' ) . Where ( n => n != null && n != " " ) . ToList ( ) . Count ; level = new int [ columnCount , rowCount ] ; placedTiles = new GameObject [ columnCount , rowCount ] ; height = rowCount ; width = columnCount ; InitializeEmptyLevel ( ) ; int row = rowCount - 1 ; foreach ( var levelRow in levelRows ) { var rowItems = levelRow . Split ( ' ' ) . ToList ( ) ; for ( var col = 0 ; col < columnCount ; col ++ ) { var tile = int . Parse ( rowItems [ col ] ) ; if ( tile == CELL_BLOCK ) { SetTileAt ( col , row , CELL_BLOCK ) ; } else if ( tile == CELL_EXIT ) { SetTileAt ( col , row , CELL_EXIT ) ; } else { SetTileAt ( col , row , CELL_OPEN ) ; } if ( tile == CELL_ENEMY ) { levelItems . Add ( Instantiate ( enemy , new Vector3 ( col , row , 0f ) , Quaternion . identity ) ) ; } else if ( tile == CELL_DOOR ) { levelItems . Add ( Instantiate ( door , new Vector3 ( col , row , 0f ) , Quaternion . identity ) ) ; } else if ( tile == CELL_KEY ) { levelItems . Add ( Instantiate ( key , new Vector3 ( col , row , 0f ) , Quaternion . identity ) ) ; } else if ( tile == CELL_TREASURE ) { levelItems . Add ( Instantiate ( treasure , new Vector3 ( col , row , 0f ) , Quaternion . identity ) ) ; } else if ( tile == CELL_EXIT ) { levelItems . Add ( Instantiate ( exit , new Vector3 ( col , row , 0f ) , Quaternion . identity ) ) ; } } row -- ; } }
Sets up the scene based on a text-file tilemap
Sets up the scene based on a text - file tilemap
// Clear out any existing tiles // Only BLOCK, OPEN, and EXIT form the actual tiles // of the map. All other "tiles" will be added as separate // elements into the scene.
levelTileMap
TextAsset
The tilemap to use in assembling the level
The tilemap to use in assembling the level