From b479275d7e2276fd80b8c8313470bf065efa9c16 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 14 Apr 2020 00:38:50 +0100 Subject: [PATCH] room/position for sprite are actually optional! you can make a sprite and never place it anywhere; colour id for sprite --- src/sprite.rs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/sprite.rs b/src/sprite.rs index ca0c136..e6249a2 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -6,8 +6,9 @@ pub struct Sprite { pub name: Option, pub animation_frames: Vec, pub dialogue_id: Option, - pub room_id: u64, - pub position: Position, + pub room_id: Option, + pub position: Option, + pub colour_id: Option, } 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 for Sprite { @@ -29,8 +42,9 @@ impl From for Sprite { let mut dialogue_id: Option = None; let mut room_id: Option = None; let mut position: Option = None; + let mut colour_id: Option = None; - for _ in 0..3 { + loop { let last_line = lines.pop().unwrap(); if last_line.starts_with("NAME") { @@ -41,24 +55,28 @@ impl From 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 = 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(), ) } }