Doors
v3 Tutorials
Goal
The purpose of this tutorial is to start placing doors in our dungeon. We want the doors to start out simple but be able to be expanded upon in the future.
- Doors can have 2 states: open or closed
- All doors start as closed
- A closed door blocks field-of-view
- A door can be opened by any actor (Player or Monster)
- Once a door is opened it cannot be closed again
- An actor entering the same cell as a door will open it automatically
- The symbol for a closed door will be a plus sign “+”
- The symbol for an open door will be a minus sign “-“
Setting Door Colors
We already set up the swatch of colors to use in our game but we never specified which colors doors should be. Let’s do that now. Open Colors.cs and add the following code to pick colors from the Swatch for different parts of the door.
Creating the Door Class
Next we need a new class to represent all of the properties of a door that we outlined in our goals at the beginning. The door class should inherit from IDrawable because we will want to draw it on the map console. Create a new file named Door.cs in the Core folder and place the following code in it.
Notice that we use the colors and symbols for the door that we determined earlier.
Updating the DungeonMap Class
Before we can start placing doors in our dungeon it’s important to add a few helper methods to our DungeonMap class for working with doors. Open DungeonMap.cs and add the following lines of code.
First add a new public property to the class at the top where we already have a list of rooms.
Be sure to initialize this list in the constructor
Next we need to add two new methods to the class. GetDoor(…) and OpenDoor(…)
Last we need to update SetActorPosition(…) and call a the new method OpenDoor(…) which we just made. Call this immediately after SetIsWalkable(…).
Edit 2/25/2017 – Thanks to rmcrackan for pointing out that I forgot to show updating the Draw(…) method to have a foreach loop for drawing each of the doors.
Door Placement Strategy
Now that we are reasonably confident that we can draw doors and we have a few helper methods for working with them, how do we know where to put them? Think about where doors are placed in regard to rooms. They should be on an outer wall. So we should start by getting all of the cells along the boundaries of our rooms.
Once we have all of those cells we should look for any open floors that would be a good candidate for a door. And how do we know if a door should go there or not? We look at the floor’s neighboring cells to see if it fits.
A good Cell for a door placement is one that has two walls across from each other and two floors as opposing neighbors.
Updating the MapGenerator Class
Lets take the strategy that we just outlined for creating door and actually write the code. Open MapGenerator.cs and add the following new methods.
That’s quite a bit of code but I hope that the comments help to understand what it is accomplishing. Now that we have the methods in place, we need to remember to call them. In the CreateMap() method we have a foreach where we call CreateRoom() on each room in the map. We also need to put a call for CreateDoors() in there.
If we run the program now we should see our doors in place and working as expected!
Final Thoughts
The 7 Day Roguelike Challenge starts this year on March 12th. This year will be the 13th challenge. It’s a fun event and I urge you to participate. If you don’t like RogueSharp or C# there are plenty of other libraries out there for other languages. You could also start from scratch and not use any existing library.
7DRL Annoucement - http://7drl.org/2017/01/18/this-years-challenge-4-12-march-2017/
As always the code for the tutorial series so far can be found on Bitbucket:
https://bitbucket.org/FaronBracy/roguesharpv3tutorial/commits/tag/15Doors
Bored waiting for the next tutorial post? The completed project is already available on Bitbucket.