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 byget_entities_with_component(). - Sparse (
sparse_components): Infrequently-accessed marker or tag components. Not included in the default entity query; useget_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
impl SimpleWorld
pub fn new(env: &Env) -> Self
Sourcepub fn next_entity_id(&self) -> EntityId
pub fn next_entity_id(&self) -> EntityId
Returns the next entity ID that will be assigned on spawn.
pub fn spawn_entity(&mut self) -> EntityId
Sourcepub fn add_component(
&mut self,
entity_id: EntityId,
component_type: Symbol,
data: Bytes,
)
pub fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, )
Add a component using the default Table storage.
Sourcepub fn add_component_with_storage(
&mut self,
entity_id: EntityId,
component_type: Symbol,
data: Bytes,
storage: ComponentStorage,
)
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.
Sourcepub fn get_component(
&self,
entity_id: EntityId,
component_type: &Symbol,
) -> Option<Bytes>
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.
Sourcepub fn remove_component(
&mut self,
entity_id: EntityId,
component_type: &Symbol,
) -> bool
pub fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, ) -> bool
Remove a component from both Table and Sparse maps transparently.
Sourcepub fn has_component(
&self,
entity_id: EntityId,
component_type: &Symbol,
) -> bool
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.
pub fn get_entities_with_component( &self, component_type: &Symbol, env: &Env, ) -> Vec<EntityId>
Sourcepub fn get_table_entities_with_component(
&self,
component_type: &Symbol,
env: &Env,
) -> Vec<EntityId>
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.
Sourcepub fn get_all_entities_with_component(
&self,
component_type: &Symbol,
env: &Env,
) -> Vec<EntityId>
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.
Sourcepub fn table_component_count(&self, component_type: &Symbol) -> usize
pub fn table_component_count(&self, component_type: &Symbol) -> usize
Returns the number of entities indexed for a component in table storage only.
Sourcepub fn component_count(&self, component_type: &Symbol) -> usize
pub fn component_count(&self, component_type: &Symbol) -> usize
Returns the number of entities indexed for a component across both storage classes.
Sourcepub fn get_typed<T: ComponentTrait>(
&self,
env: &Env,
entity_id: EntityId,
) -> Option<T>
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);Sourcepub fn set_typed<T: ComponentTrait>(
&mut self,
env: &Env,
entity_id: EntityId,
component: &T,
)
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));Sourcepub fn has_typed<T: ComponentTrait>(&self, entity_id: EntityId) -> bool
pub fn has_typed<T: ComponentTrait>(&self, entity_id: EntityId) -> bool
Check if an entity has a component of the given type.
Sourcepub fn remove_typed<T: ComponentTrait>(&mut self, entity_id: EntityId) -> bool
pub fn remove_typed<T: ComponentTrait>(&mut self, entity_id: EntityId) -> bool
Remove a component of the given type from an entity.
pub fn despawn_entity(&mut self, entity_id: EntityId)
Trait Implementations§
Source§impl Clone for SimpleWorld
impl Clone for SimpleWorld
Source§fn clone(&self) -> SimpleWorld
fn clone(&self) -> SimpleWorld
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SimpleWorld
impl Debug for SimpleWorld
Source§impl RuntimeWorld for SimpleWorld
impl RuntimeWorld for SimpleWorld
fn backend(&self) -> WorldBackend
fn entity_count(&self) -> usize
fn version(&self) -> u64
fn has_component(&self, entity_id: EntityId, component_type: &Symbol) -> bool
fn entities_with_component( &self, component_type: &Symbol, storage: QueryStorage, env: &Env, ) -> Vec<EntityId>
Source§impl RuntimeWorldMut for SimpleWorld
impl RuntimeWorldMut for SimpleWorld
fn spawn_entity(&mut self) -> EntityId
fn despawn_entity(&mut self, entity_id: EntityId, _env: &Env)
fn get_component( &self, entity_id: EntityId, component_type: &Symbol, ) -> Option<Bytes>
fn add_component( &mut self, entity_id: EntityId, component_type: Symbol, data: Bytes, _env: &Env, )
fn remove_component( &mut self, entity_id: EntityId, component_type: &Symbol, _env: &Env, ) -> bool
fn get_typed<T: ComponentTrait>( &self, env: &Env, entity_id: EntityId, ) -> Option<T>
fn set_typed<T: ComponentTrait>( &mut self, env: &Env, entity_id: EntityId, component: &T, )
fn has_typed<T: ComponentTrait>(&self, entity_id: EntityId) -> bool
fn remove_typed<T: ComponentTrait>( &mut self, env: &Env, entity_id: EntityId, ) -> bool
Source§impl TryFromVal<Env, &SimpleWorld> for Val
impl TryFromVal<Env, &SimpleWorld> for Val
type Error = ConversionError
fn try_from_val(env: &Env, val: &&SimpleWorld) -> Result<Self, ConversionError>
Source§impl TryFromVal<Env, SimpleWorld> for Val
impl TryFromVal<Env, SimpleWorld> for Val
type Error = ConversionError
fn try_from_val(env: &Env, val: &SimpleWorld) -> Result<Self, ConversionError>
Source§impl TryFromVal<Env, Val> for SimpleWorld
impl TryFromVal<Env, Val> for SimpleWorld
type Error = ConversionError
fn try_from_val(env: &Env, val: &Val) -> Result<Self, ConversionError>
Auto Trait Implementations§
impl Freeze for SimpleWorld
impl !RefUnwindSafe for SimpleWorld
impl !Send for SimpleWorld
impl !Sync for SimpleWorld
impl Unpin for SimpleWorld
impl UnsafeUnpin for SimpleWorld
impl !UnwindSafe for SimpleWorld
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
impl<E, T, U> FromVal<E, T> for Uwhere
E: Env,
U: TryFromVal<E, T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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