summaryrefslogtreecommitdiff
path: root/src/tileset.rs
blob: bb64c1290defc803d13de0e27ab28744b1ce93b7 (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
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
use ggez::filesystem::File;
use ggez::graphics::Rect;
use std::collections::HashMap;
use std::f32::consts::PI;

use crate::animations::Animation;
use crate::constants::{self, FLIP_A, FLIP_D, FLIP_H, FLIP_V};
use crate::tile::{Properties, Tile};
use crate::xmlelements::XMLElements;

pub struct Tileset {
    tiles: HashMap<usize, Tile>,
}

impl Tileset {
    pub fn new(file: File) -> Tileset {
        let elements = XMLElements::new(file);

        let height = elements
            .get_element_attribute("image", "height")
            .unwrap()
            .parse::<usize>()
            .unwrap();
        let columns = elements
            .get_element_attribute("tileset", "columns")
            .unwrap()
            .parse::<usize>()
            .unwrap();

        let rows = height / (constants::TILE_HEIGHT as usize);

        let mut tiles = HashMap::new();
        tiles.insert(0, Tile::default());

        let w = 1.0 / columns as f32;
        let h = 1.0 / rows as f32;
        let mut id = 1;
        for r in 0..rows {
            for c in 0..columns {
                let x = c as f32 / columns as f32;
                let y = r as f32 / rows as f32;

                let tile_element = elements.get_elements("tile").into_iter().find(|e| {
                    XMLElements::get_attribute(e, "id")
                        .unwrap()
                        .parse::<usize>()
                        .unwrap()
                        + 1
                        == id
                });

                let properties = match tile_element {
                    Some(tile_element) => {
                        Properties::new(elements.get_children(&tile_element, "property"))
                    }
                    None => Properties::default(),
                };

                tiles.insert(id, Tile::new(Rect::new(x, y, w, h), properties));
                id += 1;
            }
        }

        for (id, tile) in tiles.clone().into_iter() {
            for i in 1..8 {
                let (new_id, new_tile) = match i {
                    1 => ((id | FLIP_H), tile.clone()),
                    2 => ((id | FLIP_V), flip(tile.clone())),
                    3 => ((id | FLIP_D), tile.clone()),
                    4 => ((id | FLIP_D | FLIP_H), tile.clone()),
                    5 => ((id | FLIP_D | FLIP_V), tile.clone()),
                    6 => ((id | FLIP_H | FLIP_V), tile.clone()),
                    7 => ((id | FLIP_A), tile.clone()),
                    _ => (0, Tile::default()),
                };

                if new_id != 0 {
                    tiles.insert(new_id, new_tile);
                }
            }
        }

        Tileset { tiles }
    }

    pub fn get_spawn_tiles(&self) -> HashMap<usize, Tile> {
        self.tiles
            .clone()
            .into_iter()
            .filter(|(_, t)| t.properties.spawn.is_some())
            .collect()
    }

    pub fn get_animation(&self, tile_id: usize) -> Animation {
        let first_tile = self
            .tiles
            .iter()
            .find(|(id, _)| id == &&tile_id)
            .unwrap()
            .1
            .clone();

        if first_tile.properties.entity.is_some() {
            Animation::new(
                self.tiles
                    .values()
                    .cloned()
                    .filter(|t| t.properties.entity == first_tile.properties.entity)
                    .collect(),
            )
        } else {
            Animation::new(vec![first_tile])
        }
    }

    pub fn get_tile_by_entity_keyframe(&self, entity: &str, keyframe: usize) -> Tile {
        self.tiles
            .values()
            .find(|t| {
                t.properties.entity == Some(entity.to_string())
                    && Some(keyframe) == t.properties.keyframe
            })
            .unwrap()
            .clone()
    }
}

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

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