-
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.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 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,61 @@ | ||
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 {} | ||
|
||
fn main() { | ||
// Create a new world | ||
let world = World::new(); | ||
|
||
// Register system | ||
let _sys = world | ||
.system_builder::<(Position, Velocity)>() | ||
.on_each_entity(|_entity, (pos, vel)| { | ||
pos.x += vel.x; | ||
pos.y += vel.y; | ||
}); | ||
|
||
// Create an entity with name Bob, add Position and food preference | ||
let bob = world | ||
.new_entity_named(CStr::from_bytes_with_nul(b"Bob\0").unwrap()) | ||
.set_component(Position { x: 0.0, y: 0.0 }) | ||
.set_component(Velocity { x: 1.0, y: 2.0 }) | ||
.add_pair::<Eats, Apples>(); | ||
|
||
// Show us what you got | ||
println!( | ||
"{}'s got [{}]", | ||
bob.get_name(), | ||
bob.get_archetype().to_string().unwrap() | ||
); | ||
|
||
// Run systems twice. Usually this function is called once per frame | ||
world.progress(); | ||
world.progress(); | ||
|
||
// See if Bob has moved (he has) | ||
let pos = bob.get_component::<Position>(); | ||
let pos_ref = unsafe { pos.as_ref().unwrap() }; | ||
println!("Bob's position: {:?}", pos_ref); | ||
|
||
// Output | ||
// Bob's got [Position, Velocity, (Identifier,Name), (Eats,Apples)] | ||
// Bob's position: Position { x: 2.0, y: 4.0 } | ||
} |