1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
use ::Entity; use std::ops::Index; pub struct Entities(Vec<Entity>); impl Entities { pub fn new() -> Entities { Entities(vec![]) } pub fn push(&mut self, entity: Entity) { self.0.push(entity); } pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn vec(&self) -> Vec<&Entity> { self.0.iter().collect() } pub fn with_label(&self, label: &str) -> Vec<&Entity> { self.0 .iter() .filter(|e| e.label == label) .collect() } pub fn with_component<T: 'static>(&self) -> Vec<&Entity> { self.0 .iter() .filter(|e| e.components.contains::<T>()) .collect() } pub fn with_component_mut<T: 'static>(&mut self) -> Vec<&mut Entity> { self.0 .iter_mut() .filter(|e| e.components.contains::<T>()) .collect() } pub fn filter_map_to_component<T: 'static>(&self) -> Vec<&T> { self.0 .iter() .filter_map(|e| e.components.get::<T>()) .collect() } pub fn filter_map_to_component_mut<T: 'static>(&mut self) -> Vec<&mut T> { self.0 .iter_mut() .filter_map(|e| e.components.get_mut::<T>()) .collect() } } impl Index<usize> for Entities { type Output = Entity; fn index(&self, index: usize) -> &Entity { &self.0[index] } } #[cfg(test)] mod tests { use super::*; use ::EntityBuilder; #[derive(Debug, PartialEq)] struct TestComponent(pub u8); #[derive(Debug, PartialEq)] struct AnotherComponent; #[test] fn iter_works() { let mut entities = Entities::new(); entities.push(EntityBuilder::create_str("test1") .add(TestComponent(1)) .build()); entities.push(EntityBuilder::create_str("test2") .add(AnotherComponent) .build()); let res = entities.vec(); assert_eq!(2, res.len()); } #[test] fn with_label_works() { let mut entities = Entities::new(); let ent1 = EntityBuilder::create_str("test1") .add(TestComponent(1)) .build(); let ent2 = EntityBuilder::create_str("test2") .add(AnotherComponent) .build(); entities.push(ent1); entities.push(ent2); let mut results = entities.with_label("test1"); assert_eq!(1, results.len()); assert_eq!("test1", results.pop().unwrap().label); assert!(entities.with_label("aaaaaa").is_empty()); } #[test] fn with_component_works() { let mut entities = Entities::new(); entities.push(EntityBuilder::create_str("test") .add(TestComponent(1)) .build()); entities.push(EntityBuilder::create_str("test") .add(AnotherComponent) .build()); let test_component = entities.with_component::<TestComponent>().pop().unwrap(); assert!(test_component.components.contains::<TestComponent>()); } #[test] fn with_component_mut_works() { let mut entities = Entities::new(); entities.push(EntityBuilder::create_str("test") .add(TestComponent(1)) .build()); entities.push(EntityBuilder::create_str("test") .add(AnotherComponent) .build()); let test_component = entities.with_component_mut::<TestComponent>().pop().unwrap(); assert!(test_component.components.contains::<TestComponent>()); let mut unwrapped = test_component.components.get_mut::<TestComponent>().unwrap(); unwrapped.0 = 2; assert_eq!(unwrapped, &mut TestComponent(2)); } #[test] fn filter_map_to_component_works() { let mut entities = Entities::new(); entities.push(EntityBuilder::create_str("test") .add(TestComponent(1)) .build()); entities.push(EntityBuilder::create_str("test") .add(AnotherComponent) .build()); let test_component = entities.filter_map_to_component::<TestComponent>().pop(); assert_eq!(test_component, Some(&TestComponent(1))); } #[test] fn filter_map_to_component_mut_works() { let mut entities = Entities::new(); entities.push(EntityBuilder::create_str("test") .add(TestComponent(1)) .build()); entities.push(EntityBuilder::create_str("test") .add(AnotherComponent) .build()); let test_component = entities.filter_map_to_component_mut::<TestComponent>().pop(); assert_eq!(test_component, Some(&mut TestComponent(1))); let mut unwrapped = test_component.unwrap(); unwrapped.0 = 2; assert_eq!(unwrapped, &mut TestComponent(2)); } }