The Assets Class

Well, we've done this before, so don't expect any surprises. Listing 12-1 shows you the code of the Assets class.

Listing 12-1., Loading and Storing Assets as Always package com.badlogic.androidgames.droidinvaders;

import com.badlogic.androidgames.framework.Music; import com.badlogic.androidgames.framework.Sound; import; import; import; import; import; import; import com.badlogic.androidgames.framework.impl.GLGame;

public class Assets {

public static Texture background; public static TextureRegion backgroundRegion; public static Texture items; public static TextureRegion logoRegion; public static TextureRegion menuRegion; public static TextureRegion gameOverRegion; public static TextureRegion pauseRegion; public static TextureRegion settingsRegion; public static TextureRegion touchRegion; public static TextureRegion accelRegion; public static TextureRegion touchEnabledRegion; public static TextureRegion accelEnabledRegion; public static TextureRegion soundRegion; public static TextureRegion soundEnabledRegion; public static TextureRegion leftRegion; public static TextureRegion rightRegion;

public static TextureRegion fireRegion; public static TextureRegion pauseButtonRegion; public static Font font;

We have a couple of members storing the texture of the UI elements as well as the background image. We also store a couple of TextureRegions as well as a Font. This covers all our UI needs.

public static Texture explosionTexture; public static Animation explosionAnim; public static Vertices3 shipModel; public static Texture shipTexture; public static Vertices3 invaderModel; public static Texture invaderTexture; public static Vertices3 shotModel; public static Vertices3 shieldModel;

We also have textures and Vertices3 instances that store the models and textures of our game's objects. We also have an Animation instance that holds the frames of the explosion animation.

public static Music music; public static Sound clickSound; public static Sound explosionSound; public static Sound shotSound;

Finally we have a couple of Music and Sound instances storing the game's audio.

public static void load(GLGame game) {

background = new Texture(game, "background.jpg", true); backgroundRegion = new TextureRegion(background, 0, 0, 480, 320); items = new Texture(game, "items.png", true); logoRegion = new TextureRegion(items, 0, 256, 384, 128); menuRegion = new TextureRegion(items, 0, 128, 224, 64); gameOverRegion = new TextureRegion(items, 224, 128, 128, 64); pauseRegion = new TextureRegion(items, 0, 192, 160, 64); settingsRegion = new TextureRegion(items, 0, 160, 224, 32); touchRegion = new TextureRegion(items, 0, 384, 64, 64); accelRegion = new TextureRegion(items, 64, 384, 64, 64); touchEnabledRegion = new TextureRegion(items, 0, 448, 64, 64); accelEnabledRegion = new TextureRegion(items, 64, 448, 64, 64); soundRegion = new TextureRegion(items, 128, 384, 64, 64); soundEnabledRegion = new TextureRegion(items, 190, 384, 64, 64); leftRegion = new TextureRegion(items, 0, 0, 64, 64); rightRegion = new TextureRegion(items, 64, 0, 64, 64); fireRegion = new TextureRegion(items, 128, 0, 64, 64); pauseButtonRegion = new TextureRegion(items, 0, 64, 64, 64); font = new Font(items, 224, 0, 16, 16, 20);

The load() method starts of by creating the Ul-related stuff. It's just some texture loading and region creation as usual.

explosionTexture = new Texture(game, "explode.png", true); TextureRegion[] keyFrames = new TextureRegion[16]; int frame = 0;

keyFrames[frame++] = new TextureRegion(expIosionTexture, x, y, 64, 64);

keyFrames[frame++] = new TextureRegion(expIosionTexture, x, y, 64, 64);

explosionAnim = new Animation(0.1f, keyFrames);

Next we create the Texture for the explosion animation along with the TextureRegions for each frame and the Animation instance. We simply loop from the top left to the bottom right in 64-pixel increments and create one TextureRegion per frame. All the regions are then fed to an Animation instance, whose frame duration is 0.1 second.

shipTexture = new Texture(game, "ship.png", true); shipModel = ObjLoader.Ioad(game, "ship.obj"); invaderTexture = new Texture(game, "invader.png", true); invaderModel = ObjLoader.Ioad(game, "invader.obj"); shieldModel = ObjLoader.Ioad(game, "shield.obj"); shotModel = ObjLoader.Ioad(game, "shot.obj");

Next we load the models and textures for the ship, the invaders, the shield blocks, and the shots. Pretty simple with our mighty ObjLoader, isn't it? Note that we use mipmapping for the Textures.

music = game.getAudio().newMusic("music.mp3"); music.setLooping(true); music.setVolume(0.5f); if (Settings.soundEnabIed);

cIickSound = game.getAudio().newSound("click.ogg"); expIosionSound = game.getAudio().newSound("explosion.ogg"); shotSound = game.getAudio().newSound("shot.ogg");

Finally we load the music and sound effects of the game. You can see a reference to the Settings class, which is essentially the same as in Super Jumper and Mr. Nom. This method will be called once when our game is started in the Droidlnvaders class we'll implement in a minute. Once all assets are loaded we can forget about most of them, except for the Textures, which we need to reload if the game is paused and then resumed.

public static void reload() { background.reload(); items.reload(); expIosionTexture.reload(); shipTexture.reload(); invaderTexture.reload(); if (Settings.soundEnabIed);

That's where the reload() method comes in. We'll call this method in the DroidInvaders.onResume() method so that our textures will be reloaded and the music will be unpaused.

public static void playSound(Sound sound) { if (Settings.soundEnabIed);

Finally we have the same convenience method we had in Super Jumper that will ease the pain of playing back a sound effect a little. When the user disabled sound we just don't play anything in this method.

NOTE Although this method of loading and managing assets is easy to implement, it can become a mess if you have more than a handful of assets. Another issue is that sometimes not all assets will fit into memory all at once. For simple games like the ones we've developed in his book the method is fine. I often use it in my games as well. For larger games you have to consider a more elaborate asset management strategy.

0 0

Post a comment