150 lines
3.7 KiB
Rust
150 lines
3.7 KiB
Rust
use std::fs;
|
|
use std::collections::HashMap;
|
|
|
|
use crate::GameAction;
|
|
use crate::Player;
|
|
use crate::DEBUG_MODE;
|
|
use crate::output_map;
|
|
use crate::function;
|
|
|
|
pub fn process_input
|
|
(
|
|
user_input: String,
|
|
player: &mut Player,
|
|
map: &Vec<Vec<char>>,
|
|
blocks: &HashMap<char, Vec<([u8; 3],bool, &str)>>,
|
|
) -> GameAction
|
|
{
|
|
// Split commands up, so "help && travel 5" > [["help"], ["travel", "5]]
|
|
let mut commands: Vec<Vec<&str>> = Vec::new();
|
|
if user_input != ""
|
|
{
|
|
let split_user_input: Vec<&str> = user_input.split("&&").collect();
|
|
for whole_command in &split_user_input
|
|
{
|
|
let mut split_command: Vec<&str> = whole_command.split(" ").collect();
|
|
if split_command[0] == ""
|
|
{
|
|
split_command.remove(0);
|
|
}
|
|
if split_command.last().unwrap() == &""
|
|
{
|
|
split_command.remove(split_command.len()-1);
|
|
}
|
|
commands.push(split_command);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
println!("No command");
|
|
}
|
|
|
|
// Loop over each command
|
|
for command in commands
|
|
{
|
|
match command[0].to_lowercase().as_str()
|
|
{
|
|
"help"|"h"|":h" =>
|
|
if let Ok(help_text) = fs::read_to_string("data/help.txt") // Output text from help file
|
|
{
|
|
println!("\n{}", help_text);
|
|
},
|
|
"exit"|"quit"|"q"|":q"|":wq" =>
|
|
{
|
|
return GameAction::Exit;
|
|
},
|
|
"map"|"world"|"m" =>
|
|
{
|
|
output_map::output_map // Call output_map fuctino from output_map.rs
|
|
(
|
|
&map,
|
|
&blocks,
|
|
&player.coordinates,
|
|
); // Output the map
|
|
},
|
|
"clear"|"cls" =>
|
|
{
|
|
function::clear_screen();
|
|
},
|
|
"player" =>
|
|
{
|
|
println!("{:?}", player);
|
|
},
|
|
"cut"|"chop"|"mine"|"pick"|"axe"|"destroy"|"skill" =>
|
|
{
|
|
let vector: &str = command[1];
|
|
|
|
match vector
|
|
{
|
|
"north"|"n"|"up" |"u" => function::skill_item('n', &player, &map),
|
|
"east" |"e"|"right"|"r" => function::skill_item('e', &player, &map),
|
|
"south"|"s"|"down" |"d" => function::skill_item('s', &player, &map),
|
|
"west" |"w"|"left" |"l" => function::skill_item('w', &player, &map),
|
|
_ => {println!("Invalid direction"); break;},
|
|
}
|
|
},
|
|
"travel"|"move"|"go" =>
|
|
{
|
|
let mut magnitude: usize = 1;
|
|
let mut vector: &str = "";
|
|
if command.len() == 3
|
|
{
|
|
if let Ok(number) = command[1].parse::<usize>()
|
|
{
|
|
magnitude = number;
|
|
vector = command[2];
|
|
}
|
|
else if let Ok(number) = command[2].parse::<usize>()
|
|
{
|
|
magnitude = number;
|
|
vector = command[1];
|
|
}
|
|
}
|
|
else if command.len() == 2
|
|
{
|
|
vector = command[1];
|
|
}
|
|
else
|
|
{
|
|
println!("Invalid travel command");
|
|
return GameAction::Error;
|
|
};
|
|
if DEBUG_MODE == true
|
|
{
|
|
println!("{}, {} blocks", vector, magnitude);
|
|
}
|
|
for _block in 0..magnitude
|
|
{
|
|
// Movement
|
|
match vector
|
|
{
|
|
"north"|"n"|"up" |"u" => player.coordinates.z -= 1,
|
|
"east" |"e"|"right"|"r" => player.coordinates.x += 1,
|
|
"south"|"s"|"down" |"d" => player.coordinates.z += 1,
|
|
"west" |"w"|"left" |"l" => player.coordinates.x -= 1,
|
|
_ => {println!("Invalid travel direction"); break;},
|
|
}
|
|
|
|
// Collision
|
|
let character: char = map[player.coordinates.z as usize][player.coordinates.x as usize];
|
|
if blocks.get(&character).unwrap()[0].1 == true
|
|
{
|
|
println!("Bumped into something!");
|
|
match vector
|
|
{
|
|
"north"|"n"|"up" |"u" => player.coordinates.z += 1,
|
|
"east" |"e"|"right"|"r" => player.coordinates.x -= 1,
|
|
"south"|"s"|"down" |"d" => player.coordinates.z -= 1,
|
|
"west" |"w"|"left" |"l" => player.coordinates.x += 1,
|
|
_ => println!("Invalid travel direction"),
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
_ => println!("Invalid Input"),
|
|
}
|
|
}
|
|
GameAction::Continue
|
|
}
|