Bevy Version: | 0.9 | (outdated!) |
---|
The App
Relevant official examples: All of them ;)
In particular, check out the complete game examples:
alien_cake_addict
,
breakout
.
To enter the bevy runtime, you need to configure an App
. The app
is how you define the structure of all the things that make up your project:
plugins, systems, event types,
states, stages…
fn main() {
App::new()
// Bevy itself:
.add_plugins(DefaultPlugins)
// resources:
.insert_resource(StartingLevel(3))
// if it implements `Default` or `FromWorld`
.init_resource::<MyFancyResource>()
// events:
.add_event::<LevelUpEvent>()
// systems to run once at startup:
.add_startup_system(spawn_things)
// systems to run each frame:
.add_system(player_level_up)
.add_system(debug_levelups)
.add_system(debug_stats_change)
// ...
// launch the app!
.run();
}
Technically, the App
contains the ECS World(s) (where all
the data is stored) and Schedule(s) (where all the systems
to run are stored). For advanced use-cases, Sub-apps are a
way to have more than one ECS World and Schedule.
Local resources do not need to be registered. They are part of their respective systems.
Component types do not need to be registered.
Schedules cannot (yet) be modified at runtime; all systems you
want to run must be added/configured in the App
ahead of time.
The data in the ECS World can be modified at any time; create/destroy your entities and resources, from systems using Commands, or exclusive systems using direct World access.
Resources can also be initialized ahead of time, here in the
App
builder.
Builtin Bevy Functionality
The Bevy game engine's own functionality is represented as a plugin group. Every typical Bevy app must first add it, using either:
DefaultPlugins
if you are making a full game/appMinimalPlugins
for something like a headless server.
Quitting the App
To cleanly shut down bevy, send an AppExit
event from any system:
use bevy::app::AppExit;
fn exit_system(mut exit: EventWriter<AppExit>) {
exit.send(AppExit);
}
For prototyping, Bevy provides a convenient system you can add, to close the
focused window on pressing the Esc
key. When all windows are closed, Bevy will
quit automatically.
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_system(bevy::window::close_on_esc)
.run();
}