room/position for sprite are actually optional! you can make a sprite and never place it anywhere;

colour id for sprite
This commit is contained in:
Max Bradbury 2020-04-14 00:38:50 +01:00
parent 389cfe5f10
commit b479275d7e
1 changed files with 29 additions and 11 deletions

View File

@ -6,8 +6,9 @@ pub struct Sprite {
pub name: Option<String>,
pub animation_frames: Vec<Image>,
pub dialogue_id: Option<String>,
pub room_id: u64,
pub position: Position,
pub room_id: Option<u64>,
pub position: Option<Position>,
pub colour_id: Option<u64>,
}
impl Sprite {
@ -18,6 +19,18 @@ impl Sprite {
fn dialogue_line(&self) -> String {
optional_data_line("DLG", self.dialogue_id.as_ref())
}
fn room_position_line(&self) -> String {
if self.room_id.is_some() && self.position.is_some() {
format!("\nPOS {} {}", self.room_id.unwrap().to_base36(), self.position.as_ref().unwrap().to_string())
} else {
"".to_string()
}
}
fn colour_line(&self) -> String {
optional_data_line("COL", self.colour_id.as_ref())
}
}
impl From<String> for Sprite {
@ -29,8 +42,9 @@ impl From<String> for Sprite {
let mut dialogue_id: Option<String> = None;
let mut room_id: Option<u64> = None;
let mut position: Option<Position> = None;
let mut colour_id: Option<u64> = None;
for _ in 0..3 {
loop {
let last_line = lines.pop().unwrap();
if last_line.starts_with("NAME") {
@ -41,24 +55,28 @@ impl From<String> for Sprite {
let last_line = last_line.replace("POS ", "");
let room_position: Vec<&str> = last_line.split(' ').collect();
room_id = Some(from_base36(&room_position[0]));
if room_position.len() < 2 {
panic!("Bad room/position for sprite: {}", string);
}
position = Some(Position::from(room_position[1].to_string()));
} else if last_line.starts_with("COL") {
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
} else {
lines.push(last_line);
break;
}
}
let room_id = room_id.unwrap();
let position = position.unwrap();
// todo dedupe
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| {
Image::from(frame.to_string())
}).collect();
Sprite { id, name, animation_frames, dialogue_id, room_id, position }
Sprite { id, name, animation_frames, dialogue_id, room_id, position, colour_id }
}
}
@ -66,13 +84,13 @@ impl ToString for Sprite {
#[inline]
fn to_string(&self) -> String {
format!(
"SPR {}\n{}{}{}\nPOS {} {}",
"SPR {}\n{}{}{}{}{}",
self.id.to_base36(),
self.animation_frames.to_string(),
self.name_line(),
self.dialogue_line(),
self.room_id.to_base36(),
self.position.to_string(),
self.room_position_line(),
self.colour_line(),
)
}
}