Commit fb2c2638 authored by Florian Blasius's avatar Florian Blasius 🤘
Browse files

Fix shareing of shared components.

parent 849b89f6
......@@ -19,6 +19,14 @@ impl System<EntityStore, StringComponentStore> for PrintSystem {
if let Ok(content) = c_store.get::<String>("content", *entity) {
println!("{}", content);
}
if let Ok(content) = c_store.get::<String>("my_content", *entity) {
println!("my_content: {}", content);
}
if let Ok(content) = c_store.get::<String>("my_extra_content", *entity) {
println!("my_extra_content: {}", content);
}
}
}
}
......@@ -46,6 +54,26 @@ fn main() {
)
.build();
let second_source = world
.create_entity()
.components(
StringComponentBuilder::new()
.with("header", String::from("Header 3"))
.with_shared_source_key::<String>("my_content", "content", source)
.build(),
)
.build();
world
.create_entity()
.components(
StringComponentBuilder::new()
.with("header", String::from("Header 4"))
.with_shared_source_key::<String>("my_extra_content", "my_content", second_source)
.build(),
)
.build();
world.create_system(PrintSystem).with_priority(1).build();
world.run();
......
......@@ -94,6 +94,15 @@ impl ComponentStore for TypeComponentStore {
self.shared.remove(&k);
}
}
fn print_entity(&self, entity: impl Into<Entity>) {
let entity = entity.into();
let _blub = self
.components
.iter()
.filter(|(k, _)| k.0 == entity)
.map(|(_, _)| println!("blub"));
}
}
impl TypeComponentStore {
......@@ -111,22 +120,14 @@ impl TypeComponentStore {
}
/// Registers a sharing of the given component between the given entities.
pub fn register_shared_box(
&mut self,
target: impl Into<Entity>,
source: SharedComponentBox,
) {
pub fn register_shared_box(&mut self, target: impl Into<Entity>, source: SharedComponentBox) {
let target_key = (target.into(), source.type_id);
self.components.remove(&target_key);
self.shared.insert(target_key, source.source);
}
/// Register a `component_box` for the given `entity`.
pub fn register_box(
&mut self,
entity: impl Into<Entity>,
component_box: ComponentBox,
) {
pub fn register_box(&mut self, entity: impl Into<Entity>, component_box: ComponentBox) {
let entity = entity.into();
let (type_id, component) = component_box.consume();
......
......@@ -186,4 +186,7 @@ pub trait ComponentStore {
/// Removes and entity from the store.
fn remove_entity(&mut self, entity: impl Into<Entity>);
/// Print infos about the given entity.
fn print_entity(&self, entity: impl Into<Entity>);
}
......@@ -95,30 +95,35 @@ impl ComponentStore for StringComponentStore {
self.shared.remove(&k);
}
}
fn print_entity(&self, entity: impl Into<Entity>) {
let entity = entity.into();
println!("Components of entity: {}", entity.0);
for (k, v) in self.components.iter().filter(|&(k, _)| k.0 == entity) {
println!("Key: {:?}, Value: {:?}", k, v);
}
println!("Shared components of entity: {}", entity.0);
for (k, v) in self.shared.iter().filter(|&(k, _)| k.0 == entity) {
println!("Key: {:?}, Value: {:?}", k, v);
}
}
}
impl StringComponentStore {
/// Register a `component` for the given `entity`.
pub fn register<C: Component>(
&mut self,
key: impl Into<String>,
entity: Entity,
component: C,
) {
pub fn register<C: Component>(&mut self, key: impl Into<String>, entity: Entity, component: C) {
self.components
.insert((entity, key.into()), Box::new(component));
}
/// Registers a sharing of the given component between the given entities. Uses as source key the component key.
pub fn register_shared<C: Component>(
&mut self,
key: &str,
target: Entity,
source: Entity,
) {
pub fn register_shared<C: Component>(&mut self, key: &str, target: Entity, source: Entity) {
self.register_shared_by_source_key::<C>(key, key, target, source);
}
/// Registers a sharing of the given component between the given entities.
pub fn register_shared_by_source_key<C: Component>(
&mut self,
key: &str,
......@@ -133,12 +138,7 @@ impl StringComponentStore {
}
/// Registers a sharing of the given component between the given entities. Uses as source key the component key.
pub fn register_shared_box(
&mut self,
key: &str,
target: Entity,
source: SharedComponentBox,
) {
pub fn register_shared_box(&mut self, key: &str, target: Entity, source: SharedComponentBox) {
self.register_shared_box_by_source_key(key, key, target, source);
}
......@@ -157,12 +157,7 @@ impl StringComponentStore {
}
/// Register a `component_box` for the given `entity`.
pub fn register_box(
&mut self,
key: &str,
entity: Entity,
component_box: ComponentBox,
) {
pub fn register_box(&mut self, key: &str, entity: Entity, component_box: ComponentBox) {
let (_, component) = component_box.consume();
self.components.insert((entity, key.into()), component);
}
......@@ -193,9 +188,10 @@ impl StringComponentStore {
key: impl Into<String>,
entity: Entity,
) -> Result<(Entity, String), NotFound> {
let key = key.into();
self.shared
.get(&(entity, key.into()))
.ok_or_else(|| NotFound::Entity(entity))
.get(&(entity, key.clone()))
.ok_or_else(|| NotFound::Key((entity, key)))
.map(|s| s.clone())
}
......@@ -203,7 +199,18 @@ impl StringComponentStore {
fn source(&self, entity: Entity, key: impl Into<String>) -> Result<(Entity, String), NotFound> {
let key = (entity, key.into());
if !self.components.contains_key(&key) {
return self.source_from_shared(key.1, key.0);
let mut source = self.source_from_shared(key.1.clone(), key.0);
loop {
if source.is_err() || self.components.contains_key(source.as_ref().unwrap()) {
return source;
}
source = self.source_from_shared(
source.as_ref().unwrap().1.as_str(),
source.as_ref().unwrap().0,
);
}
}
Result::Ok(key)
......
......@@ -15,6 +15,8 @@ pub enum NotFound {
ComponentKey(String),
/// Unknown error
Unknown(String),
/// Key could not be found
Key((Entity, String))
}
impl Default for NotFound {
......
......@@ -99,6 +99,11 @@ where
&mut self.entity_component_manager
}
/// Print infos about the given entity.
pub fn print_entity(&self, entity: impl Into<Entity>) {
self.entity_component_manager.component_store().print_entity(entity);
}
/// Run all systems of the world.
pub fn run(&mut self) {
if self.first_run {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment