diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-26 20:05:27 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-26 20:16:27 +0100 |
commit | 815d04da602c58ed8b13eeb612fe73180204039d (patch) | |
tree | c3703e598656c070edbc271ccaa9117e071d6d4f /ecs/examples | |
parent | 1019924a29527eba2c8ec8bd976ece6ed76075b0 (diff) |
fix(ecs): make Component trait not automatic & add derive macro
Diffstat (limited to 'ecs/examples')
-rw-r--r-- | ecs/examples/multiple_queries.rs | 5 | ||||
-rw-r--r-- | ecs/examples/simple.rs | 29 | ||||
-rw-r--r-- | ecs/examples/with_local.rs | 23 |
3 files changed, 36 insertions, 21 deletions
diff --git a/ecs/examples/multiple_queries.rs b/ecs/examples/multiple_queries.rs index a4a5d2d..4f4198a 100644 --- a/ecs/examples/multiple_queries.rs +++ b/ecs/examples/multiple_queries.rs @@ -1,18 +1,21 @@ use std::fmt::Display; -use ecs::{Query, World}; +use ecs::{Component, Query, World}; +#[derive(Component)] struct Health { health: u32, } +#[derive(Component)] enum AttackStrength { Strong, Weak, } +#[derive(Component)] struct EnemyName { name: String, diff --git a/ecs/examples/simple.rs b/ecs/examples/simple.rs index b58d2ba..b8dc50f 100644 --- a/ecs/examples/simple.rs +++ b/ecs/examples/simple.rs @@ -1,14 +1,21 @@ -use ecs::{Query, World}; +use ecs::{Component, Query, World}; +#[derive(Component)] struct SomeData { num: u64, } -fn say_hello(mut query: Query<(SomeData, String)>) +#[derive(Component)] +struct Greeting { - for (data, text) in query.iter_mut() { - println!("Hello {}: {}", text, data.num); + greeting: String, +} + +fn say_hello(mut query: Query<(SomeData, Greeting)>) +{ + for (data, greeting) in query.iter_mut() { + println!("{}: {}", greeting.greeting, data.num); } } @@ -24,9 +31,17 @@ fn main() world.register_system(Event::Start, say_hello); - world.create_entity((SomeData { num: 987_654 }, "Yoo".to_string())); - - world.create_entity((SomeData { num: 345 }, "Haha".to_string())); + world.create_entity(( + SomeData { num: 987_654 }, + Greeting { + greeting: "Good afternoon".to_string(), + }, + )); + + world.create_entity(( + SomeData { num: 345 }, + Greeting { greeting: "Good evening".to_string() }, + )); world.emit(&Event::Start); } diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs index 0bd8f66..334f129 100644 --- a/ecs/examples/with_local.rs +++ b/ecs/examples/with_local.rs @@ -1,28 +1,29 @@ use ecs::component::Local; -use ecs::system::{Input as SystemInput, Into, System}; -use ecs::{Query, World}; +use ecs::system::{Into, System}; +use ecs::{Component, Query, World}; +#[derive(Component)] struct SomeData { num: u64, } +#[derive(Component)] struct Name { name: String, } +#[derive(Component)] struct SayHelloState { cnt: usize, } -impl SystemInput for SayHelloState {} - -fn say_hello(mut query: Query<(SomeData, String)>, mut state: Local<SayHelloState>) +fn say_hello(mut query: Query<(SomeData,)>, mut state: Local<SayHelloState>) { - for (data, text) in query.iter_mut() { - println!("Hello there. Count {}: {}: {}", state.cnt, text, data.num); + for (data,) in query.iter_mut() { + println!("Hello there. Count {}: {}", state.cnt, data.num); state.cnt += 1; } @@ -64,13 +65,9 @@ fn main() .initialize((SayHelloState { cnt: 0 },)), ); - world.create_entity(( - SomeData { num: 987_654 }, - "Yoo".to_string(), - Name { name: "Bob".to_string() }, - )); + world.create_entity((SomeData { num: 987_654 }, Name { name: "Bob".to_string() })); - world.create_entity((SomeData { num: 345 }, "Haha".to_string())); + world.create_entity((SomeData { num: 345 },)); world.emit(&Event::Update); |