components.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use specs::{Component, VecStorage, NullStorage};
  2. pub fn register(world: &mut specs::World) {
  3. world.register::<Position>();
  4. world.register::<Velocity>();
  5. world.register::<Sprite>();
  6. world.register::<Background>();
  7. world.register::<Foreground>();
  8. world.register::<Decoration>();
  9. world.register::<Movable>();
  10. }
  11. #[derive(Component, Debug)]
  12. #[storage(VecStorage)]
  13. pub struct Position {
  14. pub x: f32,
  15. pub y: f32,
  16. }
  17. impl Position {
  18. pub fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
  19. ggez::nalgebra::Point2::new(self.x * 3.0, self.y * 3.0)
  20. }
  21. }
  22. #[derive(Component, Debug)]
  23. #[storage(VecStorage)]
  24. pub struct Velocity {
  25. pub dx: f32,
  26. pub dy: f32,
  27. }
  28. impl Velocity {
  29. pub fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
  30. ggez::nalgebra::Point2::new(self.dx * 3.0, self.dy * 3.0)
  31. }
  32. }
  33. #[derive(Component, Debug)]
  34. #[storage(VecStorage)]
  35. pub struct Sprite {
  36. pub u: u8,
  37. pub v: u8,
  38. }
  39. impl Sprite {
  40. pub fn to_rect(&self) -> ggez::graphics::Rect {
  41. ggez::graphics::Rect {
  42. x: (1.0 / 32.0) * self.u as f32,
  43. y: (1.0 / 32.0) * self.v as f32,
  44. w: 1.0 / 32.0,
  45. h: 1.0 / 32.0,
  46. }
  47. }
  48. }
  49. #[derive(Component, Default, Debug)]
  50. #[storage(NullStorage)]
  51. pub struct Background;
  52. #[derive(Component, Default, Debug)]
  53. #[storage(NullStorage)]
  54. pub struct Foreground;
  55. #[derive(Component, Default, Debug)]
  56. #[storage(NullStorage)]
  57. pub struct Decoration;
  58. #[derive(Component, Default, Debug)]
  59. #[storage(NullStorage)]
  60. pub struct Movable;