From 3f2ef2989be20cd6af1e1eec09930dfb353d9d52 Mon Sep 17 00:00:00 2001 From: Indradb <60851042+Indra-db@users.noreply.github.com> Date: Tue, 5 Mar 2024 00:02:52 +0700 Subject: [PATCH] rename remove_component + introduce common.rs for examples + more examples --- flecs_ecs/examples/common.rs | 29 +++++++++++++++++ flecs_ecs/examples/entity_basics.rs | 49 +++++++++++++++++++++++++++++ flecs_ecs/examples/hello_world.rs | 23 ++------------ flecs_ecs/src/core/entity.rs | 4 +-- flecs_ecs/src/core/world.rs | 10 +++--- flecs_ecs/tests/component_test.rs | 8 ++--- flecs_ecs/tests/entity_test.rs | 6 ++-- 7 files changed, 92 insertions(+), 37 deletions(-) create mode 100644 flecs_ecs/examples/common.rs create mode 100644 flecs_ecs/examples/entity_basics.rs diff --git a/flecs_ecs/examples/common.rs b/flecs_ecs/examples/common.rs new file mode 100644 index 00000000..30b656e0 --- /dev/null +++ b/flecs_ecs/examples/common.rs @@ -0,0 +1,29 @@ +pub use flecs_ecs::core::*; +pub use flecs_ecs_derive::Component; +#[allow(unused_imports)] +pub use std::ffi::CStr; + +#[derive(Debug, Default, Clone, Component)] +pub struct Position { + pub x: f32, + pub y: f32, +} + +#[derive(Default, Clone, Component)] +pub struct Velocity { + pub x: f32, + pub y: f32, +} + +#[derive(Default, Clone, Component)] +pub struct Eats; +#[derive(Default, Clone, Component)] +pub struct Apples; + +#[derive(Default, Clone, Component)] +pub struct Walking; + +#[allow(dead_code)] +fn main() { + //this file is for common structs and functions used in the examples +} diff --git a/flecs_ecs/examples/entity_basics.rs b/flecs_ecs/examples/entity_basics.rs new file mode 100644 index 00000000..ce98f3a0 --- /dev/null +++ b/flecs_ecs/examples/entity_basics.rs @@ -0,0 +1,49 @@ +mod common; +use common::*; + +fn main() { + let world = World::new(); + + // Create an entity with name Bob + let bob = world + .new_entity_named(CStr::from_bytes_with_nul(b"Bob\0").unwrap()) + // The set operation finds or creates a component, and sets it. + // Components are automatically registered with the world + .set(Position { x: 10.0, y: 20.0 }) + // The add operation adds a component without setting a value. This is + // useful for tags, or when adding a component with its default value. + .add::(); + + // Get the value for the Position component + let pos = bob.get::().unwrap(); + println!("Bob's position: {:?}", pos); + + // Overwrite the value of the Position component + bob.set(Position { x: 20.0, y: 30.0 }); + + // Create another named entity + let alice = world + .new_entity_named(CStr::from_bytes_with_nul(b"Alice\0").unwrap()) + .set(Position { x: 10.0, y: 20.0 }); + + // Add a tag after entity is created + alice.add::(); + + // Print all of the components the entity has. This will output: + // Position, Walking, (Identifier,Name) + println!("[{}]", alice.get_archetype()); + + // Remove tag + alice.remove::(); + + // Iterate all entities with position + world.each_entity::<(Position,)>(|entity, pos| { + println!("{} has {:?}", entity.get_name(), pos); + }); + + // Output + // Bob's position: Position { x: 10.0, y: 20.0 } + // [Position, Walking, (Identifier,Name)] + // Alice has (Position { x: 10.0, y: 20.0 },) + // Bob has (Position { x: 20.0, y: 30.0 },) +} diff --git a/flecs_ecs/examples/hello_world.rs b/flecs_ecs/examples/hello_world.rs index 3f2a634b..80ce66af 100644 --- a/flecs_ecs/examples/hello_world.rs +++ b/flecs_ecs/examples/hello_world.rs @@ -1,24 +1,5 @@ -use std::ffi::CStr; - -use flecs_ecs::core::*; -use flecs_ecs_derive::Component; - -#[derive(Debug, Default, Clone, Component)] -struct Position { - x: f32, - y: f32, -} - -#[derive(Default, Clone, Component)] -struct Velocity { - x: f32, - y: f32, -} - -#[derive(Default, Clone, Component)] -struct Eats {} -#[derive(Default, Clone, Component)] -struct Apples {} +mod common; +use common::*; fn main() { // Create a new world diff --git a/flecs_ecs/src/core/entity.rs b/flecs_ecs/src/core/entity.rs index e6dd439e..081a96a5 100644 --- a/flecs_ecs/src/core/entity.rs +++ b/flecs_ecs/src/core/entity.rs @@ -526,7 +526,7 @@ impl Entity { /// /// * C++ API: `entity_builder::remove` #[doc(alias = "entity_builder::remove")] - pub fn remove_component>(self) -> Self { + pub fn remove>(self) -> Self { let world = self.world; self.remove_id(T::get_id(world)) } @@ -541,7 +541,7 @@ impl Entity { /// /// * C++ API: `entity_builder::remove` #[doc(alias = "entity_builder::remove")] - pub fn remove_component_enum>(self) -> Self { + pub fn remove_enum>(self) -> Self { let world = self.world; self.remove_pair_ids(T::get_id(world), unsafe { EcsWildcard }) } diff --git a/flecs_ecs/src/core/world.rs b/flecs_ecs/src/core/world.rs index d481981c..e1cd691c 100644 --- a/flecs_ecs/src/core/world.rs +++ b/flecs_ecs/src/core/world.rs @@ -1566,9 +1566,8 @@ impl World { /// * C++ API: `world::remove` #[doc(alias = "world::remove")] #[inline(always)] - pub fn remove_component>(&self) { - Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)) - .remove_component::(); + pub fn remove>(&self) { + Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)).remove::(); } /// Remove singleton enum component. @@ -1582,9 +1581,8 @@ impl World { /// * C++ API: `world::remove` #[doc(alias = "world::remove")] #[inline(always)] - pub fn remove_component_enum>(&self) { - Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)) - .remove_component_enum::(); + pub fn remove_enum>(&self) { + Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)).remove_enum::(); } /// Remove singleton pair with enum tag. diff --git a/flecs_ecs/tests/component_test.rs b/flecs_ecs/tests/component_test.rs index d7d551b6..1ad23293 100644 --- a/flecs_ecs/tests/component_test.rs +++ b/flecs_ecs/tests/component_test.rs @@ -56,9 +56,9 @@ fn temp_test_hook() { let vel_e1 = entity.get::().unwrap(); assert_eq!(vel_e1.x, 0.0); assert_eq!(vel_e1.y, 0.0); - entity.remove_component::(); + entity.remove::(); - entity.remove_component::(); + entity.remove::(); assert_eq!(unsafe { COUNT }, 1); entity2.set(Velocity { x: 3.0, y: 5.0 }); @@ -67,9 +67,9 @@ fn temp_test_hook() { assert_eq!(vel_e2.y, 50.0); assert_eq!(unsafe { COUNT2 }, 1); - entity.remove_component::(); + entity.remove::(); assert_eq!(unsafe { COUNT }, 1); - entity2.remove_component::(); + entity2.remove::(); assert_eq!(unsafe { COUNT }, 0); } } diff --git a/flecs_ecs/tests/entity_test.rs b/flecs_ecs/tests/entity_test.rs index 2e19e8bd..8d79f7ce 100644 --- a/flecs_ecs/tests/entity_test.rs +++ b/flecs_ecs/tests/entity_test.rs @@ -179,7 +179,7 @@ fn entity_remove() { entity.add::(); assert!(entity.has::()); - entity.remove_component::(); + entity.remove::(); assert!(!entity.has::()); } @@ -262,9 +262,7 @@ fn entity_remove_2() { assert!(entity.has::()); assert!(entity.has::()); - entity - .remove_component::() - .remove_component::(); + entity.remove::().remove::(); assert!(!entity.has::()); assert!(!entity.has::());