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

Change component definitions to use Self and destruct system data tuple in parameter list. #206

Merged
merged 1 commit into from
Jun 26, 2017
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
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[gi]: https://badges.gitter.im/slide-rs/specs.svg
[gl]: https://gitter.im/slide-rs/specs

Specs is an Entity-Component System written in Rust.
Specs is an Entity-Component System written in Rust.
Unlike most other ECS libraries out there, it provides

* easy parallelism
Expand All @@ -41,11 +41,11 @@ struct Vel(f32);
struct Pos(f32);

impl Component for Vel {
type Storage = VecStorage<Vel>;
type Storage = VecStorage<Self>;
}

impl Component for Pos {
type Storage = VecStorage<Pos>;
type Storage = VecStorage<Self>;
}

struct SysA;
Expand All @@ -56,13 +56,10 @@ impl<'a> System<'a> for SysA {
// see the `full` example.
type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Vel>);

fn run(&mut self, data: Self::SystemData) {
fn run(&mut self, (mut pos, vel): Self::SystemData) {
// The `.join()` combines multiple components,
// so we only access those entities which have
// both of them.

let (mut pos, vel) = data;

for (pos, vel) in (&mut pos, &vel).join() {
pos.0 += vel.0;
}
Expand Down
57 changes: 29 additions & 28 deletions benches/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ type Vec2 = Vector2<f32>;
struct Pos(Vec2);

impl Component for Pos {
type Storage = VecStorage<Pos>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
struct Vel(Vec2);

impl Component for Vel {
type Storage = VecStorage<Vel>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
struct Force(Vec2);

impl Component for Force {
type Storage = VecStorage<Force>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
struct InvMass(f32);

impl Component for InvMass {
type Storage = VecStorage<InvMass>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
struct Lifetime(f32);

impl Component for Lifetime {
type Storage = VecStorage<Lifetime>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -59,7 +59,7 @@ struct Ball {
}

impl Component for Ball {
type Storage = VecStorage<Ball>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -69,7 +69,7 @@ struct Rect {
}

impl Component for Rect {
type Storage = VecStorage<Rect>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -79,14 +79,14 @@ enum Spawner {
}

impl Component for Spawner {
type Storage = HashMapStorage<Spawner>;
type Storage = HashMapStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
struct SpawnRequests(usize);

impl Component for SpawnRequests {
type Storage = HashMapStorage<SpawnRequests>;
type Storage = HashMapStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -97,7 +97,7 @@ struct Collision {
}

impl Component for Collision {
type Storage = DenseVecStorage<Collision>;
type Storage = DenseVecStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -107,7 +107,7 @@ struct Room {
}

impl Component for Room {
type Storage = HashMapStorage<Room>;
type Storage = HashMapStorage<Self>;
}

#[derive(Clone, Copy, Debug)]
Expand All @@ -117,14 +117,14 @@ enum Color {
}

impl Component for Color {
type Storage = VecStorage<Color>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Copy, Debug, Default)]
struct KillsEnemy;

impl Component for KillsEnemy {
type Storage = NullStorage<KillsEnemy>;
type Storage = NullStorage<Self>;
}

// -- Resources --
Expand All @@ -143,11 +143,9 @@ impl<'a> System<'a> for Integrate {
ReadStorage<'a, InvMass>,
Fetch<'a, DeltaTime>);

fn run(&mut self, data: Self::SystemData) {
fn run(&mut self, (mut pos, mut vel, mut force, inv_mass, delta): Self::SystemData) {
use cgmath::Zero;

let (mut pos, mut vel, mut force, inv_mass, delta) = data;

let delta: f32 = delta.0;

for (pos, vel, force, inv_mass) in (&mut pos, &mut vel, &mut force, &inv_mass).join() {
Expand Down Expand Up @@ -176,21 +174,24 @@ impl<'a> System<'a> for Spawn {
WriteStorage<'a, Color>,
WriteStorage<'a, SpawnRequests>);

fn run(&mut self, data: Self::SystemData) {
fn run(
&mut self,
(
entities,
spawner,
mut pos,
mut vel,
mut force,
mut inv_mass,
mut ball,
mut rect,
mut color,
mut requests
): Self::SystemData
) {
use cgmath::Zero;
use rand::Rng;

let (entities,
spawner,
mut pos,
mut vel,
mut force,
mut inv_mass,
mut ball,
mut rect,
mut color,
mut requests) = data;

let mut rng = thread_rng();
let mut gen = || rng.gen_range(-4.0, 4.0);

Expand Down
4 changes: 2 additions & 2 deletions benches/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use specs::{Component, HashMapStorage, Join, ParJoin, VecStorage, World};
struct CompInt(i32);

impl Component for CompInt {
type Storage = VecStorage<CompInt>;
type Storage = VecStorage<Self>;
}

#[derive(Clone, Debug)]
struct CompBool(bool);

impl Component for CompBool {
type Storage = HashMapStorage<CompBool>;
type Storage = HashMapStorage<Self>;
}

fn create_world() -> World {
Expand Down
36 changes: 18 additions & 18 deletions book/src/02_hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Let's start by creating some data:
use specs::{Component, VecStorage};

#[derive(Debug)]
struct Position {
x: f32,
y: f32
struct Position {
x: f32,
y: f32
}

impl Component for Position {
type Storage = VecStorage<Position>;
type Storage = VecStorage<Self>;
}

#[derive(Debug)]
Expand All @@ -39,11 +39,11 @@ struct Velocity {
}

impl Component for Velocity {
type Storage = VecStorage<Velocity>;
type Storage = VecStorage<Self>;
}
```

These will be our components, stored in a `VecStorage`
These will be our components, stored in a `VecStorage`
(see [the storages chapter][sc] for more details), so we can associate some data
with an entity. Before doing that, we need to create a world, because this is
where the storage for all the components is located.
Expand Down Expand Up @@ -79,7 +79,7 @@ struct HelloWorld;

impl<'a> System<'a> for HelloWorld {
type SystemData = ();

fn run(&mut self, data: Self::SystemData) {}
}
```
Expand All @@ -99,10 +99,10 @@ struct HelloWorld;

impl<'a> System<'a> for HelloWorld {
type SystemData = ReadStorage<'a, Position>;

fn run(&mut self, position: Self::SystemData) {
use specs::Join;

for position in position.join() {
println!("Hello, {:?}", &position);
}
Expand Down Expand Up @@ -136,13 +136,13 @@ Here the complete example of this chapter:
use specs::{Component, ReadStorage, System, VecStorage, World, RunNow};

#[derive(Debug)]
struct Position {
x: f32,
y: f32
struct Position {
x: f32,
y: f32
}

impl Component for Position {
type Storage = VecStorage<Position>;
type Storage = VecStorage<Self>;
}

#[derive(Debug)]
Expand All @@ -152,17 +152,17 @@ struct Velocity {
}

impl Component for Velocity {
type Storage = VecStorage<Velocity>;
type Storage = VecStorage<Self>;
}

struct HelloWorld;

impl<'a> System<'a> for HelloWorld {
type SystemData = ReadStorage<'a, Position>;

fn run(&mut self, position: Self::SystemData) {
use specs::Join;

for position in position.join() {
println!("Hello, {:?}", &position);
}
Expand All @@ -172,9 +172,9 @@ impl<'a> System<'a> for HelloWorld {
fn main() {
let mut world = World::new();
world.register::<Position>();

world.create_entity().with(Position { x: 4.0, y: 7.0 }).build();

let mut hello_world = HelloWorld;
hello_world.run_now(&world.res);
}
Expand Down
Loading