use specs::{Component, VecStorage, NullStorage}; /// Register all the components with the world. pub fn register(world: &mut specs::World) { world.register::(); world.register::(); world.register::(); world.register::(); world.register::(); world.register::(); world.register::(); } /// The `Position` component represents (in world coordinates) a thing /// that has a position in the world, measured from the top-left of /// the thing. #[derive(Component, Debug)] #[storage(VecStorage)] pub struct Position { pub x: f32, pub y: f32, } impl Position { /// Convert a `Position` to a screen point pub fn to_point(&self) -> ggez::nalgebra::Point2 { ggez::nalgebra::Point2::new(self.x * 3.0, self.y * 3.0) } } /// The `Velocity` componenent is present on any entity that moves /// through the world, and represents its rate of change per /// time-unit. #[derive(Component, Debug)] #[storage(VecStorage)] pub struct Velocity { pub dx: f32, pub dy: f32, } /// The `Sprite` components represents the current display location of /// a sprite in the spritesheet. #[derive(Component, Debug)] #[storage(VecStorage)] pub struct Sprite { pub u: u8, pub v: u8, } impl Sprite { /// Convert a `Sprite` into the rectangle that specifies the /// sprite location on the spritesheet pub fn to_rect(&self) -> ggez::graphics::Rect { ggez::graphics::Rect { x: (1.0 / 32.0) * self.u as f32, y: (1.0 / 32.0) * self.v as f32, w: 1.0 / 32.0, h: 1.0 / 32.0, } } } /// A drawing-phase component: represents tiles that appear in the /// background of everything. #[derive(Component, Default, Debug)] #[storage(NullStorage)] pub struct Background; /// A drawing-phase component: represents tiles which appear in the /// foreground, possibly entities. #[derive(Component, Default, Debug)] #[storage(NullStorage)] pub struct Foreground; /// A drawing-phase component: represents tiles which appear on top of /// everything else, such as the tops of trees or roofs of houses. #[derive(Component, Default, Debug)] #[storage(NullStorage)] pub struct Decoration; /// A component that represents entities which are controlled by the /// keyboard. #[derive(Component, Default, Debug)] #[storage(NullStorage)] pub struct Controlled; /// A component that represents entities which can collide with other /// things. #[derive(Component, Debug)] #[storage(VecStorage)] pub struct Collision { pub has_collision: bool }