Game Programming Patterns
In the course of the subject "Game Programming Patterns", our docent asked us to create a Unity game using a hand full of patterns. The structure of the game and code patterns will be presented below.
The player steers the blue blob-character using mouse clicks on the ground. Furthermore he got 3 abilities: Q-dash, W-Jump, E-extend tongue. Each of these require a certain amount of energy, which the player gains by collecting food with their tongue. Other blob-creatures are also running around, looking for these delicious pieces of food. Unfortunately, the player is so small they think of him as food as well. When they got close enough they can take a bite of the player: His energy will be reduced by one the amount of one of the four bars below. Is the player able to fill all four of them with energy, he wins.
Code
Here you can download the scripts or the whole Unity-Project itself, if you want to take a closer look.
Generic Singleton
Scripts like "GameManager" or "PlayerLevelSupervisor" inherit from "Singleton", which makes sure there is only one instance of them at any given time. Also, it makes the scripts accessable anywhere.
State Pattern
The player can have 5 levels which he gains or loses during the game.
To express this, the script has an enum "Level", which get's used for the variable "playerLvl". It get's set in the function "SetPlayerLevel()".
To express this, the script has an enum "Level", which get's used for the variable "playerLvl". It get's set in the function "SetPlayerLevel()".
Observer Pattern
The player gains energy by consuming food or loses some when an enemy takes a bite.
When this happens, the event "EnergyChanged()" in "PlayerBlob" get's called. Subscribed are "UpdatePlayerEnergyUI()" from "PlayerInterfaceManager" and "SetPlayerLevel()" from "PlayerLevelSupervisor".
When this happens, the event "EnergyChanged()" in "PlayerBlob" get's called. Subscribed are "UpdatePlayerEnergyUI()" from "PlayerInterfaceManager" and "SetPlayerLevel()" from "PlayerLevelSupervisor".
Object Pooling
Food objects get spawned over and over again, as well eaten by the player or hostile blobs. They get spawned from an pool. "FoodPool" derives from the generic class "GameObjectPool". If "GetObject()" is called, the queue will return an object, or - if none are there - instanciate a new object to return.
Flyweight Pattern - Scriptable objects
"CharacterTemplate" derives from "Scriptable Object". The player as well as hostile blobs implement a SO based on "CharacterTemplate", which makes them share some variables like "energyLoss" and "baseEnergy".