1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Flax is a performant and ergonomic Entity Component System.
//!
//! The world is organized by simple identifiers known as an [`Entity`](https://docs.rs/flax/latest/flax/entity/struct.Entity.html), which can have any number of components attached to them.
//!
//! Systems operate on the world's entities and provide the application logic.
//!
//! Consider reading the [**User Guide**](https://ten3roberts.github.io/flax/guide)
//!
//! ## Features
//!
//! - [Declarative component macro](https://docs.rs/flax/latest/flax/macro.component.html)
//! - [Expressive query system](https://docs.rs/flax/latest/flax/query/struct.Query.html)
//! - [Change detection](https://docs.rs/flax/latest/flax/component/struct.Component.html#method.modified)
//! - [Query filtering](https://docs.rs/flax/latest/flax/filter/index.html)
//! - [System execution](https://docs.rs/flax/latest/flax/system/struct.System.html)
//! - [Multithreaded system execution](https://docs.rs/flax/latest/flax/schedule/struct.Schedule.html)
//! - [Many to many entity relation and graphs](https://docs.rs/flax/latest/flax/macro.component.html#relations)
//! - [Reflection through component metadata](https://docs.rs/flax/latest/flax/macro.component.html)
//! - [Ergonomic entity builder](https://docs.rs/flax/latest/flax/entity/struct.EntityBuilder.html)
//! - [Serialization and deserialization](https://docs.rs/flax/latest/flax/serialize/)
//! - [(async) event subscription](https://docs.rs/flax/latest/flax/world/struct.World.html#method.subscribe)
//! - [Runtime components](https://docs.rs/flax/latest/flax/world/struct.World.html#method.spawn_component)
//! - ...and more
//!
//! ## [Live Demo](https://ten3roberts.github.io/flax/asteroids)
//! See a live demo of asteroids using wasm [here](https://ten3roberts.github.io/flax/asteroids).
//!
//! [Source](https://github.com/ten3roberts/flax/blob/main/asteroids/src/main.rs)
//!
//! ## Example Usage
//!
//! ```rust
//! // Declare static components
//! use flax::*;
//!
//! component! {
//! health: f32,
//! regen: f32,
//! pos: (f32, f32),
//! player: (),
//! items: Vec<String>,
//! }
//!
//! let mut world = World::new();
//!
//! // Spawn an entity
//! let p = EntityBuilder::new()
//! .set(health(), 50.0)
//! .tag(player())
//! .set(pos(), (0.0, 0.0))
//! .set(regen(), 1.0)
//! .set_default(items())
//! .spawn(&mut world);
//!
//! let mut query = Query::new((health().as_mut(), regen()));
//!
//! // Apply health regeneration for all matched entites
//! for (health, regen) in &mut query.borrow(&world) {
//! *health = (*health + regen).min(100.0);
//! }
//!
//! ```
//!
//! ## Systems
//! Queries with logic can be abstracted into a system, and multiple systems can be
//! collected into a schedule.
//!
//! ```rust
//! # use flax::*;
//! # component! {
//! # health: f32,
//! # regen: f32,
//! # pos: (f32, f32),
//! # player: (),
//! # items: Vec<String>,
//! # }
//! # fn main() -> anyhow::Result<()> {
//! # let mut world = World::new();
//! let regen_system = System::builder()
//! .with_query(Query::new((health().as_mut(), regen())))
//! .for_each(|(health, regen)| {
//! *health = (*health + regen).min(100.0);
//! })
//! .boxed();
//!
//! let despawn_system = System::builder()
//! .with_query(Query::new(entity_ids()).filter(health().le(0.0)))
//! .with_cmd_mut()
//! .build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
//! for id in &mut q {
//! cmd.despawn(id);
//! }
//! })
//! .boxed();
//!
//! let mut schedule = Schedule::from([regen_system, despawn_system]);
//!
//! schedule.execute_par(&mut world)?;
//!
//! # Ok(())
//! # }
//! ```
//! ## Relations
//!
//! Flax provides first class many-many relations between entities, which is useful for tree scene
//! hierarchies, graphs, and physics joints between entities.
//!
//! Relations can be both state-less or have associated data, like spring or joint strengths.
//!
//! Relations are cache friendly and querying children of does not require random access. In
//! addition, relations are cleaned up on despawns and are stable during serialization, even if the
//! entity ids migrate due to collisions.
//!
//! See [the guide](https://ten3roberts.github.io/flax/guide/fundamentals/relations.html) for more
//! details.
//!
//! ```rust
//! # use flax::{*, components::*};
//! component! {
//! child_of(parent): () => [ Debuggable ],
//! }
//!
//! let mut world = World::new();
//!
//! let parent = Entity::builder()
//! .set(name(), "Parent".into())
//! .spawn(&mut world);
//!
//! let child1 = Entity::builder()
//! .set(name(), "Child1".into())
//! .set_default(child_of(parent))
//! .spawn(&mut world);
//!
//!
//! ```
//!
//!
//! ## Comparison to other ECS
//!
//! Compared to other ECS implementations, a component is simply another `Entity`
//! identifier to which data is attached. This means the same "type" can be added to
//! an entity multiple times.
//!
//! A limitation of existing implementations such as [specs](https://github.com/amethyst/specs), [planck](https://github.com/jojolepro/planck_ecs/), or [hecs](https://github.com/Ralith/hecs) is that newtype wrappers need to be created to allow components of the same inner type to coexist.
//!
//! This leads to having to forward all trait implementations trough e.g
//! `derive-more` or dereferencing the *newtypes* during usage.
//!
//! By making components separate from the type the components can work together without `deref` or
//! newtype construction.
//!
//! ```rust
//! # use flax::*;
//! # use glam::*;
//! component! {
//! velocity: Vec3,
//! position: Vec3,
//! }
//! # fn main() -> anyhow::Result<()> {
//! # let mut world = World::new();
//! # let entity = EntityBuilder::new().set(velocity(), glam::vec3(1.0, 3.0, 5.0)).set_default(position()).spawn(&mut world);
//!
//! let vel = world.get(entity, velocity())?;
//! let mut pos = world.get_mut(entity, position())?;
//! let dt = 0.1;
//!
//! *pos += *vel * dt;
//! # Ok(())
//! # }
//! ```
//!
//! On a further note, since the components have to be declared beforehand, it limits the amount of types which can be
//! inserted as components. This fixes subtle bugs which come by having the type
//! dictate the component, such as using an `Arc<Type>` instead of just `Type`,
//! which leads to subsequent systems not finding the `Type` on the entity.
//!
//! Using statically declared components makes the rust type system disallow
//! these cases and catches these bugs earlier.
//!
//! ## Motivation
//!
//! During development of a game in school I used the `hecs` ECS. It is an awesome
//! library, and the author [Ralith](https://github.com/Ralith) has been wonderful in accepting
//! contributions and inquiries.
//!
//! Despite this, I often made subtle bugs with *similar* types. The game engine was
//! cluttered with gigantic newtypes for `Velocity`, `Position` with many *deref*
//! coercions in order to coexist.
//!
//! ## Unsafe
//! This library makes use of unsafe for type erasure and the allocation in storage
//! of `ComponentBuffer`s and `Archetype`s.
extern crate alloc;
/// Structured component storage
/// Provides a buffer for holding multiple types simultaneously
/// Contains a commandbuffer
/// Low level component construction
/// Provides entity identifiers
/// Filter items yielded queries
/// System execution
/// Contains the main ecs world
/// Provides a debug visitor
// mod cascade;
/// Defines the single error type and result alias
/// Subscribe to changes in the world
/// Traits for fetching multiple component values simultaneously
/// Formatting utilities
/// Component metadata used for reflection
/// Query the world
/// Low level relation construction
/// System execution
/// Allows for efficient serialization and deserialization of the world and the
/// entities therein
/// Provides a sink trait for sending events
/// Provides tuple utilities like `cloned`
/// vtable implementation for dynamic dispatching
// Required due to macro
pub use ;
pub use CommandBuffer;
pub use Component;
pub use ;
pub use ;
pub use ;
pub use Error;
pub use ;
pub use ;
pub use ;
pub use RelationExt;
pub use ;
pub use ;
pub use World;
pub use ArchetypeSearcher;
pub use ComponentVTable;
pub use *;