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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use cache;
use collision;
use collision::{BoundingBox, Collision, CollisionSide};
use sdl2::rect::Rect;
use sdl2::render::{Renderer, Texture};
use sdl2_image::LoadTexture;
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::path::Path;
use std::rc::Rc;
use vector::PositionChange;
use viewport::Viewport;

/// The direction that a sprite is facing
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum Direction {
    Left,
    Right,
}

pub trait Renderable {
    fn render(&self, renderer: &mut Renderer, dest: Rect);
}

/// A mutable rectangle for a sprite so it can be moved around
#[derive(Clone, PartialEq)]
pub struct SpriteRectangle {
    pub x: i32,
    pub y: i32,
    pub w: u32,
    pub h: u32,
}

impl SpriteRectangle {
    pub fn new(x: i32, y: i32, w: u32, h: u32) -> SpriteRectangle {
        SpriteRectangle {
            x: x,
            y: y,
            w: w,
            h: h,
        }
    }

    /// Creates a sprite rectangle from a SDL2 rectangle
    pub fn from_rect(rect: Rect) -> SpriteRectangle {
        SpriteRectangle {
            x: rect.x(),
            y: rect.y(),
            w: rect.width(),
            h: rect.height(),
        }
    }

    /// Returns a SDL Rect created from the SpriteRectangle
    /// Used for rendering SpriteRectangles in SDL
    pub fn to_sdl(&self) -> Option<Rect> {
        Rect::new(self.x, self.y, self.w, self.h).unwrap()
    }

    /// Mutates a sprite rectangle based on the position change given
    pub fn apply_change(&mut self, change: &PositionChange) {
        self.x += change.x;
        self.y += change.y;
        if change.w < 0 {
            self.w -= change.w.abs() as u32;
        } else {
            self.w += change.w as u32;
        }
        if change.h < 0 {
            self.h -= change.h.abs() as u32;
        } else {
            self.w += change.w as u32;
        }
    }
}

/// A sprite data type that uses reference counting
/// to reuse the texture on multiple sub-sprites
#[derive(Clone)]
pub struct Sprite {
    tex: Rc<RefCell<Texture>>,
    src: Rect,
}

impl Sprite {
    pub fn new(texture: Texture) -> Sprite {
        let tex_query = texture.query();

        Sprite {
            tex: Rc::new(RefCell::new(texture)),
            src: Rect::new_unwrap(0, 0, tex_query.width, tex_query.height),
        }
    }

    /// Loads a new sprite from a path string to a sprite image file
    pub fn load(renderer: &Renderer, path: &str) -> Option<Sprite> {
        let sprite_cache = cache::sprite_cache();

        // if sprite is cached, return from cache
        if let Ok(ref cache) = sprite_cache.cache.lock() {
            if let Some(sprite) = cache.get(path).map(|sprite| sprite.clone()) {
                return Some(sprite);
            }
        }

        // otherwise load sprite from texture
        let sprite = renderer.load_texture(Path::new(path)).ok().map(Sprite::new);

        // cache result if successful
        if let Some(ref sprite) = sprite {
            if let Ok(ref mut cache) = sprite_cache.cache.lock() {
                cache.insert(path.to_owned(), sprite.clone());
            }
        }

        sprite
    }

    /// Returns a sub-sprite from a rectangle region of the original sprite
    pub fn region(&self, rect: Rect) -> Option<Sprite> {
        let new_src = Rect::new_unwrap(rect.x() + self.src.x(),
                                       rect.y() + self.src.y(),
                                       rect.width(),
                                       rect.height());

        if collision::rect_contains_rect(self.src, new_src) {
            Some(Sprite {
                tex: self.tex.clone(),
                src: new_src,
            })
        } else {
            None
        }
    }

    /// Returns the dimensions of the sprite
    pub fn size(&self) -> (u32, u32) {
        (self.src.width(), self.src.height())
    }
}

impl Renderable for Sprite {
    /// Render the sprite image onto the rectangle
    fn render(&self, renderer: &mut Renderer, dest: Rect) {
        renderer.copy(&mut self.tex.borrow_mut(), Some(self.src), Some(dest));
    }
}

/// Represents an animated sprite with multiple frames
#[derive(Clone)]
pub struct AnimatedSprite {
    /// Frames that will be rendered
    frames: Vec<Sprite>,
    /// Time between frames
    frame_delay: f64,
    /// Total time sprite has been alive
    current_time: f64,
}

impl AnimatedSprite {
    /// Creates a new animated sprite with the given Sprite frames and a frame delay
    fn new(frames: Vec<Sprite>, frame_delay: f64) -> AnimatedSprite {
        AnimatedSprite {
            frames: frames,
            frame_delay: frame_delay,
            current_time: 0.0,
        }
    }

    pub fn with_fps(frames: Vec<Sprite>, fps: f64) -> AnimatedSprite {
        if fps == 0.0 {
            panic!("Passed 0 to AnimatedSprite::with_fps");
        }

        AnimatedSprite::new(frames, 1.0 / fps)
    }

    fn set_frame_delay(&mut self, frame_delay: f64) {
        self.frame_delay = frame_delay;
    }

    fn set_fps(&mut self, fps: f64) {
        if fps == 0.0 {
            panic!("Passed 0 to AnimatedSprite::set_fps");
        }

        self.set_frame_delay(1.0 / fps);
    }

    /// Updates the animated sprite with the elapsed time
    pub fn add_time(&mut self, elapsed: f64) {
        self.current_time += elapsed;

        if self.current_time < 0.0 {
            self.current_time = (self.frames.len() - 1) as f64 * self.frame_delay;
        }
    }
}

impl Renderable for AnimatedSprite {
    /// Renders the current frame of the animated sprite
    fn render(&self, renderer: &mut Renderer, dest: Rect) {
        if self.frames.len() == 0 {
            panic!("There has to be at least one frame!");
        }

        let current_frame = (self.current_time / self.frame_delay) as usize % self.frames.len();

        let frame = &self.frames[current_frame];
        frame.render(renderer, dest);
    }
}

/// Contains configuration fields for parsing a spritesheet
pub struct SpritesheetConfig {
    /// The width of each animation frame
    pub width: u32,
    /// The height of each animation frame
    pub height: u32,
    /// The number of frames in each row of the spritesheet
    pub sprites_in_row: i32,
    /// The path to the spritesheet file
    pub path: &'static str,
}

/// A spritesheet manager that returns sprites within a range
pub struct Spritesheet {
    config: SpritesheetConfig,
    spritesheet: Sprite,
}

impl Spritesheet {
    /// Loads a spritesheet given a configuration object and a SDL2 renderer
    pub fn new(config: SpritesheetConfig, renderer: &mut Renderer) -> Spritesheet {
        let spritesheet = match Sprite::load(renderer, config.path) {
            Some(spritesheet) => spritesheet,
            None => panic!("{} is not a valid path", config.path),
        };

        Spritesheet {
            config: config,
            spritesheet: spritesheet,
        }
    }

    /// Returns a Vec of sprites within a certain range.
    /// The number of the start and end ranges are from row to
    /// row wrapping around. For example a 3x3 grid would have
    /// indexes like this:
    /// ```
    /// 0 1 2
    /// 3 4 5
    /// 6 7 8
    /// ```
    pub fn range(&self, start: i32, end: i32) -> Vec<Sprite> {
        (start..end)
            .map(|elem| {
                let x = elem % self.config.sprites_in_row;
                let y = elem / self.config.sprites_in_row;

                let region = Rect::new_unwrap((self.config.width as i32) * x,
                                              (self.config.height as i32) * y,
                                              self.config.width,
                                              self.config.height);
                self.spritesheet.region(region)
            })
            .flat_map(|sprite| sprite)
            .collect()
    }
}

/// An animation manager that retrieves the animations for a state
pub struct Animations<State> {
    fps: f64,
    animations: HashMap<State, (AnimatedSprite, BoundingBox)>,
    /// Saves the current state for better performance
    curr_state: Option<State>,
    /// Saves the current bounding box for better performance
    curr_bbox: Option<BoundingBox>,
    /// Saves the current animation for better performance
    curr_anim: Option<AnimatedSprite>,
}

impl<State> Animations<State>
    where State: Clone + Eq + Hash
{
    pub fn new(fps: f64) -> Animations<State> {
        Animations {
            fps: fps,
            animations: HashMap::new(),
            curr_state: None,
            curr_bbox: None,
            curr_anim: None,
        }
    }

    pub fn add(&mut self, s: State, anims: Vec<Sprite>, bound: BoundingBox) {
        self.animations.insert(s, (AnimatedSprite::with_fps(anims, self.fps), bound));
    }

    fn set_state(&mut self, s: &State) {
        // Insert the saved bounding box and animation back into the hashmap
        if let (Some(state), Some(bbox), Some(anim)) = (self.curr_state.take(),
                                                        self.curr_bbox.take(),
                                                        self.curr_anim.take()) {
            self.animations.insert(state, (anim, bbox));
        }

        // Save the new state
        self.curr_state = Some(s.clone());
        if let Some((anim, bbox)) = self.animations.remove(s) {
            self.curr_bbox = Some(bbox);
            self.curr_anim = Some(anim);
        }
    }

    /// Returns an immutable reference to the animation for the given state
    pub fn anim(&mut self, s: &State) -> Option<&AnimatedSprite> {
        if let Some(ref state) = self.curr_state {
            if state == s {
                return self.curr_anim.as_ref();
            }
        }

        self.set_state(s);
        self.curr_anim.as_ref()
    }

    /// Returns a mutable reference to the animation for the given state
    pub fn anim_mut(&mut self, s: &State) -> Option<&mut AnimatedSprite> {
        if let Some(ref state) = self.curr_state {
            if state == s {
                return self.curr_anim.as_mut();
            }
        }

        self.set_state(s);
        self.curr_anim.as_mut()
    }

    /// Returns an immutable reference to the bounding box for the given state
    pub fn bbox(&mut self, s: &State) -> Option<&BoundingBox> {
        if let Some(ref state) = self.curr_state {
            if state == s {
                return self.curr_bbox.as_ref();
            }
        }

        self.set_state(s);
        self.curr_bbox.as_ref()
    }

    /// Returns a mutable reference to the bounding box for the given state
    pub fn bbox_mut(&mut self, s: &State) -> Option<&mut BoundingBox> {
        if let Some(ref state) = self.curr_state {
            if state == s {
                return self.curr_bbox.as_mut();
            }
        }

        self.set_state(s);
        self.curr_bbox.as_mut()
    }

    /// Maps a function that mutates a bounding box over all of the
    /// bounding boxes in the animation
    pub fn map_bbox_mut<F>(&mut self, f: F)
        where F: Fn(&mut BoundingBox)
    {
        if let Some(ref mut bbox) = self.curr_bbox {
            f(bbox);
        }

        for animation in self.animations.iter_mut() {
            let bbox = &mut (animation.1).1;
            f(bbox);
        }
    }

    /// Checks if the animation at the state collides with another bounding box
    /// and returns the side of the collision if it happens
    pub fn collides_with(&mut self,
                         s: &State,
                         other_bbox: &Option<BoundingBox>)
                         -> Option<CollisionSide> {
        if let Some(bounding_box) = self.bbox(s) {
            if let Some(ref bbox) = *other_bbox {
                return bounding_box.collides_with(bbox);
            }
        }

        None
    }

    /// Adds time to the current animation
    pub fn add_time(&mut self, s: &State, elapsed: f64) {
        if let Some(animation) = self.anim_mut(s) {
            animation.add_time(elapsed);
        }
    }

    /// Renders an animation in the manager
    pub fn render(&mut self,
                  s: &State,
                  rect: &SpriteRectangle,
                  viewport: &mut Viewport,
                  renderer: &mut Renderer,
                  debug: bool) {
        if debug {
            if let Some(bounding_box) = self.bbox(s) {
                match *bounding_box {
                    BoundingBox::Rectangle(ref rect) => {
                        renderer.set_draw_color(::sdl2::pixels::Color::RGB(230, 230, 230));
                        let (rx, ry) = viewport.relative_point((rect.x, rect.y));
                        let rect = Rect::new_unwrap(rx, ry, rect.w, rect.h);
                        renderer.fill_rect(rect);
                    }
                }
            }
        }

        let (rx, ry) = viewport.relative_point((rect.x, rect.y));
        let rect = Rect::new_unwrap(rx, ry, rect.w, rect.h);

        if let Some(animation) = self.anim_mut(s) {
            animation.render(renderer, rect);
        } else {
            panic!("Could not find animation");
        }
    }
}