Game Design Patterns

5 min. read

Game Design Patterns

Intro

Interfaces: Use interfaces so that your code works with any class that implements it, instead of just the one that does today.

Observers and messaging: lets 2 parts of the game talk to each other so that tomorrow, it can be 3 or 4.

Flexibility lets us change our game quickly and development speed is absolutely vital for getting to a fun experience! You cant design a fun experience on paper, it comes with, it demands, iteration and experimentation! The aster you can try out ideas and see how they FEEL, the more you can try and the more likely you are to find something great

“is that it’s easier to make a fun game fast than it is to make a fast game fun”

Excerpt From: Nystrom, Robert. “Game Programming Patterns.” iBooks.

Command Pattern
Gang of Four: Commands are an object-oriented replacements for callbacks

A Command represents a thing that can be done

Create base class of Command, then all the other Actions use command base class to Execute everything.

Make sure every data modification goes through a command.

Examples
Configurable input
User input, like keyboard, mouse clicks, etc..
Users can swap out buttons for different actions
Create a base class
Class Command
{
public virtual execute()
}
Class JumpCommand : Command
{
public override execute(){
jump();
}
private void jump(){}
}
Class FireCommand : Command
{
Public override execute(){
fire();
}
Private void fire();
}
Class InputHandler{
Public Command buttonX;
Public Command buttonY;
Public Command buttonA;
Public Command buttonB;
if(Input.GetButtonUp(“X”)) buttonX.execute();
if(Input.GetButtonUp(“Y”)) buttonY.execute();
if(Input.GetButtonUp(“A”)) buttonA.execute();
if(Input.GetButtonUp(“B”)) buttonB.execute();
}

Side note: instead of check if each button is NULL, we can create an object which execute function is empty

Commands can be combined with queueing with Event Queues
Event Queue use producers and consumers
If we make commands serializable, we can send these commands of the network. Which is important for multi-player games.

Undo and Redo
Class Command{
Public virtual execute()
Public virtual undo()
}

Cache the properties of the object before the execute function is called, this way, undo can be used. Store only the properties that you need, not the entire object.

For redo, use a list, with a pointer at the current state.

JavaScript example:
function makeMoveUnitCommand(unit, x, y) { var xBefore, yBefore; return { execute: function() { xBefore = unit.x(); yBefore = unit.y(); unit.moveTo(x, y); }, undo: function() { unit.moveTo(xBefore, yBefore); } }; }

Unity implementation
Test if it is better to have a class, which is instantiated or having a behaviour attached to the gameobject. This could cause the GC to run and cause spikes in performance.
Flyweight Pattern
Share data between objects

Observer Pattern
The Subject, can notify the Observers of any changes.
Use Actions or Delegates and Events.

Prototype Pattern

Singleton Pattern

State Pattern

AMVCC
Application
Model
Hold the application’s core data and state, such as player health or gun ammo.
Serialize, deserialize, and/or convert between types.
Load/save data (locally or on the web).
Notify Controllers of the progress of operations.
Store the Game State for the Game’s Finite State Machine.
Never access Views.
View
Can get data from Models in order to represent up-to-date game state to the user. For example, a View method player.Run() can internally use model.speed to manifest the player abilities.
Should never mutate Models.
Strictly implements the functionalities of its class. For example:
A PlayerView should not implement input detection or modify the Game State.
A View should act as a black box that has an interface, and notifies of important events.
Does not store core data (like speed, health, lives,…).

Controller
Do not store core data.
Can sometimes filter notifications from undesired Views.
Update and use the Model’s data.
Manages Unity’s scene workflow.

Component

References
http://www.habrador.com/tutorials/programming-patterns/

http://www.habrador.com/tutorials/programming-patterns/2-flyweight-pattern/

http://www.habrador.com/tutorials/programming-patterns/1-command-pattern/

https://cglearn.codelight.eu/pub/programming-patterns-in-computer-games/introduction-to-patterns-in-game-development

https://www.toptal.com/unity-unity3d/unity-with-mvc-how-to-level-up-your-game-development

https://bitbucket.org/eduardo_costa/thelab-unity-mvc/overview

https://en.wikipedia.org/wiki/Software_design_pattern

https://github.com/Naphier/unity-design-patterns

https://bf8ff395-a-62cb3a1a-s-sites.googlegroups.com/site/sdfgfgtyuyiiohftdfghjgkk43296/kjkjhi3832/Game-Programming-Patterns.pdf?attachauth=ANoY7coz9nzrfR36GwJrjSer6mX9030a8bhHzYxNa14cLRkrlXgiv40odU6gT_j1rv257r8oyO0L2wfxQJfIJjyBw0zBwpP4AqaH1yiDtvMkqrMyppkWBvbDcrG2lJr5rkaNkAZiMsZKtwGzXmwyToNoIuKkZTdWIMZKTzQTLybgbjcb90FiW1OjMyMO4_PQOgsFKZ8RiWdJOHtgTlZrRwY0NASoHQkaUWthsYqkVOKuDRvheukzw_5lQQ6qARqIMx5XgAdwWLF8mgNHfx63UkD_zuCRwUdgdg%3D%3D&attredirects=0

http://www.satori.org/game-programming-gems/