components.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. use specs::{Component, VecStorage, NullStorage};
  2. /// Register all the components with the world.
  3. pub fn register(world: &mut specs::World) {
  4. world.register::<Position>();
  5. world.register::<Velocity>();
  6. world.register::<Sprite>();
  7. world.register::<Background>();
  8. world.register::<Foreground>();
  9. world.register::<Decoration>();
  10. world.register::<Controlled>();
  11. }
  12. /// The `Position` component represents (in world coordinates) a thing
  13. /// that has a position in the world, measured from the top-left of
  14. /// the thing.
  15. #[derive(Component, Debug)]
  16. #[storage(VecStorage)]
  17. pub struct Position {
  18. pub x: f32,
  19. pub y: f32,
  20. }
  21. impl Position {
  22. /// Convert a `Position` to a screen point
  23. pub fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
  24. ggez::nalgebra::Point2::new(self.x * 3.0, self.y * 3.0)
  25. }
  26. }
  27. /// The `Velocity` componenent is present on any entity that moves
  28. /// through the world, and represents its rate of change per
  29. /// time-unit.
  30. #[derive(Component, Debug)]
  31. #[storage(VecStorage)]
  32. pub struct Velocity {
  33. pub dx: f32,
  34. pub dy: f32,
  35. }
  36. /// The `Sprite` components represents the current display location of
  37. /// a sprite in the spritesheet.
  38. #[derive(Component, Debug)]
  39. #[storage(VecStorage)]
  40. pub struct Sprite {
  41. pub u: u8,
  42. pub v: u8,
  43. }
  44. impl Sprite {
  45. /// Convert a `Sprite` into the rectangle that specifies the
  46. /// sprite location on the spritesheet
  47. pub fn to_rect(&self) -> ggez::graphics::Rect {
  48. ggez::graphics::Rect {
  49. x: (1.0 / 32.0) * self.u as f32,
  50. y: (1.0 / 32.0) * self.v as f32,
  51. w: 1.0 / 32.0,
  52. h: 1.0 / 32.0,
  53. }
  54. }
  55. }
  56. /// A drawing-phase component: represents tiles that appear in the
  57. /// background of everything.
  58. #[derive(Component, Default, Debug)]
  59. #[storage(NullStorage)]
  60. pub struct Background;
  61. /// A drawing-phase component: represents tiles which appear in the
  62. /// foreground, possibly entities.
  63. #[derive(Component, Default, Debug)]
  64. #[storage(NullStorage)]
  65. pub struct Foreground;
  66. /// A drawing-phase component: represents tiles which appear on top of
  67. /// everything else, such as the tops of trees or roofs of houses.
  68. #[derive(Component, Default, Debug)]
  69. #[storage(NullStorage)]
  70. pub struct Decoration;
  71. /// A component that represents entities which are controlled by the
  72. /// keyboard.
  73. #[derive(Component, Default, Debug)]
  74. #[storage(NullStorage)]
  75. pub struct Controlled;
  76. /// A component that represents entities which can collide with other
  77. /// things.
  78. #[derive(Component, Debug)]
  79. #[storage(VecStorage)]
  80. pub struct Collision {
  81. pub has_collision: bool
  82. }