117 lines
3.1 KiB
Rust
117 lines
3.1 KiB
Rust
use colored::Colorize; // For coloring the output
|
|
use std::collections::HashMap;
|
|
|
|
use crate::Coordinates;
|
|
use crate::DEBUG_MODE;
|
|
|
|
|
|
// Output the map based on map vector
|
|
pub fn output_map(
|
|
ground_map: &Vec<Vec<char>>,
|
|
above_map: &Vec<Vec<char>>,
|
|
player_coordinates: &Coordinates,
|
|
)
|
|
{
|
|
let (screen_width, screen_height) = termion::terminal_size().unwrap();
|
|
let distance_you_can_see: [i16; 2] =
|
|
[
|
|
(screen_width as f32 / 2.0) as i16,
|
|
(screen_height as f32 / 2.0) as i16
|
|
];
|
|
if DEBUG_MODE == true
|
|
{
|
|
println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height);
|
|
}
|
|
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
|
|
('#', [255,196,196]), // Stone bricks
|
|
('♣', [0, 153,51]), // Oak tree
|
|
]
|
|
.iter().cloned().collect();
|
|
|
|
// Loop over array and print each block
|
|
for z in player_coordinates.z - distance_you_can_see[1]..player_coordinates.z + distance_you_can_see[1]
|
|
{
|
|
if z >= 0 && z < ground_map.len() as i16
|
|
{
|
|
for x in player_coordinates.x - distance_you_can_see[0]..player_coordinates.x+distance_you_can_see[0]
|
|
{
|
|
if x >= 0 && x < ground_map[z as usize].len() as i16
|
|
{
|
|
let character: char = ground_map[z as usize][x as usize];
|
|
let above_character: char = above_map[z as usize][x as usize];
|
|
// Check if the character is at the current coordinates
|
|
if z == player_coordinates.z && x == 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!("");
|
|
}
|
|
}
|
|
}
|