Game Design Pattern: Command Pt.2- Game Dev Series 137
Objective: playing stored contents/command by using CommandManager.
In this tutorial, we will continue to create a manager class that would play all command we created in order, and also can rewind or clear it.
CommandManager
To make it easier to call, the CommandManager must be a singleton. Then create a list variable to it to store the executed command.
Inside this manager, we would need some methods to do the following functions:
- Adding command into list
- Playing all command from list
- Playing all command in rewind from list
- Setting all color into white when finished changing
- Reset the list
Adding command into list
In this method, we would pass in the current executed command when clicking on cube, and store it into list.
By doing this, we can use List.Add(object).
Then we need to call this method in PlayerInput after execute the command.
Playing & reverse all command
The Playing is quite simple. We need to make it replay all result of command in list, what’s more, we will need it to play one per second. The best way to present this is creating a coroutine.
For the rewind playing, we can use the reverse function of LINQ to do this part. Before we using LINQ, you will need to add a namespace of System.Linq to this script.
Then we create another coroutine to reverse playing the list.
Done and reset
All you need to set cubes to white, is calling every cubes by tag, then set their color to white in foreach loop.
The last one: reset.
Just Clear out the whole list and that is all you need.
Assign to buttons and test
Now we just need to create an empty object to contain this manager class in game scene, and assign these methods to all buttons.
And we can test the result of using Command Design Pattern.