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