diff --git a/src/lib.rs b/src/lib.rs index eddc1ab..7468e6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,45 +2,39 @@ pub mod colour; pub mod palette; pub mod image; pub mod mocks; +pub mod tile; use colour::Colour; use palette::Palette; use image::Image; +use tile::Tile; #[derive(Debug, Eq, PartialEq)] -struct Tile { - id: String, // base36 string - name: Option, - wall: bool, - animation_frames: Vec, -} - -#[derive(Debug, Eq, PartialEq)] -struct Position { +pub struct Position { x: u8, y: u8, } #[derive(Debug, Eq, PartialEq)] -struct Instance { +pub struct Instance { position: Position, id: String, // item / ending id } #[derive(Debug, Eq, PartialEq)] -struct ExitInstance { +pub struct ExitInstance { position: Position, exit: Exit, } #[derive(Debug, Eq, PartialEq)] -struct Dialogue { +pub struct Dialogue { id: String, contents: String, } #[derive(Debug, Eq, PartialEq)] -struct Sprite { +pub struct Sprite { id: String, // lowercase base36 name: Option, animation_frames: Vec, @@ -58,7 +52,7 @@ pub struct Avatar { } #[derive(Debug, Eq, PartialEq)] -struct Item { +pub struct Item { id: String, animation_frames: Vec, name: Option, @@ -66,7 +60,7 @@ struct Item { } #[derive(Debug, Eq, PartialEq)] -struct Exit { +pub struct Exit { /// destination room: String, /// id position: Position, @@ -74,13 +68,13 @@ struct Exit { // same as a dialogue basically #[derive(Debug, Eq, PartialEq)] -struct Ending { +pub struct Ending { id: String, dialogue: String, } #[derive(Debug, Eq, PartialEq)] -struct Room { +pub struct Room { id: String, palette: String, // id name: Option, @@ -91,13 +85,13 @@ struct Room { } #[derive(Debug, Eq, PartialEq)] -struct Variable { +pub struct Variable { id: String, initial_value: String, } #[derive(Debug, PartialEq)] -struct Game { +pub struct Game { name: String, version: f64, room_format: u8, @@ -373,88 +367,6 @@ impl AnimationFrames for Vec { } } -impl From for Tile { - fn from(string: String) -> Tile { - let mut lines: Vec<&str> = string.lines().collect(); - - let id = lines[0].replace("TIL ", ""); - - let last_line = lines.pop().unwrap(); - let wall = match last_line == "WAL true" { - true => true, - false => { - lines.push(last_line); - false - } - }; - - let last_line = lines.pop().unwrap(); - let name = match last_line.starts_with("NAME") { - true => Some(last_line.replace("NAME ", "").to_string()), - false => { - lines.push(last_line); - None - } - }; - - let animation_frames = lines[1..].join(""); - let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); - let animation_frames: Vec = animation_frames.iter().map(|&frame| { - Image::from(frame.to_string()) - }).collect(); - - Tile { id, name, wall, animation_frames } - } -} - -#[test] -fn test_tile_from_string() { - let output = Tile::from(include_str!("../test/resources/tile").to_string()); - - let expected = Tile { - id: "z".to_string(), - name: Some("concrete 1".to_string()), - wall: true, - animation_frames: vec![ - Image { - pixels: vec![1; 64] - } - ], - }; - - assert_eq!(output, expected); -} - -impl ToString for Tile { - #[inline] - fn to_string(&self) -> String { - format!( - "TIL {}\n{}{}{}", - self.id, - self.animation_frames.to_string(), - if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap())} else {"".to_string() }, - if self.wall {"\nWAL true"} else {""} - ) - } -} - -#[test] -fn test_tile_to_string() { - let output = Tile { - id: "7a".to_string(), - name: Some("chequers".to_string()), - wall: false, - animation_frames: vec![ - mocks::image::chequers_1(), - mocks::image::chequers_2(), - ] - }.to_string(); - - let expected = include_str!("../test/resources/tile-chequers").to_string(); - - assert_eq!(output, expected); -} - impl From for Position { fn from(string: String) -> Position { // e.g. "2,5" diff --git a/src/tile.rs b/src/tile.rs new file mode 100644 index 0000000..3e07001 --- /dev/null +++ b/src/tile.rs @@ -0,0 +1,93 @@ +use crate::AnimationFrames; +use crate::image::Image; +use crate::mocks; + +#[derive(Debug, Eq, PartialEq)] +pub struct Tile { + pub id: String, // base36 string + pub name: Option, + pub wall: bool, + pub animation_frames: Vec, +} + +impl From for Tile { + fn from(string: String) -> Tile { + let mut lines: Vec<&str> = string.lines().collect(); + + let id = lines[0].replace("TIL ", ""); + + let last_line = lines.pop().unwrap(); + let wall = match last_line == "WAL true" { + true => true, + false => { + lines.push(last_line); + false + } + }; + + let last_line = lines.pop().unwrap(); + let name = match last_line.starts_with("NAME") { + true => Some(last_line.replace("NAME ", "").to_string()), + false => { + lines.push(last_line); + None + } + }; + + let animation_frames = lines[1..].join(""); + let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); + let animation_frames: Vec = animation_frames.iter().map(|&frame| { + Image::from(frame.to_string()) + }).collect(); + + Tile { id, name, wall, animation_frames } + } +} + +impl ToString for Tile { + #[inline] + fn to_string(&self) -> String { + format!( + "TIL {}\n{}{}{}", + self.id, + self.animation_frames.to_string(), + if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap())} else {"".to_string() }, + if self.wall {"\nWAL true"} else {""} + ) + } +} + +#[test] +fn test_tile_from_string() { + let output = Tile::from(include_str!("../test/resources/tile").to_string()); + + let expected = Tile { + id: "z".to_string(), + name: Some("concrete 1".to_string()), + wall: true, + animation_frames: vec![ + Image { + pixels: vec![1; 64] + } + ], + }; + + assert_eq!(output, expected); +} + +#[test] +fn test_tile_to_string() { + let output = Tile { + id: "7a".to_string(), + name: Some("chequers".to_string()), + wall: false, + animation_frames: vec![ + mocks::image::chequers_1(), + mocks::image::chequers_2(), + ] + }.to_string(); + + let expected = include_str!("../test/resources/tile-chequers").to_string(); + + assert_eq!(output, expected); +}