From 376c04297ec898fba7811726400905b62d261f90 Mon Sep 17 00:00:00 2001 From: serxoz Date: Wed, 7 Sep 2022 10:52:55 +0200 Subject: [PATCH] empezando... --- .gitignore | 1 + Cargo.lock | 7 ++++++ Cargo.toml | 8 +++++++ README.md | 6 ++++++ src/main.rs | 23 ++++++++++++++++++++ src/rlib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/main.rs create mode 100644 src/rlib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0d5dedf --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "reentrada" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5171771 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "reentrada" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/README.md b/README.md new file mode 100644 index 0000000..3d12b3f --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Reentrada + +Aventura en texto. Sírveme como práctica para aprender Rust. +Seguindo o tutorial: https://www.riskpeep.com/2022/08/make-text-adventure-game-rust-1.html + + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..029b320 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,23 @@ +pub mod rlib; + +fn main() { + // intro + println!("Bienvenido a Reentrada. Una aventura en el espacio."); + println!(""); + println!("Despiertas en la oscuridad con un fuerte dolor de cabeza."); + println!("Un fuerte sonido de alarma machaca tus oídos. No ayuda nada a tu dolor de cabeza."); + println!(""); + + let mut command = rlib::Command::new(); + let mut output: String; + + // main loop + while command.verb != "salir" { + command = rlib::get_input(); + output = rlib::update_state(&command); + rlib::update_screen(output); + } + + // salir + println!("Adios."); +} diff --git a/src/rlib.rs b/src/rlib.rs new file mode 100644 index 0000000..7390960 --- /dev/null +++ b/src/rlib.rs @@ -0,0 +1,61 @@ +use std::io::{self, Write}; + +pub struct Command { + pub verb: String, + pub noun: String, +} + +impl Command { + pub fn new() -> Command { + Command { + verb: String::new(), + noun: String::new(), + } + } + + fn parse(&mut self, input_str: &str) { + let mut split_input_iter = input_str.trim().split_whitespace(); + + self.verb = split_input_iter.next().unwrap_or_default().to_string(); + self.noun = split_input_iter.next().unwrap_or_default().to_string(); + } +} + +pub fn get_input() -> Command { + // prompt + println!(""); + print!("> "); + io::stdout().flush().unwrap(); + + let mut input_str = String::new(); + + io::stdin() + .read_line(&mut input_str) + .expect("Error leyendo..."); + println!(""); + + // parse + let mut command = Command::new(); + command.parse(input_str.as_str()); + + // return + command +} + +pub fn update_state(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."), + _ => output = format!("No se como hacer eso."), + } + + // return + output +} + +pub fn update_screen(output: String) { + println!("{}", output); +}