-
Notifications
You must be signed in to change notification settings - Fork 1
Game World
This chapter is about the GameWorld
class provided via the GameContext
of braingdx. Every screen has an own GameWorld
object, available via the context.getGameWorld()
method.
Worlds contain objects of type GameObject
in order to make games alive:
// inside an AbstractScreen implementation
GameWorld world = context.getGameWorld();
// Creating a new game object in the world
GameObject object = world.addGameObject();
// Set the position of the game object in the world
object.setPosition(100, 200);
When running the game you will not see anything because the concept of game objects is virtual. Game objects exist in the game world but the game world does not know how to display them. Also the game objects itself have no information about rendering since they are stateful plain Java objects.
By default a game world has infinite size in terms of bounds. However, when building a side scroller or space game you want to remove game objects as soon as they leave the screen/bounds of the world. braingdx provides a utility of so called World Bounds:
GameWorld world = context.getGameWorld();
// Set the world bounds to 1000px width and 2000px height
world.setBounds(new RectWorldBounds(1000, 2000));
As soon as a game object leaves the bounds completely it will get removed from the game world. Implement your own bounds implementation by implementing the WorldBounds
interface.