trex/
lib.rs

1//! An entity component system inspired by _entityx_.
2//!
3//! # Examples
4//!
5//! A simple 2D physics simulation.
6//!
7//! ```
8//! #[macro_use]
9//! extern crate trex;
10//!
11//! use trex::*;
12//!
13//! // The components used in the simulation.
14//! pub struct Position { pub x: f32, pub y: f32 }
15//! pub struct Velocity { pub dx: f32, pub dy: f32 }
16//! pub struct Acceleration { pub ddx: f32, pub ddy: f32 }
17//!
18//! components!(Position, Velocity, Acceleration);
19//!
20//! pub struct PhysicsSystem {
21//!     filter: ComponentFilter, // Used to select entities with the components of interest to this
22//!                              // system.
23//! }
24//!
25//! impl PhysicsSystem {
26//!     pub fn new() -> PhysicsSystem {
27//!         PhysicsSystem {
28//!             filter: ComponentFilter::new()
29//!                 .with::<Position>()
30//!                 .with::<Velocity>()
31//!                 .with::<Acceleration>(),
32//!         }
33//!     }
34//! }
35//!
36//! impl System for PhysicsSystem {
37//!     fn update(&mut self, world: &mut World, queue: &EventQueue, emitter: &mut EventEmitter, dt: f32) {
38//!         let dt_secs = dt / 1000.0;
39//!         for entity in world.filter(&self.filter) {
40//!             assert!(world.has::<Position>(entity));
41//!             assert!(world.has::<Velocity>(entity));
42//!             assert!(world.has::<Acceleration>(entity));
43//!             let (dx, dy) = {
44//!                 let &Acceleration { ddx, ddy } = world.get::<Acceleration>(entity).unwrap();
45//!                 let mut vel = world.get_mut::<Velocity>(entity).unwrap();
46//!                 vel.dx += ddx * dt_secs;
47//!                 vel.dy += ddy * dt_secs;
48//!                 (vel.dx, vel.dy)
49//!             };
50//!             let mut pos = world.get_mut::<Position>(entity).unwrap();
51//!             pos.x += dx * dt_secs;
52//!             pos.y += dy * dt_secs;
53//!         }
54//!         emitter.emit(trex::Halt);
55//!     }
56//! }
57//!
58//! struct TestSystem;
59//!
60//! impl System for TestSystem {
61//!     fn update(&mut self, world: &mut World, _queue: &EventQueue, _emitter: &mut EventEmitter, _dt: f32) {
62//!         let entity = world.lookup("Test").unwrap();
63//!         let pos = world.get::<Position>(entity).unwrap();
64//!         assert_eq!(pos.x, 9.0);
65//!         assert_eq!(pos.y, 12.0);
66//!     }
67//! }
68//!
69//! fn main() {
70//!     let world = {
71//!         let mut world = World::new();
72//!         world.register::<Position>();
73//!         world.register::<Velocity>();
74//!         world.register::<Acceleration>();
75//!
76//!         // Create an entity that accelerates in the x and y directions.
77//!         let entity = world.create();
78//!         world.tag(entity, "Test");
79//!         world.add(entity, Position { x: 1.0, y: 2.0 });
80//!         world.add(entity, Velocity { dx: 3.0, dy: 4.0 });
81//!         world.add(entity, Acceleration { ddx: 5.0, ddy: 6.0 });
82//!         world
83//!     };
84//!
85//!     let mut queue = EventQueue::new();
86//!     let mut emitter = EventEmitter::new();
87//!
88//!     let mut simulation = Simulation::new(world, queue, emitter);
89//!     simulation.register(PhysicsSystem::new());
90//!     simulation.register(TestSystem);
91//!
92//!     // Run a single iteration of the simulation.
93//!     simulation.update(1000.0);
94//!     assert!(simulation.halt());
95//! }
96//! ```
97
98extern crate vec_map;
99extern crate bit_set;
100
101mod component;
102mod event;
103mod family;
104mod id;
105mod simulation;
106mod system;
107mod time;
108mod world;
109
110#[macro_use]
111mod macros;
112
113pub use family::{Family, FamilyMember};
114pub use event::{EventQueue, EventEmitter};
115pub use simulation::{Halt, Simulation};
116pub use system::System;
117pub use time::calc_millis;
118pub use world::{ComponentFilter, Entity, World};