diff --git a/type-id-test-rs/.gitignore b/type-id-test-rs/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/type-id-test-rs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/type-id-test-rs/Cargo.lock b/type-id-test-rs/Cargo.lock deleted file mode 100644 index 70a7578..0000000 --- a/type-id-test-rs/Cargo.lock +++ /dev/null @@ -1,53 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "component-macro" -version = "0.1.0" -dependencies = [ - "quote", - "syn", -] - -[[package]] -name = "proc-macro2" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "syn" -version = "2.0.58" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "type-id-test-rs" -version = "0.1.0" -dependencies = [ - "component-macro", -] - -[[package]] -name = "unicode-ident" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/type-id-test-rs/Cargo.toml b/type-id-test-rs/Cargo.toml deleted file mode 100644 index 9bb1c3e..0000000 --- a/type-id-test-rs/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "type-id-test-rs" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -component-macro = { version = "0.1.0", path = "./component-macro" } diff --git a/type-id-test-rs/src/component.rs b/type-id-test-rs/src/component.rs deleted file mode 100644 index 1bdc386..0000000 --- a/type-id-test-rs/src/component.rs +++ /dev/null @@ -1,6 +0,0 @@ -use std::any::{Any, TypeId}; - -pub trait Component { - fn inner_type_id(&self) -> TypeId; - fn as_any(&mut self) -> &mut dyn Any; -} diff --git a/type-id-test-rs/src/main.rs b/type-id-test-rs/src/main.rs deleted file mode 100644 index 8cbe276..0000000 --- a/type-id-test-rs/src/main.rs +++ /dev/null @@ -1,124 +0,0 @@ -#![allow(dead_code)] - -use std::any::{Any, TypeId}; - -use component::Component; -use component_macro::Component; - -mod component; - -struct Context<'a> { - entities: &'a mut Vec<(u64, Vec>)>, -} - -impl<'a> Context<'a> { - pub fn entities_with_component(&self) -> Vec { - let entity_type_id = TypeId::of::(); - self.entities - .iter() - .filter_map(|(id, components)| { - let contains_component = components.iter().any(|entity| { - let is_id = (*entity).inner_type_id() == entity_type_id; - is_id - }); - if contains_component { - Some(*id) - } else { - None - } - }) - .collect() - } - - pub fn entity_component(&mut self, entity_id: u64) -> &mut T { - let entity_type_id = TypeId::of::(); - let (_id, components) = self - .entities - .iter_mut() - .find(|(id, _)| *id == entity_id) - .unwrap(); - - let component = components - .iter_mut() - .find_map(|entity| { - let is_id = (*entity).inner_type_id() == entity_type_id; - if is_id { - Some(entity.as_any().downcast_mut::().unwrap()) - } else { - None - } - }) - .unwrap(); - component - } -} - -trait System { - fn on_update(&self, context: &mut Context); -} - -#[derive(Component)] -struct Position { - pub x: i32, - pub y: i32, -} - -#[derive(Component)] -struct NotPosition { - pub a: i32, - pub b: i32, -} - -struct Gravity; -struct NotGravity; - -impl System for Gravity { - fn on_update(&self, context: &mut Context) { - let entities_with_position = context.entities_with_component::(); - for entity_id in entities_with_position { - let position = context.entity_component::(entity_id); - position.y -= 1; - } - } -} - -impl System for NotGravity { - fn on_update(&self, context: &mut Context) { - let entities_with_position = context.entities_with_component::(); - for entity_id in entities_with_position { - let position = context.entity_component::(entity_id); - position.a -= 1; - } - } -} - -fn main() { - let mut entities: Vec<(u64, Vec>)> = Vec::new(); - let mut systems: Vec> = Vec::new(); - - systems.push(Box::new(Gravity)); - systems.push(Box::new(NotGravity)); - entities.push(( - 0, - vec![ - Box::new(Position { x: 0, y: 0 }), - Box::new(NotPosition { a: 0, b: 0 }), - ], - )); - - entities.push(( - 1, - vec![ - Box::new(Position { x: 0, y: 0 }), - Box::new(NotPosition { a: 0, b: 0 }), - ], - )); - - let mut context = Context { - entities: &mut entities, - }; - - for system in systems { - system.on_update(&mut context) - } -}