com.rs 3.3 KB

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