saving and loading save files

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

View File

@@ -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::<Player>(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");
}
}

View File

@@ -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<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]> =
[

View File

@@ -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<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 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);
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)

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(())
}