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
use crate::sprite;
use openfile;
use std::fs;

#[derive(Clone)]
pub struct map {
    pub chars: Vec<String>,
    pub x: Vec<i64>,
    pub y: Vec<i64>,
}
impl map {
    pub fn to_sprite(&self) -> sprite::sprite {
        sprite::sprite {
            chars: self.chars.clone(),
            x: self.x.clone(),
            y: self.y.clone(),
        }
    }
    pub fn new() -> map {
        map {
            chars: Vec::new(),
            x: Vec::new(),
            y: Vec::new(),
        }
    }
    pub fn find_all_of_X(&self, ch: String) -> Vec<i64> {
        // returns a vector of the position of all instanses of a certain character
        let mut all: Vec<i64> = Vec::new();
        for x in 0..self.chars.len() {
            if self.chars[x] == ch {
                all.push(x as i64);
            }
        }
        all
    }
}
// this parses a text file into a map

pub fn load(filename: &str) -> map {
    let file = openfile::readFileLines(filename); // calls and returns what loadvec does
    loadvec(file)
}
pub fn loadvec(file: Vec<String>) -> map {
    let mut x: Vec<i64> = Vec::new();
    let mut y: Vec<i64> = Vec::new();
    let mut c: Vec<String> = Vec::new();
    let mut yy = 0;
    let mut xx = 0;

    for i in file {
        for ii in i.chars() {
            // ignore the spaces
            if ii != ' ' {
                x.push(xx);
                y.push(yy);
                c.push(ii.to_string())
            }
            xx += 1;
        }
        xx = 0;
        yy += 1;
    }

    map {
        chars: c,
        x: x,
        y: y,
    }
}
pub fn to_map(str: String) -> map {
    //makes a string into a map
    let sttr = str;
    let mut vec_str: Vec<String> = Vec::new();
    let vecsttr: Vec<&str> = sttr.split("\n").collect();
    for x in vecsttr {
        vec_str.push(x.to_string());
    }

    loadvec(vec_str)
}

pub struct folder {
    maps: Vec<map>,
    names: Vec<String>,
    meta: Vec<i8>,
}
impl folder {
    pub fn load_asset_map(&self, name: &str) -> Result<map, String> {
        // find and push the map

        for x in 0..self.names.len() {
            if self.names[x] == name {
                if self.meta[x] == 0 {
                    if self.maps[x].x.len() <= 0 {
                        return Err("Could not be found".to_string());
                    }
                    return Ok(self.maps[x].clone());
                }
            }
        }
        Err("Loading error".to_string())
    }
    pub fn load_asset_sprite(&self, name: &str) -> Result<sprite::sprite, String> {
        // find and push the sprite
        for x in 0..self.names.len() {
            if self.names[x] == name {
                if self.meta[x] == 1 {
                    if self.maps[x].x.len() <= 0 {
                        return Err("Could not be found".to_string());
                    }
                    return Ok(self.maps[x].to_sprite());
                }
            }
        }
        Err("Loading error".to_string())
    }
}

pub fn load_from_folder(directory: String, prefix_map: String, prefix_sprite: String) -> folder {
    let mut folderpr = folder {
        maps: Vec::new(),
        names: Vec::new(),
        meta: Vec::new(),
    };
    for entry in fs::read_dir(directory).expect("Error reading folder") {
        // parses the folder into a folder struct
        let entry = entry
            .expect("error")
            .path()
            .into_os_string()
            .into_string()
            .expect("error");
        let entry_n = entry.split("/").collect::<Vec<&str>>(); //temp parse var
        let entry_n = entry_n[entry_n.len() - 1].split(".").collect::<Vec<&str>>(); // temp parse var

        let entry_name = entry_n[0]; //final name

        if entry.contains(&prefix_map) {
            //finds all map elements
            folderpr.maps.push(load(&entry.clone()));
            folderpr.names.push(entry_name.to_string());
            folderpr.meta.push(0);
        }
        if entry.contains(&prefix_sprite) {
            // finding all sprite elements
            folderpr.maps.push(load(&entry));
            folderpr.names.push(entry_name.to_string());
            folderpr.meta.push(1);
        }
    }
    folderpr
}