com.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. world.register::<Collision>();
  12. world.register::<Blocking>();
  13. }
  14. /// The `Position` component represents (in world coordinates) a thing
  15. /// that has a position in the world, measured from the top-left of
  16. /// the thing.
  17. #[derive(Component, Debug, Clone)]
  18. #[storage(VecStorage)]
  19. pub struct Position {
  20. pub x: f32,
  21. pub y: f32,
  22. }
  23. impl Position {
  24. /// Convert a `Position` to a screen point
  25. pub fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
  26. ggez::nalgebra::Point2::new(self.x * 3.0, self.y * 3.0)
  27. }
  28. pub fn to_grid(&self) -> (i32, i32) {
  29. ((self.x / 24.0) as i32,
  30. (self.y / 24.0) as i32,
  31. )
  32. }
  33. pub fn moved(&self, vel: &Velocity) -> Position {
  34. Position {
  35. x: self.x + vel.dx,
  36. y: self.x + vel.dy,
  37. }
  38. }
  39. }
  40. /// The `Velocity` componenent is present on any entity that moves
  41. /// through the world, and represents its rate of change per
  42. /// time-unit.
  43. #[derive(Component, Debug)]
  44. #[storage(VecStorage)]
  45. pub struct Velocity {
  46. pub dx: f32,
  47. pub dy: f32,
  48. }
  49. /// The `Sprite` components represents the current display location of
  50. /// a sprite in the spritesheet.
  51. #[derive(Component, Debug)]
  52. #[storage(VecStorage)]
  53. pub struct Sprite {
  54. pub u: u8,
  55. pub v: u8,
  56. }
  57. impl Sprite {
  58. /// Convert a `Sprite` into the rectangle that specifies the
  59. /// sprite location on the spritesheet
  60. pub fn to_rect(&self) -> ggez::graphics::Rect {
  61. ggez::graphics::Rect {
  62. x: (1.0 / 32.0) * self.u as f32,
  63. y: (1.0 / 32.0) * self.v as f32,
  64. w: 1.0 / 32.0,
  65. h: 1.0 / 32.0,
  66. }
  67. }
  68. }
  69. /// A drawing-phase component: represents tiles that appear in the
  70. /// background of everything.
  71. #[derive(Component, Default, Debug)]
  72. #[storage(NullStorage)]
  73. pub struct Background;
  74. /// A drawing-phase component: represents tiles which appear in the
  75. /// foreground, possibly entities.
  76. #[derive(Component, Default, Debug)]
  77. #[storage(NullStorage)]
  78. pub struct Foreground;
  79. /// A drawing-phase component: represents tiles which appear on top of
  80. /// everything else, such as the tops of trees or roofs of houses.
  81. #[derive(Component, Default, Debug)]
  82. #[storage(NullStorage)]
  83. pub struct Decoration;
  84. /// A component that represents entities which are controlled by the
  85. /// keyboard.
  86. #[derive(Component, Default, Debug)]
  87. #[storage(NullStorage)]
  88. pub struct Controlled;
  89. /// A component that represents entities which can collide with other
  90. /// things.
  91. #[derive(Component, Debug)]
  92. #[storage(VecStorage)]
  93. pub struct Collision {
  94. pub has_collision: bool
  95. }
  96. #[derive(Component)]
  97. #[storage(VecStorage)]
  98. pub struct Blocking {
  99. pub volume: Box<dyn ncollide2d::shape::Shape<f32>>,
  100. }
  101. impl Blocking {
  102. pub fn new() -> Blocking { Default::default() }
  103. }
  104. impl Default for Blocking {
  105. fn default() -> Blocking {
  106. Blocking {
  107. volume: Box::new(ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(11.0, 11.0))),
  108. }
  109. }
  110. }