use bevy::{prelude::*, reflect::TypeRegistry, utils::Duration};
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.register_type::<ComponentA>()
.register_type::<ComponentB>()
.add_startup_system(save_scene_system.exclusive_system())
.add_startup_system(load_scene_system.system())
.add_startup_system(infotext_system.system())
.add_system(print_system.system())
.run();
}
#[derive(Reflect, Default)]
#[reflect(Component)] struct ComponentA {
pub x: f32,
pub y: f32,
}
#[derive(Reflect)]
#[reflect(Component)]
struct ComponentB {
pub value: String,
#[reflect(ignore)]
pub time_since_startup: Duration,
}
impl FromWorld for ComponentB {
fn from_world(world: &mut World) -> Self {
let time = world.get_resource::<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>) {
let scene_handle: Handle<DynamicScene> = asset_server.load("scenes/load_scene_example.scn.ron");
scene_spawner.spawn_dynamic(scene_handle);
asset_server.watch_for_changes().unwrap();
}
fn print_system(query: Query<(Entity, &ComponentA), Changed<ComponentA>>) {
for (entity, component_a) in query.iter() {
println!(" Entity({})", entity.id());
println!(
" ComponentA: {{ x: {} y: {} }}\n",
component_a.x, component_a.y
);
}
}
fn save_scene_system(world: &mut World) {
let mut scene_world = World::new();
let mut component_b = ComponentB::from_world(world);
component_b.value = "hello".to_string();
scene_world.spawn().insert_bundle((
component_b,
ComponentA { x: 1.0, y: 2.0 },
Transform::identity(),
));
scene_world
.spawn()
.insert_bundle((ComponentA { x: 3.0, y: 4.0 },));
let type_registry = world.get_resource::<TypeRegistry>().unwrap();
let scene = DynamicScene::from_world(&scene_world, &type_registry);
println!("{}", scene.serialize_ron(&type_registry).unwrap());
}
fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
},
text: Text::with_section(
"Nothing to see in this window! Check the console output!",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 50.0,
color: Color::WHITE,
},
Default::default(),
),
..Default::default()
});
}