empezando cos obxetos

This commit is contained in:
serxoz 2022-09-08 19:13:08 +02:00
parent 211acf60ea
commit 1a1c1c5fd8
5 changed files with 47 additions and 4 deletions

View File

@ -1,10 +1,11 @@
{ {
"tag": "0", "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": { "salidas": {
"norte": "1", "norte": "1",
"este": "", "este": "",
"sur": "", "sur": "",
"oeste": "" "oeste": ""
} },
"objetos": ["o0"]
} }

5
objects/o0.json Normal file
View File

@ -0,0 +1,5 @@
{
"tag": "o0",
"nombre": "palanca",
"descripcion": "Trozo de metal, servirá como palanca."
}

View File

@ -1,6 +1,7 @@
pub mod rlib; pub mod rlib;
pub mod rlocation; pub mod rlocation;
pub mod player; pub mod player;
pub mod objects;
use crate::player::Player; use crate::player::Player;

25
src/objects.rs Normal file
View File

@ -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
}

View File

@ -1,4 +1,5 @@
use crate::player::Player; use crate::player::Player;
use crate::objects::get_object;
use serde::Deserialize; use serde::Deserialize;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -9,6 +10,7 @@ pub struct Location {
tag: String, // tamén é o nome do arquivo, mentras non se implementa bbdd tag: String, // tamén é o nome do arquivo, mentras non se implementa bbdd
descripcion: String, descripcion: String,
salidas: Salidas, salidas: Salidas,
objetos: Vec<String>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -22,16 +24,25 @@ pub struct Salidas {
pub fn execute_look(player: &mut Player, nombre: &str) -> String { pub fn execute_look(player: &mut Player, nombre: &str) -> String {
let output: String; let output: String;
let player_location = &player.location;
let location = get_location(player_location.to_string());
if nombre == "sala" { if nombre == "sala" {
let player_location = &player.location;
if player_location.as_str() != "" { if player_location.as_str() != "" {
let location = get_location(player_location.to_string());
output = String::from(location.descripcion); output = String::from(location.descripcion);
} else { } else {
output = String::from("Navengado el ether!"); output = String::from("Navengado el ether!");
} }
} else { } 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?") output = String::from("Qué quieres mirar?")
} }