made initialisation
This commit is contained in:
parent
f873317129
commit
0e912b0ff7
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "dave",
|
||||
"name": "phillip",
|
||||
"skills": {
|
||||
"woodcutting": 0,
|
||||
"mining": 0
|
||||
},
|
||||
"coordinates": {
|
||||
"x": 30,
|
||||
"z": 30,
|
||||
"x": 28,
|
||||
"z": 25,
|
||||
"map": "world"
|
||||
},
|
||||
"inventory": [
|
||||
@ -15,12 +15,8 @@
|
||||
1
|
||||
],
|
||||
[
|
||||
"cigarette",
|
||||
5
|
||||
],
|
||||
[
|
||||
"cp",
|
||||
3
|
||||
"vape",
|
||||
1
|
||||
]
|
||||
]
|
||||
}
|
16
src/function.rs
Normal file
16
src/function.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use std::io::{stdin,stdout,Write};
|
||||
|
||||
pub fn input() -> String{
|
||||
let mut s=String::new();
|
||||
let _=stdout().flush();
|
||||
stdin().read_line(&mut s).expect("Did not enter a correct string");
|
||||
if let Some('\n')=s.chars().next_back()
|
||||
{
|
||||
s.pop();
|
||||
}
|
||||
if let Some('\r')=s.chars().next_back()
|
||||
{
|
||||
s.pop();
|
||||
}
|
||||
return s;
|
||||
}
|
31
src/initialise.rs
Normal file
31
src/initialise.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use crate::Player;
|
||||
use crate::Coordinates;
|
||||
use crate::Skills;
|
||||
use crate::function;
|
||||
|
||||
pub fn initialise() -> Player
|
||||
{
|
||||
println!("Name:");
|
||||
let name = function::input();
|
||||
let player: Player = Player
|
||||
{
|
||||
name: name,
|
||||
skills: Skills
|
||||
{
|
||||
woodcutting: 0,
|
||||
mining: 0,
|
||||
},
|
||||
coordinates: Coordinates
|
||||
{
|
||||
x: 28,
|
||||
z: 25,
|
||||
map: "world".to_string()
|
||||
},
|
||||
inventory: vec!
|
||||
{
|
||||
("axe".to_string(), 1),
|
||||
("vape".to_string(), 1)
|
||||
}
|
||||
};
|
||||
return player;
|
||||
}
|
37
src/main.rs
37
src/main.rs
@ -1,4 +1,3 @@
|
||||
use std::io::{stdin,stdout,Write};
|
||||
use std::fs;
|
||||
use std::process::{Command};
|
||||
use serde::{Serialize, Deserialize};
|
||||
@ -7,6 +6,8 @@ use serde::{Serialize, Deserialize};
|
||||
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
|
||||
|
||||
const HELP_STRING: &str = "'help' = output's this help text
|
||||
'travel' = travel to a given location
|
||||
@ -38,18 +39,6 @@ fn clear_screen()
|
||||
.status()
|
||||
.expect("Failed to clear screen");
|
||||
}
|
||||
fn input() -> String{
|
||||
let mut s=String::new();
|
||||
let _=stdout().flush();
|
||||
stdin().read_line(&mut s).expect("Did not enter a correct string");
|
||||
if let Some('\n')=s.chars().next_back() {
|
||||
s.pop();
|
||||
}
|
||||
if let Some('\r')=s.chars().next_back() {
|
||||
s.pop();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
@ -65,13 +54,16 @@ fn main()
|
||||
{
|
||||
println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height);
|
||||
}
|
||||
let player: Player;
|
||||
|
||||
if let Ok(save_file_contents) = fs::read_to_string(save_file_path) // Load save file
|
||||
{
|
||||
if let Ok(player) = serde_json::from_str::<Player>(save_file_contents.as_str())
|
||||
{
|
||||
println!("{:?}", player);
|
||||
|
||||
if debug_mode == true
|
||||
{
|
||||
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
|
||||
@ -83,17 +75,17 @@ fn main()
|
||||
); // Output the map
|
||||
'game_loop: loop
|
||||
{
|
||||
if ! debug_mode
|
||||
if debug_mode == false
|
||||
{
|
||||
clear_screen()
|
||||
}
|
||||
print!("> ");
|
||||
let user_input: String = input();
|
||||
let user_input: String = function::input();
|
||||
if user_input == "help"
|
||||
{
|
||||
print!("\n{}\n", HELP_STRING);
|
||||
}
|
||||
else if user_input == "exit"
|
||||
else if user_input == "exit" || user_input == "quit"
|
||||
{
|
||||
if let Err(e) = save::save(&save_file_path, &player)
|
||||
{
|
||||
@ -108,15 +100,24 @@ fn main()
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize};
|
||||
use std::io::{self, Write};
|
||||
use std::fs::File;
|
||||
|
||||
use crate::Player;
|
||||
use crate::Player;
|
||||
|
||||
pub fn save(
|
||||
save_file_path: &str, // data/save.json by default
|
||||
|
Loading…
x
Reference in New Issue
Block a user