tile module

This commit is contained in:
Max Bradbury 2020-04-12 12:53:11 +01:00
parent a3bfdc68b1
commit a1782a6bed
2 changed files with 106 additions and 101 deletions

View File

@ -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<String>,
wall: bool,
animation_frames: Vec<Image>,
}
#[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<String>,
animation_frames: Vec<Image>,
@ -58,7 +52,7 @@ pub struct Avatar {
}
#[derive(Debug, Eq, PartialEq)]
struct Item {
pub struct Item {
id: String,
animation_frames: Vec<Image>,
name: Option<String>,
@ -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<String>,
@ -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<Image> {
}
}
impl From<String> 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<Image> = 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<String> for Position {
fn from(string: String) -> Position {
// e.g. "2,5"

93
src/tile.rs Normal file
View File

@ -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<String>,
pub wall: bool,
pub animation_frames: Vec<Image>,
}
impl From<String> 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<Image> = 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);
}