exit module

This commit is contained in:
Max Bradbury 2020-04-12 13:49:19 +01:00
parent b887e178bb
commit 6090d36559
2 changed files with 43 additions and 40 deletions

41
src/exit.rs Normal file
View File

@ -0,0 +1,41 @@
use crate::Position;
#[derive(Debug, Eq, PartialEq)]
pub struct Exit {
/// destination
pub(crate) room: String, /// id
pub(crate) position: Position,
}
impl From<String> for Exit {
fn from(string: String) -> Exit {
// e.g. "4 3,3"
let room_position: Vec<&str> = string.split(' ').collect();
let room = room_position[0].to_string();
let position = Position::from(room_position[1].to_string());
Exit { room, position }
}
}
impl ToString for Exit {
fn to_string(&self) -> String {
format!("{} {}", self.room, self.position.to_string())
}
}
#[test]
fn test_exit_from_string() {
assert_eq!(
Exit::from("a 12,13".to_string()),
Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
);
}
#[test]
fn test_exit_to_string() {
assert_eq!(
Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(),
"8 5,6".to_string()
);
}

View File

@ -1,6 +1,7 @@
pub mod avatar; pub mod avatar;
pub mod colour; pub mod colour;
pub mod dialogue; pub mod dialogue;
pub mod exit;
pub mod palette; pub mod palette;
pub mod image; pub mod image;
pub mod item; pub mod item;
@ -12,6 +13,7 @@ pub mod tile;
use avatar::Avatar; use avatar::Avatar;
use colour::Colour; use colour::Colour;
use dialogue::Dialogue; use dialogue::Dialogue;
use exit::Exit;
use palette::Palette; use palette::Palette;
use image::Image; use image::Image;
use item::Item; use item::Item;
@ -31,13 +33,6 @@ pub struct ExitInstance {
exit: Exit, exit: Exit,
} }
#[derive(Debug, Eq, PartialEq)]
pub struct Exit {
/// destination
room: String, /// id
position: Position,
}
// same as a dialogue basically // same as a dialogue basically
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Ending { pub struct Ending {
@ -290,39 +285,6 @@ impl AnimationFrames for Vec<Image> {
} }
} }
impl From<String> for Exit {
fn from(string: String) -> Exit {
// e.g. "4 3,3"
let room_position: Vec<&str> = string.split(' ').collect();
let room = room_position[0].to_string();
let position = Position::from(room_position[1].to_string());
Exit { room, position }
}
}
#[test]
fn test_exit_from_string() {
assert_eq!(
Exit::from("a 12,13".to_string()),
Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
);
}
impl ToString for Exit {
fn to_string(&self) -> String {
format!("{} {}", self.room, self.position.to_string())
}
}
#[test]
fn test_exit_to_string() {
assert_eq!(
Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(),
"8 5,6".to_string()
);
}
impl From<String> for Ending { impl From<String> for Ending {
fn from(string: String) -> Ending { fn from(string: String) -> Ending {
let string = string.replace("END ", ""); let string = string.replace("END ", "");