Another day, Another hour. Not the most exciting hour of work so far, but it was necessary! Tidy up of code and a rushed State Machine implementation, with Menu state!
After 4 hours of rushed coding, you would probably imagine that the code is getting slightly tangled so I’ve decided to make the 5th hour, and maybe every other 5th hour, a ‘code clean up’ session.
State Machine / Manager
The main mess was that the whole game was running out of the Game1.cs file, which is basically XNA’s main() function. I needed to add some sort of State Machine to keep everything in separate states (Menu and Gameplay) and just to tidy things up slightly. So my implementation of the State Machine consists of a base ‘State’ class and a ‘PlayState’ + ‘MenuState’ classes which inherit from this base class. Each overrides the main functions of the State class (Init, Update, Draw, etc.) and implements their own game state code!
Then I have one instance of the ‘State’ class in the Game1.cs. This instance is initialized as a ‘new MenuState()’ and the game begins to load up the menu. If I wish to change state, I’d ‘new’ the instance of the ‘State’ class into a new inherited class of ‘State’ – eg. currentState = new PlayState();. This will be changed pretty soon so that I have a ‘Map’ of states, each can be enabled and disabled. This would be a better method since multiple states could run at the same time – E.G. ‘PlayState’ and ‘PauseState’ instead of putting all of the ‘Pause’ code into the PlayState. Let me know if you think another way would be better for a small windows game =).
At the present time, the Shockwave was being managed in the main PlayState, which was a little untidy. I’ve created a shockwave manager class which tidies things up a lot too =).
To go along with the state machine, I’ve created a ‘MenuState’ which is at the moment, 2 buttons, ‘Play’ and ‘Quit’ made from SpriteFont text. Each button has a BoundingBox which will test in its Update() method whether the user has touched it or not. The ‘Button’ class is the base class and the ‘TextButton’ is an inherited class with a SpriteFont and some text in it. When these are pressed it calls a ChangeState() function passing in an enum of states, this then changes the state as mentioned above!
As usual now, I’ve create a 10x speed video over the 60 minutes (65 for this hour) of me franticly coding, the video of how the game looks is now at 1x speed at the end of the 10x speed video. So just skip ahead to 6 mins if you want to see the current progress!
If anyone has any tips on what rules there are for adding music onto these video’s, it’d be greatly appreciated too, since they are a little plain without any on. Are you allowed to put music on if you credit the creators or what?!
Hour 5 Over, If you have any suggestions code wise or gameplay wise, I’d happily take them on bored =), constructive criticism only though please! I know my coding could be better, everyone’s can be improved!