saving and loading save files

This commit is contained in:
deadvey 2025-02-15 02:21:33 +00:00
parent 18012c7223
commit bda48d9718
8 changed files with 194 additions and 115 deletions

View File

@ -6,3 +6,5 @@ edition = "2021"
[dependencies] [dependencies]
colored = "2.2.0" colored = "2.2.0"
termion = "4.0.3" termion = "4.0.3"
serde = { version = "1.0.127", features = ["derive"] }
serde_json = "1.0"

View File

@ -1,3 +1,26 @@
{ {
"coordinates": ["13","23"] "name": "dave",
"skills": {
"woodcutting": 0,
"mining": 0
},
"coordinates": {
"x": 30,
"z": 30,
"map": "world"
},
"inventory": [
[
"axe",
1
],
[
"cigarette",
5
],
[
"cp",
3
]
]
} }

View File

@ -3,30 +3,30 @@
8 8888888888888888888888888 8 ^^^^^^^^^^^^^^^^^^^^^^^^^ 8 8888888888888888888888888 8 ^^^^^^^^^^^^^^^^^^^^^^^^^
8 8 ^^^^^^^^^^^^^^^^^^^^^^ 8 8 ^^^^^^^^^^^^^^^^^^^^^^
88888888 88888888 888 888888 ^^^^^^^^^^^^^^^^^^^^^ 88888888 88888888 888 888888 ^^^^^^^^^^^^^^^^^^^^^
8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^ 8 8 8 8 8 8888 88 ^^^^^^^^^^^^^^^^^^^^^^
8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ 8 8 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^
8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ 88888888 8 8 8 8 8 88888 ^^^^^^^^^^^^^^^^^^^^^^^
8 8 8 8 ♣ ♣ ^^^^^^^^^^^^^^^^^^^^^^^ 8 8 8 8 8 ♣ 8 8 8 ♣ ^^^^^^^^^^^^^^^^^^^^^^^
8888888888888888 88888888888 ^^^^^^^^^^^^^^^^^^ ^^ 8 8888888888888888 88888888888 8 8 ^^^^^^^^^^^^^^^^^^ ^^
8 8 ^^^^^^^^^^^^^^ ^^^ 8 8 8 888888888 ^^^^^^^^^^^^^^ ^^^
8 8888888 88888888 88888888 ^^^^^^^^^^^ ^^^^^ 88888888 8888888 88888888 88888888 ^^^^^^^^^^^ ^^^^^
8 8 8 8 8 8 8 ♣ ^^^^^^^ ^^^ 8 8 8 8 8 8 8 ♣ ^^^^^^^ ^^^
8 8 8 8 8 8 ^^^^ 8 8 8 8 8 8888888 ^^^^
♣ 8 8 8 88888888 8 ♣ ^^ ♣ 8 8 8 88888888 8 8 ♣ ^^
8 88888 8 8 8 ^ 8 88888 8 8 8 8 ^
8 8888 8 88 88888888 ^^ 8 8888 8 88 88888888 ^^
8 88888 8 8 8 8 8 ^^^ 8 88888 8 8 8 8 8 8 ^^^
8 8 8 8 8 8 8 ^^^ 8888888 8 8 8 8 8 8888888 ^^^
8 88888888 8 88888888 8 ♣ ♣ ^^^^ 8 8 88888888 8 88888888 8 ♣ ♣ ^^^^
8 8 888888 8 ♣ ^^^ 8 8 888888 8 ♣ ^^^
88888 8 8 8 888 88888 ♣ ^^^^ 8 88888 8 8 8 888 88888 ♣ ^^^^
8 8 8 8 8 ♣ ♣ ^^ 88888 8 8 8 8 ♣ ♣ ^^
8 88888888888888 8888888 8 ♣ ^ 8 88888888888888 8888888 8 ♣ ^
8 8 8 8 ♣ ^ 8 8 8 8 ♣ ^
88888 88888 ♣ ^ 88888 888888 88888 ♣ ^
♣ ♣ ^ 8 8 ♣ ♣ ^
♣ ^ 8 ♣ ^
♣ ♣ ^ ^^ 888888 ♣ ♣ ^ ^^
♣ ♣ ♣ ♣ ^^^^^^^^^^ ♣ ♣ ♣ ♣ ^^^^^^^^^^
♣ ♣ ♣ ♣ ^^^^^^^^ ♣ ♣ ♣ ♣ ^^^^^^^^
♣ ♣ ♣ ♣ ^^^^^^^ ♣ ♣ ♣ ♣ ^^^^^^^
@ -42,24 +42,24 @@
♣ ♣ ♣ ♣ ♣ ♣
♣ ♣ ♣ ♣
♣ ♣ ♣ ♣
♣ ♣ ♣ ♣ %%%%
♣ ♣ ♣ ♣ % %
♣ ♣ ♣ ♣ % %
%%% %%
%
%% %
% %%%%
% % % %
%%%% % %
% % % %
♣ ♣ %%%%%%% % % %%%
% % % %
% % %%% % %
% %%%%% %
%%% %%%%%%%%
% %%
%
%%%%%%%%%

View File

@ -2,31 +2,31 @@
'''''''';;;;;;'''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;'''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''"'""'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''"""""%"'''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''"%%%%%""""''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''"'""'"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'""'""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%""""""""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ "''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''""''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[
"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ "''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''"''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''""'"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%''''""''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%%%""""''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%'''''"''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[CC[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'''''''''''''''''''''''''''''''''[[[[[[[[[[[[[CC[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[
'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''';;;;;;'''''''''''''''[[[[[CCC[[[[[[[[[ '''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''""'''''''''''''';;;;;;'''''''''''''''[[[[[CCC[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''';;;;'''';;;;''''''''''''[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''''''''';;;;'''';;;;''''''''''''[[[[[[[[[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''';;;;;'''''''''';;;;;''''''''''[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''''';;;;;'''''''''';;;;;''''''''''[[[[[[[[[[[[[[[
'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[
'''''''';;;;;'''''''''''';;;;;;''''';;;;;''''''''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[ "''''''';;;;;'''''''''''';;;;;;''''';;;;;''''"'''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[
'''''''';;;;;''''''''''''';;;;'''''';;;;;''''''';;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[ '''''''';;;;;''''''''''''';;;;'''''';;;;;'''""'";;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[
"'''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[ "''''''''''''''%%%%'''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[
''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[ '''''''''''''''%%%%%""""""";;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[
'''''''''''''''"'''''''''''';;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[ '''''''''''''''""""''''''''";;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[
'''''''''''''''''''''''''''''''';;;;;;;;"'"'''"''''"""''"'"''"''"''"'"'"'""'"''""'"'"'""';;[[[[[[[[[[ '''''''''''''''''''''''''''''''';;;;;;;;"'"'''"''''"""''"'"''"''"''"'"'"'""'"''""'"'"'""';;[[[[[[[[[[
"''''''''''''''''"''''''''''''''';;;;;'""'"''"'""'''"'''''''''"'''"'"'""''"'"'''''"'''"'"';''[[[[[[[[ "''''''''''''''''"''''''''''''''';;;;;'""'"''"'""'''"'''''''''"'''"'"'""''"'"'''''"'''"'"';''[[[[[[[[
"''''''''''''''''''''''''''''''''';;;;;;;'"''''''''''''''"''''''''''''''''''''''''''"''"'';;''[[[[[[[ "''''''''''''''''''''''''''''''''';;;;;;;'"''''''''''''''"''''''''''''''''''''''''''"''"'';;''[[[[[[[
@ -45,16 +45,16 @@
''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;''''' ''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;'''''
''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;'''' ''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;''''
''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;; ''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;;
''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;'''''''''''''''''''' ''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;;;''""""""''''''''''
'''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;'''''''''''''''''''''' '''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;;;;;;"""";"""""'''''''
'''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~ '''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;''''""""";"""""""""""''''~
''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''''''''''''''''''''''''~ ''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''"""""";"""";""";""''''~
'''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''''''''''''''''''''''''~ '''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''"";""""";""""""""""'''~
''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;''''''''''''''''''''''''''''''''''''''''''~ ''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;'''''''''''''''''''''""""""""""";"""""''''~
:'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;''''''''''''''''''''''''''''''''''''''''''''' :'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;'''''''''''''''''''''''''""";";"""""";"""''''
::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''' ::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;'''''''''''''''''''''''''''''''"""""""""""""""''''
:::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''''''''' :::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;''''''''''''''''''''''''''''''''''''''""""";"""""""'''''
:::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''''''''~~~~~~~~~~~ :::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''""""""~~~~~~~~~~~
:~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~''''' :~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~'''''
~~~~~~~~~~~~~~;~~~~~~~~~~~~~~~~~~:::::;;:::::::::::::::::::::::::::::::~~~~~~~~~~:::::::::''''''''''' ~~~~~~~~~~~~~~;~~~~~~~~~~~~~~~~~~:::::;;:::::::::::::::::::::::::::::::~~~~~~~~~~:::::::::'''''''''''
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~:::;:::::::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~::::::::::::::::::::'' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~:::;:::::::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~::::::::::::::::::::''

View File

@ -1,18 +1,35 @@
use std::io::{stdin,stdout,Write}; use std::io::{stdin,stdout,Write};
use std::fs;
use std::process::{Command}; use std::process::{Command};
use serde::{Serialize, Deserialize};
// Declare the modules from these files // Declare the modules from these files
mod parse_map; // parse_map.rs mod parse_map; // parse_map.rs
mod output_map; // output_map.rs mod output_map; // output_map.rs
mod save; // save.rs
const HELP_STRING: &str = "'help' = output's this help text const HELP_STRING: &str = "'help' = output's this help text
'travel' = travel to a given location 'travel' = travel to a given location
"; ";
#[derive(Serialize, Deserialize, Debug)]
struct Coordinates { struct Coordinates {
x: i16, x: i16,
// y: i16,
z: i16, z: i16,
map: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Skills {
woodcutting: u8,
mining: u8,
}
#[derive(Serialize, Deserialize, Debug)]
struct Player {
name: String,
skills: Skills,
coordinates: Coordinates,
inventory: Vec<(String, u64)>,
} }
fn clear_screen() fn clear_screen()
@ -34,10 +51,10 @@ fn input() -> String{
return s; return s;
} }
fn main() fn main()
{ {
let debug_mode = true; let debug_mode: bool = true;
let save_file_path: &str = "data/save.json";
let (screen_width, screen_height) = termion::terminal_size().unwrap(); let (screen_width, screen_height) = termion::terminal_size().unwrap();
let distance_you_can_see: [i16; 2] = let distance_you_can_see: [i16; 2] =
[ [
@ -49,22 +66,19 @@ fn main()
println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height);
} }
// Player data if let Ok(save_file_contents) = fs::read_to_string(save_file_path) // Load save file
let map: &str = "world";
let player_coordinates: Coordinates = Coordinates
{ {
x: 30, if let Ok(player) = serde_json::from_str::<Player>(save_file_contents.as_str())
//y: 1, {
z: 30, println!("{:?}", player);
};
if let Ok((ground_map, above_map)) = parse_map::parse_map(map) // Call the parse map function from parse_map.rs if let Ok((ground_map, above_map)) = parse_map::parse_map(&player.coordinates) // 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 output_map::output_map // Call output_map fuctino from output_map.rs
( (
&ground_map, &ground_map,
&above_map, &above_map,
&player_coordinates, &player.coordinates,
&distance_you_can_see &distance_you_can_see
); // Output the map ); // Output the map
'game_loop: loop 'game_loop: loop
@ -81,7 +95,11 @@ fn main()
} }
else if user_input == "exit" else if user_input == "exit"
{ {
// TO DO: SAVE if let Err(e) = save::save(&save_file_path, &player)
{
eprintln!("Error saving game: {}",e);
continue;
}
print!("Exiting...\n"); print!("Exiting...\n");
break 'game_loop; break 'game_loop;
} }
@ -91,4 +109,14 @@ fn main()
{ {
eprintln!("Error parsing map files."); eprintln!("Error parsing map files.");
} }
}
else
{
eprintln!("Failed to parse save file. Invalid JSON:\n{}", save_file_contents);
}
}
else
{
println!("No save file, creating new character");
}
} }

View File

@ -4,7 +4,12 @@ use std::collections::HashMap;
use crate::Coordinates; use crate::Coordinates;
// Output the map based on map vector // 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]) 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]> = let blocks: HashMap<char, [u8; 3]> =
[ [

View File

@ -1,13 +1,14 @@
use std::fs; // For reading the map and save files use std::fs; // For reading the map and save files
use crate::Coordinates;
// This function reads the map file and puts it into a 2d vector of integers, each integer // 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 // 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> pub fn parse_map(player_coordinates: &Coordinates) -> Result<(Vec<Vec<char>>, Vec<Vec<char>>), u8>
{ {
let mut ground_map: Vec<Vec<char>> = Vec::new(); // Initialises the Ground map vector 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 mut above_map: Vec<Vec<char>> = Vec::new(); // Initialises the Above Ground map vector
let ground_file = format!("data/{}_ground.map",map); let ground_file = format!("data/{}_ground.map",player_coordinates.map);
let above_file = format!("data/{}_above.map",map); let above_file = format!("data/{}_above.map",player_coordinates.map);
// Read the ground map file // Read the ground map file
if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file) if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file)

20
src/save.rs Normal file
View File

@ -0,0 +1,20 @@
use serde::{Serialize, Deserialize};
use std::io::{self, Write};
use std::fs::File;
use crate::Player;
pub fn save(
save_file_path: &str, // data/save.json by default
player: &Player
) -> io::Result<()>
{
let player_json = serde_json::to_string_pretty(&player).unwrap();
println!("Saving...");
let mut save_file = File::create(save_file_path)?;
save_file.write_all(player_json.as_bytes())?;
println!("Saved...");
Ok(())
}