com.rs 3.4 KB

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