Using text files are okay, but using PNG or Bitmap graphics are better. A pixel can potentially store up to 4 bytes of information, and can be easily be created using photo manipulation tools.
First, before all, you should have an "Entity" or "GameObject" class that everything in your scene can inherit. Example:
public class Entity
{
public float x, y, width, height;
public void onStep() {}
public void onDraw() {}
public RectF getRectF()
{
RectF rect = new RectF();
rect.left = x;
rect.top = y;
rect.right = x + width;
rect.bottom = y + height;
return rect;
}
}
Second, you should have a handful of different classes that extend the Entity class, such as a Player class, a Wall class, and maybe a Coin class:
public class Player extends Entity {...}
public class Wall extends Entity {...}
public class Coin extends Entity {...}
Your next step would be to override the Player's step event, and in that step event, iterate through all of the wall entities like so:
@Override public void onStep()
{
for (Entity e : my_active_entities)
{
if (e instanceof Wall)
{
Wall wall = (Wall) e;
if (RectF.intersects(wall.getRectF(), this.getRectF()))
{
// We now know that our player is intersecting with "e"
// We also know that "e" is a wall
wall.doSomething();
this.doSomething();
}
}
}
}
Lastly, would be to create your own stage loader. This will iterate through your graphic and plot objects on the screen based on the color of the current pixel.
Bitmap bitmap = getOurBitmapFromSomewhere();
for (int x = 0; x < bitmap.getWidth(); x++)
for (int y = 0; y < bitmap.getHeight(); y++)
{
int color = bitmap.getPixel(x, y);
switch (color)
{
case 0xFF00FF00:
Player player = new Player();
player.x = x * TILE_SIZE;
player.y = y * TILE_SIZE;
my_active_entities.add(player);
break;
}
}
This wraps up my two cents of game object handling, collision detection, and level loading. Anyone who stumbles by this is more than welcome to use this code or this concept of code in their software.