Skip to content

Commit

Permalink
Merge pull request #25 from ten3roberts/dev2
Browse files Browse the repository at this point in the history
chore: naming
  • Loading branch information
ten3roberts authored Nov 19, 2024
2 parents 4624f61 + d1327d1 commit c06ea01
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let regen_system = System::builder()
.boxed();

let despawn_system = System::builder()
.with_query(Query::new(entity_ids()).filter(health().le(0.0)))
.with_query(Query::new(entity_ids()).with_filter(health().le(0.0)))
.with_cmd_mut()
.build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
for id in &mut q {
Expand Down
6 changes: 3 additions & 3 deletions benches/common/serialize_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::repeat;
use bincode::Options;
use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand All @@ -80,7 +80,7 @@ impl Benchmark {
pub fn run_row(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand Down
4 changes: 2 additions & 2 deletions benches/common/serialize_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::repeat;

use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand Down
6 changes: 3 additions & 3 deletions examples/guide/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, CommandBuffer, Component, Debuggable, Entity, EntityBorrow, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
EntityBorrow, FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
Expand Down Expand Up @@ -99,7 +99,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(query)
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand Down
4 changes: 2 additions & 2 deletions examples/guide/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()> {

tracing_subscriber::fmt().init();

use flax::serialize::{SerdeBuilder, SerializeFormat};
use flax::serialize::{SerializationContextBuilder, SerializeFormat};
tracing::info!("It works");

let mut world = World::new();
Expand Down Expand Up @@ -45,7 +45,7 @@ fn main() -> anyhow::Result<()> {
// ANCHOR_END: setup

// ANCHOR: serialize
let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(name())
.with(position())
.with(velocity())
Expand Down
8 changes: 4 additions & 4 deletions examples/guide/systems.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, BoxedSystem, CommandBuffer, Component, Debuggable, Entity, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, BoxedSystem, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
Expand Down Expand Up @@ -31,7 +31,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand All @@ -48,7 +48,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand Down
2 changes: 1 addition & 1 deletion examples/query/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() {
Query::new((world_position().as_mut(), position())).with_strategy(Dfs::new(child_of)),
)
.build(
|mut query: DfsBorrow<(Mutable<Vec3>, Component<Vec3>), All, ()>| {
|mut query: DfsBorrow<(ComponentMut<Vec3>, Component<Vec3>), All, ()>| {
query.traverse(&Vec3::ZERO, |(world_pos, &pos), _, &parent_pos| {
*world_pos = pos + parent_pos;
*world_pos
Expand Down
2 changes: 1 addition & 1 deletion src/archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub(crate) struct ArchetypeIndex {
}

impl Debug for ArchetypeIndex {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ArchetypeIndex")
.field("components", &self.components)
.finish()
Expand Down
6 changes: 3 additions & 3 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
metadata::Metadata,
relation::RelationExt,
vtable::{ComponentVTable, UntypedVTable},
Entity, Mutable,
Entity, ComponentMut,
};

/// Trait alias for a 'static + Send + Sync type which can be used as a
Expand Down Expand Up @@ -260,8 +260,8 @@ impl<T: ComponentValue> Component<T> {
}

/// Transform this into a mutable fetch
pub const fn as_mut(self) -> Mutable<T> {
Mutable(self)
pub const fn as_mut(self) -> ComponentMut<T> {
ComponentMut(self)
}

/// Transform this into a (maybe) mutable fetch
Expand Down
29 changes: 24 additions & 5 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use bitflags::bitflags;
use itertools::Itertools;

use crate::{
Expand Down Expand Up @@ -37,6 +38,18 @@ impl Event {
}
}

bitflags! {
/// The type of ECS event
pub struct EventKindFilter: u8 {
/// Component was added
const ADDED = 1;
/// Component was removed
const REMOVED = 2;
/// Component was modified
const MODIFIED = 4;
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// The type of ECS event
pub enum EventKind {
Expand Down Expand Up @@ -139,8 +152,9 @@ pub trait EventSubscriber: ComponentValue {
subscriber: self,
}
}

/// Filter a subscriber to only receive events of a specific kind
fn filter_event_kind(self, event_kind: EventKind) -> FilterEventKind<Self>
fn filter_event_kind(self, event_kind: EventKindFilter) -> FilterEventKind<Self>
where
Self: Sized,
{
Expand Down Expand Up @@ -426,7 +440,7 @@ where

/// Filter a subscriber to only receive events of a specific kind
pub struct FilterEventKind<S> {
event_kind: EventKind,
event_kind: EventKindFilter,
subscriber: S,
}

Expand All @@ -435,23 +449,28 @@ where
S: EventSubscriber,
{
fn on_added(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Added {
if self.event_kind.contains(EventKindFilter::ADDED) {
self.subscriber.on_added(storage, event)
}
}

fn on_modified(&self, event: &EventData) {
if self.event_kind == EventKind::Modified {
if self.event_kind.contains(EventKindFilter::MODIFIED) {
self.subscriber.on_modified(event)
}
}

fn on_removed(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Removed {
if self.event_kind.contains(EventKindFilter::REMOVED) {
self.subscriber.on_removed(storage, event)
}
}

#[inline]
fn matches_component(&self, desc: ComponentDesc) -> bool {
self.subscriber.matches_component(desc)
}

fn is_connected(&self) -> bool {
self.subscriber.is_connected()
}
Expand Down
6 changes: 3 additions & 3 deletions src/fetch/component_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch};
#[derive(Debug, Clone)]
/// Mutable component fetch
/// See [crate::Component::as_mut]
pub struct Mutable<T>(pub(crate) Component<T>);
pub struct ComponentMut<T>(pub(crate) Component<T>);

impl<'w, T> Fetch<'w> for Mutable<T>
impl<'w, T> Fetch<'w> for ComponentMut<T>
where
T: ComponentValue,
{
Expand Down Expand Up @@ -64,7 +64,7 @@ where
}
}

impl<'q, T: ComponentValue> FetchItem<'q> for Mutable<T> {
impl<'q, T: ComponentValue> FetchItem<'q> for ComponentMut<T> {
type Item = &'q mut T;
}

Expand Down
16 changes: 8 additions & 8 deletions src/fetch/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
archetype::ChangeKind,
component::ComponentValue,
filter::{ChangeFilter, Filtered, NoEntities, Union},
Component, EntityIds, FetchExt, Mutable,
Component, EntityIds, FetchExt, ComponentMut,
};

/// Allows transforming a fetch into another.
Expand Down Expand Up @@ -32,14 +32,14 @@ impl<T: ComponentValue> TransformFetch<Added> for Component<T> {
}
}

impl<T: ComponentValue> TransformFetch<Modified> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Modified> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Modified) -> Self::Output {
self.filtered(NoEntities)
}
}

impl<T: ComponentValue> TransformFetch<Added> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Added> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Added) -> Self::Output {
self.filtered(NoEntities)
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_modified_struct() {
use crate::{fetch::Cloned, Component, Fetch, Mutable, Opt};
use crate::{fetch::Cloned, Component, Fetch, ComponentMut, Opt};

component! {
a: i32,
Expand All @@ -193,8 +193,8 @@ mod tests {
struct MyFetch {
a: Component<i32>,
b: Cloned<Component<String>>,
c: Mutable<f32>,
other: Opt<Mutable<()>>,
c: ComponentMut<f32>,
other: Opt<ComponentMut<()>>,
}

let mut world = World::new();
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_inserted_struct() {
use crate::{fetch::Cloned, Component, EntityIds, Fetch, Mutable};
use crate::{fetch::Cloned, Component, EntityIds, Fetch, ComponentMut};

#[derive(Debug)]
struct Custom;
Expand All @@ -294,7 +294,7 @@ mod tests {
a: Component<i32>,
b: Cloned<Component<String>>,
#[fetch(ignore)]
c: Mutable<Custom>,
c: ComponentMut<Custom>,
}

let mut world = World::new();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
//! .boxed();
//!
//! let despawn_system = System::builder()
//! .with_query(Query::new(entity_ids()).filter(health().le(0.0)))
//! .with_query(Query::new(entity_ids()).with_filter(health().le(0.0)))
//! .with_cmd_mut()
//! .build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
//! for id in &mut q {
Expand Down Expand Up @@ -264,7 +264,7 @@ pub use entity_ref::{EntityRef, EntityRefMut};
pub use entry::{Entry, OccupiedEntry, VacantEntry};
pub use error::Error;
pub use fetch::{
relations_like, EntityIds, Fetch, FetchExt, FetchItem, Mutable, Opt, OptOr, Relations,
relations_like, ComponentMut, EntityIds, Fetch, FetchExt, FetchItem, Opt, OptOr, Relations,
};

pub use metadata::{Debuggable, Exclusive};
Expand Down
Loading

0 comments on commit c06ea01

Please sign in to comment.