-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9928a2
commit dacb5e1
Showing
7 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use core::ops::Deref; | ||
|
||
use atomic_refcell::AtomicRef; | ||
|
||
use crate::{Component, ComponentValue, Entity, EntityIds, EntityRef, Mutable, RefMut}; | ||
|
||
use super::{copied::Copied, Cloned}; | ||
|
||
/// Generically access an entity in the world | ||
pub trait EntityAccess<'a> { | ||
/// The produced item | ||
type Item; | ||
/// Access the entity | ||
fn get(&'a self, entity: &'a EntityRef) -> Option<Self::Item>; | ||
} | ||
|
||
impl<'a, T: ComponentValue> EntityAccess<'a> for Component<T> { | ||
type Item = AtomicRef<'a, T>; | ||
|
||
fn get(&self, entity: &'a EntityRef) -> Option<Self::Item> { | ||
entity.get(*self).ok() | ||
} | ||
} | ||
|
||
impl<'a, T: ComponentValue> EntityAccess<'a> for Mutable<T> { | ||
type Item = RefMut<'a, T>; | ||
|
||
fn get(&self, entity: &'a EntityRef) -> Option<Self::Item> { | ||
entity.get_mut(self.0).ok() | ||
} | ||
} | ||
|
||
impl<'a> EntityAccess<'a> for EntityIds { | ||
type Item = Entity; | ||
|
||
fn get(&'a self, entity: &'a EntityRef) -> Option<Self::Item> { | ||
Some(entity.id()) | ||
} | ||
} | ||
|
||
impl<'a, T: EntityAccess<'a>, V> EntityAccess<'a> for Cloned<T> | ||
where | ||
T::Item: Deref<Target = V>, | ||
V: 'a + Clone, | ||
{ | ||
type Item = V; | ||
|
||
fn get(&'a self, entity: &'a EntityRef) -> Option<Self::Item> { | ||
Some(self.0.get(entity)?.clone()) | ||
} | ||
} | ||
|
||
impl<'a, T: EntityAccess<'a>, V> EntityAccess<'a> for Copied<T> | ||
where | ||
T::Item: Deref<Target = V>, | ||
V: 'a + Copy, | ||
{ | ||
type Item = V; | ||
|
||
fn get(&'a self, entity: &'a EntityRef) -> Option<Self::Item> { | ||
Some(*self.0.get(entity)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use core::mem; | ||
|
||
use crate::{ | ||
archetype::{Archetype, Slice}, | ||
entity::EntityLocation, | ||
fetch::{FetchPrepareData, PreparedFetch}, | ||
EntityRef, Fetch, World, | ||
}; | ||
|
||
pub struct QueryOne<'w, Q: Fetch<'w>> { | ||
prepared: Q::Prepared, | ||
item: <Q::Prepared as PreparedFetch<'static>>::Item, | ||
} | ||
|
||
impl<'w, Q: Fetch<'w>> QueryOne<'w, Q> { | ||
pub(crate) fn new( | ||
fetch: &'w Q, | ||
world: &'w World, | ||
arch: &'w Archetype, | ||
loc: EntityLocation, | ||
) -> Option<Self> { | ||
let mut prepared = fetch.prepare(FetchPrepareData { | ||
world, | ||
arch, | ||
arch_id: loc.arch_id, | ||
old_tick: 0, | ||
new_tick: world.advance_change_tick(), | ||
})?; | ||
|
||
let item = { | ||
let mut chunk = unsafe { prepared.create_chunk(Slice::single(loc.slot)) }; | ||
|
||
unsafe { <Q::Prepared as PreparedFetch<'_>>::fetch_next(&mut chunk) } | ||
}; | ||
|
||
let item = unsafe { | ||
mem::transmute::< | ||
<Q::Prepared as PreparedFetch<'_>>::Item, | ||
<Q::Prepared as PreparedFetch<'static>>::Item, | ||
>(item) | ||
}; | ||
|
||
Some(Self { prepared, item }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use flax::{component, name, Entity, World}; | ||
|
||
#[test] | ||
fn entity_access() { | ||
component! { | ||
a: i32, | ||
b: String, | ||
} | ||
|
||
let mut world = World::new(); | ||
|
||
let id = Entity::builder() | ||
.set(name(), "a".into()) | ||
.set(a(), 5) | ||
.set(b(), "Foo".into()) | ||
.spawn(&mut world); | ||
|
||
let entity = world.entity(id).unwrap(); | ||
} |