diff --git a/locations/0.json b/locations/0.json index c0ae3a9..4c25c80 100644 --- a/locations/0.json +++ b/locations/0.json @@ -1,10 +1,11 @@ { "tag": "0", - "descripcion": "Estás en un pasillo muy oscuro.\nEscuchas un sonido de alarma en la lejanía.\nApenas puedes ver una luz pulsante al norte.\n\n---\nSalidas: norte", + "descripcion": "Estás en un pasillo muy oscuro.\nEscuchas un sonido de alarma en la lejanía.\nApenas puedes ver una luz pulsante al norte.\nEn el suelo encuntras una palanca.\n\n---\nSalidas: norte", "salidas": { "norte": "1", "este": "", "sur": "", "oeste": "" - } + }, + "objetos": ["o0"] } diff --git a/objects/o0.json b/objects/o0.json new file mode 100644 index 0000000..6533103 --- /dev/null +++ b/objects/o0.json @@ -0,0 +1,5 @@ +{ + "tag": "o0", + "nombre": "palanca", + "descripcion": "Trozo de metal, servirá como palanca." +} diff --git a/src/main.rs b/src/main.rs index 31cadbb..7ba5bc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ pub mod rlib; pub mod rlocation; pub mod player; +pub mod objects; use crate::player::Player; diff --git a/src/objects.rs b/src/objects.rs new file mode 100644 index 0000000..ff20d41 --- /dev/null +++ b/src/objects.rs @@ -0,0 +1,25 @@ +use serde::Deserialize; +use std::fs::File; +use std::io::Read; + +#[derive(Debug, Deserialize)] +#[allow(dead_code)] // evita os warnings de que non se le o campo +pub struct Object { + pub tag: String, // tamén é o nome do arquivo, mentras non se implementa bbdd + pub nombre: String, + pub descripcion: String, +} + +// get_object le o arquivo do obxeto e devolve o json +pub fn get_object(tag: String) -> Object { + let dir = "objects"; + let path = format!("{}/{}.json", dir, tag); + + let mut file = File::open(path).expect("Obxeto non atopado."); + let mut data = String::new(); + file.read_to_string(&mut data).expect("Error durante a lectura do obxeto."); + + let object: Object = serde_json::from_str(&data).expect("JSON was not well-formatted"); + + object +} diff --git a/src/rlocation.rs b/src/rlocation.rs index d310f44..80fe932 100644 --- a/src/rlocation.rs +++ b/src/rlocation.rs @@ -1,4 +1,5 @@ use crate::player::Player; +use crate::objects::get_object; use serde::Deserialize; use std::fs::File; use std::io::Read; @@ -9,6 +10,7 @@ pub struct Location { tag: String, // tamén é o nome do arquivo, mentras non se implementa bbdd descripcion: String, salidas: Salidas, + objetos: Vec, } #[derive(Debug, Deserialize)] @@ -22,16 +24,25 @@ pub struct Salidas { pub fn execute_look(player: &mut Player, nombre: &str) -> String { let output: String; + let player_location = &player.location; + let location = get_location(player_location.to_string()); if nombre == "sala" { - let player_location = &player.location; if player_location.as_str() != "" { - let location = get_location(player_location.to_string()); output = String::from(location.descripcion); } else { output = String::from("Navengado el ether!"); } } else { + // Recorre os obxetos da sala + for o in location.objetos { + let objeto = get_object(o); + if nombre == objeto.nombre { + output = String::from(objeto.descripcion); + return output; + } + } + output = String::from("Qué quieres mirar?") }