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.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 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>) {
let scene_handle: Handle<DynamicScene> = asset_server.load("scenes/load_scene_example.scn");
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, resources: &mut Resources) {
let mut world = World::new();
world.spawn((
ComponentA { x: 1.0, y: 2.0 },
ComponentB {
value: "hello".to_string(),
..ComponentB::from_resources(resources)
},
Transform::default(),
));
world.spawn((ComponentA { x: 3.0, y: 4.0 },));
let type_registry = resources.get::<TypeRegistry>().unwrap();
let scene = DynamicScene::from_world(&world, &type_registry);
println!("{}", scene.serialize_ron(&type_registry).unwrap());
}
fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
commands.spawn(CameraUiBundle::default()).spawn(TextBundle {
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,
..Default::default()
},
},
..Default::default()
});
}