function for optional lines of game data
This commit is contained in:
parent
85f6501b2c
commit
b9dac0932f
16
src/lib.rs
16
src/lib.rs
|
@ -30,6 +30,7 @@ use room::Room;
|
|||
use sprite::Sprite;
|
||||
use tile::Tile;
|
||||
use variable::Variable;
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Instance {
|
||||
|
@ -95,3 +96,18 @@ impl ToBase36 for u64 {
|
|||
fn test_to_base36() {
|
||||
assert_eq!((37 as u64).to_base36(), "11");
|
||||
}
|
||||
|
||||
/// e.g. `\nNAME DLG_0`
|
||||
fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
|
||||
if item.is_some() {
|
||||
format!("\n{} {}", label, item.unwrap())
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional_data_line() {
|
||||
let output = optional_data_line("NAME", mock::item().name);
|
||||
assert_eq!(output, "\nNAME door".to_string());
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{AnimationFrames, Image, Position, mock, from_base36, ToBase36};
|
||||
use crate::{AnimationFrames, Image, Position, mock, from_base36, ToBase36, optional_data_line};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Sprite {
|
||||
|
@ -10,6 +10,16 @@ pub struct Sprite {
|
|||
pub position: Position,
|
||||
}
|
||||
|
||||
impl Sprite {
|
||||
fn name_line(&self) -> String {
|
||||
optional_data_line("NAME", self.name.as_ref())
|
||||
}
|
||||
|
||||
fn dialogue_line(&self) -> String {
|
||||
optional_data_line("DLG", self.dialogue_id.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Sprite {
|
||||
fn from(string: String) -> Sprite {
|
||||
let mut lines: Vec<&str> = string.lines().collect();
|
||||
|
@ -59,8 +69,8 @@ impl ToString for Sprite {
|
|||
"SPR {}\n{}{}{}\nPOS {} {}",
|
||||
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_id.as_ref().is_some() { format!("\nDLG {}", self.dialogue_id.as_ref().unwrap()) } else { "".to_string() },
|
||||
self.name_line(),
|
||||
self.dialogue_line(),
|
||||
self.room_id.to_base36(),
|
||||
self.position.to_string(),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue