Skip to content

Commit

Permalink
rename remove_component + introduce common.rs for examples + more exa…
Browse files Browse the repository at this point in the history
…mples
  • Loading branch information
Indra-db committed Mar 4, 2024
1 parent 02498ee commit 3f2ef29
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 37 deletions.
29 changes: 29 additions & 0 deletions flecs_ecs/examples/common.rs
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions flecs_ecs/examples/entity_basics.rs
Original file line number Diff line number Diff line change
@@ -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::<Walking>();

// Get the value for the Position component
let pos = bob.get::<Position>().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::<Walking>();

// Print all of the components the entity has. This will output:
// Position, Walking, (Identifier,Name)
println!("[{}]", alice.get_archetype());

// Remove tag
alice.remove::<Walking>();

// 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 },)
}
23 changes: 2 additions & 21 deletions flecs_ecs/examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/src/core/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ impl Entity {
///
/// * C++ API: `entity_builder::remove`
#[doc(alias = "entity_builder::remove")]
pub fn remove_component<T: CachedComponentData + ComponentType<Struct>>(self) -> Self {
pub fn remove<T: CachedComponentData + ComponentType<Struct>>(self) -> Self {
let world = self.world;
self.remove_id(T::get_id(world))
}
Expand All @@ -541,7 +541,7 @@ impl Entity {
///
/// * C++ API: `entity_builder::remove`
#[doc(alias = "entity_builder::remove")]
pub fn remove_component_enum<T: CachedComponentData + ComponentType<Enum>>(self) -> Self {
pub fn remove_enum<T: CachedComponentData + ComponentType<Enum>>(self) -> Self {
let world = self.world;
self.remove_pair_ids(T::get_id(world), unsafe { EcsWildcard })
}
Expand Down
10 changes: 4 additions & 6 deletions flecs_ecs/src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,9 +1566,8 @@ impl World {
/// * C++ API: `world::remove`
#[doc(alias = "world::remove")]
#[inline(always)]
pub fn remove_component<T: CachedComponentData + ComponentType<Struct>>(&self) {
Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world))
.remove_component::<T>();
pub fn remove<T: CachedComponentData + ComponentType<Struct>>(&self) {
Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)).remove::<T>();
}

/// Remove singleton enum component.
Expand All @@ -1582,9 +1581,8 @@ impl World {
/// * C++ API: `world::remove`
#[doc(alias = "world::remove")]
#[inline(always)]
pub fn remove_component_enum<T: CachedComponentData + ComponentType<Enum>>(&self) {
Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world))
.remove_component_enum::<T>();
pub fn remove_enum<T: CachedComponentData + ComponentType<Enum>>(&self) {
Entity::new_from_existing_raw(self.raw_world, T::get_id(self.raw_world)).remove_enum::<T>();
}

/// Remove singleton pair with enum tag.
Expand Down
8 changes: 4 additions & 4 deletions flecs_ecs/tests/component_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ fn temp_test_hook() {
let vel_e1 = entity.get::<Velocity>().unwrap();
assert_eq!(vel_e1.x, 0.0);
assert_eq!(vel_e1.y, 0.0);
entity.remove_component::<Velocity>();
entity.remove::<Velocity>();

entity.remove_component::<Position>();
entity.remove::<Position>();
assert_eq!(unsafe { COUNT }, 1);

entity2.set(Velocity { x: 3.0, y: 5.0 });
Expand All @@ -67,9 +67,9 @@ fn temp_test_hook() {
assert_eq!(vel_e2.y, 50.0);
assert_eq!(unsafe { COUNT2 }, 1);

entity.remove_component::<Position>();
entity.remove::<Position>();
assert_eq!(unsafe { COUNT }, 1);
entity2.remove_component::<Position>();
entity2.remove::<Position>();
assert_eq!(unsafe { COUNT }, 0);
}
}
6 changes: 2 additions & 4 deletions flecs_ecs/tests/entity_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn entity_remove() {
entity.add::<Position>();
assert!(entity.has::<Position>());

entity.remove_component::<Position>();
entity.remove::<Position>();
assert!(!entity.has::<Position>());
}

Expand Down Expand Up @@ -262,9 +262,7 @@ fn entity_remove_2() {
assert!(entity.has::<Position>());
assert!(entity.has::<Velocity>());

entity
.remove_component::<Position>()
.remove_component::<Velocity>();
entity.remove::<Position>().remove::<Velocity>();

assert!(!entity.has::<Position>());
assert!(!entity.has::<Velocity>());
Expand Down

0 comments on commit 3f2ef29

Please sign in to comment.