Create related_by query, remove tracing feature from examples

This commit is contained in:
SeanOMik 2024-12-21 14:31:10 -05:00
parent c3330c044b
commit 3298d17e61
Signed by: SeanOMik
GPG key ID: FEC9E2FC15235964
10 changed files with 154 additions and 234 deletions
Cargo.lock
crates/lyra-ecs/src
examples
2d
fixed-timestep-rotating-model
lua-scripting
many-lights
shadows
simple_scene

233
Cargo.lock generated
View file

@ -835,15 +835,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "deranged"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
]
[[package]]
name = "digest"
version = "0.10.7"
@ -1161,19 +1152,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "generator"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb949699c3e4df3a183b1d2142cb24277057055ed23c68ed58894f76c517223"
dependencies = [
"cfg-if",
"libc",
"log",
"rustversion",
"windows 0.58.0",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@ -1336,7 +1314,7 @@ dependencies = [
"presser",
"thiserror",
"winapi",
"windows 0.52.0",
"windows",
]
[[package]]
@ -1747,19 +1725,6 @@ dependencies = [
"value-bag",
]
[[package]]
name = "loom"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca"
dependencies = [
"cfg-if",
"generator",
"scoped-tls",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "loop9"
version = "0.1.5"
@ -1843,10 +1808,6 @@ dependencies = [
"syn 2.0.77",
"thiserror",
"tracing",
"tracing-appender",
"tracing-log",
"tracing-subscriber",
"tracing-tracy",
"unique",
"uuid",
"wgpu",
@ -2004,15 +1965,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata 0.1.10",
]
[[package]]
name = "maybe-rayon"
version = "0.1.1"
@ -2258,12 +2210,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-derive"
version = "0.4.2"
@ -2741,12 +2687,6 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.20"
@ -2988,17 +2928,8 @@ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata 0.4.7",
"regex-syntax 0.8.4",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax 0.6.29",
"regex-automata",
"regex-syntax",
]
[[package]]
@ -3009,15 +2940,9 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax 0.8.4",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.8.4"
@ -3082,12 +3007,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rustversion"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]]
name = "ryu"
version = "1.0.18"
@ -3440,37 +3359,6 @@ dependencies = [
"weezl",
]
[[package]]
name = "time"
version = "0.3.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885"
dependencies = [
"deranged",
"itoa",
"num-conv",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tiny-skia"
version = "0.11.4"
@ -3541,18 +3429,6 @@ dependencies = [
"tracing-core",
]
[[package]]
name = "tracing-appender"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf"
dependencies = [
"crossbeam-channel",
"thiserror",
"time",
"tracing-subscriber",
]
[[package]]
name = "tracing-attributes"
version = "0.1.27"
@ -3591,49 +3467,14 @@ version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]
[[package]]
name = "tracing-tracy"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6a90519f16f55e5c62ffd5976349f10744435a919ecff83d918300575dfb69b"
dependencies = [
"tracing-core",
"tracing-subscriber",
"tracy-client",
]
[[package]]
name = "tracy-client"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373db47331c3407b343538df77eea2516884a0b126cdfb4b135acfd400015dd7"
dependencies = [
"loom",
"once_cell",
"tracy-client-sys",
]
[[package]]
name = "tracy-client-sys"
version = "0.24.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49cf0064dcb31c99aa1244c1b93439359e53f72ed217eef5db50abd442241e9a"
dependencies = [
"cc",
]
[[package]]
name = "ttf-parser"
version = "0.24.1"
@ -4112,17 +3953,7 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be"
dependencies = [
"windows-core 0.52.0",
"windows-targets 0.52.6",
]
[[package]]
name = "windows"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6"
dependencies = [
"windows-core 0.58.0",
"windows-core",
"windows-targets 0.52.6",
]
@ -4135,60 +3966,6 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
dependencies = [
"windows-implement",
"windows-interface",
"windows-result",
"windows-strings",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-implement"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.77",
]
[[package]]
name = "windows-interface"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.77",
]
[[package]]
name = "windows-result"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-strings"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [
"windows-result",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-sys"
version = "0.45.0"

View file

@ -6,6 +6,7 @@ use super::{filter::{AsFilter, Filter, FilterFetch}, AsQuery, Fetch, Query};
pub type View<'a, Q, F = ()> = ViewState<'a, <Q as AsQuery>::Query, <F as AsQuery>::Query>;
#[derive(Clone)]
pub struct ViewState<'a, Q: Query, F: Filter> {
world: &'a World,
query: Q,
@ -172,6 +173,16 @@ impl<'a, Q: Query> ViewOneState<'a, Q> {
None
}
/// Consumes `self`, adding a query to the view.
pub fn expand<U: AsQuery>(self, query: U::Query) -> ViewOneState<'a, (Q, U::Query)> {
//ViewOneState::new(self.world, (self.query, query), self.filter, self.archetypes)
ViewOneState {
world: &self.world,
tick: self.tick,
query: (self.query, query),
}
}
}
impl<Q: Query> FnArgFetcher for ViewOneState<'_, Q> {

View file

@ -4,6 +4,7 @@ use lyra_ecs_derive::Component;
use crate::query::Filter;
use crate::query::Query;
use crate::query::ViewOneState;
use crate::query::ViewState;
use crate::Entity;
@ -16,6 +17,9 @@ pub use relates_to::*;
mod relate_pair;
pub use relate_pair::*;
mod related_by;
pub use related_by::*;
mod child_of;
pub use child_of::*;
@ -96,6 +100,7 @@ impl World {
self.insert(origin, comp);
}
}
impl<'a, Q, F> ViewState<'a, Q, F>
where
Q: Query,
@ -110,6 +115,15 @@ where
self.expand::<RelatesTo<R>>(rel)
}
pub fn related_by<R>(self, target: Entity) -> ViewState<'a, Q, (F, QueryRelatedBy<R>)>
where
R: Relation,
{
let rel = QueryRelatedBy::new(Some(target));
self.with::<QueryRelatedBy<R>>(rel)
}
/// Consumes `self` to return a view that fetches the origin, target, and a reference to
/// the relation that the entities have together.
pub fn relate_pair<R>(self) -> ViewState<'a, (Q, QueryRelatePair<R>), F>
@ -121,6 +135,30 @@ where
}
}
impl<'a, Q> ViewOneState<'a, Q>
where
Q: Query,
{
/// Consumes `self` to return a view that fetches the relation to a specific target entity.
pub fn relates_to<R>(self, target: Entity) -> ViewOneState<'a, (Q, QueryRelatesTo<R>)>
where
R: Relation,
{
let rel = QueryRelatesTo::new(Some(target));
self.expand::<RelatesTo<R>>(rel)
}
/// Consumes `self` to return a view that fetches the origin, target, and a reference to
/// the relation that the entities have together.
pub fn relate_pair<R>(self) -> ViewOneState<'a, (Q, QueryRelatePair<R>)>
where
R: Relation,
{
let rel = QueryRelatePair::<R>::new();
self.expand::<RelatePair<R>>(rel)
}
}
#[cfg(test)]
mod tests {
use crate::{query::{Entities, ViewState}, relation::QueryRelatesTo, tests::Vec2, World};

View file

@ -0,0 +1,94 @@
use std::{any::TypeId, cell::Ref, marker::PhantomData};
use crate::{query::{AsQuery, Fetch, Query}, ComponentColumn, Entity, World};
use super::{Relation, RelationTargetComponent};
pub struct FetchRelatedBy<'a, T> {
col: &'a ComponentColumn,
origin: Entity,
_phantom: PhantomData<&'a T>
}
impl<'a, R> Fetch<'a> for FetchRelatedBy<'a, R>
where
R: Relation,
{
type Item = bool;
fn dangling() -> Self {
unreachable!()
}
fn can_visit_item(&mut self, entity: crate::ArchetypeEntityId) -> bool {
unsafe {
let comp: Ref<RelationTargetComponent<R>> = self.col.get(entity.0 as usize);
comp.origin == self.origin
}
}
unsafe fn get_item(&mut self, _: crate::world::ArchetypeEntityId) -> Self::Item {
true
}
}
pub struct QueryRelatedBy<R> {
target: Option<Entity>,
_marker: PhantomData<R>,
}
impl<R> Copy for QueryRelatedBy<R> {}
impl<R> Clone for QueryRelatedBy<R> {
fn clone(&self) -> Self {
*self
}
}
impl<R> QueryRelatedBy<R> {
pub fn new(target: Option<Entity>) -> Self {
Self {
target,
_marker: PhantomData
}
}
}
impl<R> Query for QueryRelatedBy<R>
where
R: Relation + 'static
{
type Item<'a> = bool;
type Fetch<'a> = FetchRelatedBy<'a, R>;
fn new() -> Self {
panic!("RelatedBy MUST be made with View::related_by since it requires State provided by \
that function.")
}
fn can_visit_archetype(&self, archetype: &crate::archetype::Archetype) -> bool {
archetype.has_column(TypeId::of::<RelationTargetComponent<R>>())
}
unsafe fn fetch<'a>(&self, _world: &'a World, archetype: &'a crate::archetype::Archetype, tick: crate::Tick) -> Self::Fetch<'a> {
let _ = tick;
let col = archetype.get_column(TypeId::of::<RelationTargetComponent<R>>())
.expect("You ignored 'can_visit_archetype'!");
FetchRelatedBy {
col,
origin: self.target.expect("Filter not initialized"),
_phantom: PhantomData,
}
}
}
/// A filter that returns entities that are the target of a relation.
pub struct RelatedBy<R: Relation> {
_marker: PhantomData<R>,
}
impl<R: Relation> AsQuery for RelatedBy<R> {
type Query = QueryRelatedBy<R>;
}

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy"] }
lyra-engine = { path = "../../" }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy"] }
lyra-engine = { path = "../../" }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy", "lua_scripting"] }
lyra-engine = { path = "../../", features = ["lua_scripting"] }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy"] }
lyra-engine = { path = "../../" }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy"] }
lyra-engine = { path = "../../" }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"

View file

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lyra-engine = { path = "../../", features = ["tracy"] }
lyra-engine = { path = "../../" }
anyhow = "1.0.75"
async-std = "1.12.0"
tracing = "0.1.37"