movimiento entre localizaciós

This commit is contained in:
serxoz 2022-09-07 19:19:00 +02:00
parent 376c04297e
commit 2344d4b7ff
4 changed files with 58 additions and 4 deletions

View File

@ -1,4 +1,8 @@
pub mod rlib; pub mod rlib;
pub mod rlocation;
pub mod player;
use crate::player::Player;
fn main() { fn main() {
// intro // intro
@ -10,14 +14,15 @@ fn main() {
let mut command = rlib::Command::new(); let mut command = rlib::Command::new();
let mut output: String; let mut output: String;
let mut player = Player{name: "manolo".to_string(), location: 0};
// main loop // main loop
while command.verb != "salir" { while command.verb != "salir" {
command = rlib::get_input(); command = rlib::get_input();
output = rlib::update_state(&command); output = rlib::update_state(&mut player, &command);
rlib::update_screen(output); rlib::update_screen(output);
} }
// salir // salir
println!("Adios."); println!("Adiós.");
} }

4
src/player.rs Normal file
View File

@ -0,0 +1,4 @@
pub struct Player {
pub name: String,
pub location: usize,
}

View File

@ -1,4 +1,6 @@
use std::io::{self, Write}; use std::io::{self, Write};
use crate::rlocation::{Location, execute_go, execute_look};
use crate::player::Player;
pub struct Command { pub struct Command {
pub verb: String, pub verb: String,
@ -42,13 +44,13 @@ pub fn get_input() -> Command {
command command
} }
pub fn update_state(command: &Command) -> String { pub fn update_state(player: &mut Player, command: &Command) -> String {
let output: String; let output: String;
match command.verb.as_str() { match command.verb.as_str() {
"salir" => output = format!("Saliendo.\nGracias por jugar! :D"), "salir" => output = format!("Saliendo.\nGracias por jugar! :D"),
"mirar" => output = format!("Está muy oscuro, no puedes ver nada excepto la luz pulsante."), "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."), _ => output = format!("No se como hacer eso."),
} }

43
src/rlocation.rs Normal file
View File

@ -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();
}