-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename remove_component + introduce common.rs for examples + more exa…
…mples
- Loading branch information
Showing
7 changed files
with
92 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 },) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters