World class
A Bevy-inspired Entity-Component-System container backed by DIRegistry.
Components, resources, entity-presence markers, and bundles all live as dependencies in an internal DIRegistry; queries use its reverse type index to fetch matching entities in O(K) rather than scanning every dependency. The DI registry is exposed via registry so existing DI tooling (lifecycle callbacks, snapshots, change listeners) keeps working.
final world = World()
..insertResource(const GameTime(0))
..addStartupSystem(SpawnPlayer())
..addSystem(MovementSystem())
..addSystem(DamageSystem());
while (running) {
world.update(const Duration(milliseconds: 16));
}
Constructors
-
World({Option<
DIRegistry> registry = const None()})
Properties
- elapsed → Duration
-
Cumulative time advanced through update since this world was created.
no setter
-
entities
→ Iterable<
WorldEntity> -
All entities currently alive in this world.
no setter
- entityCount → int
-
Number of live entities.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- isDisposed → bool
-
Whether dispose has been called.
no setter
- registry → DIRegistry
-
The backing DI registry. Components are stored under their owning
entity's group; resources live under
_resourceGroup; the alive-tag component anchors empty entities. Direct manipulation is supported but rarely needed — prefer the typed World / WorldEntity API.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
startupSystems
→ List<
System> -
Systems registered to run once before the first update.
no setter
-
systems
→ List<
System> -
Systems registered to run every update, in execution order.
no setter
Methods
-
addPlugin(
EcsPlugin plugin) → World -
Installs
plugin, letting it register systems, resources, and event subscriptions. Returnsthisfor chaining. Bevy'sapp.add_plugin(...). -
addStartupSystem(
System system) → World -
Adds
systemto the startup schedule. Runs once, before the first regular update. Returnsthisfor chaining. -
addSystem(
System system) → World -
Adds
systemto the schedule. Runs every update in registration order. Returnsthisfor chaining. -
clearEntities(
) → void - Removes every entity and component but keeps systems, resources, and event subscriptions.
-
despawn(
WorldEntity entity) → bool -
Removes
entityand all of its components. Returnstrueif the entity was alive. -
dispose(
) → void -
Tears the world down: disposes every system (startup and regular),
cascades
dispose()to every component / resource that mixes ServiceMixin, clears the registry, drops event subscriptions, and marks the world so further update calls are no-ops. -
each1<
T1 extends Component> () → Iterable< (WorldEntity, T1)> -
Iterates
(entity, T1)for every entity with aT1component. Inspired by Bevy'sQuery<&T>. -
each2<
T1 extends Component, T2 extends Component> () → Iterable< (WorldEntity, T1, T2)> -
Iterates
(entity, T1, T2)for every entity with both components. -
each3<
T1 extends Component, T2 extends Component, T3 extends Component> () → Iterable< (WorldEntity, T1, T2, T3)> -
Iterates
(entity, T1, T2, T3)for every entity with all three. -
get<
T extends Component> (WorldEntity entity) → Option< T> -
Reads the component of type
Tfromentity, or None if absent. -
getResource<
T extends Resource> () → Option< T> -
Returns the resource of type
T, or None if not registered. -
has<
T extends Component> (WorldEntity entity) → bool -
Whether
entityhas a component of typeT. -
insertResource<
T extends Resource> (T resource) → void -
Inserts or replaces the global resource of type
T. Bevy'sapp.insert_resource(...). -
mutate<
T extends Component> (WorldEntity entity, T update(T current)) → Option< T> -
Replaces the component of type
Tusingupdate. Returns the new value, or None ifentityhas no component of typeT. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onEvent<
E extends Event> (void listener(E event)) → void Function() -
Subscribes to events of type
E. The returned closure removes the subscription when called. Subtype propagation: a derived event sent via sendEvent is delivered to base-typed listeners as well. -
query1<
T1 extends Component> () → Iterable< WorldEntity> -
query2<
T1 extends Component, T2 extends Component> () → Iterable< WorldEntity> -
query3<
T1 extends Component, T2 extends Component, T3 extends Component> () → Iterable< WorldEntity> -
query4<
T1 extends Component, T2 extends Component, T3 extends Component, T4 extends Component> () → Iterable< WorldEntity> -
query5<
T1 extends Component, T2 extends Component, T3 extends Component, T4 extends Component, T5 extends Component> () → Iterable< WorldEntity> -
queryTypes(
List< Type> types) → Iterable<WorldEntity> -
Entities that hold every type in
types. The smallest matching bucket is iterated first; missing types short-circuit to an empty result. -
readEvents<
E extends Event> () → Iterable< E> -
Returns the events of type
Esent so far this (outer) tick. Filters the flat buffer byis E, so derived events are visible via base-typed readers and vice-versa is correctly excluded. -
remove<
T extends Component> (WorldEntity entity) → Option< T> -
Removes the component of type
Tfromentity. -
removeResource<
T extends Resource> () → Option< T> -
Removes and returns the resource of type
T. -
removeSystem(
System system) → bool -
Removes
systemfrom either schedule, calling its System.dispose. -
requireResource<
T extends Resource> () → T -
Returns the resource of type
Tor throws if not registered. -
sendEvent<
E extends Event> (E event) → void -
Synchronously dispatches
eventto every listener whose registered typeEis a supertype ofevent(subtype propagation —Liskovfor events), then buffers it for readEvents until the next outer tick ends. A snapshot of the listener list is taken so subscribing or unsubscribing inside a handler does not affect the current dispatch. -
set(
WorldEntity entity, Component component) → void -
Sets or replaces a component on
entity. -
spawn(
[Option< Iterable< components = const None()]) → WorldEntityComponent> > -
Spawns a new entity. If
componentsare provided, they are attached immediately. Otherwise an alive-tag component is inserted so the entity remains visible to entities / queries until it gets real components. -
spawnBundle(
Bundle bundle) → WorldEntity -
Spawns and returns the entity wrapped by
bundle. -
toString(
) → String -
A string representation of this object.
inherited
-
update(
Duration dt) → void -
Advances the world by
dt. The first call also runs every startup system. Drains the per-tick event buffer after all systems have run. No-op if the world has been disposed. -
withComponent<
T extends Component> () → Iterable< WorldEntity> -
Every alive entity that has a component of type
T. O(K) using the reverse type index in DIRegistry. A snapshot of the bucket is taken up-front so callers can safely insert, remove, or despawn entities mid-iteration.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited