make tile wall "optional" (specified or not); tile colour id
This commit is contained in:
parent
b479275d7e
commit
88c4f36e31
65
src/tile.rs
65
src/tile.rs
|
@ -6,14 +6,31 @@ use crate::mock;
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub wall: bool,
|
pub wall: Option<bool>, // this is "optional" in that a tile can have `WAL true|false` or neither
|
||||||
pub animation_frames: Vec<Image>,
|
pub animation_frames: Vec<Image>,
|
||||||
|
pub colour_id: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile {
|
impl Tile {
|
||||||
fn name_line(&self) -> String {
|
fn name_line(&self) -> String {
|
||||||
optional_data_line("NAME", self.name.as_ref())
|
optional_data_line("NAME", self.name.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wall_line(&self) -> String {
|
||||||
|
if self.wall.is_some() {
|
||||||
|
format!("\nWAL {}", self.wall.unwrap())
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn colour_line(&self) -> String {
|
||||||
|
if self.colour_id.is_some() {
|
||||||
|
format!("\nCOL {}", self.colour_id.unwrap())
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<String> for Tile {
|
impl From<String> for Tile {
|
||||||
|
@ -23,31 +40,32 @@ impl From<String> for Tile {
|
||||||
|
|
||||||
let id = from_base36(&lines[0].replace("TIL ", ""));
|
let id = from_base36(&lines[0].replace("TIL ", ""));
|
||||||
|
|
||||||
let last_line = lines.pop().unwrap();
|
let mut wall = None;
|
||||||
let wall = match last_line == "WAL true" {
|
let mut name = None;
|
||||||
true => true,
|
let mut colour_id = None;
|
||||||
false => {
|
|
||||||
lines.push(last_line);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let last_line = lines.pop().unwrap();
|
loop {
|
||||||
let name = match last_line.starts_with("NAME") {
|
let last_line = lines.pop().unwrap();
|
||||||
true => Some(last_line.replace("NAME ", "").to_string()),
|
|
||||||
false => {
|
if last_line.starts_with("WAL") {
|
||||||
|
wall = Some(last_line.ends_with("true"));
|
||||||
|
} else if last_line.starts_with("NAME") {
|
||||||
|
name = Some(last_line.replace("NAME ", "").to_string());
|
||||||
|
} else if last_line.starts_with("COL") {
|
||||||
|
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
|
||||||
|
} else {
|
||||||
lines.push(last_line);
|
lines.push(last_line);
|
||||||
None
|
break;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
let animation_frames = lines[1..].join("");
|
let animation_frames = lines[1..].join("");
|
||||||
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
let animation_frames: Vec<&str> = animation_frames.split(">").collect();
|
||||||
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
||||||
Image::from(frame.to_string())
|
Image::from(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Tile { id, name, wall, animation_frames }
|
Tile { id, name, wall, animation_frames, colour_id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,11 +73,12 @@ impl ToString for Tile {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
"TIL {}\n{}{}{}",
|
"TIL {}\n{}{}{}{}",
|
||||||
self.id.to_base36(),
|
self.id.to_base36(),
|
||||||
self.animation_frames.to_string(),
|
self.animation_frames.to_string(),
|
||||||
self.name_line(),
|
self.name_line(),
|
||||||
if self.wall {"\nWAL true"} else {""}
|
self.wall_line(),
|
||||||
|
self.colour_line(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,12 +90,13 @@ fn test_tile_from_string() {
|
||||||
let expected = Tile {
|
let expected = Tile {
|
||||||
id: 35,
|
id: 35,
|
||||||
name: Some("concrete 1".to_string()),
|
name: Some("concrete 1".to_string()),
|
||||||
wall: true,
|
wall: Some(true),
|
||||||
animation_frames: vec![
|
animation_frames: vec![
|
||||||
Image {
|
Image {
|
||||||
pixels: vec![1; 64]
|
pixels: vec![1; 64]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
colour_id: None
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
|
@ -87,11 +107,12 @@ fn test_tile_to_string() {
|
||||||
let output = Tile {
|
let output = Tile {
|
||||||
id: 262,
|
id: 262,
|
||||||
name: Some("chequers".to_string()),
|
name: Some("chequers".to_string()),
|
||||||
wall: false,
|
wall: None,
|
||||||
animation_frames: vec![
|
animation_frames: vec![
|
||||||
mock::image::chequers_1(),
|
mock::image::chequers_1(),
|
||||||
mock::image::chequers_2(),
|
mock::image::chequers_2(),
|
||||||
]
|
],
|
||||||
|
colour_id: None
|
||||||
}.to_string();
|
}.to_string();
|
||||||
|
|
||||||
let expected = include_str!("test-resources/tile-chequers").to_string();
|
let expected = include_str!("test-resources/tile-chequers").to_string();
|
||||||
|
|
Loading…
Reference in New Issue