While we can add a board and tiles to the screen, they won't be visible unless we set up a Camera first. To do that we'll bootstrap a new OrthographicCameraBundle
using the new_2d
method, since 2048 is a 2d game.
fn setup(mut commands: Commands) {
commands
.spawn_bundle(OrthographicCameraBundle::new_2d());
}
In addition to the concept of a Camera, there are two new ideas here. Bundles and Commands.
Bundles are ordered collections of components. They make it a bit easier to add lots of components at one time. For example, we didn't have to set up or configure the camera's components because we used the new_2d()
method which gave us a Bundle of components that set up a 2d camera for us.
In a Bevy game, basically everything is accessible through the World
struct. The World
struct stores and exposes operations on entities, components, and their associated metadata. Commands are how we call those operations. In this example we use a command to insert a new camera into our game.
Now that we have a setup
function, we can use that as a system in our Bevy App. We only want the setup function to run once at startup because we only ever need one camera for our game, so we'll use .add_startup_system
on the AppBuilder
.
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.run()
}
To create a system from our function, we use the .system()
function call on our function. This may seem a little odd if you're unfamiliar with Extension Traits. Basically what happened here is that Bevy extended the definition of functions with the ability to call .system()
on them to turn them into Systems.
This is how our function gets it's Commands
argument. Bevy understands from the type signature what our system needs, and makes sure that is available when we want to run it. We will use this functionality later to do more interesting queries for our systems.