Now that we have implemented all the game objects of Canyon Bunny, we can complete the level loader.
First, add the following import lines to Level
:
import com.packtpub.libgdx.canyonbunny.game.objects.BunnyHead; import com.packtpub.libgdx.canyonbunny.game.objects.Feather; import com.packtpub.libgdx.canyonbunny.game.objects.GoldCoin;
Additionally, add these three member variables to the same class:
public BunnyHead bunnyHead; public Array<GoldCoin> goldcoins; public Array<Feather> feathers;
After this, modify the init()
and render()
methods:
private void init (String filename) { // player character bunnyHead = null; // objects rocks = new Array<Rock>(); goldcoins = new Array<GoldCoin>(); feathers = new Array<Feather>(); // load image file that represents the level data Pixmap pixmap = new Pixmap(Gdx.files.internal(filename)); // scan pixels from top-left to bottom-right int lastPixel = -1; for (int pixelY = 0; pixelY < pixmap.getHeight(); pixelY++) { for (int pixelX = 0; pixelX < pixmap.getWidth(); pixelX++) { ... // rock else if (BLOCK_TYPE.ROCK.sameColor(currentPixel)) { ... } // player spawn point else if (BLOCK_TYPE.PLAYER_SPAWNPOINT.sameColor(currentPixel)) { obj = new BunnyHead(); offsetHeight = -3.0f; obj.position.set(pixelX,baseHeight * obj.dimension.y + offsetHeight); bunnyHead = (BunnyHead)obj; } // feather else if(BLOCK_TYPE.ITEM_FEATHER.sameColor(currentPixel)) { obj = new Feather(); offsetHeight = -1.5f; obj.position.set(pixelX,baseHeight * obj.dimension.y + offsetHeight); feathers.add((Feather)obj); } // gold coin else if (BLOCK_TYPE.ITEM_GOLD_COIN.sameColor(currentPixel)) { obj = new GoldCoin(); offsetHeight = -1.5f; obj.position.set(pixelX,baseHeight * obj.dimension.y + offsetHeight); goldcoins.add((GoldCoin)obj); } // unknown object/pixel color else { ... } lastPixel = currentPixel; } } ... }
This code adds the actors to the level loading process in the init()
method. Next, make the following changes to the render()
method:
public void render (SpriteBatch batch) { // Draw Mountains mountains.render(batch); // Draw Rocks for (Rock rock : rocks) rock.render(batch); // Draw Gold Coins for (GoldCoin goldCoin : goldcoins) goldCoin.render(batch); // Draw Feathers for (Feather feather : feathers) feather.render(batch); // Draw Player Character bunnyHead.render(batch); // Draw Water Overlay waterOverlay.render(batch); // Draw Clouds clouds.render(batch); }
Then, add the following lines to Level
:
public void update (float deltaTime) { bunnyHead.update(deltaTime); for(Rock rock : rocks) rock.update(deltaTime); for(GoldCoin goldCoin : goldcoins) goldCoin.update(deltaTime); for(Feather feather : feathers) feather.update(deltaTime); clouds.update(deltaTime); }
We added the new actors to the render()
method and created a new update()
method so that we can collectively update all the game world objects in a level in one call.
Finally, modify the update()
method of WorldController
as the following listings:
public void update (float deltaTime) {
handleDebugInput(deltaTime);
level.update(deltaTime);
cameraHelper.update(deltaTime);
}
These changes makes sure that all the game objects contained within the level will be updated when the update()
method of WorldController
is called.
3.135.247.181