Stairs
v3 Tutorials
Goal
The purpose of this tutorial is start placing stairs in our dungeon. We want to be able to proceed down stairs to get into deeper and more difficult levels.
- Stairs can either go up or down
- The symbol for stairs going down will be a greater than sign “>”
- The symbol for stairs going up will be a less than sign “<“
- Each dungeon level will have one up and one down staircase
- Stairs going down can be descended by the player by pressing the ‘>’ or ‘.’ key
- Once a player has gone down to the next level they cannot go back
- Stairs going up are only used to indicate where the player came from
- When a player goes down stairs a brand new level is generated
Creating the Stairs Class
To begin we need a new class to represent the stairs as we outlined in our goals above. The stairs class should inherit from IDrawable because we will want to draw it on the map console. Create a new file named Stairs.cs in the Core folder and place the following code in it.
Updating DungeonMap Class
Now that our Stairs class is created, we need to update DungeonMap.cs with a few changes to be able to use the Stairs. At the top of DungeonMap.cs add the following two properties for our StairsUp and StairsDown next to the existing Rooms and Doors properties.
Next update the DungeonMap constructor to call Clear() on the SchedulingSystem when a new DungeonMap is constructed. We do this because when we make a new level by going down stairs we want to make sure that all of the monsters from the previous level are removed from the schedule and do not continue to try to act.
Now we need a new method which will check to see if the player is standing on the stairs going down. Create a method called CanMoveDownToNextLevel() with the following code.
In the Doors tutorial last time we forgot to update the Draw() method and had to have a blog reader point out the mistake. We’ll try not to make the same mistake this time. Update the Draw() method and add the following lines of code to draw the stairs.
Updating the MapGenerator Class
The next class we need to update is MapGenerator. We need to make sure that when we generate new maps, stairs are created along with the rest of the dungeon features. Open MapGenerator.cs and create a new private method named CreateStairs().
We are not doing anything too fancy to generate the stairs. We are creating the stairs up in the center of the first room that was generated. This is the same room that the player starts in and the player is also in the center of the room, so we’ll offset the X coordinate by 1 to put the stairs next to the player. The last room we generated gets stairs going down and again we place them in the center of the room.
Make sure to call the CreateStairs() method from the existing CreateMap() method right before calling PlacePlayer();
Also we want to change the signature of the MapGenerator constructor and add an additional integer parameter called mapLevel.
If you have any sort of static analysis on like FxCop it will complain about having an unused parameter in the method. We really shouldn’t add it until we are prepared to use it, but rest assured we will use it very soon.
Updating the Game Class
Open up Game.cs which will be the final class that we need to update. Start by adding a new private static int member variable in with the rest of the member variables at the top of the class.
Next change the line was setting the console title in the Main() method. Also add the _mapLevel parameter to line where we instantiate a new MapGenerator.
Finally in the OnRootConsoleUpdate(…) method where we are checking to see which key was pressed ad the following else if onto the end of the if block.
This last bit of code just checks for the “>” or “.” key being pressed. It then calls into DungeonMap.CanMoveDownToNextLevel() which will return true if the Player is standing on a stairway leading down. We then generate a new map and increment the _mapLevel.
If you run the game now you should be able to explore until you find stairs and descend deeper into the dungeon.
Closing Thoughts
Although we got our stairs working we are still missing some important things. Currently levels don’t get any more difficult the deeper we go.
As always the code for the tutorial series so far can be found on Bitbucket:
https://bitbucket.org/FaronBracy/roguesharpv3tutorial/commits/tag/16Stairs
Bored waiting for the next tutorial post? The completed project is already available on Bitbucket.