crcbl_ecs/lib.rs
1//! Entity/component storage built on system-owned arrays.
2//!
3//! # Model
4//!
5//! * **Entity** — a generational id ([`crcbl_core::Handle<EntityMarker>`]) from
6//! [`crcbl_core::Pool`]. An entity carries no data of its own.
7//! * **System** — owns a dense `Vec<T>` of component data plus a sparse
8//! entity→index map. Systems iterate their own arrays linearly (SoA,
9//! cache-friendly). Cross-system access uses sparse lookups for cold paths.
10//! * **Schedule** — an ordered sequence of systems run each tick. Each system
11//! declares the [`Shared`] resources its tick reads and writes
12//! ([`SystemTrait::access`]); the schedule derives the conflicts between
13//! them at registration, groups the systems into stages no two systems of
14//! which conflict, and, in debug builds, panics on a tick touching a
15//! resource its system did not declare. Handed a `crcbl_jobs::Pool`, it
16//! ticks each stage's systems across the pool's threads, with the same
17//! result as running them one by one.
18//! * **World** — the container: entity pool, schedule, deferred-destruction
19//! queue. `World::tick()` runs the schedule then sweeps dead entities.
20//! * **[`quantize`]** — how a replicated component declares its wire form:
21//! fixed point, half float and smallest-three rotations, applied in
22//! [`SystemTrait::replicate`] before the server compares anything.
23//!
24//! # Example
25//!
26//! ```rust
27//! use crcbl_ecs::*;
28//!
29//! let mut world = World::new();
30//!
31//! // Create entities.
32//! let player = world.spawn();
33//! let npc = world.spawn();
34//!
35//! // Register a position system and attach data.
36//! let mut positions = System::<(f32, f32, f32)>::new("position");
37//! positions.attach(player, (0.0, 0.0, 0.0));
38//! positions.attach(npc, (10.0, 0.0, 5.0));
39//! world.register_system(Box::new(positions));
40//!
41//! // Run one tick.
42//! world.tick();
43//!
44//! // Inspect.
45//! let stats = Inspector::collect(&world);
46//! assert_eq!(stats[0].name, "position");
47//! assert_eq!(stats[0].entity_count, 2);
48//!
49//! // Despawn an entity — deferred until sweep.
50//! world.despawn(npc);
51//! world.tick();
52//! assert_eq!(world.entity_count(), 1);
53//! ```
54
55mod access;
56mod component_hash;
57mod entity;
58mod game_module;
59mod inspector;
60pub mod quantize;
61mod schedule;
62mod shared;
63mod system;
64mod tick_time;
65mod world;
66
67pub use access::{Access, AccessError, Conflict, ConflictKind};
68pub use component_hash::ComponentHash;
69pub use entity::{Entity, EntityMarker};
70pub use game_module::{ClientInputs, GameModule};
71pub use inspector::{Inspector, SystemStats};
72pub use schedule::{Schedule, ScheduleClock};
73pub use shared::Shared;
74pub use system::{DebugCtx, DebugDrawFn, System, SystemTrait};
75pub use tick_time::{TICK_TIME_WINDOW, TickTime};
76pub use world::World;
77
78/// Builds an [`Entity`] from raw index and generation for use in tests.
79///
80/// Real entities are only produced by [`World::spawn`]; this exists so unit
81/// tests can fabricate handles without a full `World`.
82#[cfg(test)]
83pub(crate) fn test_entity(index: u32, generation: u32) -> Entity {
84 Entity::from_bits(((generation as u64) << 32) | index as u64)
85 .expect("test entity must have non-zero generation")
86}