Skip to content

Commit

Permalink
Improve relations
Browse files Browse the repository at this point in the history
  • Loading branch information
zakarumych committed Jul 16, 2024
1 parent 22d3d80 commit 1d7ef67
Show file tree
Hide file tree
Showing 25 changed files with 1,874 additions and 745 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = ["proc-lib", "proc"]

[workspace.package]
version = "1.0.0-rc5"
version = "1.0.0-rc6"
edition = "2021"
authors = ["Zakarum <zaq.dev@icloud.com>"]
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -43,7 +43,7 @@ default = ["std", "scheduler", "flow"]
rayon = ["dep:rayon", "std"]

[dependencies]
edict-proc = { version = "=1.0.0-rc5", path = "proc" }
edict-proc = { version = "=1.0.0-rc6", path = "proc" }
amity = { version = "0.2.1", default-features = false, features = ["alloc"] }
hashbrown = { version = "0.14" }
smallvec = { version = "1.10", features = ["union"], default-features = false }
Expand Down
10 changes: 5 additions & 5 deletions examples/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
let a = world.spawn((A,)).id();
let b = world.spawn(()).id();

world.add_relation(a, ChildOf, b).unwrap();
world.insert_relation(a, ChildOf, b).unwrap();

for (e, ChildOf) in world.view::<Entities>().relates_to::<ChildOf>(b).iter() {
println!("{} is child of {}", e, b);
Expand All @@ -58,8 +58,8 @@ fn main() {
let b = world.spawn(()).id();
let c = world.spawn(()).id();

world.add_relation(a, Likes, b).unwrap();
world.add_relation(a, Likes, c).unwrap();
world.insert_relation(a, Likes, b).unwrap();
world.insert_relation(a, Likes, c).unwrap();

let mut view = world.get::<Relates<&Likes>>(a).unwrap();
let first = view.next().unwrap();
Expand All @@ -75,7 +75,7 @@ fn main() {

let b = world.spawn(()).id();

world.add_relation(a, Enemy, b).unwrap();
world.insert_relation(a, Enemy, b).unwrap();

let q = world.view::<Entities>().relates::<Enemy>();
for (e, enemies) in q.iter() {
Expand Down Expand Up @@ -113,7 +113,7 @@ fn main() {
let a = world.spawn((A,)).id();
let b = world.spawn((B,)).id();

world.add_relation(a, LifeBound, b).unwrap();
world.insert_relation(a, LifeBound, b).unwrap();

world.despawn(a).unwrap();
assert_eq!(world.is_alive(b), false);
Expand Down
2 changes: 1 addition & 1 deletion proc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ proc-macro = true

[dependencies]
syn = "2.0"
edict-proc-lib = { version = "=1.0.0-rc5", path = "../proc-lib" }
edict-proc-lib = { version = "=1.0.0-rc6", path = "../proc-lib" }
4 changes: 2 additions & 2 deletions src/action/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ impl ActionSender {

/// Encodes an action to add relation between two entities to the [`World`].
#[inline(always)]
pub fn add_relation<R>(&self, origin: EntityId, relation: R, target: EntityId)
pub fn insert_relation<R>(&self, origin: EntityId, relation: R, target: EntityId)
where
R: Relation + Send,
{
self.push_fn(move |world| {
let _ = world.add_relation(origin, relation, target);
let _ = world.insert_relation(origin, relation, target);
});
}

Expand Down
159 changes: 155 additions & 4 deletions src/action/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::{any::TypeId, iter::FusedIterator};

use alloc::collections::VecDeque;
use smallvec::SmallVec;

use crate::{
bundle::{Bundle, ComponentBundle, DynamicBundle, DynamicComponentBundle},
Expand Down Expand Up @@ -123,6 +124,39 @@ impl<'a> ActionEncoder<'a> {
})
}

/// Encodes an action to despawn entities in batch.
#[inline(always)]
pub fn despawn_batch(&mut self, entities: impl IntoIterator<Item = EntityId>) {
let entities = entities.into_iter();

match entities.size_hint() {
(_, Some(upper)) if upper <= 8 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 8]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
(_, Some(upper)) if upper <= 16 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 16]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
(_, Some(upper)) if upper <= 32 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 32]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
_ => {
let entities = entities.into_iter().collect::<SmallVec<[_; 64]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
}
}

/// Encodes an action to insert component to the specified entity.
#[inline(always)]
pub fn insert<T>(&mut self, entity: impl Entity, component: T)
Expand Down Expand Up @@ -230,6 +264,15 @@ impl<'a> ActionEncoder<'a> {
self.drop_erased(entity, type_id::<T>())
}

/// Encodes an action to drop component from entities in batch.
#[inline(always)]
pub fn drop_batch<T>(&mut self, entities: impl IntoIterator<Item = EntityId>)
where
T: 'static,
{
self.drop_erased_batch(entities, type_id::<T>())
}

/// Encodes an action to drop component from specified entity.
#[inline(always)]
pub fn drop_erased(&mut self, entity: impl Entity, ty: TypeId) {
Expand All @@ -239,6 +282,39 @@ impl<'a> ActionEncoder<'a> {
})
}

/// Encodes an action to drop component from entities in batch.
#[inline(always)]
pub fn drop_erased_batch(&mut self, entities: impl IntoIterator<Item = EntityId>, ty: TypeId) {
let entities = entities.into_iter();

match entities.size_hint() {
(_, Some(upper)) if upper <= 8 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 8]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
(_, Some(upper)) if upper <= 16 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 16]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
(_, Some(upper)) if upper <= 32 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 32]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
_ => {
let entities = entities.into_iter().collect::<SmallVec<[_; 64]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
}
}

/// Encodes an action to drop bundle of components from specified entity.
#[inline(always)]
pub fn drop_bundle<B>(&mut self, entity: impl Entity)
Expand All @@ -253,14 +329,14 @@ impl<'a> ActionEncoder<'a> {

/// Encodes an action to add relation between two entities to the [`World`].
#[inline(always)]
pub fn add_relation<R>(&mut self, origin: impl Entity, relation: R, target: impl Entity)
pub fn insert_relation<R>(&mut self, origin: impl Entity, relation: R, target: impl Entity)
where
R: Relation + Send,
{
let origin = origin.id();
let target = target.id();
self.push_fn(move |world| {
let _ = world.add_relation(origin, relation, target);
let _ = world.insert_relation(origin, relation, target);
});
}

Expand Down Expand Up @@ -564,6 +640,39 @@ impl<'a> LocalActionEncoder<'a> {
})
}

/// Encodes an action to despawn entities in batch.
#[inline(always)]
pub fn despawn_batch(&mut self, entities: impl IntoIterator<Item = EntityId>) {
let entities = entities.into_iter();

match entities.size_hint() {
(_, Some(upper)) if upper <= 8 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 8]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
(_, Some(upper)) if upper <= 16 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 16]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
(_, Some(upper)) if upper <= 32 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 32]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
_ => {
let entities = entities.into_iter().collect::<SmallVec<[_; 64]>>();
self.push_fn(move |world| {
let _ = world.despawn_batch(entities);
});
}
}
}

/// Encodes an action to insert component to the specified entity.
#[inline(always)]
pub fn insert<T>(&mut self, entity: impl Entity, component: T)
Expand Down Expand Up @@ -687,6 +796,15 @@ impl<'a> LocalActionEncoder<'a> {
self.drop_erased(entity, type_id::<T>())
}

/// Encodes an action to drop component from entities in batch.
#[inline(always)]
pub fn drop_batch<T>(&mut self, entities: impl IntoIterator<Item = EntityId>)
where
T: 'static,
{
self.drop_erased_batch(entities, type_id::<T>())
}

/// Encodes an action to drop component from specified entity.
#[inline(always)]
pub fn drop_erased(&mut self, entity: impl Entity, ty: TypeId) {
Expand All @@ -696,6 +814,39 @@ impl<'a> LocalActionEncoder<'a> {
})
}

/// Encodes an action to drop component from entities in batch.
#[inline(always)]
pub fn drop_erased_batch(&mut self, entities: impl IntoIterator<Item = EntityId>, ty: TypeId) {
let entities = entities.into_iter();

match entities.size_hint() {
(_, Some(upper)) if upper <= 8 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 8]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
(_, Some(upper)) if upper <= 16 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 16]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
(_, Some(upper)) if upper <= 32 => {
let entities = entities.into_iter().collect::<SmallVec<[_; 32]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
_ => {
let entities = entities.into_iter().collect::<SmallVec<[_; 64]>>();
self.push_fn(move |world| {
let _ = world.drop_erased_batch(entities, ty);
});
}
}
}

/// Encodes an action to drop bundle of components from specified entity.
#[inline(always)]
pub fn drop_bundle<B>(&mut self, entity: impl Entity)
Expand All @@ -710,14 +861,14 @@ impl<'a> LocalActionEncoder<'a> {

/// Encodes an action to add relation between two entities to the [`World`].
#[inline(always)]
pub fn add_relation<R>(&mut self, origin: impl Entity, relation: R, target: impl Entity)
pub fn insert_relation<R>(&mut self, origin: impl Entity, relation: R, target: impl Entity)
where
R: Relation,
{
let origin = origin.id();
let target = target.id();
self.push_fn(move |world| {
let _ = world.add_relation(origin, relation, target);
let _ = world.insert_relation(origin, relation, target);
});
}

Expand Down
6 changes: 0 additions & 6 deletions src/entity/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ impl EntityBound<'_> {
world: PhantomData,
}
}

#[inline(always)]
pub(crate) fn wrap_slice(ids: &[EntityId]) -> &[Self] {
// Safety: `Self` is transparent wrapper over `EntityId`.
unsafe { &*(ids as *const [EntityId] as *const [Self]) }
}
}

impl<'a> Entity for EntityBound<'a> {
Expand Down
4 changes: 2 additions & 2 deletions src/flow/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl FlowWorld {
/// If relation is exclusive, then previous relation on origin is replaced, otherwise relation is added.
/// If relation is exclusive and symmetric, then previous relation on target is replaced, otherwise relation is added.
#[inline(always)]
pub fn add_relation<R>(
pub fn insert_relation<R>(
self,
origin: impl Entity,
relation: R,
Expand All @@ -622,7 +622,7 @@ impl FlowWorld {
// Safety: world reference does not escape this scope.
let world = unsafe { self.get() };

world.add_relation(origin, relation, target)
world.insert_relation(origin, relation, target)
}

/// Removes relation between two entities in the [`FlowWorld`].
Expand Down
Loading

0 comments on commit 1d7ef67

Please sign in to comment.