Example #1
0
    // Use this for initialization
    void Start()
    {
        //Initialize the board
        for (int i = 0; i < height; i++)
        {
            List <Tile> row = new List <Tile>(width);

            for (int j = 0; j < width; j++)
            {
                Tile tile = TileFactoryMethods.TileFactory(TileType.Dirty);
                tile.setPos(i, j);
                row.Add(tile);
            }

            board.Add(row);
        }

        //Add the player for testing purposes.
        Tile t = TileFactoryMethods.TileFactory(TileType.Block);

        t.setPos(2, 2);
        board[2][2] = t;

        player.currentTile = board[0][0];

        //Initialize the History stack

        history = new Stack <MoveHistory>();

        //Draw screen


        for (int col = width - 1; col >= 0; col--)
        {
            List <Transform> objRow = new List <Transform>();

            for (int row = 0; row < height; row++)
            {
                Transform obj = Instantiate(mudOverlay, new Vector3(row, col, 0), Quaternion.identity);
                objRow.Add(obj);
            }
            boardObj.Add(objRow);
        }
    }
    public override Boolean move(Actor actor, Tile block, GameBoardModel gb)
    {
        //Store the previous state info
        gb.history.Push(new MoveHistory(gb.player.clone(), block));

        //move the player
        actor.row = block.row;
        actor.col = block.col;


        //Set the current tile to clean.
        Tile t = TileFactoryMethods.TileFactory(TileType.Clean);

        t.setPos(actor.row, actor.col);
        gb.board[actor.row][actor.col] = t;


        //update the players location
        actor.currentTile = block;

        return(true);
    }
Example #3
0
    //Build the indicated level
    public void loadLevel(int level)
    {
        //Hide the play area
        loadingScreen.enabled = true;

        //Destroy any previous existing game objects
        for (int i = 0; i < boardObj.Count; i++)
        {
            for (int j = 0; j < boardObj[i].Count; j++)
            {
                Destroy(boardObj[i][j]);
                Destroy(boardTiles[i][j]);
            }
        }

        //Reset the references to the objects
        board      = new List <List <Tile> >();
        boardObj   = new List <List <GameObject> >();
        boardTiles = new List <List <GameObject> >();

        //Read the level file to find out what the level needs
        string assetText;

        using (var streamReader = new StreamReader("Assets/Levels/" + level.ToString() + ".txt", Encoding.UTF8))
        {
            assetText = streamReader.ReadToEnd();
        }

        var lines = assetText.Split('\n');

        width  = lines[0].Split('~').Length;
        height = lines.Length;

        //Go through each row
        for (int i = 0; i < height; i++)
        {
            var               singleTiles = lines[i].Split('~');
            List <Tile>       row         = new List <Tile>(width);
            List <GameObject> tileRow     = new List <GameObject>(width);

            //Go through each individual tile and create it
            for (int j = 0; j < width; j++)
            {
                Tile tile = TileFactoryMethods.TileFactory((TileType)Int32.Parse(singleTiles[j]));

                tile.setPos(i, j);
                row.Add(tile);

                //Create the object and store the returned reference
                //tileRow.Add(Instantiate(GameGraphics.BackGroundTile, new Vector3(i, j, .01f), Quaternion.identity));
            }

            board.Add(row);
            boardTiles.Add(tileRow);
        }

        //Reset the player
        player             = new ActorPlayer();
        player.currentTile = board[0][0];

        //Reset the history stack
        history = new Stack <MoveHistory>();

        //Draw Screen
        for (int col = width - 1; col >= 0; col--)
        {
            List <GameObject> objRow = new List <GameObject>();

            for (int row = 0; row < height; row++)
            {
                //GameObject obj = Instantiate(GameGraphics.MudTile, new Vector3(row, col, 0), Quaternion.identity);
                //objRow.Add(obj);
            }

            boardObj.Add(objRow);
        }

        //Reset the camera
        //This works for even numbered grids
        Vector3 newCamPos = new Vector3(width / 2 - 0.5f, height / 2, -7.5f);

        if (width % 2 == 1)
        {
            newCamPos.x += 0.5f;
        }
        if (height % 2 == 1)
        {
            newCamPos.y += 0.5f;
        }

        Camera.main.transform.position = (newCamPos);

        //Loading has finished so release the play area
        loadingScreen.enabled = false;
    }
    //How to pass level file?
    public string loadLevel(string fname)
    {
        //Hide the play area
        //loadingScreen.enabled = true;

        string result = "Error loading file.";

        history = new Stack <MoveHistory>();
        board   = new List <List <Tile> >();

        using (var streamReader = new StreamReader("Assets/Levels/" + fname, Encoding.UTF8))
        {
            JSONNode json = JSON.Parse(streamReader.ReadToEnd());


            if (json["height"].IsNull)
            {
                return("Error loading height;");
            }
            height = json["height"].AsInt;


            if (json["width"].IsNull)
            {
                return("Error loading width;");
            }
            width = json["width"].AsInt;


            //Load the default gameboard
            for (int r = 0; r < height; r++)
            {
                List <Tile> tile_row = new List <Tile>();
                for (int c = 0; c < width; c++)
                {
                    Tile t = TileFactoryMethods.TileFactory(TileType.Dirty);
                    t.setPos(r, c);
                    tile_row.Add(t);
                }

                board.Add(tile_row);
            }

            //Validate data
            if (json["start"].IsNull || json["start"]["row"].IsNull || json["start"]["col"].IsNull)
            {
                return("Error loading level stating tile.");
            }

            if (json["end"].IsNull || json["end"]["row"].IsNull || json["end"]["col"].IsNull)
            {
                return("Error loading level end tile.");
            }

            if ((json["start"]["row"] == json["end"]["row"]) && (json["start"]["col"] == json["end"]["col"]))
            {
                return("Error: Start and end cannot be the same tile");
            }

            int row, col;

            //Add the start tile
            row = json["start"]["row"].AsInt;
            col = json["start"]["col"].AsInt;

            if ((row < 0) || (row >= height) || (col < 0) || (col >= width))
            {
                return("Start tile is not in board dimensions.");
            }

            Tile tile = TileFactoryMethods.TileFactory(TileType.Start);
            tile.setPos(row, col);
            board[row][col] = tile;
            start_tile      = tile;

            //Add the end tile
            row = json["end"]["row"].AsInt;
            col = json["end"]["col"].AsInt;

            if ((row < 0) || (row >= height) || (col < 0) || (col >= width))
            {
                return("end tile is not in board dimensions.");
            }

            tile = TileFactoryMethods.TileFactory(TileType.End);
            tile.setPos(row, col);
            board[row][col] = tile;
            end_tile        = tile;

            //Load the special tiles
            blocks = new List <Tile>();
            JSONArray tiles = json["tiles"].AsArray;
            foreach (JSONObject j in tiles)
            {
                row = j["row"].AsInt;
                col = j["col"].AsInt;

                print(row + "," + col);

                tile = TileFactoryMethods.TileFactory(TileType.Block);
                tile.setPos(row, col);
                board[row][col] = tile;
                blocks.Add(tile);
            }

            result = "File Loaded";
        }

        return(result);
    }