implement ToBase36 for u64; room id to u64; sprite id to u64; tile id to u64; sprite.dialogue -> sprite.dialogue_id

This commit is contained in:
2020-04-13 14:43:23 +01:00
parent f424c49301
commit c7f1d7220c
7 changed files with 48 additions and 37 deletions

View File

@@ -1,12 +1,12 @@
use crate::{AnimationFrames, Image, Position, mock};
use crate::{AnimationFrames, Image, Position, mock, from_base36, ToBase36};
#[derive(Debug, Eq, PartialEq)]
pub struct Sprite {
pub id: String, // lowercase base36
pub id: u64,
pub name: Option<String>,
pub animation_frames: Vec<Image>,
pub dialogue: Option<String>, /// dialogue id
pub room: String, /// room id
pub dialogue_id: Option<String>,
pub room_id: u64,
pub position: Position,
}
@@ -14,10 +14,10 @@ impl From<String> for Sprite {
fn from(string: String) -> Sprite {
let mut lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("SPR ", "");
let id = from_base36(&lines[0].replace("SPR ", ""));
let mut name = None;
let mut dialogue = None;
let mut room: Option<String> = None;
let mut dialogue_id: Option<String> = None;
let mut room_id: Option<u64> = None;
let mut position: Option<Position> = None;
for _ in 0..3 {
@@ -26,11 +26,11 @@ impl From<String> for Sprite {
if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("DLG") {
dialogue = Some(last_line.replace("DLG ", "").to_string());
dialogue_id = Some(last_line.replace("DLG ", "").to_string());
} else if last_line.starts_with("POS") {
let last_line = last_line.replace("POS ", "");
let room_position: Vec<&str> = last_line.split(' ').collect();
room = Some(room_position[0].to_string());
room_id = Some(from_base36(&room_position[0]));
position = Some(Position::from(room_position[1].to_string()));
} else {
lines.push(last_line);
@@ -38,7 +38,7 @@ impl From<String> for Sprite {
}
}
let room = room.unwrap();
let room_id = room_id.unwrap();
let position = position.unwrap();
// todo dedupe
@@ -48,7 +48,7 @@ impl From<String> for Sprite {
Image::from(frame.to_string())
}).collect();
Sprite { id, name, animation_frames, dialogue, room, position }
Sprite { id, name, animation_frames, dialogue_id, room_id, position }
}
}
@@ -57,11 +57,11 @@ impl ToString for Sprite {
fn to_string(&self) -> String {
format!(
"SPR {}\n{}{}{}\nPOS {} {}",
self.id,
self.id.to_base36(),
self.animation_frames.to_string(),
if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() },
if self.dialogue.as_ref().is_some() { format!("\nDLG {}", self.dialogue.as_ref().unwrap()) } else { "".to_string() },
self.room,
if self.dialogue_id.as_ref().is_some() { format!("\nDLG {}", self.dialogue_id.as_ref().unwrap()) } else { "".to_string() },
self.room_id.to_base36(),
self.position.to_string(),
)
}