Refactor Replay

This task continues from the flyweight solution. It is advisable to make a duplicate project or branch if you are using source control.

In this task we will not add any new functionality. It is likely, that the InputHandler class has started to turn into a bowl of spaghetti by now. All the switches or if-s to handle special interactions between record and play commands have turned the class highly unstable and expanding it is turning into a daunting task.

In this task we will go back to it and clean it up a bit using the state pattern for the recording functionality. Implement the state pattern according to the class diagram and state transitions according to the state diagram.

Getting Started

  • Create a new MonoBehaviour type script for InputRecorder and move the fields that are part of recording functionality from InputHandler to the new class.

  • Add InputRecorder to the InputHandler gameobject in Unity editor. Also use the GetComponent command in InputHandler start to get reference to this new recorder.

  • From there start implementing the rest of the state pattern according to class diagram.

  • Fill in the Enter, Play, Record and Add methods according to state diagram.

  • Clean up the InputHandler from any remaining functionality that is now handled by the recorder. Only a reference to recorder and a single call to Add() method should remain.

  • Think about how to share context data with the state objects, there are multiple correct solutions for this.
  • Think about how to manage the lifecycle of concrete state objects, there are multiple correct solutions for this.

  • Method parameters are not shown on the class diagram. See Game Programming Patterns state chapter for hints. There are multiple correct solutions for this.

Class Diagram

State diagram

;