Game Development Reference
In-Depth Information
The sameColor() method will allow us to easily find out whether two colors are
exactly the same by comparing only one value instead of four.
Level holds a list of Rock instances called rocks . There is also a variable to hold an
instance of Clouds , Mountains , and WaterOverlay . All these variables are filled in
during the level loading process that takes place in the init() method.
Add the following code to the still empty init() method:
private void init (String filename) {
// objects
rocks = new Array<Rock>();
// 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++) {
AbstractGameObject obj = null;
float offsetHeight = 0;
// height grows from bottom to top
float baseHeight = pixmap.getHeight() - pixelY;
// get color of current pixel as 32-bit RGBA value
int currentPixel = pixmap.getPixel(pixelX, pixelY);
// find matching color value to identify block type at (x,y)
// point and create the corresponding game object if there is
// a match
// empty space
if (BLOCK_TYPE.EMPTY.sameColor(currentPixel)) {
// do nothing
}
// rock
else if (BLOCK_TYPE.ROCK.sameColor(currentPixel)) {
if (lastPixel != currentPixel) {
obj = new Rock();
float heightIncreaseFactor = 0.25f;
offsetHeight = -2.5f;
obj.position.set(pixelX, baseHeight * obj.dimension.y
* heightIncreaseFactor + offsetHeight);
rocks.add((Rock)obj);
} else {
rocks.get(rocks.size - 1).increaseLength(1);
 
Search WWH ::




Custom Search