summaryrefslogtreecommitdiff
path: root/ecs/examples
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/examples')
-rw-r--r--ecs/examples/event_loop.rs43
-rw-r--r--ecs/examples/extension.rs17
-rw-r--r--ecs/examples/multiple_queries.rs10
-rw-r--r--ecs/examples/optional_component.rs13
-rw-r--r--ecs/examples/relationship.rs10
-rw-r--r--ecs/examples/simple.rs8
-rw-r--r--ecs/examples/with_local.rs22
-rw-r--r--ecs/examples/with_sole.rs28
8 files changed, 64 insertions, 87 deletions
diff --git a/ecs/examples/event_loop.rs b/ecs/examples/event_loop.rs
index 8d8d84f..2365eb0 100644
--- a/ecs/examples/event_loop.rs
+++ b/ecs/examples/event_loop.rs
@@ -1,6 +1,7 @@
use ecs::actions::Actions;
-use ecs::event::Event;
-use ecs::{Component, Query, World};
+use ecs::phase::{Phase, UPDATE as UPDATE_PHASE};
+use ecs::relationship::{ChildOf, Relationship};
+use ecs::{static_entity, Component, Query, World};
#[derive(Component)]
struct Wool
@@ -20,7 +21,7 @@ struct Name
name: &'static str,
}
-fn sheer(query: Query<(Wool, Name)>)
+fn sheer(query: Query<(&mut Wool, &Name)>)
{
for (mut wool, name) in &query {
if wool.remaining == 0 {
@@ -36,7 +37,7 @@ fn sheer(query: Query<(Wool, Name)>)
}
}
-fn feed(query: Query<(Health, Name)>)
+fn feed(query: Query<(&mut Health, &Name)>)
{
for (mut health, name) in &query {
health.health += 1;
@@ -45,7 +46,7 @@ fn feed(query: Query<(Health, Name)>)
}
}
-fn age(query: Query<(Health, Name)>, mut actions: Actions)
+fn age(query: Query<(&mut Health, &Name)>, mut actions: Actions)
{
for (mut health, name) in &query {
if health.health <= 2 {
@@ -64,28 +65,28 @@ fn age(query: Query<(Health, Name)>, mut actions: Actions)
}
}
-#[derive(Debug)]
-struct EventA;
+static_entity!(
+ SHEER_PHASE,
+ (Phase, <Relationship<ChildOf, Phase>>::new(*UPDATE_PHASE))
+);
-impl Event for EventA {}
+static_entity!(
+ FEED_PHASE,
+ (Phase, <Relationship<ChildOf, Phase>>::new(*SHEER_PHASE))
+);
-#[derive(Debug)]
-struct EventB;
-
-impl Event for EventB {}
-
-#[derive(Debug)]
-struct EventC;
-
-impl Event for EventC {}
+static_entity!(
+ AGE_PHASE,
+ (Phase, <Relationship<ChildOf, Phase>>::new(*FEED_PHASE))
+);
fn main()
{
let mut world = World::new();
- world.register_system(EventA, sheer);
- world.register_system(EventB, feed);
- world.register_system(EventC, age);
+ world.register_system(*SHEER_PHASE, sheer);
+ world.register_system(*FEED_PHASE, feed);
+ world.register_system(*AGE_PHASE, age);
world.create_entity((
Wool { remaining: 30 },
@@ -93,5 +94,5 @@ fn main()
Name { name: "Bessy" },
));
- world.event_loop::<(EventA, EventB, EventC)>();
+ world.start_loop();
}
diff --git a/ecs/examples/extension.rs b/ecs/examples/extension.rs
index 2022b05..f6282e1 100644
--- a/ecs/examples/extension.rs
+++ b/ecs/examples/extension.rs
@@ -1,6 +1,6 @@
use ecs::actions::Actions;
-use ecs::event::Event;
use ecs::extension::{Collector as ExtensionCollector, Extension};
+use ecs::phase::UPDATE as UPDATE_PHASE;
use ecs::{Component, Query, World};
#[derive(Debug, Component)]
@@ -19,14 +19,9 @@ enum EvilnessLevel
Medium,
}
-#[derive(Debug)]
-struct Update;
-
-impl Event for Update {}
-
fn spawn_enemies(
- spawner_query: Query<(EnemySpawnSource, Position)>,
- enemies_query: Query<(EvilnessLevel,)>,
+ spawner_query: Query<(&EnemySpawnSource, &Position)>,
+ enemies_query: Query<(&EvilnessLevel,)>,
mut actions: Actions,
)
{
@@ -57,7 +52,7 @@ impl Extension for EnemySpawningExtension
{
fn collect(self, mut collector: ExtensionCollector<'_>)
{
- collector.add_system(Update, spawn_enemies);
+ collector.add_system(*UPDATE_PHASE, spawn_enemies);
collector.add_entity((Position { x: 187, y: 30 }, EnemySpawnSource));
}
@@ -70,8 +65,6 @@ fn main()
world.add_extension(EnemySpawningExtension);
for _ in 0..7 {
- world.emit(Update);
-
- world.perform_queued_actions();
+ world.step();
}
}
diff --git a/ecs/examples/multiple_queries.rs b/ecs/examples/multiple_queries.rs
index 2736bce..e0c957f 100644
--- a/ecs/examples/multiple_queries.rs
+++ b/ecs/examples/multiple_queries.rs
@@ -1,6 +1,6 @@
use std::fmt::Display;
-use ecs::event::start::Start as StartEvent;
+use ecs::phase::START as START_PHASE;
use ecs::{Component, Query, World};
#[derive(Component)]
@@ -31,8 +31,8 @@ impl Display for EnemyName
}
fn do_attacks(
- attacker_query: Query<(AttackStrength,)>,
- enemy_query: Query<(Health, EnemyName)>,
+ attacker_query: Query<(&AttackStrength,)>,
+ enemy_query: Query<(&mut Health, &EnemyName)>,
)
{
for (attack_strength,) in &attacker_query {
@@ -61,7 +61,7 @@ fn main()
{
let mut world = World::new();
- world.register_system(StartEvent, do_attacks);
+ world.register_system(*START_PHASE, do_attacks);
world.create_entity((
Health { health: 100 },
@@ -81,5 +81,5 @@ fn main()
world.create_entity((AttackStrength::Strong,));
world.create_entity((AttackStrength::Weak,));
- world.emit(StartEvent);
+ world.step();
}
diff --git a/ecs/examples/optional_component.rs b/ecs/examples/optional_component.rs
index e47bf2e..488dad2 100644
--- a/ecs/examples/optional_component.rs
+++ b/ecs/examples/optional_component.rs
@@ -1,4 +1,4 @@
-use ecs::event::Event;
+use ecs::phase::UPDATE as UPDATE_PHASE;
use ecs::{Component, Query, World};
#[derive(Debug, Component)]
@@ -21,7 +21,7 @@ pub struct CatName
name: String,
}
-fn pet_cats(query: Query<(CatName, PettingCapacity, Option<Aggressivity>)>)
+fn pet_cats(query: Query<(&CatName, &mut PettingCapacity, &Option<Aggressivity>)>)
{
for (cat_name, mut petting_capacity, aggressivity) in &query {
let Some(aggressivity) = aggressivity else {
@@ -48,16 +48,11 @@ fn pet_cats(query: Query<(CatName, PettingCapacity, Option<Aggressivity>)>)
}
}
-#[derive(Debug)]
-struct PettingTime;
-
-impl Event for PettingTime {}
-
fn main()
{
let mut world = World::new();
- world.register_system(PettingTime, pet_cats);
+ world.register_system(*UPDATE_PHASE, pet_cats);
world.create_entity((
CatName { name: "Jasper".to_string() },
@@ -82,5 +77,5 @@ fn main()
Aggressivity::Low,
));
- world.emit(PettingTime);
+ world.step();
}
diff --git a/ecs/examples/relationship.rs b/ecs/examples/relationship.rs
index e8fb327..240884a 100644
--- a/ecs/examples/relationship.rs
+++ b/ecs/examples/relationship.rs
@@ -1,4 +1,4 @@
-use ecs::event::start::Start as StartEvent;
+use ecs::phase::START as START_PHASE;
use ecs::relationship::Relationship;
use ecs::{Component, Query, World};
@@ -19,7 +19,9 @@ struct Health
struct Holding;
-fn print_player_stats(player_query: Query<(Player, Health, Relationship<Holding, Sword>)>)
+fn print_player_stats(
+ player_query: Query<(&Player, &Health, &Relationship<Holding, Sword>)>,
+)
{
for (_, health, sword_relationship) in &player_query {
println!("Player health: {}", health.health);
@@ -34,7 +36,7 @@ fn main()
{
let mut world = World::new();
- world.register_system(StartEvent, print_player_stats);
+ world.register_system(*START_PHASE, print_player_stats);
let sword_uid = world.create_entity((Sword { attack_strength: 17 },));
@@ -44,5 +46,5 @@ fn main()
Relationship::<Holding, Sword>::new(sword_uid),
));
- world.emit(StartEvent);
+ world.step();
}
diff --git a/ecs/examples/simple.rs b/ecs/examples/simple.rs
index 4057c84..0169062 100644
--- a/ecs/examples/simple.rs
+++ b/ecs/examples/simple.rs
@@ -1,4 +1,4 @@
-use ecs::event::start::Start as StartEvent;
+use ecs::phase::START as START_PHASE;
use ecs::{Component, Query, World};
#[derive(Component)]
@@ -13,7 +13,7 @@ struct Greeting
greeting: String,
}
-fn say_hello(query: Query<(SomeData, Greeting)>)
+fn say_hello(query: Query<(&SomeData, &Greeting)>)
{
for (data, greeting) in &query {
println!("{}: {}", greeting.greeting, data.num);
@@ -24,7 +24,7 @@ fn main()
{
let mut world = World::new();
- world.register_system(StartEvent, say_hello);
+ world.register_system(*START_PHASE, say_hello);
world.create_entity((
SomeData { num: 987_654 },
@@ -38,5 +38,5 @@ fn main()
Greeting { greeting: "Good evening".to_string() },
));
- world.emit(StartEvent);
+ world.step();
}
diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs
index 5890b90..4658fc0 100644
--- a/ecs/examples/with_local.rs
+++ b/ecs/examples/with_local.rs
@@ -1,5 +1,5 @@
use ecs::component::local::Local;
-use ecs::event::Event;
+use ecs::phase::UPDATE as UPDATE_PHASE;
use ecs::system::{Into, System};
use ecs::{Component, Query, World};
@@ -21,7 +21,7 @@ struct SayHelloState
cnt: usize,
}
-fn say_hello(query: Query<(SomeData,)>, mut state: Local<SayHelloState>)
+fn say_hello(query: Query<(&SomeData,)>, mut state: Local<SayHelloState>)
{
for (data,) in &query {
println!("Hello there. Count {}: {}", state.cnt, data.num);
@@ -30,7 +30,7 @@ fn say_hello(query: Query<(SomeData,)>, mut state: Local<SayHelloState>)
}
}
-fn say_whats_up(query: Query<(SomeData, Name)>, mut state: Local<SayHelloState>)
+fn say_whats_up(query: Query<(&SomeData, &Name)>, mut state: Local<SayHelloState>)
{
for (data, name) in &query {
println!(
@@ -42,24 +42,19 @@ fn say_whats_up(query: Query<(SomeData, Name)>, mut state: Local<SayHelloState>)
}
}
-#[derive(Debug)]
-struct Update;
-
-impl Event for Update {}
-
fn main()
{
let mut world = World::new();
world.register_system(
- Update,
+ *UPDATE_PHASE,
say_hello
.into_system()
.initialize((SayHelloState { cnt: 0 },)),
);
world.register_system(
- Update,
+ *UPDATE_PHASE,
say_whats_up
.into_system()
.initialize((SayHelloState { cnt: 0 },)),
@@ -69,9 +64,6 @@ fn main()
world.create_entity((SomeData { num: 345 },));
- world.emit(Update);
-
- println!("Haha");
-
- world.emit(Update);
+ world.step();
+ world.step();
}
diff --git a/ecs/examples/with_sole.rs b/ecs/examples/with_sole.rs
index a387bea..689e562 100644
--- a/ecs/examples/with_sole.rs
+++ b/ecs/examples/with_sole.rs
@@ -1,6 +1,7 @@
-use ecs::event::Event;
+use ecs::phase::{Phase, UPDATE as UPDATE_PHASE};
+use ecs::relationship::{ChildOf, Relationship};
use ecs::sole::Single;
-use ecs::{Component, Query, Sole, World};
+use ecs::{static_entity, Component, Query, Sole, World};
#[derive(Component)]
struct Ammo
@@ -14,7 +15,7 @@ struct AmmoCounter
counter: u32,
}
-fn count_ammo(query: Query<(Ammo,)>, mut ammo_counter: Single<AmmoCounter>)
+fn count_ammo(query: Query<(&Ammo,)>, mut ammo_counter: Single<AmmoCounter>)
{
for (ammo,) in &query {
println!("Found {} ammo", ammo.ammo_left);
@@ -30,22 +31,17 @@ fn print_total_ammo_count(ammo_counter: Single<AmmoCounter>)
assert_eq!(ammo_counter.counter, 19);
}
-#[derive(Debug)]
-struct EventA;
-
-impl Event for EventA {}
-
-#[derive(Debug)]
-struct EventB;
-
-impl Event for EventB {}
+static_entity!(
+ PRINT_AMMO_COUNT_PHASE,
+ (Phase, <Relationship<ChildOf, Phase>>::new(*UPDATE_PHASE))
+);
fn main()
{
let mut world = World::new();
- world.register_system(EventA, count_ammo);
- world.register_system(EventB, print_total_ammo_count);
+ world.register_system(*UPDATE_PHASE, count_ammo);
+ world.register_system(*PRINT_AMMO_COUNT_PHASE, print_total_ammo_count);
world.create_entity((Ammo { ammo_left: 4 },));
world.create_entity((Ammo { ammo_left: 7 },));
@@ -53,7 +49,5 @@ fn main()
world.add_sole(AmmoCounter::default()).unwrap();
- world.emit(EventA);
-
- world.emit(EventB);
+ world.step();
}