handle room wall array

This commit is contained in:
2020-04-18 14:39:17 +01:00
parent 7c566ab9d3
commit e71c87ad4c
4 changed files with 33 additions and 12 deletions

View File

@@ -9,12 +9,22 @@ pub struct Room {
pub items: Vec<Instance>,
pub exits: Vec<ExitInstance>,
pub endings: Vec<Instance>,
pub walls: Vec<u64>, // old way of handling walls...
}
impl Room {
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
fn wall_line(&self) -> String {
if self.walls.len() > 0 {
let ids: Vec<String> = self.walls.iter().map(|&id| id.to_base36()).collect();
optional_data_line("WAL", Some(ids.join(",")))
} else {
"".to_string()
}
}
}
impl From<String> for Room {
@@ -27,11 +37,16 @@ impl From<String> for Room {
let mut items: Vec<Instance> = Vec::new();
let mut exits: Vec<ExitInstance> = Vec::new();
let mut endings: Vec<Instance> = Vec::new();
let mut walls: Vec<u64> = Vec::new();
loop {
let last_line = lines.pop().unwrap();
if last_line.starts_with("NAME") {
if last_line.starts_with("WAL") {
let last_line = last_line.replace("WAL ", "");
let ids: Vec<&str> = last_line.split(",").collect();
walls = ids.iter().map(|&id| from_base36(id)).collect();
} else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("PAL") {
palette_id = from_base36(&last_line.replace("PAL ", ""));
@@ -80,7 +95,7 @@ impl From<String> for Room {
exits.reverse();
endings.reverse();
Room { id, palette_id, name, tiles, items, exits, endings }
Room { id, palette_id, name, tiles, items, exits, endings, walls }
}
}
@@ -132,10 +147,11 @@ impl ToString for Room {
}
format!(
"ROOM {}\n{}{}{}{}{}\nPAL {}",
"ROOM {}\n{}{}{}{}{}{}\nPAL {}",
self.id.to_base36(),
tiles,
self.name_line(),
self.wall_line(),
items,
exits,
endings,
@@ -148,3 +164,12 @@ impl ToString for Room {
fn test_room_to_string() {
assert_eq!(crate::mock::room().to_string(), include_str!("test-resources/room").to_string());
}
#[test]
fn test_room_walls_array() {
let output = Room::from(
include_str!("test-resources/room-with-walls").to_string()
);
assert_eq!(output.walls, vec![10, 15]);
}