mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
scenes: polish scene example. prop->property attribute. derive(Resources) to derive(FromResources)
This commit is contained in:
parent
a76bb8b507
commit
3ee5a67cdb
11 changed files with 104 additions and 67 deletions
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
|
@ -8,6 +8,7 @@
|
|||
"bools",
|
||||
"chunkset",
|
||||
"chunksets",
|
||||
"deserialization",
|
||||
"eprintln",
|
||||
"hashset",
|
||||
"multizip",
|
||||
|
|
|
@ -1,32 +1,32 @@
|
|||
[
|
||||
Entity(
|
||||
id: 1401014920,
|
||||
components: [
|
||||
|
||||
{
|
||||
"type": "load_scene::Test",
|
||||
"x": 3.0,
|
||||
"y": 4.0,
|
||||
},
|
||||
],
|
||||
),
|
||||
Entity(
|
||||
id: 99763910,
|
||||
components: [
|
||||
Entity(
|
||||
id: 510837483,
|
||||
components: [
|
||||
|
||||
{
|
||||
"type": "load_scene::Test",
|
||||
"x": 1.0,
|
||||
"y": 2.0,
|
||||
},
|
||||
{
|
||||
"type": "bevy_asset::handle::Handle<bevy_render::mesh::Mesh>",
|
||||
"id": HandleId("edcdd23f-5be5-4dbf-9a6e-9f46b2185570"),
|
||||
},
|
||||
{
|
||||
"type": "load_scene::Foo",
|
||||
"value": "hello",
|
||||
},
|
||||
],
|
||||
),
|
||||
]
|
||||
{
|
||||
"type": "load_scene::ComponentA",
|
||||
"map": {
|
||||
"x": 3.0,
|
||||
"y": 4.0,
|
||||
},
|
||||
}, ],
|
||||
),
|
||||
Entity(
|
||||
id: 1069398672,
|
||||
components: [
|
||||
|
||||
{
|
||||
"type": "load_scene::ComponentA",
|
||||
"map": {
|
||||
"x": 1.0,
|
||||
"y": 2.0,
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "load_scene::ComponentB",
|
||||
"map": {
|
||||
"value": "hello",
|
||||
},
|
||||
}, ],
|
||||
),]
|
|
@ -25,7 +25,7 @@ where
|
|||
T: 'static,
|
||||
{
|
||||
pub id: HandleId,
|
||||
#[prop(ignore)]
|
||||
#[property(ignore)]
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ struct EntityArchetypeAttributeArgs {
|
|||
pub tag: Option<bool>,
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Resource, attributes(module))]
|
||||
#[proc_macro_derive(FromResources, attributes(module))]
|
||||
pub fn derive_resource(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let fields = match &ast.data {
|
||||
|
|
|
@ -21,9 +21,9 @@ struct PropAttributeArgs {
|
|||
pub ignore: Option<bool>,
|
||||
}
|
||||
|
||||
static PROP_ATTRIBUTE_NAME: &str = "prop";
|
||||
static PROP_ATTRIBUTE_NAME: &str = "property";
|
||||
|
||||
#[proc_macro_derive(Properties, attributes(prop, module))]
|
||||
#[proc_macro_derive(Properties, attributes(property, module))]
|
||||
pub fn derive_properties(input: TokenStream) -> TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let unit_struct_punctuated = Punctuated::new();
|
||||
|
|
|
@ -7,7 +7,7 @@ pub struct Renderable {
|
|||
pub is_visible: bool,
|
||||
pub is_instanced: bool,
|
||||
pub pipelines: Vec<Handle<PipelineDescriptor>>,
|
||||
#[prop(ignore)]
|
||||
#[property(ignore)]
|
||||
pub render_resource_assignments: RenderResourceAssignments,
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
};
|
||||
use bevy_app::{EventReader, Events};
|
||||
use bevy_asset::{AssetEvent, Assets, Handle};
|
||||
use bevy_derive::Resource;
|
||||
use bevy_derive::FromResources;
|
||||
use legion::prelude::*;
|
||||
use std::{collections::HashSet, fs::File};
|
||||
|
||||
|
@ -109,7 +109,7 @@ impl Texture {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
#[derive(FromResources)]
|
||||
pub struct TextureResourceSystemState {
|
||||
event_reader: EventReader<AssetEvent<Texture>>,
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ fn event_trigger_system(
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
#[derive(FromResources)]
|
||||
struct EventListenerState {
|
||||
my_event_reader: EventReader<MyEvent>,
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
#[derive(FromResources)]
|
||||
struct State {
|
||||
event_reader: EventReader<KeyboardInput>,
|
||||
moving_right: bool,
|
||||
|
|
|
@ -11,7 +11,7 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
#[derive(FromResources)]
|
||||
struct State {
|
||||
mouse_button_event_reader: EventReader<MouseButtonInput>,
|
||||
mouse_motion_event_reader: EventReader<MouseMotionInput>,
|
||||
|
|
|
@ -1,55 +1,91 @@
|
|||
use bevy::{component_registry::ComponentRegistryContext, prelude::*};
|
||||
use bevy::{
|
||||
component_registry::ComponentRegistryContext, input::keyboard::KeyboardInput, prelude::*,
|
||||
};
|
||||
use bevy_app::FromResources;
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.add_default_plugins()
|
||||
// Registering components informs Bevy that they exist. This allows them to be used when loading scenes
|
||||
// This step is only required if you want to load your components from scene files.
|
||||
// In the future registered components will also be usable from the Bevy editor.
|
||||
.register_component::<Test>()
|
||||
.register_component::<Foo>()
|
||||
.add_startup_system(load_scene)
|
||||
// .add_startup_system(serialize_scene)
|
||||
// Unregistered components can still be used in your code, but they won't be serialized / deserialized.
|
||||
// In the future registering components will also make them usable from the Bevy editor.
|
||||
// The core Bevy plugins already register their components, so you only need this step for custom components.
|
||||
.register_component::<ComponentA>()
|
||||
.register_component::<ComponentB>()
|
||||
.add_startup_system(save_scene_system)
|
||||
.add_startup_system(load_scene_system)
|
||||
.run();
|
||||
}
|
||||
|
||||
// Registered components must implement the `Properties` and `FromResources` traits.
|
||||
// The `Properties` trait enables serialization, deserialization, dynamic property access, and change detection.
|
||||
// `Properties` enable a bunch of cool behaviors, so its worth checking out the dedicated `properties.rs` example.
|
||||
// The `FromResources` trait determines how your component is constructed.
|
||||
// For simple use cases you can just implement the `Default` trait (which automatically implements FromResources)
|
||||
// The simplest registered component just needs these two derives:
|
||||
#[derive(Properties, Default)]
|
||||
struct Test {
|
||||
struct ComponentA {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[derive(Properties, Default)]
|
||||
struct Foo {
|
||||
// Some components have fields that cannot (or should not) be written to scene files. These can be ignored with
|
||||
// the #[property(ignore)] attribute. This is also generally where the `FromResources` trait comes into play.
|
||||
// This gives you access to your App's current ECS `Resources` when you construct your component.
|
||||
#[derive(Properties)]
|
||||
struct ComponentB {
|
||||
pub value: String,
|
||||
#[property(ignore)]
|
||||
pub event_reader: EventReader<KeyboardInput>,
|
||||
}
|
||||
|
||||
fn load_scene(world: &mut World, resources: &mut Resources) {
|
||||
impl FromResources for ComponentB {
|
||||
fn from_resources(resources: &Resources) -> Self {
|
||||
let event_reader = resources.get_event_reader::<KeyboardInput>();
|
||||
ComponentB {
|
||||
event_reader,
|
||||
value: "Default Value".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn save_scene_system(world: &mut World, resources: &mut Resources) {
|
||||
// Scenes can be created from any ECS World.
|
||||
world
|
||||
.build()
|
||||
.build_entity()
|
||||
.add(ComponentA { x: 1.0, y: 2.0 })
|
||||
.add(ComponentB {
|
||||
value: "hello".to_string(),
|
||||
..ComponentB::from_resources(resources)
|
||||
})
|
||||
.build_entity()
|
||||
.add(ComponentA { x: 3.0, y: 4.0 });
|
||||
|
||||
// The component registry resource contains information about all registered components. This is used to construct scenes.
|
||||
let component_registry = resources.get::<ComponentRegistryContext>().unwrap();
|
||||
let scene = Scene::from_world(world, &component_registry.value.read().unwrap());
|
||||
|
||||
// Scenes can be serialized like this:
|
||||
println!("{}", scene.serialize_ron().unwrap());
|
||||
|
||||
// TODO: save scene
|
||||
}
|
||||
|
||||
fn load_scene_system(world: &mut World, resources: &mut Resources) {
|
||||
let asset_server = resources.get::<AssetServer>().unwrap();
|
||||
let mut scenes = resources.get_mut::<Assets<Scene>>().unwrap();
|
||||
let component_registry = resources.get::<ComponentRegistryContext>().unwrap();
|
||||
|
||||
|
||||
// Scenes are loaded just like any other asset.
|
||||
let scene_handle: Handle<Scene> = asset_server
|
||||
.load_sync(&mut scenes, "assets/scene/load_scene_example.scn")
|
||||
.unwrap();
|
||||
let scene = scenes.get(&scene_handle).unwrap();
|
||||
|
||||
// Scenes can be added to any ECS World. Adding scenes also uses the component registry.
|
||||
let component_registry = resources.get::<ComponentRegistryContext>().unwrap();
|
||||
scene
|
||||
.add_to_world(world, resources, &component_registry.value.read().unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn serialize_scene(world: &mut World, resources: &mut Resources) {
|
||||
let component_registry = resources.get::<ComponentRegistryContext>().unwrap();
|
||||
world
|
||||
.build()
|
||||
.add(Test { x: 1.0, y: 2.0 })
|
||||
.add(Foo {
|
||||
value: "hello".to_string(),
|
||||
})
|
||||
.build_entity()
|
||||
.add(Test { x: 3.0, y: 4.0 });
|
||||
|
||||
let scene = Scene::from_world(world, &component_registry.value.read().unwrap());
|
||||
println!("{}", scene.serialize_ron().unwrap());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue