implement optional palette id for room

This commit is contained in:
Max Bradbury 2020-04-18 15:50:01 +01:00
parent c441fce485
commit 78b2319d4f
2 changed files with 15 additions and 7 deletions

View File

@ -143,7 +143,7 @@ pub fn item() -> Item {
pub fn room() -> Room {
Room {
id: 10,
palette_id: 9,
palette_id: Some(9),
name: Some("cellar 7".to_string()),
tiles: vec![
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"1l".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),
@ -209,7 +209,7 @@ pub fn game_default() -> Game {
rooms: vec![
Room {
id: 0,
palette_id: 0,
palette_id: Some(0),
name: None,
tiles: vec![
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),

View File

@ -3,7 +3,7 @@ use crate::{Exit, ExitInstance, Instance, Position, from_base36, ToBase36, optio
#[derive(Debug, Eq, PartialEq)]
pub struct Room {
pub id: u64,
pub palette_id: u64, // id
pub palette_id: Option<u64>, // optional in very early versions
pub name: Option<String>,
pub tiles: Vec<String>, // tile ids
pub items: Vec<Instance>,
@ -25,6 +25,14 @@ impl Room {
"".to_string()
}
}
fn palette_line(&self) -> String {
if self.palette_id.is_some() {
optional_data_line("PAL", Some(self.palette_id.unwrap().to_base36()))
} else {
"".to_string()
}
}
}
impl From<String> for Room {
@ -34,7 +42,7 @@ impl From<String> for Room {
let mut lines: Vec<&str> = string.lines().collect();
let id = from_base36(&lines[0]);
let mut name = None;
let mut palette_id = 0;
let mut palette_id = None;
let mut items: Vec<Instance> = Vec::new();
let mut exits: Vec<ExitInstance> = Vec::new();
let mut endings: Vec<Instance> = Vec::new();
@ -50,7 +58,7 @@ impl From<String> for Room {
} 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 ", ""));
palette_id = Some(from_base36(&last_line.replace("PAL ", "")));
} else if last_line.starts_with("ITM") {
let last_line = last_line.replace("ITM ", "");
let item_position: Vec<&str> = last_line.split(' ').collect();
@ -152,7 +160,7 @@ impl ToString for Room {
}
format!(
"ROOM {}\n{}{}{}{}{}{}\nPAL {}",
"ROOM {}\n{}{}{}{}{}{}{}",
self.id.to_base36(),
tiles,
self.name_line(),
@ -160,7 +168,7 @@ impl ToString for Room {
items,
exits,
endings,
self.palette_id.to_base36()
self.palette_line()
)
}
}