Doors and keys

The past few day I’ve been working on fixing the doors and the keys in our space shooter. One of my programing colleges, Andreas Lundmark (link to his blog here), had been working on the base of both the doors and the keys, however since he got stuck with the coding and had to work on other parts of the game, such as sound and the menu of the game, I decided to take a look the door and key classes.

Also, we have a new name for the game! It used to be called Fancy Mansion, however we got some feedback on the name saying that the name did not suit the game since it was not fancy any more, like the original concept suggested. The entire team agreed on, and since the game is about a fox sneaking and stealing stuff, we changed the name to Trespawser. The name is a play of words between trespasser, since you play as a burglar and paws, since the game is set in a anthropomorphic world (animals behaving like humans).

Either way, the doors and the keys, and yes, the doors are made out of laser.doorKey

In Trespawser the player finds keys scattered around the level which unlocks different doors that when locked cuts off access to some part of the level. The also player needs all these keys to unlock the area where the main objective is located, which is required on order to win the game. The different doors and keys are all color coded, which makes it easy for the player to know what key goes to what door.

The problem we had was that you could unlock any door with any key, and also you could sell the keys, which both were fatal flaws for this mechanic. What I did was pretty simple and straight forward.

I gave the doors and the keys an enum (doorColor and keyColor) which would tell me what color each instance was, and paired each doorColor with a keyColor using std::pair.

Std::pair works like this, you have two containers which you fill with two data types, in my case, an enum doorColor and a enum keyColor. You can then use the .first and the .second operator to access these containers. Pairing things is particularly good whenever needing to create some sort of “connection” between two variables, like a string and a texture, or I my case, a doorColor and a keyColor.std pair visualized

This way, I could easily check if any of the keys that the player has in its inventory has the same color as the door the player is trying to unlock has. Simply using the current keys’ keyColor and checking if the current door is paired with the same keyColor, I can see if it is the right key.

Another way of doing this could have need to simply using some more if-statements, checking if the door is a certain color, and the key is the same color, then unlock the door, however I believe my way has better code structure, since it does not get as messy when all I need is to use one if-statement to see if the door is paired with the same keyColor as the key in question.

I also think this way is a bit more fool-proof than using several if-statements, since it would require me to hard code some more. Now all happens by foolproof magic.

Another solution would have been to give the keys and the doors same enum, like an enum Color, and assigned each key and door with just a Color, instead of a doorColor and a keyColor, and checked if they where the same. That would probably have been as effective as using pairs, if not even better, since the computer would not have to allocate memory for the pair containers. However, doing it the way I did, I learned how to use std::pair, a very basic function, I know, though learning by doing is always best when it come to programing.

Btw, tomorrow is beta-release, and I’d say that we are almost feature complete!

About Johan Holmér

2014  Programming