moshimoshi

Crates.iomoshimoshi
lib.rsmoshimoshi
version0.2.0
sourcesrc
created_at2023-09-09 15:41:38.070529
updated_at2023-09-16 14:03:28.20601
descriptionA small crate to sugar working with command callbacks in bevy.
homepage
repositoryhttps://github.com/iiYese/moshimoshi
max_upload_size
id968218
size16,170
(iiYese)

documentation

README

moshimoshi

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

Crates.io Docs.rs

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()
}

Version table

Bevy version moshimoshi verison
0.11 0.1 - 0.2
Commit count: 5

cargo fmt