1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/// Macro for easily creating block classes
///
/// ## NOTE:
/// The Type enum must have a Block subtype like this:
/// ```
/// enum Type {
///     ....,
///     Block,
/// }
/// ```
/// and the Message must have a None subtype like this:
/// ```
/// enum Message {
///     ....,
///     None,
/// }
/// ```
///
/// ## Example:
/// ```
/// block! {
///     actor_type: ActorType,
///     actor_message: ActorMessage,
///     blocks: {
///         block {
///             name: GrassBlock, // the name of the block
///             path: "assets/spritesheet.png", // the path of the spritesheet
///             index: 20, // the sprite index inside the spritesheet
///             width: 5, // width of block
///             height: 5, // height of block
///             sprites_in_row: 10, // number of blocks in the spritesheet in a row
///             size: 20, // size of the rendered block
///         }
///
///         block {
///             ....
///         }
///     }
/// }
/// ```
#[macro_export]
macro_rules! block {
    (
        actor_type: $actor_type:ident,
        actor_message: $actor_message:ident,
        blocks: {
            $(
                block {
                    name: $name:ident,
                    path: $path:expr,
                    index: $index:expr,
                    width: $width:expr,
                    height: $height:expr,
                    sprites_in_row: $sprites_in_row:expr,
                    size: $size:expr,
                    collision_filter: $filter:expr
                }
            )*
        }
    ) => {
        $(
            pub struct $name {
                pub rect: ::mold2d::SpriteRectangle,
                pub sprite: ::mold2d::Sprite,
                id: i32,
            }

            impl $name {
                pub fn new(id: i32,
                           position: (i32, i32),
                           renderer: &mut ::sdl2::render::Renderer,
                           _fps: f64)
                           -> $name {
                    let anim_data = ::mold2d::SpritesheetConfig {
                        width: $width,
                        height: $height,
                        sprites_in_row: $sprites_in_row,
                        path: $path,
                    };

                    let anim = ::mold2d::Spritesheet::new(anim_data, renderer);
                    let mut sprite_anims = anim.range($index, $index + 1);
                    let sprite = sprite_anims.pop().unwrap();

                    $name {
                        id: id,
                        rect: ::mold2d::SpriteRectangle::new(position.0,
                                                             position.1,
                                                             $size,
                                                             $size),
                        sprite: sprite,
                    }
                }
            }

            impl ::mold2d::Actor<$actor_type, $actor_message> for $name {
                fn handle_message(&mut self, _: &$actor_message) -> $actor_message {
                    $actor_message::None
                }

                #[allow(unused_imports)]
                fn collides_with(&mut self,
                                 other_actor: &::mold2d::ActorData<$actor_type>)
                                 -> Option<::mold2d::CollisionSide> {
                    use ::mold2d::Collision;
                    self.rect.collides_with(other_actor.rect)
                }

                fn update(&mut self,
                          _context: &mut ::mold2d::Context,
                          _elapsed: f64)
                          -> ::mold2d::PositionChange {
                    ::mold2d::PositionChange::new()
                }

                #[allow(unused_imports)]
                fn render(&mut self,
                          context: &mut ::mold2d::Context,
                          viewport: &mut ::mold2d::Viewport,
                          _elapsed: f64) {
                    use ::mold2d::Renderable;
                    let (rx, ry) = viewport.relative_point((self.rect.x, self.rect.y));
                    let rect = ::sdl2::rect::Rect::new_unwrap(rx, ry, self.rect.w, self.rect.h);

                    self.sprite.render(&mut context.renderer, rect);
                }

                fn data(&mut self) -> ::mold2d::ActorData<$actor_type> {
                    ::mold2d::ActorData {
                        id: self.id,
                        state: 0 as u32,
                        damage: 0,
                        resolves_collisions: false,
                        collision_filter: $filter,
                        rect: self.rect.to_sdl().unwrap(),
                        bounding_box: Some(::mold2d::BoundingBox::Rectangle(self.rect.clone())),
                        actor_type: $actor_type::Block,
                    }
                }
            }
        )*
    }
}