Dev Blog 9 – State Machines and Particles

Before getting into my post mortem for my event driven game, I thought I would take a little time to mention two things of importance that I have learned in the past 12 weeks. Game state machines and particles in Unity.

As part of the brief for our event driven game, we were required to implement a game state manager. So what is a game state manager? It is a set of enumerators that determine the current state of your game or part of your game. For example, the different states of a game could be Paused, Running, Player Died etc. These states will affect the code that needs to run. For example, if the player is in the pause menu, the game does not need to be checking to see if the player is trying to make the character move. I learned a great deal about state managers from Matthew Small’s “Using a State Machine in Unity” Youtube tutorial and from The Debug Log podcast “Design Patterns: State

A state manager can also be used on a part of the game, for instance, if a player is driving a vehicle, running on foot or flying a craft, controls will be different for these movements.

The below state manager was put on the game manager in a group project. One of the benefits of a state manager for me in a group environment was that I could assign someone to work on the logic for a particular state. Under the systemState.Pause case, another team member could add code that only affected the game when the game was paused. This means that they did not have to change any logic that was already implemented.

switch (currentState)
        {
            case systemState.Pause:
                //PLEASE INSERT PAUSE CODE!!!
                break;

            case systemState.Spawning:
                //print("Spawning");

                totalLevelTime += Time.deltaTime;

                spawnsRemaining = enemyWaves.levelWaves[enemyWave - 1].Count;

                if (spawnsRemaining <= 0) { currentState = systemState.Combat; } if (timeBetweenSpawns > spawnDelay)
                {
                    timeBetweenSpawns -= spawnDelay + Time.deltaTime;
                    SpawnWave();
                }
                else
                    timeBetweenSpawns += Time.deltaTime;

                break;


            case systemState.TenSec:

                tenSecTime.text = (System.Convert.ToInt32(waitTime - waitedTime + 0.49f)).ToString();

               /* if (Input.GetKeyDown(KeyCode.Space))
                {
                    CompleteTenSec();
                }*/

                if (waitedTime < waitTime)
                {
                    waitedTime += Time.deltaTime;
                }
                else
                {
                    CompleteTenSec();
                }
                break;

            case systemState.Combat:
                totalLevelTime += Time.deltaTime;

                if (currentEnemies <= 0)
                {
                    enemiesDestroyed = true;
                }

                if (enemiesDestroyed)
                {
                    if (enemyWave < 10)
                    {
                        StartTenSec();
                    }
                    else
                        Win();
                }
                break;
        }

In this example, we had four states; Pause, Spawning, TenSec and Combat. Each state had different logic and was modular and easy to understand. This works when the state is only one or the other. This does not work if a game can be in two states, unless you are using another way to track that using a second state or boolean.

This is another tool that should be considered for the majority of projects and I will be implementing many more state managers I am sure.

I spoke about polish in a previous blog. Particles was something I identified as a way to add polish to a game. I mentioned dust particles and water/splash particles. I could also have used particles on the back of the spaceship to indicate when the engines were engaged.

Particles can be as simple as you like, they can be used to give feedback to players when they carry out an action and they can make a game look polished. I started out with simple unity particles.

The sun has a texture on a sphere and several layers of particles to give the effect that is shown above. I have also used a Halo effect on the main sphere.

Just playing with Unity particle gave me experience. Changing things like colour over lifetime, rotation over lifetime and different emission volumes and different materials, can give a wide variety of effects.

Leave a Reply

Your email address will not be published. Required fields are marked *