Skip to main content

SimpleWorld

Struct SimpleWorld 

Source
pub struct SimpleWorld { /* private fields */ }
Expand description

Simplified game world optimized for Soroban on-chain storage.

Uses Map-based storage for O(log n) component lookups instead of linear scans. This is the recommended ECS container for Soroban contracts.

§Dual-Map storage

Components are split into two maps based on their ComponentStorage kind:

  • Table (components): Frequently-iterated components (e.g., Position, Velocity). Queried by get_entities_with_component().
  • Sparse (sparse_components): Infrequently-accessed marker or tag components. Not included in the default entity query; use get_all_entities_with_component() to include them.

Both maps are transparent to get_component(), has_component(), and remove_component().

§Example

use cougr_core::component::ComponentStorage;
use cougr_core::simple_world::SimpleWorld;
use soroban_sdk::{symbol_short, Bytes, Env};

let env = Env::default();
let mut world = SimpleWorld::new(&env);
let entity_id = world.spawn_entity();
world.add_component(entity_id, symbol_short!("position"), Bytes::new(&env));
world.add_component_with_storage(
    entity_id,
    symbol_short!("marker"),
    Bytes::new(&env),
    ComponentStorage::Sparse,
);
assert!(world.has_component(entity_id, &symbol_short!("position")));

Implementations§

Source§

impl SimpleWorld

Source

pub const fn spec_xdr() -> [u8; 1716]

Source§

impl SimpleWorld

Source

pub fn new(env: &Env) -> Self

Source

pub fn version(&self) -> u64

Returns the current world version for cache invalidation.

Source

pub fn next_entity_id(&self) -> EntityId

Returns the next entity ID that will be assigned on spawn.

Source

pub fn env(&self) -> &Env

Returns the environment backing this world storage.

Source

pub fn spawn_entity(&mut self) -> EntityId

Source

pub fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, )

Add a component using the default Table storage.

Source

pub fn add_component_with_storage( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, storage: ComponentStorage, )

Add a component, routing to the Table or Sparse map based on storage.

Source

pub fn get_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> Option<Bytes>

Get a component’s data, checking both Table and Sparse maps transparently.

Source

pub fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, ) -> bool

Remove a component from both Table and Sparse maps transparently.

Source

pub fn has_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> bool

Check if an entity has a component in either Table or Sparse storage.

Source

pub fn get_entities_with_component( &self, component_type: &Symbol, env: &Env, ) -> Vec<EntityId>

Source

pub fn get_table_entities_with_component( &self, component_type: &Symbol, env: &Env, ) -> Vec<EntityId>

Get entities that have the given component in Table storage only. This is the fast path for querying frequently-iterated components.

Source

pub fn get_all_entities_with_component( &self, component_type: &Symbol, env: &Env, ) -> Vec<EntityId>

Get entities that have the given component in either Table or Sparse storage.

Source

pub fn table_component_count(&self, component_type: &Symbol) -> usize

Returns the number of entities indexed for a component in table storage only.

Source

pub fn component_count(&self, component_type: &Symbol) -> usize

Returns the number of entities indexed for a component across both storage classes.

Source

pub fn get_typed<T: ComponentTrait>( &self, env: &Env, entity_id: EntityId, ) -> Option<T>

Get a component and deserialize it into the concrete type.

§Example
use cougr_core::component::Position;
use cougr_core::simple_world::SimpleWorld;
use soroban_sdk::Env;

let env = Env::default();
let mut world = SimpleWorld::new(&env);
let entity_id = world.spawn_entity();
world.set_typed(&env, entity_id, &Position::new(10, 20));
let pos: Option<Position> = world.get_typed::<Position>(&env, entity_id);
assert_eq!(pos.unwrap().x, 10);
Source

pub fn set_typed<T: ComponentTrait>( &mut self, env: &Env, entity_id: EntityId, component: &T, )

Serialize a component and store it, using the type’s default storage kind.

§Example
use cougr_core::component::Position;
use cougr_core::simple_world::SimpleWorld;
use soroban_sdk::Env;

let env = Env::default();
let mut world = SimpleWorld::new(&env);
let entity_id = world.spawn_entity();
world.set_typed(&env, entity_id, &Position::new(10, 20));
assert!(world.has_typed::<Position>(entity_id));
Source

pub fn has_typed<T: ComponentTrait>(&self, entity_id: EntityId) -> bool

Check if an entity has a component of the given type.

Source

pub fn remove_typed<T: ComponentTrait>(&mut self, entity_id: EntityId) -> bool

Remove a component of the given type from an entity.

Source

pub fn despawn_entity(&mut self, entity_id: EntityId)

Trait Implementations§

Source§

impl Clone for SimpleWorld

Source§

fn clone(&self) -> SimpleWorld

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SimpleWorld

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl RuntimeWorld for SimpleWorld

Source§

fn backend(&self) -> WorldBackend

Source§

fn entity_count(&self) -> usize

Source§

fn version(&self) -> u64

Source§

fn has_component(&self, entity_id: EntityId, component_type: &Symbol) -> bool

Source§

fn entities_with_component( &self, component_type: &Symbol, storage: QueryStorage, env: &Env, ) -> Vec<EntityId>

Source§

impl RuntimeWorldMut for SimpleWorld

Source§

fn spawn_entity(&mut self) -> EntityId

Source§

fn despawn_entity(&mut self, entity_id: EntityId, _env: &Env)

Source§

fn get_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> Option<Bytes>

Source§

fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, _env: &Env, )

Source§

fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, _env: &Env, ) -> bool

Source§

fn get_typed<T: ComponentTrait>( &self, env: &Env, entity_id: EntityId, ) -> Option<T>

Source§

fn set_typed<T: ComponentTrait>( &mut self, env: &Env, entity_id: EntityId, component: &T, )

Source§

fn has_typed<T: ComponentTrait>(&self, entity_id: EntityId) -> bool

Source§

fn remove_typed<T: ComponentTrait>( &mut self, env: &Env, entity_id: EntityId, ) -> bool

Source§

impl TryFromVal<Env, &SimpleWorld> for Val

Source§

impl TryFromVal<Env, SimpleWorld> for Val

Source§

impl TryFromVal<Env, Val> for SimpleWorld

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, C> Compare<&T> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare(&self, a: &&T, b: &&T) -> Result<Ordering, <C as Compare<&T>>::Error>

Source§

impl<T, U, E, C> Compare<(T, U)> for C
where C: Compare<T, Error = E, Error = E> + Compare<U>,

Source§

type Error = E

Source§

fn compare( &self, a: &(T, U), b: &(T, U), ) -> Result<Ordering, <C as Compare<(T, U)>>::Error>

Source§

impl<T, U, V, E, C> Compare<(T, U, V)> for C
where C: Compare<T, Error = E, Error = E, Error = E> + Compare<U> + Compare<V>,

Source§

impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W>,

Source§

impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
where C: Compare<T, Error = E, Error = E, Error = E, Error = E, Error = E> + Compare<U> + Compare<V> + Compare<W> + Compare<X>,

Source§

impl<T, C> Compare<Box<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Box<T>, b: &Box<T>, ) -> Result<Ordering, <C as Compare<Box<T>>>::Error>

Source§

impl<T, C> Compare<Option<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Option<T>, b: &Option<T>, ) -> Result<Ordering, <C as Compare<Option<T>>>::Error>

Source§

impl<T, C> Compare<Rc<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Rc<T>, b: &Rc<T>, ) -> Result<Ordering, <C as Compare<Rc<T>>>::Error>

Source§

impl<T, C> Compare<Vec<T>> for C
where C: Compare<T>,

Source§

type Error = <C as Compare<T>>::Error

Source§

fn compare( &self, a: &Vec<T>, b: &Vec<T>, ) -> Result<Ordering, <C as Compare<Vec<T>>>::Error>

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<E, T, U> FromVal<E, T> for U
where E: Env, U: TryFromVal<E, T>,

Source§

fn from_val(e: &E, v: &T) -> U

Source§

impl<T> FromXdr for T
where T: TryFromVal<Env, Val>,

Source§

type Error = <T as TryFromVal<Env, Val>>::Error

The error type returned if the Val cannot be converted into the target type.
Source§

fn from_xdr(env: &Env, b: &Bytes) -> Result<T, <T as FromXdr>::Error>

Deserializes the value from XDR Bytes. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<E, T, U> IntoVal<E, T> for U
where E: Env, T: FromVal<E, U>,

Source§

fn into_val(&self, e: &E) -> T

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToXdr for T
where T: IntoVal<Env, Val>,

Source§

fn to_xdr(self, env: &Env) -> Bytes

Serializes the value to XDR as Bytes.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E, T, U> TryIntoVal<E, T> for U
where E: Env, T: TryFromVal<E, U>,

Source§

type Error = <T as TryFromVal<E, U>>::Error

Source§

fn try_into_val(&self, env: &E) -> Result<T, <U as TryIntoVal<E, T>>::Error>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V