summaryrefslogtreecommitdiff
path: root/src/math.rs
blob: c747ccea12de396659a95a998639d2f0dd67de4c (plain)
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
use ggez::graphics::Rect;
use std::f32::consts::PI;
use std::time::Instant;

pub fn convert_angle_to_rad(angle: f32) -> f32 {
    angle * (PI / 180.0)
}

pub fn flip(rect: Rect) -> Rect {
    let mut r = rect;
    r.x *= -1.0;
    r.x -= rect.w;
    r
}

pub fn next_source(source: Rect, animation: &[(usize, Rect)], timer: Instant) -> (Rect, Instant) {
    if let Some(mut i) = animation.iter().position(|a| a.1 == source) {
        if timer.elapsed().as_millis() > animation[i].0 as u128 {
            i = if i == animation.len() - 1 { 0 } else { i + 1 };
            (animation[i].1, Instant::now())
        } else {
            (source, timer)
        }
    } else if !animation.is_empty() {
        (animation[0].1, timer)
    } else {
        (source, timer)
    }
}