made initialisation

This commit is contained in:
deadvey 2025-02-16 03:22:32 +00:00
parent f873317129
commit 0e912b0ff7
5 changed files with 72 additions and 28 deletions

View File

@ -1,12 +1,12 @@
{ {
"name": "dave", "name": "phillip",
"skills": { "skills": {
"woodcutting": 0, "woodcutting": 0,
"mining": 0 "mining": 0
}, },
"coordinates": { "coordinates": {
"x": 30, "x": 28,
"z": 30, "z": 25,
"map": "world" "map": "world"
}, },
"inventory": [ "inventory": [
@ -15,12 +15,8 @@
1 1
], ],
[ [
"cigarette", "vape",
5 1
],
[
"cp",
3
] ]
] ]
} }

16
src/function.rs Normal file
View 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
View 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;
}

View File

@ -1,4 +1,3 @@
use std::io::{stdin,stdout,Write};
use std::fs; use std::fs;
use std::process::{Command}; use std::process::{Command};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
@ -7,6 +6,8 @@ use serde::{Serialize, Deserialize};
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 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 const HELP_STRING: &str = "'help' = output's this help text
'travel' = travel to a given location 'travel' = travel to a given location
@ -38,18 +39,6 @@ fn clear_screen()
.status() .status()
.expect("Failed to clear screen"); .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() fn main()
{ {
@ -65,13 +54,16 @@ fn main()
{ {
println!("Screen Width: {}, Screen Height: {}", screen_width, screen_height); 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(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()) if let Ok(player) = serde_json::from_str::<Player>(save_file_contents.as_str())
{
if debug_mode == true
{ {
println!("{:?}", player); println!("{:?}", player);
}
if let Ok((ground_map, above_map)) = parse_map::parse_map(&player.coordinates) // 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
@ -83,17 +75,17 @@ fn main()
); // Output the map ); // Output the map
'game_loop: loop 'game_loop: loop
{ {
if ! debug_mode if debug_mode == false
{ {
clear_screen() clear_screen()
} }
print!("> "); print!("> ");
let user_input: String = input(); let user_input: String = function::input();
if user_input == "help" if user_input == "help"
{ {
print!("\n{}\n", HELP_STRING); 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) if let Err(e) = save::save(&save_file_path, &player)
{ {
@ -108,15 +100,24 @@ fn main()
else else
{ {
eprintln!("Error parsing map files."); eprintln!("Error parsing map files.");
return ();
} }
} }
else else
{ {
eprintln!("Failed to parse save file. Invalid JSON:\n{}", save_file_contents); eprintln!("Failed to parse save file. Invalid JSON:\n{}", save_file_contents);
return ();
} }
} }
else else
{ {
println!("No save file, creating new character"); 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();
} }
} }