bevy_action/
lib.rs

1use core::marker::PhantomData;
2
3use bevy_app::{App, Plugin};
4use bevy_ecs::prelude::Entity;
5use bevy_reflect::TypeUuid;
6
7/// Implemented for any custom action type.
8///
9/// NOTE: Currently requires implementing [`TypeUuid`]. This may be removed in the future.
10pub trait Action:
11    Sized
12    + Clone
13    + std::fmt::Debug
14    + PartialEq
15    + Eq
16    + std::hash::Hash
17    + Send
18    + Sync
19    + 'static
20    + Default
21    + TypeUuid
22{
23}
24
25/// An ordinary Bevy event that contains the action and the entity that fired it.
26#[derive(Clone, Debug)]
27pub struct ActionEvent<T: Action> {
28    pub action: T,
29    pub entity: Entity,
30}
31
32/// NOTE: Must be registered for each custom action type.
33#[derive(Default)]
34pub struct ActionPlugin<T: Action>(PhantomData<T>);
35
36impl<T: Action> Plugin for ActionPlugin<T> {
37    fn build(&self, app: &mut App) {
38        app.add_event::<ActionEvent<T>>();
39    }
40}