From f1e31886a32d286b8218781adc94e1e0011c729e Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Fri, 18 Mar 2016 10:25:09 +0000 Subject: [PATCH] Allow searching for entities within the manager --- Cargo.lock | 29 ++++++++++++++----------- src/ecs/mod.rs | 50 +++++++++++++++++++++++++++++++++++++++++++- src/types/bit/set.rs | 5 ++++- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d13fb8..52b37bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,11 @@ name = "bitflags" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitflags" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "0.4.2" @@ -98,7 +103,7 @@ name = "cookie" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "openssl 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -305,13 +310,13 @@ dependencies = [ "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "solicit 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", "traitobject 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "typeable 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "unicase 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicase 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -484,20 +489,20 @@ dependencies = [ [[package]] name = "openssl" -version = "0.7.6" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys-extras 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys-extras 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "openssl-sys" -version = "0.7.6" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -509,12 +514,12 @@ dependencies = [ [[package]] name = "openssl-sys-extras" -version = "0.7.6" +version = "0.7.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -647,7 +652,7 @@ name = "steven_openssl" version = "0.0.1" dependencies = [ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -695,7 +700,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "unicase" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/ecs/mod.rs b/src/ecs/mod.rs index 6641179..54922b4 100644 --- a/src/ecs/mod.rs +++ b/src/ecs/mod.rs @@ -45,12 +45,35 @@ pub struct Key { _t: PhantomData, } +/// Used to search for entities with the requested components. +pub struct Filter { + bits: BSet, +} + +impl Filter { + /// Creates an empty filter which matches everything + pub fn new() -> Filter { + Filter { + bits: BSet::new(0), + } + } + + /// Adds the component to the filter. + pub fn with(mut self, key: Key) -> Self { + if self.bits.capacity() <= key.id { + self.bits.resize(key.id + 1); + } + self.bits.set(key.id, true); + self + } +} + impl Manager { /// Creates a new manager. pub fn new() -> Manager { Manager { num_components: 0, - entities: vec![], + entities: vec![(Some(BSet::new(0)), 0)], // Has the world entity pre-defined free_entities: vec![], components: vec![], @@ -58,6 +81,31 @@ impl Manager { } } + /// Returns the world entity. This should never be removed. + pub fn get_world(&self) -> Entity { + Entity { + id: 0, + generation: 0, + } + } + + /// Returns all entities matching the filter + pub fn find(&self, filter: &Filter) -> Vec { + let mut ret = vec![]; + // Skip the world entity. + for (i, &(ref set, gen)) in self.entities[1..].iter().enumerate() { + if let Some(set) = set.as_ref() { + if set.includes_set(&filter.bits) { + ret.push(Entity { + id: i + 1, + generation: gen, + }); + } + } + } + ret + } + /// Allocates a new entity without any components. pub fn create_entity(&mut self) -> Entity { if let Some(id) = self.free_entities.pop() { diff --git a/src/types/bit/set.rs b/src/types/bit/set.rs index 23ef4e8..0a620ad 100644 --- a/src/types/bit/set.rs +++ b/src/types/bit/set.rs @@ -41,6 +41,10 @@ impl Set { self.data.resize((new_size + 63) / 64, 0); } + pub fn capacity(&self) -> usize { + self.data.len() * 64 + } + pub fn set(&mut self, i: usize, v: bool) { if v { self.data[i >> 6] |= 1 << (i & 0x3F) @@ -54,7 +58,6 @@ impl Set { } pub fn includes_set(&self, other: &Set) -> bool { - debug_assert!(self.data.len() == other.data.len()); for (a, b) in self.data.iter().zip(&other.data) { if a & b != *b { return false;