private TileGrid LoadPart(string name, short part)
    {
        List <string> text = new List <string>();
        TileGrid      tileGrid;

        using (StreamReader streamReader = new StreamReader("Content/" + name + ".txt"))
        {
            string line  = streamReader.ReadLine();
            int    width = line.Length;

            //read the file
            while (line != null)
            {
                text.Add(line);
                line = streamReader.ReadLine();
            }

            //make a grid for the tiles
            tileGrid = new TileGrid(width + 1, text.Count + 1, "TileGrid");

            //Load the tiles into the grid
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < text.Count; ++y)
                {
                    Tile tile = LoadTile(text[y][x], x, y, part);
                    if (tile != null)
                    {
                        tileGrid.Add(tile, x, y);
                        if (tile is WallTile)
                        {
                            tile.Position += new Vector3(0, 200, 0);
                        }
                    }
                }
            }
        }
        return(tileGrid);
    }
    /// <summary>
    /// Loads the level's grid of tiles
    /// </summary>
    /// <param name="name">The name of the file from which to open the level</param>
    /// <returns>Returns the TileGrid filled with the complete level</returns>
    private TileGrid LoadLevel(string name)
    {
        List<string> text = new List<string>();
        TileGrid tileGrid;
        using (StreamReader streamReader = new StreamReader(name))
        {
            string line = streamReader.ReadLine();
            int width = line.Length;

            //read the file
            while (line != null)
            {
                text.Add(line);
                line = streamReader.ReadLine();
            }

        //make a grid for the tiles
        tileGrid = new TileGrid(width + 1, text.Count + 1, "TileGrid");

            //Load the tiles into the grid
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < text.Count; ++y)
                {
                    Tile tile = LoadTile(text[y][x], x, y, name);
                    if (tile != null)
                    {
                        tileGrid.Add(tile, x, y);
                        if (tile is WallTile)
                        {
                            tile.Position += new Vector3(0, 200, 0);
                        }
                    }
                }
            }
        }
        return tileGrid;
    }
Exemple #3
0
    /// <summary>
    /// Creating/generating the level itself
    /// </summary>
    /// <param name="roomNumber">The number of the room</param>
    /// <param name="tiles">The size the Mainpath should be, counted in tiles</param>
    /// <param name="chased">Whether or not the monster is chasing the player</param>
    public RandomLevel(int roomNumber, int tiles = 12, bool chased = false, int noteID = 0)
    {
        //Setting a boolean for the monster
        this.chased = chased;

        //Assining the variables
        tileList = new Dictionary <Point, Tile>();
        keyList  = new List <Point>();

        random = GameEnvironment.Random;

        //Select the tiles to use
        int pathType = random.Next(4);

        if (pathType < 9)
        {
            pathID = "0" + (pathType + 1);
        }
        else
        {
            pathID = "" + (pathType + 1);
        }

        int wallType = random.Next(4);

        if (wallType < 9)
        {
            wallID = "0" + (wallType + 1);
        }
        else
        {
            pathID = "" + (wallType + 1);
        }
        newTile    = new EntryTile(pathID);
        this.tiles = tiles;

        //Create the startpoint
        position = Point.Zero;
        tileList.Add(position, newTile);
        keyList.Add(position);

        //Creating the paths
        CreateMainPath();
        for (int i = random.Next(1, (int)Math.Pow(tiles, 2 / 3)); i > 0; i--)
        {
            CreateSidePath(random.Next(3, 15), chased);
        }

        //making the tile grid
        TileGrid tileGrid = Grid;

        gameObjects.Add(tileGrid);

        //making the player
        player = new Player(Vector3.Zero);
        gameObjects.Add(player);
        player.Parent = this;
        player.LoadContent();

        foreach (GameObject obj in tileGrid.Objects)
        {
            if (obj != null)
            {
                if (obj.ID == "EntryTile")
                {
                    player.Position = new Vector3(obj.Position.X, obj.Position.Y + GameObjectGrid.CellHeight, obj.Position.Z);
                    foreach (GameObject tile in tileGrid.Objects)
                    {
                        if (tile != null)
                        {
                            if (tile.Position.X == obj.Position.X + 200 && tile.Position.Z == obj.Position.Z && tile.ID == "PathTile")
                            {
                                player.viewAngleX = 0f;
                            }
                            if (tile.Position.X == obj.Position.X && tile.Position.Z == obj.Position.Z + 200 && tile.ID == "PathTile")
                            {
                                player.viewAngleX = (float)(Math.PI / 2);
                            }
                            if (tile.Position.X == obj.Position.X - 200 && tile.Position.Z == obj.Position.Z && tile.ID == "PathTile")
                            {
                                player.viewAngleX = (float)(Math.PI);
                            }
                            if (tile.Position.X == obj.Position.X && tile.Position.Z == obj.Position.Z - 200 && tile.ID == "PathTile")
                            {
                                player.viewAngleX = (float)(Math.PI * 1.5);
                            }
                        }
                    }
                }
            }
        }

        //Adding decoration objects
        TileGrid grid = Find("TileGrid") as TileGrid;

        for (int x = 0; x < grid.Columns; x++)
        {
            for (int y = 0; y < grid.Rows; y++)
            {
                if (grid.Get(x, y) != null)
                {
                    if (grid.Get(x, y).ID == "WallTile" && GameEnvironment.Random.Next(15) == 0)
                    {
                        try
                        {
                            if (grid.Get(x + 1, y) != null && grid.Get(x + 1, y).ID == "PathTile" && grid.Get(x + 1, y).ID != "DecorationTile")
                            {
                                AddDecoration(grid.Get(x, y).Position, new Vector3(-1, 0, 0));
                                grid.Add(new DecorationTile(pathID), x + 1, y);
                            }
                            else if (grid.Get(x, y + 1) != null && grid.Get(x, y + 1).ID == "PathTile" && grid.Get(x, y + 1).ID != "DecorationTile")
                            {
                                AddDecoration(grid.Get(x, y).Position, new Vector3(0, 0, -1));
                                grid.Add(new DecorationTile(pathID), x, y + 1);
                            }
                            else if (grid.Get(x - 1, y) != null && grid.Get(x - 1, y).ID == "PathTile" && grid.Get(x - 1, y).ID != "DecorationTile")
                            {
                                AddDecoration(grid.Get(x, y).Position, new Vector3(1, 0, 0));
                                grid.Add(new DecorationTile(pathID), x - 1, y);
                            }
                            else if (grid.Get(x, y - 1) != null && grid.Get(x, y - 1).ID == "PathTile" && grid.Get(x, y - 1).ID != "DecorationTile")
                            {
                                AddDecoration(grid.Get(x, y).Position, new Vector3(0, 0, 1));
                                grid.Add(new DecorationTile(pathID), x, y - 1);
                            }
                        }
                        catch (IndexOutOfRangeException e) { Console.WriteLine(e.StackTrace); }
                    }
                }
            }
        }

        //Add the note
        if (noteID != 0)
        {
            NoteObject note = new NoteObject(noteID.ToString());
            note.Parent = this;
            CreateNote(note, tileGrid);
            gameObjects.Add(note);
        }

        //Making the monster
        if (chased)
        {
            monster        = new Monster(Grid.Objects);
            monster.Parent = this;
            monster.LoadContent();
            gameObjects.Add(monster);
        }

        //Making the stamina bar
        stamina = new Stamina();
        gameObjects.Add(stamina);
        stamina.Parent = this;
        exitText.text  = "Press E to proceed";

        //Making the room counter
        roomCounter      = new TextGameObject("text");
        roomCounter.text = roomNumber.ToString();
        gameObjects.Add(roomCounter);
    }