Game Development Reference
In-Depth Information
The scanning is done by looping through each pixel starting from the top-left
corner of the image to the bottom-right corner. The baseHeight variable is set to
the maximum level height minus the current height of the currently scanned pixel,
which results in a flipped vertical pixel position. What this basically means is that
the game objects will appear at their correct height in the game world, although
scanning is done from top to bottom and the game objects grow from the bottom up.
The offsetHeight variable is used to individually offset an object to correctly fit into
the game world. The currentPixel variable stores the color value of the currently
scanned pixel. Next, this value is compared to each defined color code of a game
object until a match has been found. If no color code matches, there will be an error
message logged to the console to indicate an implementation error in our game. The
error can either mean the use of an undefined color code or that the identification
method does not handle this color code. The error message will contain the decoded
RGBA values to make troubleshooting a bit less painful.
After the scanning process, there are still some game objects left that need to
be initialized. These are the decoration game objects Clouds , Mountains , and
WaterOverlay . These are passed over to the width of Pixmap , which is the actual
length of the level they need to know to work correctly.
Lastly, Pixmap is disposed to properly free the occupied memory.
The code part we left out up until now is to look at what happens if there is a
matching color code. The current implementation already handles the color code of
every game object we want to use, but except for the rock game objects, there is no
defined action for these yet. A new rock will be created and added to the list of rocks
if the corresponding color code matches. There is also a lastPixel variable that
stores the last value of currentPixel after each iteration inside the loop. This value
is used to detect adjacent rock pixels that will increase the length of the last created
rock by one, in this case, instead of creating a new one.
Add the following code to the still empty render() method:
public void render (SpriteBatch batch) {
// Draw Mountains
mountains.render(batch);
// Draw Rocks
for (Rock rock : rocks)
rock.render(batch);
// Draw Water Overlay
waterOverlay.render(batch);
 
Search WWH ::




Custom Search