From 47751e6647ea78316db0aa2d4132697bdd3521a4 Mon Sep 17 00:00:00 2001 From: Indradb <60851042+Indra-db@users.noreply.github.com> Date: Mon, 4 Mar 2024 16:48:01 +0700 Subject: [PATCH] Init examples w/ hello_world --- flecs_ecs/examples/hello_world.rs | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 flecs_ecs/examples/hello_world.rs diff --git a/flecs_ecs/examples/hello_world.rs b/flecs_ecs/examples/hello_world.rs new file mode 100644 index 00000000..f481a6b7 --- /dev/null +++ b/flecs_ecs/examples/hello_world.rs @@ -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::(); + + // 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::(); + 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 } +}