worlds and tiles - Vrekt/Lunar GitHub Wiki
Worlds
Worlds are essential for each level of your game. Worlds for example, handle entities, tiles, rendering and more. Below is a basic example of a custom World.
public class MyWorld extends World {
public MyWorld(String name, int width, int height) {
super(name, width, height);
}
@Override
public void onDraw(Graphics graphics) {
// Draw all entities, draw textures, etc.
}
@Override
public void onTick() {
// Update entities, etc.
}
}
name
is the name of the world.width, height
are the dimensions of the newly created world.
The World object also holds many useful methods, these include
- adding/removing entities.
- Drawing all entities/tiles at once.
- RayTracing and rendering methods.
- etc.
Tiles/Sprites
Tiles are your textures and game objects. Tiles include the unique ID, texture, dimensions, whether its solid or not and more.
To make a new tile we must start by getting the right texture for our tile.
This can be done with the SpriteManager
. The SpriteManager
is a utility designed to extract textures from a
sprite-sheet.
Loading the sprite-sheet in is as easy as this.
SpriteManager sm = new SpriteManager(SpriteManager.load("pathToSheet.png"));
Now we can extract our texture. My texture is at 0, 0 and its 64x64.
BufferedImage myTexture = sm.getSectionAt(0, 0, 64, 64);
Now that we have the texture we can create a basic tile.
Tile tile = new Tile(myTexture, 0, true);
Storing tiles is easy also, use the AssetManager
.
AssetManager am = new AssetManager();
Now lets add our newly created tile.
am.addTile(tile);