Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into remove-removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 29, 2023
2 parents 77b072a + 7f72f51 commit 794d7b1
Show file tree
Hide file tree
Showing 31 changed files with 588 additions and 213 deletions.
51 changes: 28 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@ members = ["flax-derive", "asteroids"]
[dependencies]
flax-derive = { path = "./flax-derive", version = "0.5.0", optional = true }

flume = { version = "0.10.14", default_features = false, optional = true }
flume = { version = "0.11.0", default_features = false, optional = true }


atomic_refcell = { version = "0.1.10", default_features = false }
bitflags = { version = "2.3.1", default_features = false }
anyhow = { version = "1.0.71", default_features = false }
itertools = "0.10.5"
rayon = { version = "1.7.0", default_features = false, optional = true }
tokio = { version = "1.28.1", default_features = false, features = [
atomic_refcell = { version = "0.1.13", default_features = false }
bitflags = { version = "2.4.1", default_features = false }
anyhow = { version = "1.0.75", default_features = false }
itertools = "0.11.0"
rayon = { version = "1.8.0", default_features = false, optional = true }
tokio = { version = "1.33.0", default_features = false, features = [
"sync",
], optional = true }
smallvec = { version = "1.10.0", default_features = false }
tracing = { version = "0.1.37", optional = true }
tynm = "0.1.7"
serde = { version = "1.0.163", features = ["derive"], optional = true }
erased-serde = { version = "0.3.25", features = [], optional = true }
once_cell = "1.17.1"
smallvec = { version = "1.11.1", default_features = false }
tracing = { version = "0.1.40", optional = true }
tynm = "0.1.9"
serde = { version = "1.0.190", features = ["derive"], optional = true }
erased-serde = { version = "0.3.31", features = [], optional = true }
once_cell = "1.18.0"

[dev-dependencies]
tokio = { version = "1.28.1", features = ["test-util", "macros"] }
futures = "0.3.28"
itertools = { version = "0.10.5", default_features = false }
tokio = { version = "1.33.0", features = ["test-util", "macros"] }
futures = "0.3.29"
itertools = { version = "0.11.0", default_features = false }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
serde_json = "1.0.96"
glam = { version = "0.24.0", features = ["rand", "serde", "scalar-math"] }
serde_json = "1.0.107"
glam = { version = "0.24.2", features = ["rand", "serde", "scalar-math"] }
rand = "0.8.5"
tracing-tree = "0.2.3"
tracing-tree = "0.2.5"
bincode = "1.3.3"
ron = "0.8.0"
tracing = "0.1.37"
criterion = "0.4.0"
pretty_assertions = "1.3.0"
ron = "0.8.1"
tracing = "0.1.40"
criterion = "0.5.1"
pretty_assertions = "1.4.0"

[profile.bench]
debug = true
Expand Down Expand Up @@ -121,6 +121,11 @@ name = "systems"
path = "./examples/guide/systems.rs"
required-features = ["std", "rayon"]

[[example]]
name = "springs"
path = "./examples/guide/springs.rs"
required-features = ["std"]

[[bench]]
name = "benchmarks"
harness = false
Expand Down
26 changes: 0 additions & 26 deletions Makefile.toml

This file was deleted.

10 changes: 5 additions & 5 deletions asteroids/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ publish = false

[dependencies]
flax = { path = "../", default-features = false, features = [ "flume", "derive", ] }
macroquad = "0.3.25"
macroquad = "0.4.4"
# sapp-wasm = "=0.1.26"

flume = { version = "0.10.14", default_features = false }
flume = { version = "0.11.0", default_features = false }
rand = { version = "0.8.5", default_features = false, features = ["std_rng"] }
tracing = "0.1.37"
tracing = "0.1.40"
tracing-subscriber = "0.3.17"
tracing-tree = "0.2.3"
itertools = "0.10.5"
tracing-tree = "0.2.5"
itertools = "0.11.0"
anyhow = "1.0.71"
79 changes: 52 additions & 27 deletions examples/guide/relations.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use flax::{
components::{child_of, name},
relation::RelationExt,
*,
};
use flax::{components::name, relation::RelationExt, *};
use itertools::Itertools;
use tracing_subscriber::{prelude::*, registry};
use tracing_tree::HierarchicalLayer;

fn main() -> anyhow::Result<()> {
registry().with(HierarchicalLayer::default()).init();
basic()?;
exclusive()?;
Ok(())
}

fn basic() -> anyhow::Result<()> {
let mut world = World::new();

// ANCHOR: relation_basic
component! {
spring_joint(other): f32 => [Debuggable],
child_of(id): (),
}

let mut world = World::new();

let parent = Entity::builder()
.set(name(), "Parent".into())
.spawn(&mut world);
Expand All @@ -30,7 +31,6 @@ fn main() -> anyhow::Result<()> {
.set(name(), "Child2".into())
.set_default(child_of(parent))
.spawn(&mut world);

// ANCHOR_END: relation_basic

// ANCHOR: many_to_many
Expand All @@ -42,49 +42,41 @@ fn main() -> anyhow::Result<()> {

tracing::info!("World: {world:#?}");

// Connect child1 with two entities via springs of different strength
world.set(child1, spring_joint(child2), 1.5)?;
world.set(child1, spring_joint(parent2), 7.4)?;
// Give child1 yet one more parent
world.set(child1, child_of(parent2), ())?;

tracing::info!(
"Connections from child1({child1}): {:?}",
Query::new(relations_like(spring_joint))
Query::new(relations_like(child_of))
.borrow(&world)
.get(child1)?
.collect_vec()
);

// ANCHOR_END: many_to_many
// ANCHOR: query

let children_of_parent = Query::new(entity_ids())
// Mathes a relation exactly
let children_of_parent: Vec<Entity> = Query::new(entity_ids())
.with(child_of(parent))
.borrow(&world)
.iter()
.collect_vec();
.collect_vec(&world);

tracing::info!("Children: {children_of_parent:?}");

let all_children = Query::new(entity_ids())
// Matches a relation with any parent
let all_children: Vec<Entity> = Query::new(entity_ids())
.filter(child_of.with_relation())
.borrow(&world)
.iter()
.collect_vec();
.collect_vec(&world);

tracing::info!("Children: {all_children:?}");

let roots = Query::new(entity_ids())
.filter(child_of.without_relation())
.borrow(&world)
.iter()
.collect_vec();
.collect_vec(&world);

tracing::info!("Roots: {roots:?}");

// ANCHOR_END: query

// ANCHOR: lifetime

tracing::info!(
"has relation to: {parent2}: {}",
world.has(child1, child_of(parent2))
Expand All @@ -105,3 +97,36 @@ fn main() -> anyhow::Result<()> {

Ok(())
}

fn exclusive() -> anyhow::Result<()> {
let mut world = World::new();

// ANCHOR: exclusive
component! {
child_of(parent): () => [ Exclusive ],
}

let id1 = Entity::builder().spawn(&mut world);
let id2 = Entity::builder().spawn(&mut world);

let id3 = Entity::builder()
.set_default(child_of(id1))
.spawn(&mut world);

let entity = world.entity_mut(id3).unwrap();

tracing::info!(
"relations of {id3}: {:?}",
entity.relations(child_of).map(|v| v.0).collect_vec()
);

world.set(id3, child_of(id2), ()).unwrap();

let entity = world.entity_mut(id3).unwrap();
tracing::info!(
"relations of {id3}: {:?}",
entity.relations(child_of).map(|v| v.0).collect_vec()
);
// ANCHOR_END: exclusive
Ok(())
}
68 changes: 68 additions & 0 deletions examples/guide/springs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use flax::{component, components::name, entity_ids, Dfs, Entity, FetchExt, Query, World};
use glam::{vec2, Vec2};
use tracing_subscriber::{prelude::*, registry};
use tracing_tree::HierarchicalLayer;

fn main() {
registry().with(HierarchicalLayer::default()).init();

let mut world = World::new();

// ANCHOR: main
struct Spring {
strength: f32,
length: f32,
}

impl Spring {
fn new(strength: f32, length: f32) -> Self {
Self { strength, length }
}
}
component! {
spring_joint(id): Spring,
position: Vec2,
}

let id1 = Entity::builder()
.set(name(), "a".into())
.set(position(), vec2(1.0, 4.0))
.spawn(&mut world);

// Connect id2 to id1 with a spring of strength 2.0
let id2 = Entity::builder()
.set(name(), "b".into())
.set(spring_joint(id1), Spring::new(2.0, 1.0))
.set(position(), vec2(2.0, 0.0))
.spawn(&mut world);

let _id3 = Entity::builder()
.set(name(), "c".into())
.set(spring_joint(id1), Spring::new(2.0, 3.0))
.set(position(), vec2(2.0, 3.0))
.spawn(&mut world);

let _id4 = Entity::builder()
.set(name(), "d".into())
.set(spring_joint(id2), Spring::new(5.0, 0.5))
.set(position(), vec2(1.0, 0.0))
.spawn(&mut world);

let mut query = Query::new((entity_ids(), name().cloned(), position()))
.with_strategy(Dfs::new(spring_joint));

query
.borrow(&world)
.traverse(&None, |(id, name, &pos), strength, parent| {
if let (Some(spring), Some((parent_name, parent_pos))) = (strength, parent) {
let distance = pos.distance(*parent_pos) - spring.length;
let force = distance * spring.strength;
tracing::info!("spring acting with {force:.1}N between {parent_name} and {name}");
} else {
tracing::info!(%id, name, "root");
}

Some((name, pos))
});
// ANCHOR_END: main
}
14 changes: 7 additions & 7 deletions flax-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ license = "MIT"
proc-macro = true

[dependencies]
proc-macro2 = "1.0.58"
proc-macro-crate = "1.3.1"
syn = "2.0.16"
quote = "1.0.27"
itertools = "0.10.5"
proc-macro2 = "1.0.69"
proc-macro-crate = "2.0.0"
syn = "2.0.38"
quote = "1.0.33"
itertools = "0.11.0"

[dev-dependencies]
glam = "0.24.0"
pretty_assertions = "1.3.0"
glam = "0.24.2"
pretty_assertions = "1.4.0"
2 changes: 1 addition & 1 deletion guide/src/diving_deeper/dynamic_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ entity as the generation is not needed.
{{ #include ../../../examples/guide/dynamic_components.rs:relation }}
```

When despawning either the relation component or object entity, the "parent",
When despawning either the relation component or target entity, the "parent",
the component is removed from all entities.
Loading

0 comments on commit 794d7b1

Please sign in to comment.