seperate files
This commit is contained in:
parent
589fe0e1c0
commit
18012c7223
3
data/save.json
Normal file
3
data/save.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"coordinates": ["13","23"]
|
||||||
|
}
|
@ -6,13 +6,13 @@
|
|||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''"'""'"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'""'""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
|
"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
|
||||||
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
|
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
|
||||||
@ -24,18 +24,18 @@
|
|||||||
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[
|
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[
|
||||||
'''''''';;;;;'''''''''''';;;;;;''''';;;;;''''''''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[
|
'''''''';;;;;'''''''''''';;;;;;''''';;;;;''''''''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[
|
||||||
'''''''';;;;;''''''''''''';;;;'''''';;;;;''''''';;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[
|
'''''''';;;;;''''''''''''';;;;'''''';;;;;''''''';;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[
|
||||||
''''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[
|
"'''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[
|
||||||
''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''''''''''''''''''''''';;;;;[[[[[[[[[[[
|
''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[
|
||||||
'''''''''''''''''''''''''''';;;;;;;''';;;''''''''''''''''''''''''''''''''''''''''''''';;;;;[[[[[[[[[[
|
'''''''''''''''"'''''''''''';;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[
|
||||||
'''''''''''''''''''''''''''''''';;;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''';;[[[[[[[[[[
|
'''''''''''''''''''''''''''''''';;;;;;;;"'"'''"''''"""''"'"''"''"''"'"'"'""'"''""'"'"'""';;[[[[[[[[[[
|
||||||
''''''''''''''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''''''''''''''''''''''';''[[[[[[[[
|
"''''''''''''''''"''''''''''''''';;;;;'""'"''"'""'''"'''''''''"'''"'"'""''"'"'''''"'''"'"';''[[[[[[[[
|
||||||
'''''''''''''''''''''''''''''''''';;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''';;''[[[[[[[
|
"''''''''''''''''''''''''''''''''';;;;;;;'"''''''''''''''"''''''''''''''''''''''''''"''"'';;''[[[[[[[
|
||||||
'''''''''''''''''''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''''''''''''''''''';''''''[[[
|
'''''''''''''''''''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''''''''''''''''''';''''''[[[
|
||||||
''''''''''''''''''''''''''''''''''''''''';;;''''''''''''''''''''''''''''''''''''''''''''''';''''''''[
|
''''''''''''''''''''''''''''''''''''''''';;;'''''''''''''''''''''''''''''''''''''''''''''"';''''''''[
|
||||||
'''''''''''''''''''''''''''''''''''''''~'';;;;''''''~''''''''''''''''''''''''''''''''''''';''''''''''
|
'''''''''''"'''''''''''''''''''''''''''~'';;;;''''''~''''''''''''''''''''''''''''''''''''';''''''''''
|
||||||
'''''''''''''''''''''''''''''''''''''''~'''';;''''''~'''''''''''''''''''''''''''''''''''';'''''''''''
|
'''''''''''''''''''''''''''''''''''''''~'''';;''''''~'''''''''''''''''''''''''''''''''''';'''''''''''
|
||||||
''''''''~''''''''''''''''''''''''''''''~''''';''''~~'''''''''''''''''''''''''''''''''''';;'''''''''''
|
"'''''''~''''''''''''''''''''''''''''''~''''';''''~~'''''''''''''''''''''''''''''''''''';;'''''''''''
|
||||||
''''''''~~~~~~~~~~''''''''''''''''''''~~''''';;''~~'''''''''''''''''''''''''''''''''''';;''''''''''''
|
''''''''~~~~~~~~~~''''''''''''''"'''''~~''''';;''~~'''''''''''''''''''''''''''''''''''';;''''''''''''
|
||||||
'''''''''''''''''~~~~'''''''''''''''''~''''''';'~~'''''''''''''''''''''''''''''''''''';''''''''''''''
|
'''''''''''''''''~~~~'''''''''''''''''~''''''';'~~'''''''''''''''''''''''''''''''''''';''''''''''''''
|
||||||
''''''''''''''''''''~~'''''''''''''''~~''''''';'~~''''''''''''''''''''''''''''''''''';'''''''''''''''
|
''''''''''''''''''''~~'''''''''''''''~~''''''';'~~''''''''''''''''''''''''''''''''''';'''''''''''''''
|
||||||
'''''''''''''''''''''~''''''''''''''~~~~~~~~~~%~~'''''''''''''''''''''''''''''''''''';'''''''''''''''
|
'''''''''''''''''''''~''''''''''''''~~~~~~~~~~%~~'''''''''''''''''''''''''''''''''''';'''''''''''''''
|
||||||
@ -45,14 +45,14 @@
|
|||||||
''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;'''''
|
''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;'''''
|
||||||
''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;''''
|
''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;''''
|
||||||
''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;;
|
''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;;
|
||||||
''''''''''''''''''''''~~'''''''''''''''''''''''';;;''''''''''''''''''''''''''';;;''''''''''''''''''''
|
''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;''''''''''''''''''''
|
||||||
'''''''''''''''''''''~~''''''''''''''''''''''''''';;;;;;;''''''''''''''''';;;;;''''''''''''''''''''''
|
'''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;''''''''''''''''''''''
|
||||||
'''''''''''''''''''~~~'''''''''''''''''''''''''''''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~
|
'''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~
|
||||||
''''''''''''''''''~~~'''''''''''''''''''''''''''''''''''';;'''''''''''''''''''''''''''''''''''''''''~
|
''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''''''''''''''''''''''''~
|
||||||
'''''''''''''''::~~~~''''''''''''''''''''''''''''''''''';;''''''''''''''''''''''''''''''''''''''''''~
|
'''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''''''''''''''''''''''''~
|
||||||
''''''''''''':::~~~~~:'''''''''''''''''''''''''''''''';;;;''''''''''''''''''''''''''''''''''''''''''~
|
''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;''''''''''''''''''''''''''''''''''''''''''~
|
||||||
:'''''''''''::::~~~~~~::'''''''''''''''''''''''''';;;;;;'''''''''''''''''''''''''''''''''''''''''''''
|
:'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;'''''''''''''''''''''''''''''''''''''''''''''
|
||||||
::::'''::::::~~~~;~~~~::::::'''''''''''''''';;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''''
|
::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;''''''''''''''''''''''''''''''''''''''''''''''''''
|
||||||
:::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|
:::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|
||||||
:::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''''''''~~~~~~~~~~~
|
:::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''''''''~~~~~~~~~~~
|
||||||
:~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~'''''
|
:~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~'''''
|
||||||
|
199
src/main.rs
199
src/main.rs
@ -1,11 +1,17 @@
|
|||||||
use std::fs; // For reading the map and save files
|
use std::io::{stdin,stdout,Write};
|
||||||
use colored::Colorize; // For coloring the output
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::process::{Command};
|
use std::process::{Command};
|
||||||
|
|
||||||
|
// Declare the modules from these files
|
||||||
|
mod parse_map; // parse_map.rs
|
||||||
|
mod output_map; // output_map.rs
|
||||||
|
|
||||||
|
const HELP_STRING: &str = "'help' = output's this help text
|
||||||
|
'travel' = travel to a given location
|
||||||
|
";
|
||||||
|
|
||||||
struct Coordinates {
|
struct Coordinates {
|
||||||
x: i16,
|
x: i16,
|
||||||
y: i16,
|
// y: i16,
|
||||||
z: i16,
|
z: i16,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,153 +21,19 @@ fn clear_screen()
|
|||||||
.status()
|
.status()
|
||||||
.expect("Failed to clear screen");
|
.expect("Failed to clear screen");
|
||||||
}
|
}
|
||||||
|
fn input() -> String{
|
||||||
// This function reads the map file and puts it into a 2d vector of integers, each integer
|
let mut s=String::new();
|
||||||
// Refers to a block (see top comment) and the function returns this vector
|
let _=stdout().flush();
|
||||||
fn parse_map(map: &str) -> Result<(Vec<Vec<char>>, Vec<Vec<char>>), u8>
|
stdin().read_line(&mut s).expect("Did not enter a correct string");
|
||||||
{
|
if let Some('\n')=s.chars().next_back() {
|
||||||
let mut ground_map: Vec<Vec<char>> = Vec::new(); // Initialises the Ground map vector
|
s.pop();
|
||||||
let mut above_map: Vec<Vec<char>> = Vec::new(); // Initialises the Above Ground map vector
|
|
||||||
let ground_file = format!("data/{}_ground.map",map);
|
|
||||||
let above_file = format!("data/{}_above.map",map);
|
|
||||||
|
|
||||||
// Read the ground map file
|
|
||||||
if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file)
|
|
||||||
{
|
|
||||||
let ground_file_contents = parsed_ground_file_contents;
|
|
||||||
let ground_rows = ground_file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows
|
|
||||||
|
|
||||||
// Loop over each row to parse each column of it
|
|
||||||
for row in 0..ground_rows.len()
|
|
||||||
{
|
|
||||||
// Turn each column into an integer and collect it into a row vector
|
|
||||||
let current_row = ground_rows[row].chars().collect();
|
|
||||||
ground_map.push(current_row); // Then push this vector to the full map vector
|
|
||||||
}
|
}
|
||||||
|
if let Some('\r')=s.chars().next_back() {
|
||||||
|
s.pop();
|
||||||
}
|
}
|
||||||
else
|
return s;
|
||||||
{
|
|
||||||
eprintln!("Error: Could not read file: {}", ground_file);
|
|
||||||
return Err(1)
|
|
||||||
}
|
|
||||||
// Read the above ground map file
|
|
||||||
if let Ok(parsed_above_file_contents) = fs::read_to_string(&above_file)
|
|
||||||
{
|
|
||||||
let above_file_contents = parsed_above_file_contents;
|
|
||||||
let above_rows = above_file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows
|
|
||||||
|
|
||||||
// Loop over each row to parse each column of it
|
|
||||||
for row in 0..above_rows.len()
|
|
||||||
{
|
|
||||||
// Turn each column into an integer and collect it into a row vector
|
|
||||||
let current_row = above_rows[row].chars().collect();
|
|
||||||
above_map.push(current_row); // Then push this vector to the full map vector
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
eprintln!("Error: Could not read file: {}", ground_file);
|
|
||||||
return Err(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok((ground_map, above_map))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output the map based on map vector
|
|
||||||
fn output_map(ground_map: Vec<Vec<char>>, above_map: Vec<Vec<char>>, player_coordinates: Coordinates, distance_you_can_see: [i16; 2])
|
|
||||||
{
|
|
||||||
let blocks: HashMap<char, [u8; 3]> =
|
|
||||||
[
|
|
||||||
('X', [255,0, 0]), // Null
|
|
||||||
('\'', [0, 255,0]), // Grass
|
|
||||||
('"', [153,102,0]), // Dirt
|
|
||||||
(';', [128,128,128]), // Cobbles
|
|
||||||
('~', [0, 0, 255]), // Water/sea
|
|
||||||
('D', [0, 128,128]), // Diamond
|
|
||||||
('C', [0, 0, 0]), // Coal
|
|
||||||
('i', [138,74 ,24]), // Iron
|
|
||||||
('%', [195,162,103]), // Wood planks
|
|
||||||
(':', [195,162,103]), // Sand
|
|
||||||
('[', [196,196,196]), // Stone
|
|
||||||
('^', [196,128,196]), // Mountain
|
|
||||||
('8', [255,196,196]), // Stone bricks
|
|
||||||
('♣', [0, 153,51]), // Oak tree
|
|
||||||
]
|
|
||||||
.iter().cloned().collect();
|
|
||||||
|
|
||||||
// Loop over array and print each block
|
|
||||||
for i in player_coordinates.z - distance_you_can_see[1]..player_coordinates.z + distance_you_can_see[1]
|
|
||||||
{
|
|
||||||
if i >= 0 && i < ground_map.len() as i16
|
|
||||||
{
|
|
||||||
for j in player_coordinates.x - distance_you_can_see[0]..player_coordinates.x+distance_you_can_see[0]
|
|
||||||
{
|
|
||||||
if j >= 0 && j < ground_map[i as usize].len() as i16
|
|
||||||
{
|
|
||||||
let character: char = ground_map[i as usize][j as usize];
|
|
||||||
let above_character: char = above_map[i as usize][j as usize];
|
|
||||||
// Check if the character is at the current coordinates
|
|
||||||
if i == player_coordinates.z && j == player_coordinates.x
|
|
||||||
{
|
|
||||||
print!
|
|
||||||
(
|
|
||||||
"{}",
|
|
||||||
" "
|
|
||||||
.on_truecolor(40,235,181)
|
|
||||||
.truecolor(0,0,0)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// Else check if there's something on the above y level
|
|
||||||
else if blocks.contains_key(&above_character) && above_character != ' '
|
|
||||||
{
|
|
||||||
print!
|
|
||||||
(
|
|
||||||
"{}",
|
|
||||||
above_character // Output's the current block
|
|
||||||
.to_string()
|
|
||||||
.truecolor(
|
|
||||||
blocks.get(&above_character).unwrap()[0],
|
|
||||||
blocks.get(&above_character).unwrap()[1],
|
|
||||||
blocks.get(&above_character).unwrap()[2]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// Else print what is below (the ground)
|
|
||||||
else if blocks.contains_key(&character)
|
|
||||||
{
|
|
||||||
print!
|
|
||||||
(
|
|
||||||
"{}",
|
|
||||||
character // Output's the current block
|
|
||||||
.to_string()
|
|
||||||
.truecolor(
|
|
||||||
blocks.get(&character).unwrap()[0],
|
|
||||||
blocks.get(&character).unwrap()[1],
|
|
||||||
blocks.get(&character).unwrap()[2]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// If the character is invalid print a null value
|
|
||||||
else
|
|
||||||
{
|
|
||||||
print!
|
|
||||||
(
|
|
||||||
"{}",
|
|
||||||
"X"
|
|
||||||
.to_string()
|
|
||||||
.truecolor(
|
|
||||||
blocks.get(&'X').unwrap()[0],
|
|
||||||
blocks.get(&'X').unwrap()[1],
|
|
||||||
blocks.get(&'X').unwrap()[2]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println!("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main()
|
fn main()
|
||||||
{
|
{
|
||||||
@ -182,23 +54,38 @@ fn main()
|
|||||||
let player_coordinates: Coordinates = Coordinates
|
let player_coordinates: Coordinates = Coordinates
|
||||||
{
|
{
|
||||||
x: 30,
|
x: 30,
|
||||||
y: 1,
|
//y: 1,
|
||||||
z: 30,
|
z: 30,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok((ground_map, above_map)) = parse_map(map)
|
if let Ok((ground_map, above_map)) = parse_map::parse_map(map) // Call the parse map function from parse_map.rs
|
||||||
{ // Parse the map file into a vector
|
{ // Parse the map file into a vector
|
||||||
|
output_map::output_map // Call output_map fuctino from output_map.rs
|
||||||
|
(
|
||||||
|
&ground_map,
|
||||||
|
&above_map,
|
||||||
|
&player_coordinates,
|
||||||
|
&distance_you_can_see
|
||||||
|
); // Output the map
|
||||||
|
'game_loop: loop
|
||||||
|
{
|
||||||
if ! debug_mode
|
if ! debug_mode
|
||||||
{
|
{
|
||||||
clear_screen()
|
clear_screen()
|
||||||
}
|
}
|
||||||
output_map
|
print!("> ");
|
||||||
(
|
let user_input: String = input();
|
||||||
ground_map,
|
if user_input == "help"
|
||||||
above_map,
|
{
|
||||||
player_coordinates,
|
print!("\n{}\n", HELP_STRING);
|
||||||
distance_you_can_see
|
}
|
||||||
); // Output the map
|
else if user_input == "exit"
|
||||||
|
{
|
||||||
|
// TO DO: SAVE
|
||||||
|
print!("Exiting...\n");
|
||||||
|
break 'game_loop;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
100
src/output_map.rs
Normal file
100
src/output_map.rs
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
use colored::Colorize; // For coloring the output
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::Coordinates;
|
||||||
|
|
||||||
|
// Output the map based on map vector
|
||||||
|
pub fn output_map(ground_map: &Vec<Vec<char>>, above_map: &Vec<Vec<char>>, player_coordinates: &Coordinates, distance_you_can_see: &[i16; 2])
|
||||||
|
{
|
||||||
|
let blocks: HashMap<char, [u8; 3]> =
|
||||||
|
[
|
||||||
|
('X', [255,0, 0]), // Null
|
||||||
|
('\'', [0, 255,0]), // Grass
|
||||||
|
('"', [153,102,0]), // Dirt
|
||||||
|
(';', [128,128,128]), // Cobbles
|
||||||
|
('~', [0, 0, 255]), // Water/sea
|
||||||
|
('D', [0, 128,128]), // Diamond
|
||||||
|
('C', [0, 0, 0]), // Coal
|
||||||
|
('i', [138,74 ,24]), // Iron
|
||||||
|
('%', [195,162,103]), // Wood planks
|
||||||
|
(':', [195,162,103]), // Sand
|
||||||
|
('[', [196,196,196]), // Stone
|
||||||
|
('^', [196,128,196]), // Mountain
|
||||||
|
('8', [255,196,196]), // Stone bricks
|
||||||
|
('♣', [0, 153,51]), // Oak tree
|
||||||
|
]
|
||||||
|
.iter().cloned().collect();
|
||||||
|
|
||||||
|
// Loop over array and print each block
|
||||||
|
for i in player_coordinates.z - distance_you_can_see[1]..player_coordinates.z + distance_you_can_see[1]
|
||||||
|
{
|
||||||
|
if i >= 0 && i < ground_map.len() as i16
|
||||||
|
{
|
||||||
|
for j in player_coordinates.x - distance_you_can_see[0]..player_coordinates.x+distance_you_can_see[0]
|
||||||
|
{
|
||||||
|
if j >= 0 && j < ground_map[i as usize].len() as i16
|
||||||
|
{
|
||||||
|
let character: char = ground_map[i as usize][j as usize];
|
||||||
|
let above_character: char = above_map[i as usize][j as usize];
|
||||||
|
// Check if the character is at the current coordinates
|
||||||
|
if i == player_coordinates.z && j == player_coordinates.x
|
||||||
|
{
|
||||||
|
print!
|
||||||
|
(
|
||||||
|
"{}",
|
||||||
|
" "
|
||||||
|
.on_truecolor(40,235,181)
|
||||||
|
.truecolor(0,0,0)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Else check if there's something on the above y level
|
||||||
|
else if blocks.contains_key(&above_character) && above_character != ' '
|
||||||
|
{
|
||||||
|
print!
|
||||||
|
(
|
||||||
|
"{}",
|
||||||
|
above_character // Output's the current block
|
||||||
|
.to_string()
|
||||||
|
.truecolor(
|
||||||
|
blocks.get(&above_character).unwrap()[0],
|
||||||
|
blocks.get(&above_character).unwrap()[1],
|
||||||
|
blocks.get(&above_character).unwrap()[2]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Else print what is below (the ground)
|
||||||
|
else if blocks.contains_key(&character)
|
||||||
|
{
|
||||||
|
print!
|
||||||
|
(
|
||||||
|
"{}",
|
||||||
|
character // Output's the current block
|
||||||
|
.to_string()
|
||||||
|
.truecolor(
|
||||||
|
blocks.get(&character).unwrap()[0],
|
||||||
|
blocks.get(&character).unwrap()[1],
|
||||||
|
blocks.get(&character).unwrap()[2]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// If the character is invalid print a null value
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print!
|
||||||
|
(
|
||||||
|
"{}",
|
||||||
|
"X"
|
||||||
|
.to_string()
|
||||||
|
.truecolor(
|
||||||
|
blocks.get(&'X').unwrap()[0],
|
||||||
|
blocks.get(&'X').unwrap()[1],
|
||||||
|
blocks.get(&'X').unwrap()[2]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
src/parse_map.rs
Normal file
52
src/parse_map.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
use std::fs; // For reading the map and save files
|
||||||
|
|
||||||
|
// This function reads the map file and puts it into a 2d vector of integers, each integer
|
||||||
|
// Refers to a block (see top comment) and the function returns this vector
|
||||||
|
pub fn parse_map(map: &str) -> Result<(Vec<Vec<char>>, Vec<Vec<char>>), u8>
|
||||||
|
{
|
||||||
|
let mut ground_map: Vec<Vec<char>> = Vec::new(); // Initialises the Ground map vector
|
||||||
|
let mut above_map: Vec<Vec<char>> = Vec::new(); // Initialises the Above Ground map vector
|
||||||
|
let ground_file = format!("data/{}_ground.map",map);
|
||||||
|
let above_file = format!("data/{}_above.map",map);
|
||||||
|
|
||||||
|
// Read the ground map file
|
||||||
|
if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file)
|
||||||
|
{
|
||||||
|
let ground_file_contents = parsed_ground_file_contents;
|
||||||
|
let ground_rows = ground_file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows
|
||||||
|
|
||||||
|
// Loop over each row to parse each column of it
|
||||||
|
for row in 0..ground_rows.len()
|
||||||
|
{
|
||||||
|
// Turn each column into an integer and collect it into a row vector
|
||||||
|
let current_row = ground_rows[row].chars().collect();
|
||||||
|
ground_map.push(current_row); // Then push this vector to the full map vector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eprintln!("Error: Could not read file: {}", ground_file);
|
||||||
|
return Err(1)
|
||||||
|
}
|
||||||
|
// Read the above ground map file
|
||||||
|
if let Ok(parsed_above_file_contents) = fs::read_to_string(&above_file)
|
||||||
|
{
|
||||||
|
let above_file_contents = parsed_above_file_contents;
|
||||||
|
let above_rows = above_file_contents.split("\n").collect::<Vec<_>>(); // Split the file contents into rows
|
||||||
|
|
||||||
|
// Loop over each row to parse each column of it
|
||||||
|
for row in 0..above_rows.len()
|
||||||
|
{
|
||||||
|
// Turn each column into an integer and collect it into a row vector
|
||||||
|
let current_row = above_rows[row].chars().collect();
|
||||||
|
above_map.push(current_row); // Then push this vector to the full map vector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eprintln!("Error: Could not read file: {}", ground_file);
|
||||||
|
return Err(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok((ground_map, above_map))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user