From 9b821f26eb9d26094d8fc470924f91e2c5b7add8 Mon Sep 17 00:00:00 2001 From: copygirl Date: Sun, 22 Sep 2024 07:43:10 +0200 Subject: [PATCH] Add Archetype::add, remove, with and without --- src/ecs/archetype.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/ecs/archetype.rs b/src/ecs/archetype.rs index 179ce71..fdebfb5 100644 --- a/src/ecs/archetype.rs +++ b/src/ecs/archetype.rs @@ -32,6 +32,34 @@ impl Archetype { pub fn has(&self, component: impl Into) -> bool { self.components.binary_search(&component.into()).is_ok() } + + pub fn add(&mut self, component: impl Into) { + let component = component.into(); + if let Err(index) = self.components.binary_search(&component) { + self.components.insert(index, component) + } + } + + pub fn remove(&mut self, component: impl Into) { + let component = component.into(); + if let Ok(index) = self.components.binary_search(&component) { + self.components.remove(index); + } + } + + #[inline] + #[must_use] + pub fn with(mut self, component: impl Into) -> Self { + self.add(component); + self + } + + #[inline] + #[must_use] + pub fn without(mut self, component: impl Into) -> Self { + self.remove(component); + self + } } impl FromIterator for Archetype