reimplement avatar as a plain sprite; handle sprite items as these are mostly needed for avatar

This commit is contained in:
2020-04-26 13:31:20 +01:00
parent ed6552f39c
commit cf1ca6f1b5
6 changed files with 40 additions and 135 deletions

View File

@@ -9,6 +9,7 @@ pub struct Sprite {
pub room_id: Option<u64>,
pub position: Option<Position>,
pub colour_id: Option<u64>,
pub items: Vec<String>,
}
impl Sprite {
@@ -35,6 +36,15 @@ impl Sprite {
fn colour_line(&self) -> String {
optional_data_line("COL", self.colour_id.as_ref())
}
fn item_lines(&self) -> String {
if self.items.len() == 0 {
"".to_string()
} else {
let lines: Vec<String> = self.items.iter().map(|item| format!("ITM {}", item)).collect();
format!("\n{}", lines.join("\n"))
}
}
}
impl From<String> for Sprite {
@@ -47,6 +57,7 @@ impl From<String> for Sprite {
let mut room_id: Option<u64> = None;
let mut position: Option<Position> = None;
let mut colour_id: Option<u64> = None;
let mut items: Vec<String> = Vec::new();
loop {
let last_line = lines.pop().unwrap();
@@ -67,12 +78,16 @@ impl From<String> for Sprite {
position = Some(Position::from(room_position[1].to_string()).unwrap());
} else if last_line.starts_with("COL") {
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
} else if last_line.starts_with("ITM") {
items.push(last_line.replace("ITM ", ""));
} else {
lines.push(last_line);
break;
}
}
items.reverse();
// todo dedupe
let animation_frames = lines[1..].join("");
let animation_frames: Vec<&str> = animation_frames.split(">").collect();
@@ -89,6 +104,7 @@ impl From<String> for Sprite {
room_id,
position,
colour_id,
items
}
}
}
@@ -97,13 +113,14 @@ impl ToString for Sprite {
#[inline]
fn to_string(&self) -> String {
format!(
"SPR {}\n{}{}{}{}{}",
"SPR {}\n{}{}{}{}{}{}",
self.id.to_base36(),
self.animation_frames.to_string(),
self.name_line(),
self.dialogue_line(),
self.room_position_line(),
self.colour_line(),
self.item_lines(),
)
}
}