castlecore/
core.rs

1use std::{fs::{File, self}, io::{BufReader, BufRead}};
2
3use crate::render::{self, *};
4
5/// Load map (return this map, then use this not at loop!)
6pub fn load_map(map: render::MapLayer) -> Vec<String> {
7    let mut map_path: String = String::from("map/default.cc_map");
8
9    match map {
10        MapLayer::Base => map_path = String::from("map/base.cc_map"),
11        MapLayer::Color => map_path = String::from("map/color.cc_map"),
12        MapLayer::Trigger => map_path = String::from("map/trigger.cc_map"),
13        MapLayer::Wall => map_path = String::from("map/wall.cc_map"),
14        MapLayer::SumObj(ref object) => {
15            match object {
16                Summon::Static => map_path = String::from("map/summon/static.cc_map"),
17                Summon::Dynamic => map_path = String::from("map/summon/dynamic.cc_map"),
18                Summon::NPC => map_path = String::from("map/summon/npc.cc_map"),
19                Summon::Enemy => map_path = String::from("map/summon/enemy.cc_map")
20            }   
21        }
22        MapLayer::Explore => map_path = String::from("map/explore.cc_map")
23    }
24
25    let map_path_to_file = File::open(&map_path);
26
27    let map_file = match map_path_to_file {
28        Ok(mfile) => mfile,
29        Err(error) => match error.kind() {
30            std::io::ErrorKind::NotFound => match File::create(&map_path) {
31                Ok(fc) => fc,
32                Err(e) => panic!("Problem creating the file: {:?}", e)
33            },
34            other_error => panic!("Problem opening the file: {:?}", other_error)
35        }
36    };
37
38    let reader = BufReader::new(&map_file);
39
40
41    let mut loaded_map: Vec<String> = Vec::new();
42
43    let mut line_num = 1;
44
45    for line in reader.lines() {
46
47        match line {
48            Ok(line) => loaded_map.push(line),
49            Err(_) => break
50        }
51
52        line_num = line_num + 1;
53    };
54
55    return loaded_map;
56
57}
58
59/// Create all dirs for correct engine work
60pub fn initpath() {
61    match fs::create_dir("map") {
62        Err(error) => match error.kind() {
63            std::io::ErrorKind::AlreadyExists => {},
64            e => panic!("Problem create dir {:?}", e)
65        },
66        Ok(_) => {},
67    }
68    match fs::create_dir("map/summon") {
69        Err(error) => match error.kind() {
70            std::io::ErrorKind::AlreadyExists => {},
71            e => panic!("Problem create dir {:?}", e)
72        },
73        Ok(_) => {},
74    }
75}