Crate mewo

Source
Expand description

§Mewo

§Driving the Car

//  For single threaded use cases.
//  let galaxy = Galaxy::new();
let galaxy = Arc::new(RwLock::new(Galaxy::new()));

{
    let mut galaxy = galaxy.write();
     
    //  Initialize Game.
    some_init_system(&galaxy);

    galaxy.update();
}

//  Although you can do this, you should prefer the built-in runner functions.
//
//  //  Game Loop
//  loop {
//      let galaxy = galaxy.write();
//      system_one(&galaxy);
//      system_two(&galaxy);
//      if galaxy.update().is_none() {
//          //  Game Over
//          return;
//      }
//  }

run_spawn(Arc::clone(&galaxy)).join().unwrap();

§Defining Components

Components are pieces of data that are attached to entities. There are two types of components: CheapComponent and UniqueComponent.

#[derive(Clone, Copy, CheapComponent)]
struct A;

#[derive(Clone, UniqueComponent)]
struct B;

CheapComponent is for components that are cheap to copy. UniqueComponent is for components that either don’t implement copy or shouldn’t be copied too often.

§Defining Resources

Resources are pieces of data that just exist and can be accessed with some value.

#[derive(SingleResource)]
struct PlayerEntity(Entity);

#[derive(Resource)]
struct Window(...);

Here, PlayerEntity is a single resource which can be accessed using PlayerEntity::single_resource(). There is only one PlayerEntity.

However, there can be multiple of Window. You can access each window later using literally any value.

§Systems

Systems are just functions that take a galaxy.

fn my_system(galaxy: &Galaxy) {
    todo!();
}

§Spawning an Entity

let player = galaxy
    .insert_entity()
    .insert(Player)
    .insert(SomeComponent)
    .insert(OtherComponent)
    .get_entity();

This creates an entity, however, this entity is not accessible until the next update.

§Queries

//  With Entity
for (entity, (player, health, other)) in g.query::<(&Player, &mut Health, Option<&Other>)>().eiter() {
    //  ...
}

//  Without Entity
for (player, health, other) in g.query::<(&Player, &mut Health, Option<&Other>)>().iter() {
    //  ...
}

§Getting a Specific Entity

let entity_getter = galaxy.get_entity(entity).unwrap();

entity_getter
    .insert(SomeComponent)
    .insert(OtherComponent)
    .remove(SomeOtherComponent);

//  I don't see when you would use this.
let entity = entity_getter.get_entity();

Once again, both inserted and removed components don’t show until the next update.

§Spawning a Resource

//  For single resources.
galaxy.insert_resource(PlayerEntity::single_resource(), PlayerEntity(player));

//  For (generic) resources.
//  This associates the new window with the string `"My Window"`.
galaxy.insert_resource("My Window", Window(window));

Resources that are created are instantly available unlike with components.

§Accessing a Resource

galaxy
    .get_resource::<PlayerEntity, _>(PlayerEntity::single_resource())
    .unwrap()

galaxy
    .get_resource::<Window, _>("My Window")
    .unwrap()

§Removing Stuff

galaxy.remove_entity(entity);

galaxy.remove_resource::<Window, _>("My Window");
galaxy.remove_resource::<PlayerEntity, _>(PlayerEntity::single_resource());

§Events

#[derive(Event)]
struct KeyEvent {
    pub key: Key,
}
 
galaxy.insert_event(KeyEvent { key });

for event in galaxy.get_events() {
    todo!()
}

Similar to components, inserted events don’t appear until the next update.

§Game Over

galaxy.set_exit();

Modules§

run

Macros§

ecs_err
merr
mfold
minfo
mwarn

Structs§

Arc
A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
Entity
EntityGetter
Galaxy
LogFold
LogRecord
LogSubscription
Logger
Preserve
PreserveInstance
ResourceReadGuard
ResourceWriteGuard

Enums§

LogEvent
LogTarget
ValueDuplicate

Traits§

CheapComponent
Component
Event
GenericComponent
Resource
UniqueComponent

Functions§

run_single
Block the current thread, driving the galaxy with systems.
run_spawn
Spawn a new thread and drive galaxy with systems. pre_update and post_update are called before updating and after updating respectively. pre_update and post_update don’t lock galaxy, so they don’t impose side effects on other threads. In other words, you can safely block or wait in these functions.
run_spawn_locked
Spawn a new thread and drive galaxy with systems. pre_update and post_update are called before updating and after updating respectively. pre_update and post_update don’t lock galaxy, so they don’t impose side effects on other threads. In other words, you can safely block or wait in these functions.

Type Aliases§

RwLock
A reader-writer lock

Derive Macros§

CheapComponent
Component
Event
Resource
SingleResource
UniqueComponent