-
Notifications
You must be signed in to change notification settings - Fork 1
Game World
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.
As explained in the Rendering chapter we need to register a renderer in order to display game objects on the screen:
// Retrieve the renderManager instance
RenderManager renderManager = context.getRenderManager();
// Registering a new renderer for game objects of type '1'
renderManager.register(1, new QuadRenderer(Color.RED));
// Set the type to '1'
object.setType(1);
When running again you should see a red square on the screen. Congratulations. You rendered your first GameObject
with braingdx.
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.
All supported bounds implementations
- RectWorldBounds represents a rectangle to describe the bounds of the world.
- CircleWorldBounds represents a circle to describe the bounds of the world.
-
StickyWorldBounds decorates
WorldBounds
implementations to stick the center of the bounds to anGameObject
.