Giter VIP home page Giter VIP logo

aery's Introduction

Aery

A plugin that adds a subset of Entity Relationship features to Bevy.

Crates.io Docs.rs

Currently supported:

  • ZST edge types only (simply means edges can't hold data)
  • Fragmenting on edge types
  • Cleanup policies
  • Declarative APIs for:
    • Joining
    • Traversing
    • Spawning

API tour:

Non exhaustive. Covers most common parts. It's modeling RPG mechanics resembling tears of the kingdom (please nintendo leave me alone I beg).

Boilerplate
use bevy::prelude::*;
use aery::prelude::*;

#[derive(Clone, Copy, Component)]
struct Pos(Vec3);

#[derive(Component)]
struct Character;

#[derive(Component)]
struct Weapon {
    uses: u32,
    strength: u32,
}

#[derive(Component)]
struct Stick;

#[derive(Clone, Copy)]
enum Climate {
    Freezing,
    Cold,
    Neutral,
    Hot,
    Blazing,
}

#[derive(Resource)]
struct ClimateMap {
    // ..
}

impl ClimateMap {
    fn climate_at(&self, pos: Pos) -> Climate {
        todo!()
    }
}

#[derive(Component)]
enum Food {
    Raw { freshness: f32 },
    Cooked,
    Spoiled,
}

impl Food {
    fn tick(&mut self, climate: Climate) {
        let Food::Raw { freshness } = self else { return };

        if *freshness < 0. {
            *self = Food::Spoiled;
            return
        }

        match climate {
            Climate::Neutral => *freshness -= 1.,       // spoils over time
            Climate::Cold => *freshness -= 0.1,         // spoils slowly
            Climate::Freezing => *freshness -= 0.01,    // spoils very slowly
            Climate::Hot => *freshness -= 5.,           // spoils quickly
            Climate::Blazing => *self = Food::Cooked,   // Cooks food (should add a timer)
        }
    }
}

#[derive(Component)]
struct Apple;
Modeling a player inventory (Making relations)
#[derive(Relation)]
struct Inventory;

fn setup(mut cmds: Commands) {
    // Spawn character with some starting items.
    cmds.spawn((Character, Pos(Vec3::default())))
        .scope::<Inventory>(|invt| {
            // Give them a starting weapon & 3 food items
            invt.add((Weapon { uses: 32, strength: 4 }, Stick))
                .add((Food::Raw { freshness: 128. }, Apple))
                .add((Food::Raw { freshness: 128. }, Apple))
                .add((Food::Raw { freshness: 128. }, Apple));
        });

    // Alternatively construct relatiosn manually.
    // This might be more appropriate for changing an inventory or making more complex graphs.
    let char = cmds.spawn((Character, Pos(Vec3::default()))).id();
    cmds.spawn((Weapon { uses: 32, strength: 4, }, Stick)).set::<Inventory>(char);
    cmds.spawn((Food::Raw { freshness: 128. }, Apple)).set::<Inventory>(char);
    cmds.spawn((Food::Raw { freshness: 128. }, Apple)).set::<Inventory>(char);
    cmds.spawn((Food::Raw { freshness: 128. }, Apple)).set::<Inventory>(char);
}
Making items respond to enviornment (Join operations)
fn tick_food(
    mut characters: Query<((&Character, &Pos), Relations<Inventory>)>,
    mut inventory_food: Query<&mut Food, Without<Pos>>,
    mut food: Query<(&mut Food, &Pos)>,
    climate_map: Res<ClimateMap>,
) {
    // Tick foods that are just in the world somewhere
    for (mut food, pos) in food.iter_mut() {
        food.tick(climate_map.climate_at(*pos));
    }

    // Tick foods that are in a character's inventory based on the character's position
    for ((_, pos), edges) in characters.iter() {
        let climate = climate_map.climate_at(*pos);
        edges.join::<Inventory>(&mut inventory_food).for_each(|mut food| {
            food.tick(climate);
        });
    }
}
Dropping inventory items into the world (Responding to relation changes)
fn drop_item_from_inventory(
    trigger: Trigger<UnsetEvent<Inventory>>,
    mut commands: Commands,
    characters: Query<&Pos, With<Character>>,
    food: Query<Entity, With<Food>>,
) {
    // Set an items position to the position of the character that last had the item
    // in their inventory when they drop it.
    let Ok(pos) = characters.get(trigger.event().target) else { return };
    commands.entity(trigger.entity()).insert(*pos);
}
Powering connected devices (Traversing relations & relation properties)
// This relation has a custom property. Properties can be overriden by supplying arguments to
// the attribute macro. See the `Relation` trait & `CleanupPolicy` enum for more details.
// - Symmetric: Makes relations symmetric. Setting A -R-> B also sets B -R-> A.
// - Poly: Allows holding multiple relations of that type to different entities.
//
// There are also cleanup properties. Only one of these can be supplied to the attribute macro.
// - Counted: Edge counted cleanup (eg. despawn a parent if all its children are despawned)
// - Recursive: Recursively cleans up (eg. despawn all children of a parent with the parent)
// - Total: Does both counted & recursive cleanup
#[derive(Relation)]
#[aery(Symmetric, Poly)]
struct FuseJoint;

#[derive(Component)]
struct Fan {
    orientation: Quat
}

#[derive(Component)]
struct Powered;

fn tick_devices(
    mut devices: Query<((Entity, &mut Pos), Relations<FuseJoint>)>,
    mut fans: Query<(Entity, &Fan, &mut Pos), With<Powered>>,
) {
    for (entity, fan, pos) in fans.iter_mut() {
        // Move the fan based on its orientation
        pos = todo!();

        // Track visited nodes because this is a symmetric relationship
        let mut updated = vec![entity];

        devices.traverse_mut::<FuseJoint>([entity]).for_each(|(entity, ref mut pos), _| {
            if updated.contains(&entity) {
                TCF::Close
            } else {
                // Move connected device based on fan direction
                pos = todo!();
                updated.push(*entity);
                TCF::Continue
            }
        });
    }
}
Reflecting relations
App::new()
    // We just need to register the types for relfection.
    .register_relation::<Inventory>()
    .register_relation::<FuseJoint>()
    // ..
    .run();

Version table

Bevy version Aery verison
0.14 0.7
0.13 0.6
0.12 0.5
0.11 0.3 - 0.4
0.10 0.1 - 0.2

Credits

  • Sander Mertens: Responsible for pioneering Entity Relationships in ECS and the author of Flecs which Aery has taken a lot of inspiration from.

aery's People

Contributors

iiyese avatar itsdoot avatar mrchantey avatar nakedible avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

aery's Issues

Multi-parent hierarchies?

Is it possible to model multi-parent structures using aery?
Let's say I want to model human families, where two siblings can have the same father but different mothers?

Implement RelationsItem methods for Option<&RelationsItem> as well

Looking for these methods implemented on Option<&RelationsItem<'_, RS>> as well:

aery/src/operations.rs

Lines 68 to 96 in 271fd49

impl<RS: RelationSet> RelationsItem<'_, RS> {
pub fn iter_hosts<R: Relation>(&self) -> EdgeIter<'_> {
let _ = R::ZST_OR_PANIC;
self.edges.edges.iter_hosts::<R>()
}
pub fn iter_targets<R: Relation>(&self) -> EdgeIter<'_> {
let _ = R::ZST_OR_PANIC;
self.edges.edges.iter_targets::<R>()
}
}
impl<R: RelationSet> CheckRelations for RelationsItem<'_, R> {
fn has_host(
&self,
relation: impl Into<crate::Var<crate::relation::RelationId>>,
host: impl Into<crate::Var<Entity>>,
) -> bool {
self.edges.edges.has_host(relation, host)
}
fn has_target(
&self,
relation: impl Into<crate::Var<crate::relation::RelationId>>,
target: impl Into<crate::Var<Entity>>,
) -> bool {
self.edges.edges.has_target(relation, target)
}
}

I've run into this need a decent bit in my current project, and can't seem to implement iter_hosts/iter_targets as an extension myself since EdgeIter is currently private.

Allow derive(Relation) to work with generic ZST relations

The derive macro currently doesn't support generic ZSTs, leaving me to do this instead:

pub struct Multiply<C: Composite>(PhantomData<fn(C)>);

impl<C: Composite> Relation for Multiply<C> {
   const CLEANUP_POLICY: CleanupPolicy = CleanupPolicy::Orphan;
   const EXCLUSIVE: bool = true;
   const SYMMETRIC: bool = false;
}

other library relations impl

It's cool to see people working on library relations, have you seen https://github.com/BoxyUwU/librelations its out of date but relies on a fork of bevy_ecs to allow for despawning entities to also despawn the targets of their relations. I don't do anything with bevy anymore these days, best of luck with your relations impl and hope my previous impl might be interesting to you

Inverse WorldQuery filter of Participates

Rather than filtering by entities who are part of a relation, I want to filter by entities who are not involved in the relation.

Effectively, this:

#[derive(WorldQuery)]
pub struct Abstains<R: Relation> {
    // is not a host nor a target of relation R
    filter: (Without<Participant<R>>, Without<RootMarker<R>>),
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.