Constructor
new CloudManager(scene)
Setting the default values for the class variables
Parameters:
| Name | Type | Description |
|---|---|---|
scene |
the Phaser.Scene object used for adding images/sprites/physics/etc... |
- Source:
Methods
create()
sets up the clouds initially
drawRow(minY, maxY)
This method is the core piece to the algorithm for creating the clouds. This method is used to create, position, and draw the clouds to
the renderTextures. I basically implemented my own grid and randomly placed each cloud inside that grid cell. It will randomly pick which
renderTexture it draws to using a weighted random pick out of an array.
Parameters:
| Name | Type | Description |
|---|---|---|
minY |
the minimum y position the cloud will draw to | |
maxY |
the maximum y position the cloud will draw to |
preload()
This function is called before anything is drawn on the canvas.
This allows for asset preloading which prevents anything from drawing without
it first loading the asset.
- Source:
resetG()
called whenever the game state changes from dead/paused to idle. This resets everything including the positions of the clouds
and creates new textures to use giving more variability to the game.
setupClouds()
This method is the core feature to drawing the actual clouds. Originally, this method created many
cloud game-objects with each one having their own physics body. However, this way of creating
the clouds I found to be more cpu intensive than I wanted. So I came up with my own way to make it less
of a hassle for the player's computer. I was looking through the Phaser docs and came across a way to actually
create a custom texture dynamically. Then I ended up applying this to my algorithm and in the end, I made it so
the game only has to update 6 different images compared to the 50 I had before. This method of creation can be scaled
infinitely and will have the exact same memory usage if you were to have 6 clouds.
- Source:
startG()
called at the start of the game. This is for if this class ever needs to execute code at the start of the game
stopG()
called at the end of the game or death. This will set the state to PAUSED and then maps through
each cloud group and sets the velocity to -5 giving a much slower movement since the player will no
longer be moving forward because they have died
update()
This is the core part to the pooling/recycling of the clouds. Once one of the cloud grouped images
is 100% past the left edge of the canvas, it will reposition 100% past the far right edge which gives an illusion
of infinite clouds.