better error handling on loading above map, TODOL: Optimise map parsing

This commit is contained in:
2025-02-21 04:07:50 +00:00
parent 2b809d7450
commit 4fceee3856
6 changed files with 52 additions and 31 deletions

View File

@@ -22,3 +22,19 @@ pub fn clear_screen()
.status()
.expect("Failed to clear screen");
}
pub fn read_element_from_map(vector: &Vec<Vec<char>>, index1: usize, index2: usize) -> Result<char, ()>
{
if vector.len() <= index1
{
Err(())
}
else if vector[index1].len() <= index2
{
Err(())
}
else
{
Ok(vector[index1][index2])
}
}

View File

@@ -55,7 +55,7 @@ fn main()
{ // Parse the map file into a vector
if DEBUG_MODE == false
{
function::clear_screen;
function::clear_screen();
}
output_map::output_map // Call output_map fuctino from output_map.rs
(

View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::Coordinates;
use crate::DEBUG_MODE;
use crate::function;
// Output the map based on map vector
pub fn output_map(
@@ -51,7 +51,11 @@ pub fn output_map(
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];
let mut above_character: char = ' ';
if let Ok(above_element) = function::read_element_from_map(&above_map, z as usize, x as usize)
{
above_character = above_element;
}
// Check if the character is at the current coordinates
if z == player_coordinates.z && x == player_coordinates.x
{
@@ -64,7 +68,7 @@ pub fn output_map(
)
}
// Else check if there's something on the above y level
else if blocks.contains_key(&above_character) && above_character != ' '
else if blocks.contains_key(&above_character) && above_character != ' '
{
print!
(
@@ -108,6 +112,7 @@ pub fn output_map(
)
)
}
}
}
println!("");