Skip to content

Commit

Permalink
Merge pull request #5 from iiYese/dev
Browse files Browse the repository at this point in the history
0.3
  • Loading branch information
iiYese committed Jul 14, 2023
2 parents 0c2ee91 + 5865bc0 commit c0266f2
Show file tree
Hide file tree
Showing 11 changed files with 2,574 additions and 1,515 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aery"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["iiYese iiyese@outlook.com"]
repository = "https://github.com/iiYese/aery"
Expand All @@ -12,6 +12,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.10"
bevy = "0.11"
indexmap = "1.9.3"
aery_macros = { path = "macros", version = "0.1.0-dev" }
aery_macros = { path = "macros", version = "0.2.0-dev" }
aquamarine = "0.3.2"
88 changes: 54 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
## Aery
Non-fragmenting (slight misnomer) ZST relations for Bevy.
A plugin that adds a subset of Entity Relationship features to Bevy using Non-fragmenting
ZST relations.

[![Crates.io](https://img.shields.io/crates/v/aery)](https://crates.io/crates/aery)
[![Docs.rs](https://img.shields.io/docsrs/aery)](https://docs.rs/aery/latest/aery/)

### Currently supported:
- ZST relations
- Fragmenting on (relation) type
- Cleanup policies
- Declarative APIs for:
- Joining
- Traversing
- Spawning

### API tour:
Non exhaustive. Covers most common parts.

```rust
use bevy::prelude::*;
use aery::prelude::*;

fn main() {
App::new()
.add_plugin(Aery)
.add_startup_system(setup)
.add_system(sys)
.add_plugins(Aery)
.add_systems(Startup, setup)
.add_systems(Update, sys)
.run();
}

Expand All @@ -23,53 +36,60 @@ struct Foo;
struct Bar;

#[derive(Relation)]
struct R0;
#[cleanup(policy = "Recursive")]
struct ChildOf;

#[derive(Relation)]
#[cleanup(policy = "Recursive")]
struct R1;
#[multi]
struct Bag;

// Spawning entities with relations
fn setup(mut commands: Commands) {
let (root, foo0, foo1, bar0, bar1) = (
commands.spawn(Foo).id(),
commands.spawn(Foo).id(),
commands.spawn(Foo).id(),
commands.spawn(Bar).id(),
commands.spawn(Bar).id(),
);
// A hierarchy of Foos with (chocolate? OwO) Bars in their Bags
commands.add(|wrld: &mut World| {
wrld.spawn(Foo)
.scope::<ChildOf>(|_, mut child| {
child.insert(Foo);
child.scope_target::<Bag>(|_, mut bag| { bag.insert(Bar); });
})
.scope::<ChildOf>(|_, mut child| {
child.insert(Foo);
child.scope_target::<Bag>(|_, mut bag| { bag.insert(Bar); });
});
})
}

commands.set::<R0>(foo0, bar0);
commands.set::<R0>(foo1, bar1);
commands.set::<R1>(foo0, root);
commands.set::<R1>(foo1, root);
// Listening for relation events
fn alert(mut events: EventReader<TargetEvent>) {
for event in events.iter() {
if event.matches(Wc, TargetOp::Set, ChildOf, Wc) {
println!("{:?} was added as a child of {:?}", event.host, event.target);
}
}
}

// Relation Queries
fn sys(
foos: Query<(&Foo, Relations<(R0, R1)>)>,
foos: Query<(&Foo, Relations<(Bag, ChildOf)>)>,
roots: Query<Entity, Root<ChildOf>>,
bars: Query<&Bar>,
r1_roots: Query<Entity, Root<R1>>
) {
foos.ops()
.join::<R0>(&bars)
.breadth_first::<R1>(r1_roots.iter())
.for_each(|foo_ancestor, foo, bar| {
.join::<Bag>(&bars)
.traverse::<ChildOf>(roots.iter())
.for_each(|foo_parent, foo, bar| {
// ..
})
}
```

### What is supported:
- ZST relations
- Fragmenting on (relation) type
- Declarative joining & traversing
- Explicit despawn cleanup

### What is not supported:
- Fragmenting on target
- Target querying
- Implicit despawn cleanup

### Version table
| Bevy version | Aery verison |
|--------------|--------------|
| 0.11 | 0.3 |
| 0.10 | 0.1 - 0.2 |

### Credits
- [Sander Mertens](https://github.com/SanderMertens):
Responsible for pioneering Entity Relationships in ECS and the author of Flecs which Aery has taken
a lot of inspiration from.
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "aery_macros"
description = "Proc macros for Aery."
version = "0.1.0-dev"
version = "0.2.0-dev"
edition = "2021"
license = "MIT OR Apache-2.0"

Expand Down
9 changes: 8 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Ident, LitStr, Result};

#[proc_macro_derive(Relation, attributes(multi, cleanup))]
#[proc_macro_derive(Relation, attributes(multi, symmetric, cleanup))]
pub fn relation_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);

let ident = &ast.ident;
let exclusive = !ast.attrs.iter().any(|attr| attr.path().is_ident("multi"));
let symmetric = ast
.attrs
.iter()
.any(|attr| attr.path().is_ident("symmetric"));

let cleanup = match parse_cleanup_attr(&ast) {
Ok(cleanup) => cleanup,
Expand All @@ -19,7 +23,10 @@ pub fn relation_derive(input: TokenStream) -> TokenStream {
impl Relation for #ident {
const CLEANUP_POLICY: CleanupPolicy = CleanupPolicy::#cleanup;
const EXCLUSIVE: bool = #exclusive;
const SYMMETRIC: bool = #symmetric;
}

const _: () = <#ident as aery::relation::ZstOrPanic>::ZST_OR_PANIC;
};

output.into()
Expand Down
Loading

0 comments on commit c0266f2

Please sign in to comment.