aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/parts/entities.rs
diff options
context:
space:
mode:
Diffstat (limited to 'stockton-levels/src/parts/entities.rs')
-rw-r--r--stockton-levels/src/parts/entities.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/stockton-levels/src/parts/entities.rs b/stockton-levels/src/parts/entities.rs
new file mode 100644
index 0000000..7a5ac74
--- /dev/null
+++ b/stockton-levels/src/parts/entities.rs
@@ -0,0 +1,36 @@
+use std::iter::Iterator;
+
+pub type EntityRef = u32;
+
+/// A game entity
+pub trait IsEntity<C: HasEntities + ?Sized> {
+ fn get_attr(&self, container: &C) -> Option<&str>;
+}
+
+pub trait HasEntities {
+ type Entity: IsEntity<Self>;
+
+ fn get_entity(&self, idx: EntityRef) -> Option<&Self::Entity>;
+ fn iter_entities(&self) -> Entities<Self> {
+ Entities {
+ next: 0,
+ container: self,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy)]
+pub struct Entities<'a, T: HasEntities + ?Sized> {
+ next: EntityRef,
+ container: &'a T,
+}
+
+impl<'a, T: HasEntities> Iterator for Entities<'a, T> {
+ type Item = &'a T::Entity;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let res = self.container.get_entity(self.next);
+ self.next += 1;
+ res
+ }
+}