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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
//! Contains the ECS [`World`].
use crate::prelude::*;
/// The [`World`] is simply a collection of [`Resources`], and [`ComponentStores`].
///
/// Also stored in the world is the [`Entities`], but it is stored as a resource.
///
/// [`World`] is designed to be trivially [`Clone`]ed to allow for snapshotting the world state. The
/// is especially useful in the context of rollback networking, which requires the ability to
/// snapshot and restore state.
#[derive(Clone)]
pub struct World {
/// Stores the world resources.
pub resources: Resources,
/// Stores the world components.
pub components: ComponentStores,
}
impl std::fmt::Debug for World {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("World").finish()
}
}
impl Default for World {
fn default() -> Self {
let resources = Resources::new();
// Always initialize an Entities resource
resources.insert(Entities::default());
Self {
resources,
components: Default::default(),
}
}
}
impl World {
/// Create a new [`World`].
pub fn new() -> Self {
Self::default()
}
/// Create a new world that uses the provided entities resource.
///
/// This allows multiple worlds to avoid allocating the same entity IDs.
pub fn with_entities(entities: AtomicResource<Entities>) -> Self {
let resources = Resources::new();
resources
.untyped()
.insert_cell(entities.into_untyped())
.unwrap();
World {
resources,
components: default(),
}
}
/// Remove the component info for dead entities.
///
/// This should be called every game frame to cleanup entities that have been killed.
///
/// This will remove the component storage for all killed entities, and allow their slots to be
/// re-used for any new entities.
pub fn maintain(&self) {
let mut entities = self.resource_mut::<Entities>();
for components in self.components.components.read_only_view().values() {
let mut components = components.borrow_mut();
let killed = entities.killed();
for &entity in killed {
// Safe: We don't provide an out pointer, so it doesn't overlap the component's
// internal storage.
unsafe {
components.remove_raw(entity, None);
}
}
}
entities.clear_killed();
}
/// Run a system once.
///
/// This is good for initializing the world with setup systems.
pub fn run_system<'system, R, In, Out, S>(&self, system: S, input: In) -> Out
where
In: 'system,
Out: 'system,
S: IntoSystem<R, In, Out>,
S::Sys: 'system,
{
let mut s = system.system();
s.run(self, input)
}
/// Get an entity's components.
///
/// # Panics
///
/// Panics if the entity does not have the required components from the query.
pub fn entity_components<Q: QueryItem>(
&self,
entity: Entity,
query: Q,
) -> <Q::Iter as Iterator>::Item {
self.get_entity_components(entity, query).unwrap()
}
/// Get an entity's components.
pub fn get_entity_components<Q: QueryItem>(
&self,
entity: Entity,
query: Q,
) -> Option<<Q::Iter as Iterator>::Item> {
let mut bitset = BitSetVec::default();
if self.resource::<Entities>().bitset().contains(entity) {
bitset.set(entity);
}
query.apply_bitset(&mut bitset);
match query.get_single_with_bitset(bitset.into()) {
Ok(components) => Some(components),
Err(QuerySingleError::NoEntities) => None,
Err(QuerySingleError::MultipleEntities) => {
panic!(
"Query returned a MultipleEntities error for a bitset that \
contains at most one enabled bit"
)
}
}
}
/// Initialize a resource of type `T` by inserting it's default value.
pub fn init_resource<R: HasSchema + FromWorld>(&mut self) -> RefMut<R> {
if unlikely(!self.resources.contains::<R>()) {
let value = R::from_world(self);
self.resources.insert(value);
}
self.resource_mut()
}
/// Insert a resource.
pub fn insert_resource<R: HasSchema>(&mut self, resource: R) -> Option<R> {
self.resources.insert(resource)
}
/// Borrow a resource from the world.
/// # Panics
/// Panics if the resource does not exist in the store.
#[track_caller]
pub fn resource<T: HasSchema>(&self) -> Ref<T> {
match self.resources.get::<T>() {
Some(r) => r,
None => panic!(
"Requested resource {} does not exist in the `World`. \
Did you forget to add it using `world.insert_resource` / `world.init_resource`?",
std::any::type_name::<T>()
),
}
}
/// Borrow a resource from the world.
/// # Panics
/// Panics if the resource does not exist in the store.
#[track_caller]
pub fn resource_mut<T: HasSchema>(&self) -> RefMut<T> {
match self.resources.get_mut::<T>() {
Some(r) => r,
None => panic!(
"Requested resource {} does not exist in the `World`. \
Did you forget to add it using `world.insert_resource` / `world.init_resource`?",
std::any::type_name::<T>()
),
}
}
/// Borrow a resource from the world, if it exists.
pub fn get_resource<T: HasSchema>(&self) -> Option<Ref<T>> {
self.resources.get()
}
/// Borrow a resource from the world, if it exists.
pub fn get_resource_mut<T: HasSchema>(&mut self) -> Option<RefMut<T>> {
self.resources.get_mut()
}
/// Borrow a component store from the world.
/// # Panics
/// Panics if the component store does not exist in the world.
#[track_caller]
pub fn component<T: HasSchema>(&self) -> Ref<ComponentStore<T>> {
self.components.get::<T>().borrow()
}
/// Mutably borrow a component store from the world.
/// # Panics
/// Panics if the component store does not exist in the world.
#[track_caller]
pub fn component_mut<T: HasSchema>(&self) -> RefMut<ComponentStore<T>> {
self.components.get::<T>().borrow_mut()
}
/// Provides an interface for resetting entities, and components.
pub fn reset_internals(&mut self, reset_components: bool, reset_entities: bool) {
if reset_entities {
let mut entities = self.resource_mut::<Entities>();
entities.kill_all();
}
if reset_components {
// Clear all component stores
self.components = ComponentStores::default();
}
// Always maintain to clean up any killed entities
self.maintain();
}
}
/// Creates an instance of the type this trait is implemented for
/// using data from the supplied [`World`].
///
/// This can be helpful for complex initialization or context-aware defaults.
pub trait FromWorld {
/// Creates `Self` using data from the given [`World`].
fn from_world(world: &World) -> Self;
}
impl<T: Default> FromWorld for T {
fn from_world(_world: &World) -> Self {
T::default()
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use super::FromWorld;
#[derive(Clone, Copy, HasSchema, Debug, Eq, PartialEq, Default)]
#[repr(C)]
struct C(u32);
#[derive(Clone, HasSchema, Debug, Eq, PartialEq, Default)]
#[repr(C)]
struct Pos(i32, i32);
#[derive(Clone, HasSchema, Debug, Eq, PartialEq, Default)]
#[repr(C)]
struct Vel(i32, i32);
#[derive(Clone, HasSchema, Debug, Eq, PartialEq, Default)]
struct Marker;
// Sets up the world with a couple entities.
fn setup_world(
mut entities: ResMut<Entities>,
mut pos_comps: CompMut<Pos>,
mut vel_comps: CompMut<Vel>,
mut marker_comps: CompMut<Marker>,
) {
let ent1 = entities.create();
pos_comps.insert(ent1, Pos(0, 100));
vel_comps.insert(ent1, Vel(0, -1));
let ent2 = entities.create();
pos_comps.insert(ent2, Pos(0, 0));
vel_comps.insert(ent2, Vel(1, 1));
marker_comps.insert(ent2, Marker);
}
/// Mutates the positions based on the velocities.
fn pos_vel_system(entities: Res<Entities>, mut pos: CompMut<Pos>, vel: Comp<Vel>) {
for (_, (pos, vel)) in entities.iter_with((&mut pos, &vel)) {
pos.0 += vel.0;
pos.1 += vel.1;
}
}
/// Tests that the world's components matches the state it should after running `setup_world`.
fn test_after_setup_state(
entities: Res<Entities>,
pos: Comp<Pos>,
vel: Comp<Vel>,
marker: Comp<Marker>,
) {
let mut i = 0;
for (entity, (pos, vel)) in entities.iter_with((&pos, &vel)) {
let marker = marker.get(entity);
match (i, pos, vel, marker) {
(0, Pos(0, 100), Vel(0, -1), None) | (1, Pos(0, 0), Vel(1, 1), Some(Marker)) => (),
x => unreachable!("{:?}", x),
}
i += 1;
}
assert_eq!(i, 2);
}
/// Tests that the worlds components matches the state it should after running the
/// pos_vel_system one time.
fn test_pos_vel_1_run(
entities: Res<Entities>,
pos: Comp<Pos>,
vel: Comp<Vel>,
marker: Comp<Marker>,
) {
let mut i = 0;
for (entity, (pos, vel)) in entities.iter_with((&pos, &vel)) {
let marker = marker.get(entity);
match (i, pos, vel, marker) {
(0, Pos(0, 99), Vel(0, -1), None) | (1, Pos(1, 1), Vel(1, 1), Some(Marker)) => (),
x => unreachable!("{:?}", x),
}
i += 1;
}
assert_eq!(i, 2);
}
#[test]
fn sanity_check() {
let world = World::new();
world.run_system(setup_world, ());
// Make sure our entities exist visit properly during iteration
let test = || {};
world.run_system(test, ());
// Mutate and read some components
world.run_system(pos_vel_system, ());
// Make sure the mutations were applied
world.run_system(test_pos_vel_1_run, ());
}
#[test]
fn snapshot() {
let world1 = World::new();
world1.run_system(setup_world, ());
// Snapshot world1
let snap = world1.clone();
// Make sure the snapshot represents world1's state
snap.run_system(test_after_setup_state, ());
// Run the pos_vel system on world1
world1.run_system(pos_vel_system, ());
// Make sure world1 has properly update
world1.run_system(test_pos_vel_1_run, ());
// Make sure the snapshot hasn't changed
snap.run_system(test_after_setup_state, ());
// Run the pos vel system once on the snapshot
snap.run_system(pos_vel_system, ());
// Make sure the snapshot has updated
world1.run_system(test_pos_vel_1_run, ());
}
#[test]
fn world_is_send() {
fn send<T: Send>(_: T) {}
send(World::new())
}
#[test]
fn get_entity_components() {
let w = World::default();
let (e1, e2) = {
let mut entities = w.resource_mut::<Entities>();
(entities.create(), entities.create())
};
let state = w.components.get::<C>();
let mut comp = state.borrow_mut();
let c2 = C(2);
comp.insert(e2, c2);
assert_eq!(w.get_entity_components(e1, &comp), None);
assert_eq!(w.get_entity_components(e2, &comp), Some(&c2));
}
// ============
// From World
// ============
#[derive(Clone, HasSchema, Default)]
struct TestResource(u32);
#[derive(Clone, HasSchema)]
#[schema(opaque, no_default)]
struct TestFromWorld(u32);
impl FromWorld for TestFromWorld {
fn from_world(world: &World) -> Self {
let b = world.resource::<TestResource>();
Self(b.0)
}
}
#[test]
fn init_resource_does_not_overwrite() {
let mut w = World::default();
w.insert_resource(TestResource(0));
w.init_resource::<TestFromWorld>();
w.insert_resource(TestResource(1));
let resource = w.resource::<TestFromWorld>();
assert_eq!(resource.0, 0);
}
}