131 lines
2.5 KiB
Rust
131 lines
2.5 KiB
Rust
use std::fs;
|
|
use std::process::{Command};
|
|
use serde::{Serialize, Deserialize};
|
|
use std::collections::HashMap;
|
|
|
|
// Declare the modules from these files
|
|
mod parse_map; // parse_map.rs
|
|
mod output_map; // output_map.rs
|
|
mod save; // save.rs
|
|
mod initialise; // initialise.rs, creates a new character
|
|
mod function; // function.rs, for misc functions
|
|
mod process_input; // process_input.rs
|
|
|
|
#[macro_use]
|
|
extern crate lazy_static;
|
|
mod data;
|
|
|
|
pub enum GameAction
|
|
{
|
|
Continue,
|
|
Exit,
|
|
Error,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
struct Skills
|
|
{
|
|
woodcutting: u64,
|
|
mining: u64,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
struct Coordinates
|
|
{
|
|
x: i16,
|
|
z: i16,
|
|
map: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
struct Player
|
|
{
|
|
name: String,
|
|
skills: Skills,
|
|
coordinates: Coordinates,
|
|
inventory: Vec<(String, u64)>,
|
|
}
|
|
|
|
const DEBUG_MODE: bool = true;
|
|
|
|
fn main()
|
|
{
|
|
let save_file_path: &str = "data/save.json";
|
|
let player: Player;
|
|
|
|
if let Ok(save_file_contents) = fs::read_to_string(save_file_path) // Load save file
|
|
{
|
|
if let Ok(mut player) = serde_json::from_str::<Player>(save_file_contents.as_str())
|
|
{
|
|
if DEBUG_MODE == true
|
|
{
|
|
println!("{:?}", player);
|
|
}
|
|
if let Ok(map) = parse_map::parse_map(&player.coordinates) // Call the parse map function from parse_map.rs
|
|
{ // Parse the map file into a vector
|
|
if DEBUG_MODE == false
|
|
{
|
|
function::clear_screen();
|
|
}
|
|
output_map::output_map // Call output_map fuctino from output_map.rs
|
|
(
|
|
&map,
|
|
&data::BLOCKS,
|
|
&player.coordinates,
|
|
); // Output the map
|
|
'game_loop: loop
|
|
{
|
|
print!("> ");
|
|
let user_input: String = function::input();
|
|
let result = process_input::process_input
|
|
(
|
|
user_input,
|
|
&mut player,
|
|
&map,
|
|
&data::BLOCKS,
|
|
);
|
|
|
|
if let Err(e) = save::save(&save_file_path, &player)
|
|
{
|
|
eprintln!("Error saving game: {}",e);
|
|
continue;
|
|
}
|
|
match result
|
|
{
|
|
GameAction::Exit =>
|
|
{
|
|
println!("Exitting...");
|
|
break 'game_loop;
|
|
},
|
|
_ =>
|
|
{
|
|
continue 'game_loop;
|
|
},
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
eprintln!("Error parsing map files.");
|
|
return ();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
eprintln!("Failed to parse save file. Invalid JSON:\n{}", save_file_contents);
|
|
return ();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
println!("No save file, creating new character");
|
|
player = initialise::initialise();
|
|
if let Err(e) = save::save(&save_file_path, &player)
|
|
{
|
|
eprintln!("Error saving game: {}",e);
|
|
return ();
|
|
}
|
|
main();
|
|
}
|
|
}
|