bevy/examples/ecs/ecs_guide.rs

297 lines
12 KiB
Rust
Raw Normal View History

2020-07-17 01:47:51 +00:00
use bevy::{
app::{AppExit, ScheduleRunnerPlugin, ScheduleRunnerSettings},
2020-07-17 01:47:51 +00:00
prelude::*,
};
2020-05-03 07:21:32 +00:00
use rand::random;
use std::time::Duration;
2020-05-01 20:12:47 +00:00
/// This is a guided introduction to Bevy's "Entity Component System" (ECS)
/// All Bevy app logic is built using the ECS pattern, so definitely pay attention!
///
/// Why ECS?
/// * Data oriented: Functionality is driven by data
/// * Clean Architecture: Loose coupling of functionality / prevents deeply nested inheritance
/// * High Performance: Massively parallel and cache friendly
///
/// ECS Definitions:
///
/// Component: just a normal Rust data type. generally scoped to a single piece of functionality
/// Examples: position, velocity, health, color, name
///
/// Entity: a collection of components with a unique id
/// Examples: Entity1 { Name("Alice"), Position(0, 0) }, Entity2 { Name("Bill"), Position(10, 5) }
/// Resource: a shared global piece of data
/// Examples: asset_storage, events, system state
///
/// System: runs logic on entities, components, and resources
/// Examples: move_system, damage_system
///
/// Now that you know a little bit about ECS, lets look at some Bevy code!
2020-05-03 07:21:32 +00:00
/// We will now make a simple "game" to illustrate what Bevy's ECS looks like in practice.
//
// COMPONENTS: Pieces of functionality we add to entities. These are just normal Rust data types
//
2020-05-03 07:21:32 +00:00
// Our game will have a number of "players". Each player has a name that identifies them
struct Player {
name: String,
}
2020-05-03 07:21:32 +00:00
// Each player also has a score. This component holds on to that score
struct Score {
value: usize,
2020-05-01 20:12:47 +00:00
}
//
// RESOURCES: "Global" state accessible by systems. These are also just normal Rust data types!
//
2020-05-01 20:12:47 +00:00
2020-05-03 07:21:32 +00:00
// This resource holds information about the game:
2020-05-01 20:12:47 +00:00
#[derive(Default)]
2020-05-03 07:21:32 +00:00
struct GameState {
current_round: usize,
total_players: usize,
winning_player: Option<String>,
2020-05-01 20:12:47 +00:00
}
2020-05-03 07:48:36 +00:00
// This resource provides rules for our "game".
2020-05-03 07:21:32 +00:00
struct GameRules {
winning_score: usize,
max_rounds: usize,
max_players: usize,
2020-05-01 20:12:47 +00:00
}
//
2020-05-03 07:48:36 +00:00
// SYSTEMS: Logic that runs on entities, components, and resources. These generally run once each time the app updates.
//
2020-05-01 20:12:47 +00:00
2020-05-03 07:48:36 +00:00
// This is the simplest type of system. It just prints "This game is fun!" on each run:
2020-05-03 07:21:32 +00:00
fn print_message_system() {
println!("This game is fun!");
2020-05-01 20:12:47 +00:00
}
2020-05-03 07:21:32 +00:00
// Systems can also read and modify resources. This system starts a new "round" on each update:
2020-05-03 07:48:36 +00:00
// NOTE: "mut" denotes that the resource is "mutable"
2020-05-14 00:52:47 +00:00
// Res<GameRules> is read-only. ResMut<GameState> can modify the resource
fn new_round_system(game_rules: Res<GameRules>, mut game_state: ResMut<GameState>) {
2020-05-03 07:21:32 +00:00
game_state.current_round += 1;
println!(
"Begin round {} of {}",
game_state.current_round, game_rules.max_rounds
);
2020-05-01 20:12:47 +00:00
}
// This system updates the score for each entity with the "Player" and "Score" component.
2020-07-10 04:18:35 +00:00
fn score_system(mut query: Query<(&Player, &mut Score)>) {
for (player, mut score) in query.iter_mut() {
let scored_a_point = random::<bool>();
if scored_a_point {
score.value += 1;
println!(
"{} scored a point! Their score is: {}",
player.name, score.value
);
} else {
println!(
"{} did not score a point! Their score is: {}",
player.name, score.value
);
}
2020-05-03 07:21:32 +00:00
}
// this game isn't very fun is it :)
2020-05-01 20:12:47 +00:00
}
2020-05-03 07:21:32 +00:00
// This system runs on all entities with the "Player" and "Score" components, but it also
// accesses the "GameRules" resource to determine if a player has won.
// NOTE: resources must always come before worlds/queries in system functions
2020-05-03 07:21:32 +00:00
fn score_check_system(
2020-05-14 00:52:47 +00:00
game_rules: Res<GameRules>,
mut game_state: ResMut<GameState>,
query: Query<(&Player, &Score)>,
2020-05-28 20:36:48 +00:00
) {
for (player, score) in query.iter() {
2020-05-28 20:36:48 +00:00
if score.value == game_rules.winning_score {
game_state.winning_player = Some(player.name.clone());
}
}
}
2020-05-03 07:21:32 +00:00
// This system ends the game if we meet the right conditions. This fires an AppExit event, which tells our
2020-05-03 07:48:36 +00:00
// App to quit. Check out the "event.rs" example if you want to learn more about using events.
2020-05-03 07:21:32 +00:00
fn game_over_system(
2020-05-14 00:52:47 +00:00
game_rules: Res<GameRules>,
game_state: Res<GameState>,
mut app_exit_events: ResMut<Events<AppExit>>,
2020-05-03 07:21:32 +00:00
) {
if let Some(ref player) = game_state.winning_player {
println!("{} won the game!", player);
app_exit_events.send(AppExit);
} else if game_state.current_round == game_rules.max_rounds {
println!("Ran out of rounds. Nobody wins!");
app_exit_events.send(AppExit);
}
println!();
2020-05-01 20:12:47 +00:00
}
2020-06-04 03:08:20 +00:00
// This is a "startup" system that runs exactly once when the app starts up. Startup systems are generally used to create
2020-05-29 05:37:28 +00:00
// the initial "state" of our game. The only thing that distinguishes a "startup" system from a "normal" system is how it is registered:
// Startup: app.add_startup_system(startup_system)
// Normal: app.add_system(normal_system)
fn startup_system(commands: &mut Commands, mut game_state: ResMut<GameState>) {
2020-05-03 07:21:32 +00:00
// Create our game rules resource
commands.insert_resource(GameRules {
2020-05-03 07:21:32 +00:00
max_rounds: 10,
winning_score: 4,
max_players: 4,
});
2020-05-01 20:12:47 +00:00
2020-05-03 07:21:32 +00:00
// Add some players to our world. Players start with a score of 0 ... we want our game to be fair!
commands.spawn_batch(vec![
2020-07-10 04:18:35 +00:00
(
Player {
name: "Alice".to_string(),
},
Score { value: 0 },
),
(
Player {
name: "Bob".to_string(),
},
Score { value: 0 },
),
]);
2020-05-01 20:12:47 +00:00
2020-05-03 07:48:36 +00:00
// set the total players to "2"
2020-05-03 07:21:32 +00:00
game_state.total_players = 2;
2020-05-01 20:12:47 +00:00
}
2020-05-29 05:37:28 +00:00
// This system uses a command buffer to (potentially) add a new player to our game on each iteration.
// Normal systems cannot safely access the World instance directly because they run in parallel.
2020-07-10 04:18:35 +00:00
// Our World contains all of our components, so mutating arbitrary parts of it in parallel is not thread safe.
2020-05-01 20:12:47 +00:00
// Command buffers give us the ability to queue up changes to our World without directly accessing it
2020-07-10 04:18:35 +00:00
// NOTE: Command buffers must always come before resources and queries in system functions
2020-05-03 07:21:32 +00:00
fn new_player_system(
commands: &mut Commands,
2020-05-14 00:52:47 +00:00
game_rules: Res<GameRules>,
mut game_state: ResMut<GameState>,
2020-05-03 07:21:32 +00:00
) {
// Randomly add a new player
let add_new_player = random::<bool>();
if add_new_player && game_state.total_players < game_rules.max_players {
game_state.total_players += 1;
2020-07-10 04:18:35 +00:00
commands.spawn((
Player {
name: format!("Player {}", game_state.total_players),
},
Score { value: 0 },
));
2020-05-03 07:21:32 +00:00
println!("Player {} joined the game!", game_state.total_players);
}
2020-05-01 20:12:47 +00:00
}
2020-05-03 07:48:36 +00:00
// If you really need full, immediate read/write access to the world or resources, you can use a "thread local system".
2020-05-01 20:12:47 +00:00
// These run on the main app thread (hence the name "thread local")
2020-05-03 07:48:36 +00:00
// WARNING: These will block all parallel execution of other systems until they finish, so they should generally be avoided if you
// care about performance
2020-05-03 07:21:32 +00:00
#[allow(dead_code)]
fn thread_local_system(world: &mut World, resources: &mut Resources) {
// this does the same thing as "new_player_system"
let mut game_state = resources.get_mut::<GameState>().unwrap();
let game_rules = resources.get::<GameRules>().unwrap();
// Randomly add a new player
let add_new_player = random::<bool>();
if add_new_player && game_state.total_players < game_rules.max_players {
2020-07-10 04:18:35 +00:00
world.spawn((
Player {
name: format!("Player {}", game_state.total_players),
},
Score { value: 0 },
));
2020-05-03 07:21:32 +00:00
game_state.total_players += 1;
}
2020-05-01 20:12:47 +00:00
}
2020-07-10 04:18:35 +00:00
// Sometimes systems need their own unique "local" state. Bevy's ECS provides Local<T> resources for this case.
// Local<T> resources are unique to their system and are automatically initialized on your behalf (if they don't already exist).
2020-07-10 08:37:06 +00:00
// If you have a system's id, you can also access local resources directly in the Resources collection using `Resources::get_local()`.
2020-07-10 04:18:35 +00:00
// In general you should only need this feature in the following cases:
// 1. You have multiple instances of the same system and they each need their own unique state
// 2. You already have a global version of a resource that you don't want to overwrite for your current system
// 3. You are too lazy to register the system's resource as a global resource
2020-05-01 20:12:47 +00:00
2020-07-10 04:18:35 +00:00
#[derive(Default)]
2020-05-01 20:12:47 +00:00
struct State {
counter: usize,
}
2020-05-03 07:48:36 +00:00
// NOTE: this doesn't do anything relevant to our game, it is just here for illustrative purposes
2020-05-03 07:21:32 +00:00
#[allow(dead_code)]
fn local_state_system(mut state: Local<State>, query: Query<(&Player, &Score)>) {
for (player, score) in query.iter() {
println!("processed: {} {}", player.name, score.value);
}
println!("this system ran {} times", state.counter);
2020-05-01 20:12:47 +00:00
state.counter += 1;
}
// Our Bevy app's entry point
fn main() {
2020-05-03 07:48:36 +00:00
// Bevy apps are created using the builder pattern. We use the builder to add systems, resources, and plugins to our app
App::build()
// Resources can be added to our app like this
2020-05-03 07:21:32 +00:00
.add_resource(State { counter: 0 })
// Some systems are configured by adding their settings as a resource
.add_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs(5)))
// Plugins are just a grouped set of app builder calls (just like we're doing here).
// We could easily turn our game into a plugin, but you can check out the plugin example for that :)
// The plugin below runs our app's "system schedule" once every 5 seconds (configured above).
.add_plugin(ScheduleRunnerPlugin::default())
// Resources that implement the Default or FromResources trait can be added like this:
2020-05-03 07:21:32 +00:00
.init_resource::<GameState>()
// Startup systems run exactly once BEFORE all other systems. These are generally used for
2020-05-03 07:21:32 +00:00
// app initialization code (ex: adding entities and resources)
.add_startup_system(startup_system.system())
2020-05-03 07:48:36 +00:00
// my_system.system() calls converts normal rust functions into ECS systems:
2020-05-03 07:21:32 +00:00
.add_system(print_message_system.system())
//
// SYSTEM EXECUTION ORDER
//
// By default, all systems run in parallel. This is efficient, but sometimes order matters.
// For example, we want our "game over" system to execute after all other systems to ensure we don't
// accidentally run the game for an extra round.
//
2020-05-14 00:52:47 +00:00
// First, if a system writes a component or resource (ComMut / ResMut), it will force a synchronization.
2020-05-03 07:21:32 +00:00
// Any systems that access the data type and were registered BEFORE the system will need to finish first.
// Any systems that were registered _after_ the system will need to wait for it to finish. This is a great
// default that makes everything "just work" as fast as possible without us needing to think about it ... provided
// we don't care about execution order. If we do care, one option would be to use the rules above to force a synchronization
// at the right time. But that is complicated and error prone!
//
// This is where "stages" come in. A "stage" is a group of systems that execute (in parallel). Stages are executed in order,
2020-05-03 07:48:36 +00:00
// and the next stage won't start until all systems in the current stage have finished.
// add_system(system) adds systems to the UPDATE stage by default
// However we can manually specify the stage if we want to. The following is equivalent to add_system(score_system.system())
2020-05-03 07:21:32 +00:00
.add_system_to_stage(stage::UPDATE, score_system.system())
2020-05-03 07:48:36 +00:00
// We can also create new stages. Here is what our games stage order will look like:
2020-05-06 01:44:32 +00:00
// "before_round": new_player_system, new_round_system
2020-05-03 07:48:36 +00:00
// "update": print_message_system, score_system
// "after_round": score_check_system, game_over_system
2020-05-03 07:21:32 +00:00
.add_stage_before(stage::UPDATE, "before_round")
2020-05-03 07:48:36 +00:00
.add_stage_after(stage::UPDATE, "after_round")
2020-05-03 07:21:32 +00:00
.add_system_to_stage("before_round", new_round_system.system())
.add_system_to_stage("before_round", new_player_system.system())
2020-05-03 07:48:36 +00:00
.add_system_to_stage("after_round", score_check_system.system())
.add_system_to_stage("after_round", game_over_system.system())
2020-05-03 07:21:32 +00:00
// score_check_system will run before game_over_system because score_check_system modifies GameState and game_over_system
2020-05-04 21:18:46 +00:00
// reads GameState. This works, but it's a bit confusing. In practice, it would be clearer to create a new stage that runs
2020-05-03 07:48:36 +00:00
// before "after_round"
2020-05-03 07:21:32 +00:00
// This call to run() starts the app we just built!
.run();
2020-05-03 07:21:32 +00:00
}