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