gdritter repos wenaglia / b1f4d62
Start doing some commenting Getty Ritter 5 years ago
3 changed file(s) with 33 addition(s) and 10 deletion(s). Collapse all Expand all
11 use specs::{Component, VecStorage, NullStorage};
22
3 /// Register all the components with the world.
34 pub fn register(world: &mut specs::World) {
45 world.register::<Position>();
56 world.register::<Velocity>();
78 world.register::<Background>();
89 world.register::<Foreground>();
910 world.register::<Decoration>();
10 world.register::<Movable>();
11 world.register::<Controlled>();
1112 }
1213
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.
1317 #[derive(Component, Debug)]
1418 #[storage(VecStorage)]
1519 pub struct Position {
1822 }
1923
2024 impl Position {
25 /// Convert a `Position` to a screen point
2126 pub fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
2227 ggez::nalgebra::Point2::new(self.x * 3.0, self.y * 3.0)
2328 }
2429 }
2530
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.
2634 #[derive(Component, Debug)]
2735 #[storage(VecStorage)]
2836 pub struct Velocity {
3038 pub dy: f32,
3139 }
3240
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 }
3841
42 /// The `Sprite` components represents the current display location of
43 /// a sprite in the spritesheet.
3944 #[derive(Component, Debug)]
4045 #[storage(VecStorage)]
4146 pub struct Sprite {
4449 }
4550
4651 impl Sprite {
52 /// Convert a `Sprite` into the rectangle that specifies the
53 /// sprite location on the spritesheet
4754 pub fn to_rect(&self) -> ggez::graphics::Rect {
4855 ggez::graphics::Rect {
4956 x: (1.0 / 32.0) * self.u as f32,
5461 }
5562 }
5663
64 /// A drawing-phase component: represents tiles that appear in the
65 /// background of everything.
5766 #[derive(Component, Default, Debug)]
5867 #[storage(NullStorage)]
5968 pub struct Background;
6069
70 /// A drawing-phase component: represents tiles which appear in the
71 /// foreground, possibly entities.
6172 #[derive(Component, Default, Debug)]
6273 #[storage(NullStorage)]
6374 pub struct Foreground;
6475
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.
6578 #[derive(Component, Default, Debug)]
6679 #[storage(NullStorage)]
6780 pub struct Decoration;
6881
82 /// A component that represents entities which are controlled by the
83 /// keyboard.
6984 #[derive(Component, Default, Debug)]
7085 #[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 }
5555 .with(Sprite { u: 8, v: 0 })
5656 .with(Velocity { dx: 0.0, dy: 0.0 })
5757 .with(Foreground)
58 .with(Movable)
58 .with(Controlled)
5959 .build();
6060 }
1 use crate::components::{Movable, Velocity};
1 use crate::components::{Controlled, Velocity};
22 use crate::game::MyGame;
33
44 use sdl2::keyboard as sdl;
1010
1111 impl<'a, 't> specs::System<'a> for Move<'t> {
1212 type SystemData = (
13 specs::ReadStorage<'a, Movable>,
13 specs::ReadStorage<'a, Controlled>,
1414 specs::WriteStorage<'a, Velocity>,
1515 );
1616