Artifact post: Win and Lose states.

This week’s artifact is the implementation of a Win state and a Lose State.

Previously the only states we’ve had were the Menu State and the Game State. The menu state was the main menu and inherited from a State class and was handled by out StateManager which keeps the current state updating and drawing. The main menu have three buttons, each with a set function to be activated when the button is clicked. The one relevant to this is the New Game button which sets the current state to the game state.

BGLose

The game state is where the main game takes place and it is the only state which takes a pointer to the Level class. I decided to use this to switch between states. My idea was that i would be able to use the Statemanager’s function for changing states in the same way done in the menu state when the new game button is pressed. So, in order for the Game state to change the current state it needs to know this by some input from the game’s objects. The only way to reach a Lose state is for the player to come into contact with a guard and the player object contains a pointer to the Level class. With this i decided to let the Player send a command to the level whenever something that should change the current state happens, in this case touching a guard. In order to communicate this command through the level class i created a private state integer in the level and two functions for getting and setting it.

When the player collides with a guard it used to trigger our old and flawed reset functionality which would only reset certain objects. now however it runs the Set function of the level and sets the state integer to a certain value. Then in the game state’s update function i also made it run a check to see if the level’s state integer had changed. If it had the state would be changed to the corresponding state depending on the value. 

However something goes wrong when i attempt it, the game seems to just switch over to the menu state and get stuck there. this is even stranger as when i check the current state it tells me the lose state is indeed the current state yet it loads the menu state. My best guess it that there is some issue with how states are treated when the current state is changed. I will have to go through it properly tomorrow. I might also have another look at it after i get something to eat.