summaryrefslogtreecommitdiff
path: root/src/tile.rs
blob: 9e92fef77042c310e5498cef8536812533378a24 (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
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
use ggez::graphics::Rect;
use std::f32::consts::PI;
use xml::reader::XmlEvent;

use crate::xmlelements::XMLElements;

#[derive(Clone, Debug, PartialEq)]
pub struct Tile {
    pub source: Rect,
    pub properties: Properties,
}

impl Tile {
    pub fn new(source: Rect, properties: Properties) -> Tile {
        Tile { source, properties }
    }
}

impl Default for Tile {
    fn default() -> Tile {
        Tile::new(Rect::zero(), Properties::default())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Properties {
    pub entity: Option<String>,
    pub rotation: f32,
    pub keyframe: Option<usize>,
    pub delay: Option<usize>,
    pub spawn: Option<String>,
    pub visible: Option<bool>,
}

impl Properties {
    pub fn new(properties_elements: Vec<XmlEvent>) -> Properties {
        let entity = match XMLElements::get_attribute_value(&properties_elements, "entity") {
            Ok(entity) => entity.parse().ok(),
            Err(_) => None,
        };
        let keyframe = match XMLElements::get_attribute_value(&properties_elements, "keyframe") {
            Ok(keyframe) => keyframe.parse().ok(),
            Err(_) => None,
        };
        let delay = match XMLElements::get_attribute_value(&properties_elements, "delay") {
            Ok(delay) => delay.parse().ok(),
            Err(_) => None,
        };
        let spawn = XMLElements::get_attribute_value(&properties_elements, "spawn").ok();
        let visible = match XMLElements::get_attribute_value(&properties_elements, "visible") {
            Ok(visible) => visible.parse().ok(),
            Err(_) => None,
        };

        Properties {
            rotation: 0.0,
            entity,
            keyframe,
            delay,
            spawn,
            visible,
        }
    }
}

impl Default for Properties {
    fn default() -> Properties {
        Properties {
            rotation: 0.0,
            entity: None,
            keyframe: None,
            delay: None,
            spawn: None,
            visible: None,
        }
    }
}

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

pub fn flip(tile: Tile) -> Tile {
    let mut t = tile.clone();
    t.source.x *= -1.0;
    t.source.x -= t.source.w;
    t
}

pub fn rotate(tile: Tile, angle: f32) -> Tile {
    let mut t = tile.clone();
    t.properties.rotation = convert_angle_to_rad(angle);
    t
}