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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
pub struct Map {
map_data:Vec<char>,
width:u32,
height:u32,
upper_text:String,
lower_text:String,
}
impl Map {
pub fn new(w:u32, h:u32) -> Map {
let mut local_data = Vec::<char>::new();
for i in 0..w {
for j in 0..h {
local_data.push(
if i == 0 || j == 0 || (i == (w - 1)) || (j == (h - 1)) {
'*'
} else {
' '
}
);
}
}
Map{
map_data:local_data,
width:w,
height:h,
upper_text:String::new(),
lower_text:String::new(),
}
}
pub fn update(&mut self) -> &mut Map {
if self.upper_text.len() > 0 {
println!("");
println!("{}", self.upper_text);
println!("");
}
for i in 0..self.width {
for j in 0..self.height {
print!("{} ", self.map_data[(j * self.width + i) as usize]);
}
println!("");
}
if self.lower_text.len() > 0 {
println!("");
println!("{}", self.lower_text);
println!("");
}
self
}
pub fn clear(&mut self) -> &mut Map {
for i in 0..self.width {
for j in 0..self.height {
self.map_data[(j * self.width + i) as usize] =
if i == 0 || j == 0 || (i == (self.width - 1)) || (j == (self.height - 1)) {
'*'
} else {
' '
}
;
}
}
self
}
pub fn add_sprite(&mut self, opts:((u32, u32), char)) -> &mut Map {
let x = (opts.0).0;
let y = (opts.0).1;
let s = opts.1;
self.map_data[(y * self.width + x) as usize] = s;
self
}
pub fn remove_sprite(&mut self, pos:(u32, u32)) -> &mut Map {
self.map_data[(pos.1 * self.width + pos.0) as usize] = ' ';
self
}
pub fn move_sprite(&mut self, pos:(u32, u32), new_pos:(u32, u32)) -> &mut Map {
let x = pos.0;
let y = pos.1;
let new_x = new_pos.0;
let new_y = new_pos.1;
self.map_data[(new_y * self.width + new_x) as usize] = self.map_data[(y * self.width + x) as usize];
self.map_data[(y * self.width + x) as usize] = ' ';
self
}
pub fn get_width(&self) -> u32 {
self.width
}
pub fn get_height(&self) -> u32 {
self.height
}
pub fn set_uppertext<S>(&mut self, utext:S) -> &mut Map where S:Into<String> {
self.upper_text = utext.into();
self
}
pub fn set_lowertext<S>(&mut self, ltext:S) -> &mut Map where S:Into<String> {
self.lower_text = ltext.into();
self
}
pub fn get_uppertext(&self) -> String {
self.upper_text.clone()
}
pub fn get_lowertext(&self) -> String {
self.lower_text.clone()
}
}