2020-11-28 00:39:59 +00:00
|
|
|
use bevy::{prelude::*, reflect::TypeRegistry, utils::Duration};
|
2020-08-03 19:00:00 +00:00
|
|
|
|
|
|
|
/// This example illustrates loading and saving scenes from files
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-11-28 00:39:59 +00:00
|
|
|
.register_type::<ComponentA>()
|
|
|
|
.register_type::<ComponentB>()
|
2020-11-17 02:18:00 +00:00
|
|
|
.add_startup_system(save_scene_system)
|
|
|
|
.add_startup_system(load_scene_system)
|
|
|
|
.add_startup_system(infotext_system)
|
|
|
|
.add_system(print_system)
|
2020-08-03 19:00:00 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:39:59 +00:00
|
|
|
// Registered components must implement the `Reflect` and `FromResources` traits.
|
|
|
|
// The `Reflect` trait enables serialization, deserialization, and dynamic property access.
|
|
|
|
// `Reflect` enable a bunch of cool behaviors, so its worth checking out the dedicated `reflect.rs` example.
|
2020-08-03 19:00:00 +00:00
|
|
|
// The `FromResources` trait determines how your component is constructed when it loads. For simple use cases you can just
|
|
|
|
// implement the `Default` trait (which automatically implements FromResources). The simplest registered component just needs
|
|
|
|
// these two derives:
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Reflect, Default)]
|
|
|
|
#[reflect(Component)] // this tells the reflect derive to also reflect component behaviors
|
2020-08-03 19:00:00 +00:00
|
|
|
struct ComponentA {
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some components have fields that cannot (or should not) be written to scene files. These can be ignored with
|
2020-11-28 00:39:59 +00:00
|
|
|
// the #[reflect(ignore)] attribute. This is also generally where the `FromResources` trait comes into play.
|
2020-08-03 19:00:00 +00:00
|
|
|
// `FromResources` gives you access to your App's current ECS `Resources` when you construct your component.
|
2020-11-28 00:39:59 +00:00
|
|
|
#[derive(Reflect)]
|
|
|
|
#[reflect(Component)]
|
2020-08-03 19:00:00 +00:00
|
|
|
struct ComponentB {
|
|
|
|
pub value: String,
|
2020-11-28 00:39:59 +00:00
|
|
|
#[reflect(ignore)]
|
2020-11-22 00:38:24 +00:00
|
|
|
pub time_since_startup: Duration,
|
2020-08-03 19:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromResources for ComponentB {
|
|
|
|
fn from_resources(resources: &Resources) -> Self {
|
|
|
|
let time = resources.get::<Time>().unwrap();
|
|
|
|
ComponentB {
|
|
|
|
time_since_startup: time.time_since_startup(),
|
|
|
|
value: "Default Value".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_scene_system(asset_server: Res<AssetServer>, mut scene_spawner: ResMut<SceneSpawner>) {
|
|
|
|
// Scenes are loaded just like any other asset.
|
2020-10-18 20:48:15 +00:00
|
|
|
let scene_handle: Handle<DynamicScene> = asset_server.load("scenes/load_scene_example.scn");
|
2020-08-03 19:00:00 +00:00
|
|
|
|
2020-09-18 00:16:38 +00:00
|
|
|
// SceneSpawner can "spawn" scenes. "Spawning" a scene creates a new instance of the scene in the World with new entity ids.
|
2020-08-03 19:00:00 +00:00
|
|
|
// This guarantees that it will not overwrite existing entities.
|
2020-10-18 20:48:15 +00:00
|
|
|
scene_spawner.spawn_dynamic(scene_handle);
|
2020-08-03 19:00:00 +00:00
|
|
|
|
|
|
|
// This tells the AssetServer to watch for changes to assets.
|
|
|
|
// It enables our scenes to automatically reload in game when we modify their files
|
|
|
|
asset_server.watch_for_changes().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This system prints all ComponentA components in our world. Try making a change to a ComponentA in load_scene_example.scn.
|
|
|
|
// You should immediately see the changes appear in the console.
|
2020-11-11 04:48:34 +00:00
|
|
|
fn print_system(query: Query<(Entity, &ComponentA), Changed<ComponentA>>) {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (entity, component_a) in query.iter() {
|
2020-08-03 19:00:00 +00:00
|
|
|
println!(" Entity({})", entity.id());
|
|
|
|
println!(
|
|
|
|
" ComponentA: {{ x: {} y: {} }}\n",
|
|
|
|
component_a.x, component_a.y
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_scene_system(_world: &mut World, resources: &mut Resources) {
|
|
|
|
// Scenes can be created from any ECS World. You can either create a new one for the scene or use the current World.
|
|
|
|
let mut world = World::new();
|
|
|
|
world.spawn((
|
|
|
|
ComponentA { x: 1.0, y: 2.0 },
|
|
|
|
ComponentB {
|
|
|
|
value: "hello".to_string(),
|
|
|
|
..ComponentB::from_resources(resources)
|
|
|
|
},
|
2020-11-28 00:39:59 +00:00
|
|
|
Transform::default(),
|
2020-08-03 19:00:00 +00:00
|
|
|
));
|
|
|
|
world.spawn((ComponentA { x: 3.0, y: 4.0 },));
|
|
|
|
|
2020-11-28 00:39:59 +00:00
|
|
|
// The TypeRegistry resource contains information about all registered types (including components). This is used to construct scenes.
|
2020-08-03 19:00:00 +00:00
|
|
|
let type_registry = resources.get::<TypeRegistry>().unwrap();
|
2020-11-28 00:39:59 +00:00
|
|
|
let scene = DynamicScene::from_world(&world, &type_registry);
|
2020-08-03 19:00:00 +00:00
|
|
|
|
|
|
|
// Scenes can be serialized like this:
|
2020-11-28 00:39:59 +00:00
|
|
|
println!("{}", scene.serialize_ron(&type_registry).unwrap());
|
2020-08-03 19:00:00 +00:00
|
|
|
|
|
|
|
// TODO: save scene
|
|
|
|
}
|
2020-08-14 10:56:39 +00:00
|
|
|
|
|
|
|
// This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone text example.
|
2020-11-08 20:34:05 +00:00
|
|
|
fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
2020-12-03 21:46:15 +00:00
|
|
|
commands.spawn(CameraUiBundle::default()).spawn(TextBundle {
|
2020-11-16 04:32:23 +00:00
|
|
|
style: Style {
|
|
|
|
align_self: AlignSelf::FlexEnd,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
text: Text {
|
|
|
|
value: "Nothing to see in this window! Check the console output!".to_string(),
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
style: TextStyle {
|
|
|
|
font_size: 50.0,
|
|
|
|
color: Color::WHITE,
|
2020-08-14 10:56:39 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-11-16 04:32:23 +00:00
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-08-14 10:56:39 +00:00
|
|
|
}
|