-
Notifications
You must be signed in to change notification settings - Fork 11
/
map.rs
105 lines (84 loc) · 3.39 KB
/
map.rs
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
use ::graphics::renderer::Renderer;
use ::graphics::colour::Colour;
use ::util::vector::Vector2D;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::fs;
pub const MAP_WIDTH: i32 = 100;
pub const MAP_HEIGHT: i32 = 50;
pub struct Map {
world_position: Vector2D<i32>,
tile_data: Vec<String>,
}
fn path_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}
impl Map {
/**
* Loads a map from a file for coordinates (x, y)
*/
pub fn load(x: i32, y: i32) -> Option<Map> {
let mut map = Map {
world_position: Vector2D::new(x, y),
tile_data: Vec::with_capacity((MAP_HEIGHT) as usize)
};
let mut file_name = String::from("maps/");
file_name.push_str(x.to_string().as_str());
file_name.push(' ');
file_name.push_str(y.to_string().as_str());
if !path_exists(&file_name) {
None//panic!("Path for map '{}' does not exist", file_name);
}
else {
let file = File::open(file_name)
.expect(&format!("Unable to open file for map {} {}", x, y));
for line in BufReader::new(file).lines() {
map.tile_data.push(line.unwrap());
if map.tile_data.len() == MAP_HEIGHT as usize {
break;
}
}
Some(map)
}
}
pub fn draw_line(&self, renderer: &Renderer, line: usize, begin: usize, end: usize, draw_point: &Vector2D<i32>) {
let mut render_string = String::with_capacity((MAP_WIDTH * 2) as usize);
let ref_string = &self.tile_data[line];
//Set colour based on the batch of following chars
//Rust doesn't have static/global objects as far as I know, so I have to implement using a match
let mut cur_char = ' ';
for c in ref_string[begin..end].chars() {
if c != cur_char {
cur_char = c;
match c {
',' => render_string.push_str(&Colour::ansi_text_colour_string (14, 160, 20)),
'|' => render_string.push_str(&Colour::ansi_text_colour_string (30, 145, 35)),
'.' => render_string.push_str(&Colour::ansi_text_colour_string (124, 252, 0)),
'#' => render_string.push_str(&Colour::ansi_text_colour_string (34, 100, 34)),
'0' => render_string.push_str(&Colour::ansi_text_colour_string (34, 100, 34)),
'Y' => render_string.push_str(&Colour::ansi_text_colour_string (160, 82, 45)),
'~' => render_string.push_str(&Colour::ansi_text_colour_string (32, 178, 230)),
_ => {}
}
}
render_string.push(c);
}
renderer.draw_string("game",
&render_string,
&draw_point);
}
pub fn get_tile(&self, x: i32, y: i32) -> char {
let line = &self.tile_data[y as usize];
//rust is annoying with finding the char of a string via index, so only way i can think of is to literally iterate over the string
for (i, c) in line.chars().enumerate() {
if i == x as usize {
return c
}
}
//failure to find the index (which cannot happen) results in returning a .
'.'
}
pub fn world_position(&self) -> &Vector2D<i32> {
&self.world_position
}
}