diff --git a/src/main.rs b/src/main.rs index 029b320..afeec2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ pub mod rlib; +pub mod rlocation; +pub mod player; + +use crate::player::Player; fn main() { // intro @@ -10,14 +14,15 @@ fn main() { let mut command = rlib::Command::new(); let mut output: String; + let mut player = Player{name: "manolo".to_string(), location: 0}; // main loop while command.verb != "salir" { command = rlib::get_input(); - output = rlib::update_state(&command); + output = rlib::update_state(&mut player, &command); rlib::update_screen(output); } // salir - println!("Adios."); + println!("Adiós."); } diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..00936e4 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,4 @@ +pub struct Player { + pub name: String, + pub location: usize, +} diff --git a/src/rlib.rs b/src/rlib.rs index 7390960..ad285ed 100644 --- a/src/rlib.rs +++ b/src/rlib.rs @@ -1,4 +1,6 @@ use std::io::{self, Write}; +use crate::rlocation::{Location, execute_go, execute_look}; +use crate::player::Player; pub struct Command { pub verb: String, @@ -42,13 +44,13 @@ pub fn get_input() -> Command { command } -pub fn update_state(command: &Command) -> String { +pub fn update_state(player: &mut Player, command: &Command) -> String { let output: String; match command.verb.as_str() { "salir" => output = format!("Saliendo.\nGracias por jugar! :D"), "mirar" => output = format!("Está muy oscuro, no puedes ver nada excepto la luz pulsante."), - "ir" => output = format!("Está muy oscuro para moverte."), + "ir" => output = execute_go(player, command.noun.to_string()), _ => output = format!("No se como hacer eso."), } diff --git a/src/rlocation.rs b/src/rlocation.rs new file mode 100644 index 0000000..4e7f437 --- /dev/null +++ b/src/rlocation.rs @@ -0,0 +1,43 @@ +use crate::player::Player; + +#[derive(Debug)] +pub struct Location<'a> { + descripcion: &'a str, + tag: &'a str, +} + +const LOCATIONS: &'static [Location] = &[ + Location {descripcion: "Pasillo principal.", tag: "pasillo"}, + Location {descripcion: "Sala de máquinas.", tag: "maquinas"}, + Location {descripcion: "Vainas criogénicas.", tag: "vainas"} +]; + +pub fn execute_look(tag: String) -> String { + let salida = ""; + + return salida.to_string(); +} + +pub fn execute_go(player: &mut Player, tag: String) -> String { + let salida; + + if tag != "" { + for (pos, e) in LOCATIONS.iter().enumerate() { + // println!("Element at position {}: {:?}", pos, e.tag); + if tag == e.tag { + if pos == player.location { + salida = "No te puedes acercar mucho más..."; + } else { + player.location = pos; + salida = e.descripcion; + } + return salida.to_string(); + } + } + salida = "No encuentro ese lugar..." + } else { + salida = "No entiendo a donde quieres ir..."; + } + + return salida.to_string(); +}