Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. use specs::prelude::*;
  2. use specs_derive::*;
  3.  
  4. #[derive(Component)]
  5. #[storage(VecStorage)]
  6. struct Position {
  7. x: i32,
  8. y: i32,
  9. }
  10.  
  11. struct Color(u8, u8, u8);
  12.  
  13. #[derive(Component)]
  14. #[storage(VecStorage)]
  15. struct Drawable {
  16. char: char,
  17. background_color: Color,
  18. foreground_color: Color,
  19. }
  20.  
  21. #[derive(Component)]
  22. #[storage(VecStorage)]
  23. struct Health {
  24. max_health: i32,
  25. current_health: i32,
  26. }
  27.  
  28. struct Draw;
  29. impl<'a> System<'a> for Draw {
  30. type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Drawable>);
  31.  
  32. fn run(&mut self, (pos, drawable): Self::SystemData) {
  33. use specs::Join;
  34.  
  35. for (pos, drawable) in (&pos, &drawable).join() {
  36. println!("{} at ({}, {})", drawable.char, pos.x, pos.y);
  37. }
  38. }
  39. }
  40.  
  41. struct UpdatePos;
  42. impl<'a> System<'a> for UpdatePos {
  43. type SystemData = WriteStorage<'a, Position>;
  44.  
  45. fn run(&mut self, mut pos: Self::SystemData) {
  46. use specs::Join;
  47.  
  48. for pos in (&mut pos).join() {
  49. pos.x += 1;
  50. pos.y += 1;
  51. }
  52. }
  53. }
  54.  
  55. fn create_player(world: &mut World, char: char, pos: Position) {
  56. world.create_entity()
  57. .with(pos)
  58. .with(Health { max_health: 100, current_health: 100 })
  59. .with(Drawable { char, foreground_color: Color(0xff, 0xff, 0xff), background_color: Color(0, 0, 0)})
  60. .build();
  61. }
  62.  
  63. fn main() {
  64. let mut world = World::new();
  65. // add these lines...
  66. // world.register::<Position>();
  67. // world.register::<Drawable>();
  68. // world.register::<Health>();
  69.  
  70. let mut dispatcher = DispatcherBuilder::new()
  71. .with(UpdatePos, "update_pos", &[])
  72. .with(Draw, "draw", &["update_pos"])
  73. .build();
  74.  
  75. dispatcher.setup(&mut world.res); // ...and comment out this line to make it work
  76.  
  77. create_player(&mut world, '@', Position { x: 0, y: 0 });
  78. create_player(&mut world, 'T', Position { x: 0, y: 5 });
  79.  
  80. for _ in 0 .. 10 {
  81. dispatcher.dispatch(&world.res);
  82. world.maintain();
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement