diff --git a/Cargo.toml b/Cargo.toml index 75dba9a..f81d2fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] colored = "2.2.0" termion = "4.0.3" +serde = { version = "1.0.127", features = ["derive"] } +serde_json = "1.0" diff --git a/data/save.json b/data/save.json index 0be5d7a..05a44ee 100644 --- a/data/save.json +++ b/data/save.json @@ -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 + ] + ] +} \ No newline at end of file diff --git a/data/world_above.map b/data/world_above.map index 7bbda81..6a5dbad 100644 --- a/data/world_above.map +++ b/data/world_above.map @@ -3,30 +3,30 @@ 8 8888888888888888888888888 8 ^^^^^^^^^^^^^^^^^^^^^^^^^ 8 8 ^^^^^^^^^^^^^^^^^^^^^^ 88888888 88888888 888 888888 ^^^^^^^^^^^^^^^^^^^^^ - 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^ - 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ - 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ - 8 8 8 8 ♣ ♣ ^^^^^^^^^^^^^^^^^^^^^^^ - 8888888888888888 88888888888 ^^^^^^^^^^^^^^^^^^ ^^ - 8 8 ^^^^^^^^^^^^^^ ^^^ - 8 8888888 88888888 88888888 ^^^^^^^^^^^ ^^^^^ + 8 8 8 8 8 8888 88 ^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 8 8 8 ^^^^^^^^^^^^^^^^^^^^^^^ + 88888888 8 8 8 8 8 88888 ^^^^^^^^^^^^^^^^^^^^^^^ + 8 8 8 8 8 ♣ 8 8 8 ♣ ^^^^^^^^^^^^^^^^^^^^^^^ + 8 8888888888888888 88888888888 8 8 ^^^^^^^^^^^^^^^^^^ ^^ + 8 8 8 888888888 ^^^^^^^^^^^^^^ ^^^ + 88888888 8888888 88888888 88888888 ^^^^^^^^^^^ ^^^^^ 8 8 8 8 8 8 8 ♣ ^^^^^^^ ^^^ - 8 8 8 8 8 8 ^^^^ - ♣ 8 8 8 88888888 8 ♣ ^^ - 8 88888 8 8 8 ^ + 8 8 8 8 8 8888888 ^^^^ + ♣ 8 8 8 88888888 8 8 ♣ ^^ + 8 88888 8 8 8 8 ^ 8 8888 8 88 88888888 ^^ - 8 88888 8 8 8 8 8 ^^^ - 8 8 8 8 8 8 8 ^^^ - 8 88888888 8 88888888 8 ♣ ♣ ^^^^ - 8 8 888888 8 ♣ ^^^ - 88888 8 8 8 888 88888 ♣ ^^^^ - 8 8 8 8 8 ♣ ♣ ^^ + 8 88888 8 8 8 8 8 8 ^^^ + 8888888 8 8 8 8 8 8888888 ^^^ + 8 8 88888888 8 88888888 8 ♣ ♣ ^^^^ + 8 8 888888 8 ♣ ^^^ + 8 88888 8 8 8 888 88888 ♣ ^^^^ + 88888 8 8 8 8 ♣ ♣ ^^ 8 88888888888888 8888888 8 ♣ ^ 8 8 8 8 ♣ ^ - 88888 88888 ♣ ^ - ♣ ♣ ^ - ♣ ^ - ♣ ♣ ♣ ^ ^^ + 88888 888888 88888 ♣ ^ + 8 8 ♣ ♣ ^ + 8 ♣ ^ + ♣ 888888 ♣ ♣ ^ ^^ ♣ ♣ ♣ ♣ ^^^^^^^^^^ ♣ ♣ ♣ ♣ ^^^^^^^^ ♣ ♣ ♣ ♣ ^^^^^^^ @@ -42,24 +42,24 @@ ♣ ♣ ♣ ♣ ♣ ♣ ♣ - ♣ ♣ - ♣ ♣ - ♣ ♣ - ♣ - ♣ - ♣ - - ♣ - - - ♣ ♣ - - - - - - - + ♣ ♣ %%%% + ♣ ♣ % % + ♣ ♣ % % + ♣ %%% %% + ♣ % + ♣ %% % + % %%%% + ♣ % % % % + %%%% % % + % % % % + %%%%%%% % % %%% + % % % % + % % %%% % % + % %%%%% % + %%% %%%%%%%% + % %% + % + %%%%%%%%% diff --git a/data/world_ground.map b/data/world_ground.map index d4997e4..d2fa6a5 100644 --- a/data/world_ground.map +++ b/data/world_ground.map @@ -2,31 +2,31 @@ '''''''';;;;;;'''''''''''''''''''''';;;;;'''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[[[ '''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''"'""'"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'""'""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ -"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[CC[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[ -'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''';;;;;;'''''''''''''''[[[[[CCC[[[[[[[[[ -'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''''''';;;;'''';;;;''''''''''''[[[[[[[[[[[[[[[[[ -'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''''';;;;;'''''''''';;;;;''''''''''[[[[[[[[[[[[[[[ -'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[ -'''''''';;;;;'''''''''''';;;;;;''''';;;;;''''''''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[ -'''''''';;;;;''''''''''''';;;;'''''';;;;;''''''';;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[ -"'''''''''''''''''''''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[ -''''''''''''''''''''''''''';;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[ -'''''''''''''''"'''''''''''';;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[ +'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''"'""'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''"""""%"'''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''"%%%%%""""''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%%%%%%%%%"'''''''''''''''''''''[[[[[[[[[[[[[[[[[[i[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''""%""""""""''"''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ +"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''""''""'"""'''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[[ +"''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''"''''""'"''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''""'"'''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%''''"'''''"''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%''''""''''''''''''''''''''''''''''''''[[[[D[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%%%""""''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;%%%%%'''''"''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''''"'''''''''''''''''''''''''''''''''[[[[[[[[[[[[[CC[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''''''"''''''''''''''''''''''''''''''''''[[[[[[[[[[[[[[[[[[ +'''''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;''''''''""'''''''''''''';;;;;;'''''''''''''''[[[[[CCC[[[[[[[[[ +'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''''''''';;;;'''';;;;''''''''''''[[[[[[[[[[[[[[[[[ +'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''''';;;;;'''''''''';;;;;''''''''''[[[[[[[[[[[[[[[ +'''''''';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'''''"''''''';;;'''''''''''''''''';;''''''''''[[[[[[[[[[[[[[ +"''''''';;;;;'''''''''''';;;;;;''''';;;;;''''"'''';;;;;''''''''''''''''''''';;;;'''''''[[[[[[[[[[[[[[ +'''''''';;;;;''''''''''''';;;;'''''';;;;;'''""'";;;'''''''''''''''''''''''''''';;;'''''[[[[[[[[[[[[[[ +"''''''''''''''%%%%'''''';;;;;''''''''''''';;;;;'''''''''''''''''''''''''''''''';;;;;'''[[[[[[[[[[[[[ +'''''''''''''''%%%%%""""""";;;;;;''''''';;;;;'''''''''''''''''''"'''''''''''''''''''';;;;;[[[[[[[[[[[ +'''''''''''''''""""''''''''";;;;;;;''';;;'''"''''''''''''"''"'"''"''''"'''"'''''''""'";;;;;[[[[[[[[[[ '''''''''''''''''''''''''''''''';;;;;;;;"'"'''"''''"""''"'"''"''"''"'"'"'""'"''""'"'"'""';;[[[[[[[[[[ "''''''''''''''''"''''''''''''''';;;;;'""'"''"'""'''"'''''''''"'''"'"'""''"'"'''''"'''"'"';''[[[[[[[[ "''''''''''''''''''''''''''''''''';;;;;;;'"''''''''''''''"''''''''''''''''''''''''''"''"'';;''[[[[[[[ @@ -45,16 +45,16 @@ ''''''''''''''''''''''~~~~~~~~~~~~~'''''''''''';''''''''''''''''''''''''''''''''''';;'''''''';;;''''' ''''''''''''''''''''''~~~'''~~~'''''''''''''''';'''''''''''''''''''''''''''''''''';;''''''''''';;'''' ''''''''''''''''''''''~~''''''''''''''''''''''';;''''''''''''''''''''''''''''''';;;''''''''''''';;;;; -''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;'''''''''''''''''''' -'''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;'''''''''''''''''''''' -'''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;'''''''''''''''''''''''''~ -''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''''''''''''''''''''''''~ -'''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''''''''''''''''''''''''~ -''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;''''''''''''''''''''''''''''''''''''''''''~ -:'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;''''''''''''''''''''''''''''''''''''''''''''' -::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''' -:::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;'''''''''''''''''''''''''''''''''''''''''''''''''''''''' -:::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''''''''~~~~~~~~~~~ +''''''''''''''''''''''~~'''''''''''''''''''''''";;;''''''''''''''''''''''''''';;;;;''""""""'''''''''' +'''''''''''''''''''''~~''''''''''''''''''''''''""";;;;;;;''''''''''''''''';;;;;;;;;;"""";"""""''''''' +'''''''''''''''''''~~~'''''''''''''''''''''"'''"'''''''';;;;;;;;;;;;;;;;;;;''''""""";"""""""""""''''~ +''''''''''''''''''~~~'''''''''''''''''''''''''"'''''''''';;'''''''''''''''''''"""""";"""";""";""''''~ +'''''''''''''''::~~~~''''''''''''''''''''''''"'"'''''''';;''''''''''''''''''''"";""""";""""""""""'''~ +''''''''''''':::~~~~~:''''''''''''''''''''"'"'"''''''';;;;'''''''''''''''''''''""""""""""";"""""''''~ +:'''''''''''::::~~~~~~::'''''''''''''''''''"'''''';;;;;;'''''''''''''''''''''''''""";";"""""";"""'''' +::::'''::::::~~~~;~~~~::::::'''''''''''''""";;;;;;;'''''''''''''''''''''''''''''''"""""""""""""""'''' +:::::::::~~~~~~~~~~~~:::::::::::::::''';;;;;;''''''''''''''''''''''''''''''''''''''""""";"""""""''''' +:::~~~~~~~~~~~~~~~~~~~~:::::::::::::::;;;:::''''''''''''''''''''''''''''''''''''''''""""""~~~~~~~~~~~ :~~~~~~~~~~~~;;~~~~~~~~~~~:::::::::::;;:::::::::::::::::''''''''''''''''''''''~~~~~~~~~~~~~~~~~~''''' ~~~~~~~~~~~~~~;~~~~~~~~~~~~~~~~~~:::::;;:::::::::::::::::::::::::::::::~~~~~~~~~~:::::::::''''''''''' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~:::;:::::::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~::::::::::::::::::::'' diff --git a/src/main.rs b/src/main.rs index 7c8e123..beb9d53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,35 @@ use std::io::{stdin,stdout,Write}; +use std::fs; use std::process::{Command}; +use serde::{Serialize, Deserialize}; // Declare the modules from these files mod parse_map; // parse_map.rs mod output_map; // output_map.rs +mod save; // save.rs const HELP_STRING: &str = "'help' = output's this help text 'travel' = travel to a given location "; +#[derive(Serialize, Deserialize, Debug)] struct Coordinates { x: i16, -// y: 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() @@ -34,10 +51,10 @@ fn input() -> String{ return s; } - 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 distance_you_can_see: [i16; 2] = [ @@ -49,46 +66,57 @@ fn main() println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); } - // Player data - let map: &str = "world"; - let player_coordinates: Coordinates = Coordinates + if let Ok(save_file_contents) = fs::read_to_string(save_file_path) // Load save file { - x: 30, - //y: 1, - z: 30, - }; - - 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 - 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 let Ok(player) = serde_json::from_str::(save_file_contents.as_str()) { - if ! debug_mode - { - clear_screen() + println!("{:?}", player); + + 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 + 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 + { + clear_screen() + } + print!("> "); + let user_input: String = input(); + if user_input == "help" + { + print!("\n{}\n", HELP_STRING); + } + else if user_input == "exit" + { + if let Err(e) = save::save(&save_file_path, &player) + { + eprintln!("Error saving game: {}",e); + continue; + } + print!("Exiting...\n"); + break 'game_loop; + } + } } - print!("> "); - let user_input: String = input(); - if user_input == "help" + else { - print!("\n{}\n", HELP_STRING); - } - else if user_input == "exit" - { - // TO DO: SAVE - print!("Exiting...\n"); - break 'game_loop; + eprintln!("Error parsing map files."); } } + else + { + eprintln!("Failed to parse save file. Invalid JSON:\n{}", save_file_contents); + } } - else + else { - eprintln!("Error parsing map files."); + println!("No save file, creating new character"); } } diff --git a/src/output_map.rs b/src/output_map.rs index ff793dd..f531f9c 100644 --- a/src/output_map.rs +++ b/src/output_map.rs @@ -4,7 +4,12 @@ use std::collections::HashMap; use crate::Coordinates; // Output the map based on map vector -pub fn output_map(ground_map: &Vec>, above_map: &Vec>, player_coordinates: &Coordinates, distance_you_can_see: &[i16; 2]) +pub fn output_map( + ground_map: &Vec>, + above_map: &Vec>, + player_coordinates: &Coordinates, + distance_you_can_see: &[i16; 2] +) { let blocks: HashMap = [ diff --git a/src/parse_map.rs b/src/parse_map.rs index 69c41a3..b4b4fca 100644 --- a/src/parse_map.rs +++ b/src/parse_map.rs @@ -1,13 +1,14 @@ 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 // Refers to a block (see top comment) and the function returns this vector -pub fn parse_map(map: &str) -> Result<(Vec>, Vec>), u8> +pub fn parse_map(player_coordinates: &Coordinates) -> Result<(Vec>, Vec>), u8> { let mut ground_map: Vec> = Vec::new(); // Initialises the Ground map vector let mut above_map: Vec> = Vec::new(); // Initialises the Above Ground map vector - let ground_file = format!("data/{}_ground.map",map); - let above_file = format!("data/{}_above.map",map); + let ground_file = format!("data/{}_ground.map",player_coordinates.map); + let above_file = format!("data/{}_above.map",player_coordinates.map); // Read the ground map file if let Ok(parsed_ground_file_contents) = fs::read_to_string(&ground_file) diff --git a/src/save.rs b/src/save.rs new file mode 100644 index 0000000..76b0be0 --- /dev/null +++ b/src/save.rs @@ -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(()) +}