Manager Classes
To make the code more readable and modular we split up different parts of the game into different manager classes which can be found in src/classes/game
.
Each class handles a separate portion of the game, such as spawning and despawning obstacles in the case of ObstacleManager.js or everything involving the player in the case of PlayerCharacter.js.
An instance for each of these managers is created in the create
function of Runner.js.
Since we treat all of these managers except for the PlayerCharacter as modules, they can simply be commented out if you don't need them and the game will still run, just with a certain manager disabled.
Runner.js
// Runs once to initialize the game
create () {
// Set player and follow camera
this.playerCharacter = new PlayerCharacter(this, this.audioRefs, this.selectedCharSprite)
this.cameras.main.startFollow(this.playerCharacter.sprite)
this.cameras.main.setLerp(1, 0) // Only follow horizontally
this.cameras.main.originX = 0.08
// Initialize an instance for each manager class
// Each of these can be simply commented out to disable a module if it isn't needed
this.environmentManager = new EnvironmentManager(this, this.playerCharacter.sprite, this.selectedCloudSprite)
this.groundManager = new GroundManager(this, this.playerCharacter.sprite)
this.obstacleManager = new ObstacleManager(this, this.playerCharacter.sprite, this.selectedObstacleSprite)
this.userInterface = new UserInterface(this, this.restartGame, this.returnToMenu, this.audioRefs)
Example with EnvironmentManager, ObstacleManager and UserInterface being commented out
As mentioned before we don't go into too much detail about the gameplay implementation with phaser in this tutorial, but the official tutorial is great, so if you want to update portions of these manager classes or find the code hard to understand, please have a look at that first.
In the next chapter we will look at how blockchain functionality is implemented in dino runner.