https://github.com/rj/bevy_mutually_exclusive_components
https://github.com/rj/bevy_mutually_exclusive_components
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rj/bevy_mutually_exclusive_components
- Owner: RJ
- Created: 2025-01-11T21:48:27.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-01-12T10:26:12.000Z (4 months ago)
- Last Synced: 2025-03-26T09:47:08.666Z (about 2 months ago)
- Language: Rust
- Size: 12.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bevy Mutually Exclusive Components
Mutually exclusive groups of components for bevy.
## Usage
There is no plugin, just bring the `RegisterMutuallyExclusiveComponent` trait into scope:
```rust
use bevy_mutually_exclusive_components::prelude::*;
```Components in a group (defined by a `u32`) are mutually exclusive – if you set up a group with
components `A`, `B`, and `C`, then insert `A` into an entity, then insert `B`, `A` will be removed.```rust
#[derive(Component)]
struct A;
#[derive(Component)]
struct B;
#[derive(Component)]
struct C;// ...
// Make components A,B,C mutually exclusive
const MY_GROUP: u32 = 1;
app.register_mutually_exclusive_component::();
app.register_mutually_exclusive_component::();
app.register_mutually_exclusive_component::();// Make components X,Y mutually exclusive
const MY_OTHER_GROUP: u32 = 2;
app.register_mutually_exclusive_component::();
app.register_mutually_exclusive_component::();```
## How it works
A component hook is created for each component you register. It stores the `ComponentId` of itself
in a private `LastMutuallyExclusiveId` component on insertion.Before doing so, it checks if there is already a value in the `LastMutuallyExclusiveId` component,
and if so, removes the component with that `ComponentId`.See the test at the bottom of `lib.rs` for an example.
The `LastMutuallyExclusiveId` component takes a const generic u32, which is how groups are identified.
## Caution
Components can only have one hook, so it'll panic if you try to add a component to multiple groups.
It will also panic if a component already has a hook for another reason.
## License
Same as bevy