top of page

Assignment 9

- State Pattern -

The objective of this assignment was to create a game feature with Unity and C# that uses an implementation of the state design pattern.

Play the Game

UML Diagram

- Traffic Control System -

Utilizing the State Pattern

state.png

The police officer is one object in the game makes use of the state pattern. The UML diagram above depicts its implementation.

state.png

The moving car is another object in the game makes use of the state pattern. The UML diagram above depicts its implementation.

state.png

The state machine for the moving car (top) and police officer (bottom).

Questions and Answers

1.    Briefly, what was the game feature you chose to create?

Inspired by more complicated roadway system simulations, I decided to create a police officer that tracks how fast a nearby car is travelling. If the car is travelling over the speed limit, the cop pulls the car over, performs some checks, and then lets the car go.

2.    What is the player able to do with the game feature you chose to create?

The game feature I made doesn’t help the player. Instead, it creates a challenge for the player; a rule that the player must obey. So as a player, I want to follow the speed limit so I can not get pulled over by the cops, hindering my gameplay progression. 

3.    What is the internal state or process you are trying to control with the State design pattern?

For this example, I thought it would be appropriate to have two internal states that I control with the state design pattern. 
One of these states is the car’s state. With the state pattern, I was able to delegate specific actions to each state of the car. For example, if the car is pulled over and I call the Move() method, nothing will happen because the car is pulled over! So with the state pattern, I was able to let each state deal with how it dea;s with certain “inputs” (methods), and on my end, I can call whichever method the situation calls for and know that the current state will deal with the situation appropriately.
The other state that I sought to control with the state design pattern is the police officer’s internal state. I think the state pattern really shined here because having separate states for the police officer seemed to click more with me than having multiple states with the car for some reason. (I think since the police officer is a person, it just made more sense to me). Just like with the car, each state of the police officer handles whatever method I call on it appropriately. So if the police officer is in the process of waiting for a speeding car and I call CompleteCheck(), nothing will break because the WaitForSpeedingCarState deals with that input as is needed.

4.    What triggers changes between the states?

Since I tried to emulate a real life police officer waving down a speeding car, the state of the car depends on the state of the police officer. This means the police officer triggers changes between states for the car, but there are times the car triggers state changes by itself. To be more specific, if the police officer catches the car going over the speed limit, he will wave down the car. This process involves the police officer changing the car’s internal state to CarGettingPulledOverState. From there, the police officer waits for the car to be next to him. The car checks to see if it is near the police officer, and when it is it changes its own state to CarPulledOverState. At the same time, the police officer waits until the car is done pulling over. Once it is, the police officer changes its own state to InvestigateCarState. From this point on, the police officer triggers state changes in the car as it completes its checks on the car.

5.    What were the benefits of using the State Pattern to make your game feature?

The state pattern helped me diagram how I wanted the police officer to behave with the car and vice versa. State diagrams were actually really helpful in visualizing how I wanted to construct my program. Specifically, each state was its own class, and the transitions between them were the methods that each state class contained (just like in the textbook). Additionally, I can’t help but relate the state pattern to how objects behave in real life. For example, when designing my police officer states, I kept the thought of how a real police officer behaves in the back of my mind. In real life, a police officer would wave down a speeding car, wait for it to pull over, perform checks, and then let the car go. I think I emulated this pretty well in my program!

6.    Did you find any drawbacks to using the State Pattern?  If so, what were they?

The only drawback was that I was initially a bit conflicted on which states I should have and which methods they should call. After some reevaluation, I stuck with the textbooks idea of having separate state classes and the methods inside of them be the transitions between the two states. Overall, I just need to practice using this pattern more until I get a feel for how and when I want to use it.

7.    How are the controls required to use the game feature you created communicated to the player in your unity project?

The controls are listed in the top right of the game window with the game feature instructions.

8.    How does the game communicate to players whether or not they have successfully used the game feature?

The success of the game feature is communicated through the car getting pulled over, the police officer UI panel that provides insight into what the police officer is doing, and the car getting back on the road.

Kyle Grenier

Immersive Technology and Game Developer

app-icon.png
GitHub-Mark-Light-64px.png
LinkedIn_icon_circle.png
bottom of page