Crate moshimoshi

Crate moshimoshi 

Source
Expand description

§moshimoshi

A small crate to sugar working with command callbacks in bevy.

use bevy::prelude::*;
use moshimoshi::*;

#[derive(Component)]
struct Button;

#[derive(Component, Deref, DerefMut)]
struct OnClick(EntityCallback);

#[derive(Component, Deref, DerefMut)]
struct Counter(u32);

#[derive(Component)]
struct Text(String);

fn setup(mut commands: Commands) {
    commands.spawn((
        Button,
        Counter(0),
        Text("Click Me".to_string()),
        OnClick(moshi!([e: Entity], counter: Query<&mut Counter> => {
            **counter.get_mut(e).unwrap() += 1;
        }))
    ));
}

impl Button {
    fn update(mut commands: Commands, buttons: Query<(Entity, &OnClick), Changed<Button>>) {
        for (entity, callback) in buttons.iter() {
            commands.add(RunEntityCallback { entity, func: **callback });
        }
    }
}

fn main() {
    App::new()
        .add_systems(Update, (Button::update, apply_deferred).chain())
        .run()
}

Macros§

moshi

Structs§

Entity
Lightweight identifier of an entity.
RunEntityCallback
SystemState
Holds on to persistent state required to drive SystemParam for a System.
World
Stores and exposes operations on entities, components, resources, and their associated metadata.

Traits§

Command
A World mutation.

Type Aliases§

EntityCallback