aboutsummaryrefslogtreecommitdiff
path: root/azalea-world/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-06-20 01:19:59 -0500
committermat <github@matdoes.dev>2022-06-20 01:19:59 -0500
commitefd874957331d8bff1cefa0f43d81685690391bf (patch)
tree8c3ba4dd23f0bb15f467a49597068a62fd91ff6c /azalea-world/src
parent438d633b83dde37e3d3564706da466963a8bc999 (diff)
downloadazalea-drasl-efd874957331d8bff1cefa0f43d81685690391bf.tar.xz
add gametick event and find_one_entity
Diffstat (limited to 'azalea-world/src')
-rw-r--r--azalea-world/src/entity.rs34
-rw-r--r--azalea-world/src/lib.rs13
2 files changed, 47 insertions, 0 deletions
diff --git a/azalea-world/src/entity.rs b/azalea-world/src/entity.rs
index 2409995c..e4e9864f 100644
--- a/azalea-world/src/entity.rs
+++ b/azalea-world/src/entity.rs
@@ -79,6 +79,40 @@ impl EntityStorage {
.or_default()
.insert(entity_id);
}
+
+ /// Get an iterator over all entities.
+ #[inline]
+ pub fn entities(&self) -> std::collections::hash_map::Values<'_, u32, Entity> {
+ self.by_id.values()
+ }
+
+ pub fn find_one_entity<F>(&self, mut f: F) -> Option<&Entity>
+ where
+ F: FnMut(&Entity) -> bool,
+ {
+ for entity in self.entities() {
+ if f(entity) {
+ return Some(entity);
+ }
+ }
+ None
+ }
+
+ pub fn find_one_entity_in_chunk<F>(&self, chunk: &ChunkPos, mut f: F) -> Option<&Entity>
+ where
+ F: FnMut(&Entity) -> bool,
+ {
+ if let Some(entities) = self.by_chunk.get(chunk) {
+ for entity_id in entities {
+ if let Some(entity) = self.by_id.get(entity_id) {
+ if f(entity) {
+ return Some(entity);
+ }
+ }
+ }
+ }
+ None
+ }
}
impl Default for EntityStorage {
diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs
index dc538618..10beb309 100644
--- a/azalea-world/src/lib.rs
+++ b/azalea-world/src/lib.rs
@@ -87,6 +87,19 @@ impl World {
pub fn entity_by_id(&self, id: u32) -> Option<&Entity> {
self.entity_storage.get_by_id(id)
}
+
+ /// Get an iterator over all entities.
+ #[inline]
+ pub fn entities(&self) -> std::collections::hash_map::Values<'_, u32, Entity> {
+ self.entity_storage.entities()
+ }
+
+ pub fn find_one_entity<F>(&self, mut f: F) -> Option<&Entity>
+ where
+ F: FnMut(&Entity) -> bool,
+ {
+ self.entity_storage.find_one_entity(|entity| f(entity))
+ }
}
impl Index<&ChunkPos> for World {