Sunday 3 January 2010

Week 9 : Music & game state

Apologies for the lateness of this post, but everything is very much on track and I'll try to get back to my normal posting routine.

Since I'm outsourcing the games artwork, I had more time to do other tasks so I rearranged the schedule slightly:

Now that I had chosen the music for the game, I needed to create a system that would play/pause/stop the music at the correct time. Since inner game classes are deleted whenever the game is reset it didn't seem a good idea to include them within the core game code. So, I created an external component that monitors the global game state, accepts input and decides when to play music.

I didn't have any problems with the creation of the component but most of my problems stemmed from my existing game state system. This was created in the summer and is the base of all game phase logic and execution. I wouldn't call it a very good system, and I had I written it today, I would have used events rather than having one global game state that everything depends on.

I thought about re-writing the game state management with the use of the knowledge gained since the summer but it would be too time consuming. I decided I'd stick with what I've got and created an internal state system for the music player so that it wouldn't have to change the global state (which could be catastrophic for the game as it's main systems are fragilely balanced on the current state).

So my basic plan was to have one song play when in the menus and lobby mode and then have one looping track during the game. Once I implemented this successfully it hit me, having one song is going to get very annoying! So I added more songs to the games soundtrack but this presented me with a challenge.

I didn't want the same song to play twice in a row and I didn't want there to be a set order of songs, so I wanted them to play randomly. To achieve this I stored which song was played last then got a random number that was inside a loop that would finish only if that number was not matching the song that was last played.

Since I managed to do this pretty quickly, I decided I'd add some more functionality to the music player, letting the user change the song and go back to the last played song. I also created a small graphical panel that momentarily displays information about the song and then fades out.

Ok so what does the music player do?

- Loads all the songs and objects and tells the program its ready
- Plays songs at a lower volume than the game SFX
- Plays and loops one song during the menu & lobby phases
- Plays one song from a playlist of music during the game phase.
- Allows users to change the song to the next random song
- Allows users to go back and play the last song that was on.
- Displays a small graphical panel with information about the track that appears briefly when a new song is played.

I won't add any screen shots to this post because I'll be uploading a video next week to show how the music player works with the menu system.

No comments:

Post a Comment