Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PairComponentsRefIter and PairComponentsRefIterMut #15

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 90 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ A toy ECS library; works on Windows, macOS, Linux and WebAssembly.

## Benchmark

[Benchmark](./doc/benchmark.md)
[Benchmark result](./doc/benchmark.md)

## Usage

```rust
use xanadu::ecs::{SingleComponentIter, SingleComponentIterMut, World};
use xanadu::ecs::{
PairComponentsRefIter, PairComponentsRefIterMut, SingleComponentExclusiveIter,
SingleComponentExclusiveIterMut, World,
};

#[derive(Debug)]
pub struct Position {
Expand All @@ -22,8 +25,18 @@ pub struct Position {
pub z: f64,
}

#[derive(Debug)]
pub struct Velocity {
pub x: f64,
pub y: f64,
pub z: f64,
}

fn main() {
let mut world = World::builder().register_component::<Position>().build();
let mut world = World::builder()
.register_component::<Position>()
.register_component::<Velocity>()
.build();
for i in 0..5 {
let entity = world.new_entity();
world.attach_component(
Expand All @@ -35,22 +48,46 @@ fn main() {
},
);
}
for i in 0..3 {
let entity = world.new_entity();
world.attach_component(
entity,
Position {
x: i as f64,
y: i as f64,
z: i as f64,
},
);
world.attach_component(
entity,
Velocity {
x: i as f64 * 0.1,
y: i as f64 * 0.1,
z: i as f64 * 0.1,
},
);
}

world.execute(print_system);
world.execute(shuffle_system);
world.execute(increment_system);
world.execute(shuffle_system);
println!("Shuffled and incremented");
world.execute(print_system);
println!("===================");
world.execute(print2_system);
println!("Applying velocity");
world.execute(apply_velocity_system);
world.execute(print2_system);
}

fn print_system(iter: SingleComponentIter<'_, Position>) {
fn print_system(iter: SingleComponentExclusiveIter<'_, Position>) {
for pos in iter {
println!("Pos: [{}, {}, {}]", pos.x, pos.y, pos.z);
}
}

fn shuffle_system(iter: SingleComponentIterMut<'_, Position>) {
fn shuffle_system(iter: SingleComponentExclusiveIterMut<'_, Position>) {
for pos in iter {
let tmp = pos.x;
pos.x = pos.y;
Expand All @@ -59,13 +96,60 @@ fn shuffle_system(iter: SingleComponentIterMut<'_, Position>) {
}
}

fn increment_system(iter: SingleComponentIterMut<'_, Position>) {
fn increment_system(iter: SingleComponentExclusiveIterMut<'_, Position>) {
for pos in iter {
pos.x += 1.0;
pos.y += 2.0;
pos.z += 3.0;
}
}

fn print2_system(iter: PairComponentsRefIter<'_, Position, Velocity>) {
for (pos, vel) in iter {
println!(
"Pos: [{}, {}, {}] Vel: [{}, {}, {}]",
pos.x, pos.y, pos.z, vel.x, vel.y, vel.z
);
}
}

fn apply_velocity_system(iter: PairComponentsRefIterMut<'_, Position, Velocity>) {
for (mut pos, vel) in iter {
pos.x += vel.x;
pos.y += vel.y;
pos.z += vel.z;
}
}
```

Result:

```
Pos: [0, 0, 0]
Pos: [1, 1, 1]
Pos: [2, 2, 2]
Pos: [3, 3, 3]
Pos: [4, 4, 4]
Pos: [0, 0, 0]
Pos: [1, 1, 1]
Pos: [2, 2, 2]
Shuffled and incremented
Pos: [2, 3, 1]
Pos: [3, 4, 2]
Pos: [4, 5, 3]
Pos: [5, 6, 4]
Pos: [6, 7, 5]
Pos: [2, 3, 1]
Pos: [3, 4, 2]
Pos: [4, 5, 3]
===================
Pos: [2, 3, 1] Vel: [0, 0, 0]
Pos: [3, 4, 2] Vel: [0.1, 0.1, 0.1]
Pos: [4, 5, 3] Vel: [0.2, 0.2, 0.2]
Applying velocity
Pos: [2, 3, 1] Vel: [0, 0, 0]
Pos: [3.1, 4.1, 2.1] Vel: [0.1, 0.1, 0.1]
Pos: [4.2, 5.2, 3.2] Vel: [0.2, 0.2, 0.2]
```

## Tests
Expand Down
58 changes: 56 additions & 2 deletions src/bin/usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use xanadu::ecs::{SingleComponentExclusiveIter, SingleComponentExclusiveIterMut, World};
use xanadu::ecs::{
PairComponentsRefIter, PairComponentsRefIterMut, SingleComponentExclusiveIter,
SingleComponentExclusiveIterMut, World,
};

#[derive(Debug)]
pub struct Position {
Expand All @@ -7,8 +10,18 @@ pub struct Position {
pub z: f64,
}

#[derive(Debug)]
pub struct Velocity {
pub x: f64,
pub y: f64,
pub z: f64,
}

fn main() {
let mut world = World::builder().register_component::<Position>().build();
let mut world = World::builder()
.register_component::<Position>()
.register_component::<Velocity>()
.build();
for i in 0..5 {
let entity = world.new_entity();
world.attach_component(
Expand All @@ -20,13 +33,37 @@ fn main() {
},
);
}
for i in 0..3 {
let entity = world.new_entity();
world.attach_component(
entity,
Position {
x: i as f64,
y: i as f64,
z: i as f64,
},
);
world.attach_component(
entity,
Velocity {
x: i as f64 * 0.1,
y: i as f64 * 0.1,
z: i as f64 * 0.1,
},
);
}

world.execute(print_system);
world.execute(shuffle_system);
world.execute(increment_system);
world.execute(shuffle_system);
println!("Shuffled and incremented");
world.execute(print_system);
println!("===================");
world.execute(print2_system);
println!("Applying velocity");
world.execute(apply_velocity_system);
world.execute(print2_system);
}

fn print_system(iter: SingleComponentExclusiveIter<'_, Position>) {
Expand All @@ -51,3 +88,20 @@ fn increment_system(iter: SingleComponentExclusiveIterMut<'_, Position>) {
pos.z += 3.0;
}
}

fn print2_system(iter: PairComponentsRefIter<'_, Position, Velocity>) {
for (pos, vel) in iter {
println!(
"Pos: [{}, {}, {}] Vel: [{}, {}, {}]",
pos.x, pos.y, pos.z, vel.x, vel.y, vel.z
);
}
}

fn apply_velocity_system(iter: PairComponentsRefIterMut<'_, Position, Velocity>) {
for (mut pos, vel) in iter {
pos.x += vel.x;
pos.y += vel.y;
pos.z += vel.z;
}
}
4 changes: 2 additions & 2 deletions src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod world;

pub use component::Component;
pub use iter::{
FromWorld, SingleComponentExclusiveIter, SingleComponentExclusiveIterMut,
SingleComponentRefIter, SingleComponentRefIterMut,
FromWorld, PairComponentsRefIter, PairComponentsRefIterMut, SingleComponentExclusiveIter,
SingleComponentExclusiveIterMut, SingleComponentRefIter, SingleComponentRefIterMut,
};
pub use system::System;
pub use world::{World, WorldBuilder};
Loading