component_registry: use FromResources trait instead of Default
This commit is contained in:
		
							parent
							
								
									da52b1b034
								
							
						
					
					
						commit
						cb3a863366
					
				@ -1,12 +1,13 @@
 | 
			
		||||
use bevy_property::{Properties, Property, PropertyTypeRegistry};
 | 
			
		||||
use legion::{
 | 
			
		||||
    prelude::{Entity, World},
 | 
			
		||||
    prelude::{Entity, World, Resources},
 | 
			
		||||
    storage::{Component, ComponentResourceSet, ComponentTypeId},
 | 
			
		||||
};
 | 
			
		||||
use std::{
 | 
			
		||||
    collections::HashMap,
 | 
			
		||||
    sync::{Arc, RwLock},
 | 
			
		||||
};
 | 
			
		||||
use bevy_app::FromResources;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Default)]
 | 
			
		||||
pub struct PropertyTypeRegistryContext {
 | 
			
		||||
@ -28,7 +29,7 @@ pub struct ComponentRegistry {
 | 
			
		||||
impl ComponentRegistry {
 | 
			
		||||
    pub fn register<T>(&mut self)
 | 
			
		||||
    where
 | 
			
		||||
        T: Properties + Component + Default,
 | 
			
		||||
        T: Properties + Component + FromResources,
 | 
			
		||||
    {
 | 
			
		||||
        let registration = ComponentRegistration::of::<T>();
 | 
			
		||||
        self.short_names
 | 
			
		||||
@ -58,18 +59,18 @@ impl ComponentRegistry {
 | 
			
		||||
#[derive(Clone)]
 | 
			
		||||
pub struct ComponentRegistration {
 | 
			
		||||
    pub ty: ComponentTypeId,
 | 
			
		||||
    pub component_add_fn: fn(&mut World, Entity, &dyn Property),
 | 
			
		||||
    pub component_add_fn: fn(&mut World, resources: &Resources, Entity, &dyn Property),
 | 
			
		||||
    pub component_properties_fn: fn(&ComponentResourceSet, usize) -> &dyn Properties,
 | 
			
		||||
    pub short_name: &'static str,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ComponentRegistration {
 | 
			
		||||
    pub fn of<T: Properties + Component + Default>() -> Self {
 | 
			
		||||
    pub fn of<T: Properties + Component + FromResources>() -> Self {
 | 
			
		||||
        let ty = ComponentTypeId::of::<T>();
 | 
			
		||||
        Self {
 | 
			
		||||
            ty,
 | 
			
		||||
            component_add_fn: |world: &mut World, entity: Entity, property: &dyn Property| {
 | 
			
		||||
                let mut component = T::default();
 | 
			
		||||
            component_add_fn: |world: &mut World, resources: &Resources, entity: Entity, property: &dyn Property| {
 | 
			
		||||
                let mut component = T::from_resources(resources);
 | 
			
		||||
                component.apply(property);
 | 
			
		||||
                world.add_component(entity, component).unwrap();
 | 
			
		||||
            },
 | 
			
		||||
 | 
			
		||||
@ -3,12 +3,12 @@ use bevy_property::{Property, Properties};
 | 
			
		||||
use legion::storage::Component;
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
use crate::{PropertyTypeRegistryContext, ComponentRegistryContext};
 | 
			
		||||
use bevy_app::AppBuilder;
 | 
			
		||||
use bevy_app::{FromResources, AppBuilder};
 | 
			
		||||
 | 
			
		||||
pub trait RegisterComponent {
 | 
			
		||||
    fn register_component<T>(&mut self) -> &mut Self
 | 
			
		||||
    where
 | 
			
		||||
        T: Properties + Component + Default;
 | 
			
		||||
        T: Properties + Component + FromResources;
 | 
			
		||||
    fn register_property_type<T>(&mut self) -> &mut Self
 | 
			
		||||
    where
 | 
			
		||||
        T: Property + for<'de> Deserialize<'de>;
 | 
			
		||||
@ -17,7 +17,7 @@ pub trait RegisterComponent {
 | 
			
		||||
impl RegisterComponent for AppBuilder {
 | 
			
		||||
    fn register_component<T>(&mut self) -> &mut Self
 | 
			
		||||
    where
 | 
			
		||||
        T: Properties + Component + Default,
 | 
			
		||||
        T: Properties + Component + FromResources,
 | 
			
		||||
    {
 | 
			
		||||
        {
 | 
			
		||||
            let registry_context = self
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,7 @@
 | 
			
		||||
use anyhow::Result;
 | 
			
		||||
use bevy_component_registry::ComponentRegistry;
 | 
			
		||||
use bevy_property::DynamicProperties;
 | 
			
		||||
use legion::prelude::World;
 | 
			
		||||
use legion::prelude::{Resources, World};
 | 
			
		||||
use serde::Serialize;
 | 
			
		||||
use std::num::Wrapping;
 | 
			
		||||
use thiserror::Error;
 | 
			
		||||
@ -66,6 +66,7 @@ impl Scene {
 | 
			
		||||
    pub fn add_to_world(
 | 
			
		||||
        &self,
 | 
			
		||||
        world: &mut World,
 | 
			
		||||
        resources: &Resources,
 | 
			
		||||
        component_registry: &ComponentRegistry,
 | 
			
		||||
    ) -> Result<(), SceneAddError> {
 | 
			
		||||
        world.entity_allocator.push_next_ids(
 | 
			
		||||
@ -82,7 +83,7 @@ impl Scene {
 | 
			
		||||
                    .ok_or_else(|| SceneAddError::UnregisteredComponent {
 | 
			
		||||
                        type_name: component.type_name.to_string(),
 | 
			
		||||
                    })?;
 | 
			
		||||
                (component_registration.component_add_fn)(world, entity, component);
 | 
			
		||||
                (component_registration.component_add_fn)(world, resources, entity, component);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -34,7 +34,7 @@ fn load_scene(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
        .unwrap();
 | 
			
		||||
    let scene = scenes.get(&scene_handle).unwrap();
 | 
			
		||||
    scene
 | 
			
		||||
        .add_to_world(world, &component_registry.value.read().unwrap())
 | 
			
		||||
        .add_to_world(world, resources, &component_registry.value.read().unwrap())
 | 
			
		||||
        .unwrap();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user