-
Notifications
You must be signed in to change notification settings - Fork 1
Patterns
Namespace: WIDVE.Patterns
Location: WIDVE Unity Scripts/Patterns
The Patterns namespace contains several scripts that implement standard design patterns. These are mostly abstract classes that are designed to be uased by other classes and scripts that are more project-specific.
The Command pattern is relatively simple. It provides a way to perform actions and then undo them later in a generic way. Scripts that use the Command pattern should extend the Command class. Examples of some basic Unity commands can be seen in the UnityCommands script.
The Command class and the ICommand interface implement the Command pattern. They simply contain an Execute method and an Undo method. When executed, commands should carry out an action, and when undone, the executed action should be completely undone.
CommandHistory objects store a history of Commands that have been executed. They provide an interface to remember commands in the order they occured, and undo and redo commands as desired. CommandHistory objects can be used as ScriptableObject assets, or created through script.
Scripts that use the State pattern consist of several States and one or more StateMachine. Each state represents a single mode that the script can be in. They provide a more efficient way of laying out the logic required to switch modes. Rather than having a single function for the entire script that handles switching from one state to the next, each state includes the logic for entering and exiting the state within its own class.
The State class consists of the Enter(), Exit(), and Update() functions. Child classes may also include additional data related to the state's current settings.
The Enter() function is called when the state is entered. The Exit() function is called when the state is exited, usually just before entering another state. The Update() function should be called every frame for states that need to be updated in real time. This needs to be handled by the component that uses the state. These three functions are virtual, so any states that don't need any special logic to handle them can leave them out.
The StateMachine class keeps track of an object's current State and handles transitions between states. Setting a new state or going back to a previous state should be done through a state machine. State machines can be initialized to a certain state, or can start in the blank state.