Game Development Reference
In-Depth Information
The construction of the
Mountains
class is quite similar to
Rock
. It inherits from
AbstractGameObject
and uses a
length
variable to store the number of times the
image needs to be repeated.
The
drawMountain()
method is used to encapsulate the drawing code of a mountain
layer so that the task to draw the three layers is greatly simplified. The tinting is
achieved by setting the drawing color of
SpriteBatch
to the desired color value
using the
setColor()
method. All subsequent draw calls will now produce tinted
images. Afterwards, the drawing color is reset to a neutral white color, which simply
means that the texture colors will be no longer manipulated. This might cause future
calls to
SpriteBatch
to tint irrespective of whatever is drawn next.
The water overlay object
The water overlay game object is very simple compared to the previous game
objects. It consists of only one image. This image needs to overlay the ground of the
whole level. There are several ways to achieve this. One way would be to span the
image from side to side of the camera's viewport and move the overlay together with
the camera whenever the camera is moved. This is a good way to create an illusion
of water being everywhere in the level. Unfortunately, special care needs to be taken
if the camera is also moved in a vertical direction. Another way to implement the
image of water everywhere in the level is to just draw a single, horizontally stretched
water overlay image from start to end, as shown in the following screenshot. This is
exactly what the next code will do.
Create a new file for the
WaterOverlay
class and add the following code:
package com.packtpub.libgdx.canyonbunny.game.objects;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.packtpub.libgdx.canyonbunny.game.Assets;
public class WaterOverlay extends AbstractGameObject {