Advertisement
Guest User

123123

a guest
Feb 27th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.66 KB | None | 0 0
  1. use specs::{Builder, Component, ReadStorage, System, VecStorage, World, WorldExt, RunNow, EntityBuilder};
  2.  
  3. // =========================================================================================
  4. #[derive(Debug)]
  5. struct Position {
  6.     x: f32,
  7.     y: f32,
  8. }
  9.  
  10. impl Component for Position {
  11.     type Storage = VecStorage<Self>;
  12. }
  13.  
  14. #[derive(Debug)]
  15. struct Velocity {
  16.     x: f32,
  17.     y: f32,
  18. }
  19.  
  20. impl Component for Velocity {
  21.     type Storage = VecStorage<Self>;
  22. }
  23.  
  24. // =========================================================================================
  25.  
  26. trait ComponentHandler{
  27.     fn move_component_to<'a>(&mut self, builder: EntityBuilder<'a>) -> EntityBuilder<'a>;
  28. }
  29.  
  30. struct ComponentHandlerImpl<T: Component> {
  31.    pub component: Option<T>
  32. }
  33.  
  34. impl<T: Component + Send + Sync> ComponentHandler for ComponentHandlerImpl<T> {
  35.    fn move_component_to<'a>(&mut self, builder: EntityBuilder<'a>) -> EntityBuilder<'a> {
  36.         if let Some(comp) = std::mem::replace(&mut self.component, None) {
  37.             builder.with(comp)
  38.         } else {
  39.             builder
  40.         }
  41.     }
  42. }
  43.  
  44. // =========================================================================================
  45.  
  46. struct PrintPositionSystem;
  47.  
  48. impl<'a> System<'a> for PrintPositionSystem {
  49.     type SystemData = ReadStorage<'a, Position>;
  50.  
  51.    fn run(&mut self, position: Self::SystemData) {
  52.        use specs::Join;
  53.  
  54.        for position in position.join() {
  55.            println!("Hello, Pos {:?}", &position);
  56.        }
  57.    }
  58. }
  59.  
  60. struct PrintVelocitySystem;
  61.  
  62. impl<'a> System<'a> for PrintVelocitySystem {
  63.    type SystemData = ReadStorage<'a, Velocity>;
  64.  
  65.     fn run(&mut self, velocity: Self::SystemData) {
  66.         use specs::Join;
  67.  
  68.         for position in velocity.join() {
  69.             println!("Hello, Vel {:?}", &position);
  70.         }
  71.     }
  72. }
  73.  
  74. // =========================================================================================
  75.  
  76. fn main() {
  77.     let mut world = World::new();
  78.     world.register::<Position>();
  79.     world.register::<Velocity>();
  80.  
  81.     let mut vec: Vec<Box<dyn ComponentHandler>> = vec![
  82.         Box::new(ComponentHandlerImpl { component: Some(Position { x: 1.0, y: 2.0 }) }),
  83.         Box::new(ComponentHandlerImpl { component: Some(Velocity { x: 3.0, y: 4.0 }) }),
  84.     ];
  85.  
  86.     for component_handler in vec.iter_mut() {
  87.         component_handler.move_component_to(world.create_entity()).build();
  88.     }
  89.  
  90.     let mut print_position_system = PrintPositionSystem;
  91.     print_position_system.run_now(&world);
  92.  
  93.     let mut velocity_position_system = PrintVelocitySystem;
  94.     velocity_position_system.run_now(&world);
  95.  
  96.     world.maintain();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement