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

use crate::animations::{Animation, Frame};
use crate::constants;
use crate::property::Property;
use crate::xmlelements::XMLElements;

pub struct Tileset {
    tiles: HashMap<usize, Frame>,
    properties: Vec<Property>,
}

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, Frame::default());

        let w = 1.0 / columns as f32;
        let h = 1.0 / rows as f32;
        let mut key = 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;
                tiles.insert(key, Frame::new(Rect::new(x, y, w, h), None, 0.0));
                key += 1;
            }
        }

        let mut properties = Vec::new();

        for tile_element in elements.get_elements("tile") {
            let tile_id = XMLElements::get_attribute(&tile_element, "id")
                .unwrap()
                .parse::<usize>()
                .unwrap()
                + 1;

            let property_elements = elements.get_children(&tile_element, "property");

            properties.push(Property::new(tile_id, property_elements));
        }

        let invisible: Vec<usize> = properties
            .iter()
            .filter(|p| p.visible == Some(false))
            .map(|p| p.tile_id)
            .collect();

        for i in invisible {
            *tiles.get_mut(&i).unwrap() = Frame::default();
        }

        for tile in &tiles {}

        Tileset { tiles, properties }
    }

    pub fn get_spawn_tiles(&self) -> Vec<(String, usize)> {
        self.properties
            .clone()
            .into_iter()
            .filter(|p| p.spawn.is_some())
            .map(|p| (p.spawn.unwrap(), p.tile_id))
            .collect()
    }

    /*
    pub fn get_frames(&self, tile_id: usize) -> Vec<Frame> {
        if let Some(property) = self.properties.iter().find(|p| p.tile_id == tile_id) {
            self.properties
                .clone()
                .into_iter()
                .filter(|p| p.entity == property.entity && p.entity.is_some())
                .map(|p| Frame::new(*self.tiles.get(&p.tile_id).unwrap(), p.delay, 0.0))
                .collect()
        } else {
            Vec::new()
        }
    }
    */

    pub fn get_animation(&self, tile_id: usize) -> Animation {
        Animation::default()
    }

    pub fn get_frame_by_entity_keyframe(&self, entity: &str, keyframe: usize) -> Frame {
        let tile_id = &self
            .properties
            .iter()
            .find(|p| p.entity == Some(entity.to_string()) && Some(keyframe) == p.keyframe)
            .unwrap()
            .tile_id;

        /*
        let delay = self
            .properties
            .iter()
            .find(|p| p.tile_id == *tile_id && p.delay.is_some())
            .unwrap()
            .delay;

        Frame::new(*source, delay, 0.0)
        */

        self.tiles.get(tile_id).unwrap().clone()
    }
}