item to string; dedupe

This commit is contained in:
Max Bradbury 2020-04-06 09:02:52 +01:00
parent 5f531d9d1d
commit ad43f73027
2 changed files with 44 additions and 22 deletions

View File

@ -19,12 +19,12 @@ a library for parsing Bitsy game data.
* sprite from
* sprite to
* item from
* item to
## todo
### functions
* item to
* room from
* room to
* dialogue from

View File

@ -61,6 +61,28 @@ fn test_sprite() -> Sprite {
}
}
fn test_item() -> Item {
Item {
id: "6".to_string(),
animation_frames: vec![
Image {
pixels: vec![
0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,
0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,
]
}
],
name: Some("door".to_string()),
dialogue: Some("ITM_2".to_string())
}
}
#[derive(Debug, Eq, PartialEq)]
struct Colour {
red: u8,
@ -510,38 +532,38 @@ fn item_from_string(string: String) -> Item {
}
}
// todo dedupe
let animation_frames = lines[1..].join("");
// print!("{}", animation_frames);
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
}).collect();
Item { id, name, animation_frames, dialogue}
Item { id, name, animation_frames, dialogue }
}
#[test]
fn test_item_from_string() {
let output = item_from_string("ITM 6\n01000000\n00000000\n00000000\n00000100\n00100000\n00000000\n00000000\n00000010\nNAME door\nDLG ITM_2".to_string());
let expected = Item {
id: "6".to_string(),
animation_frames: vec![
Image {
pixels: vec![
0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,1,0,0,
0,0,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,
]
}
],
name: Some("door".to_string()),
dialogue: Some("ITM_2".to_string())
};
let expected = test_item();
assert_eq!(output, expected);
}
fn item_to_string(item: Item) -> String {
format!(
"ITM {}\n{}{}{}",
item.id,
animation_frames_to_string(item.animation_frames),
if item.name.is_some() {format!("\nNAME {}", item.name.unwrap())} else {"".to_string()},
if item.dialogue.is_some() {format!("\nDLG {}", item.dialogue.unwrap())} else {"".to_string()},
)
}
#[test]
fn test_item_to_string() {
let output = item_to_string(test_item());
let expected = "ITM 6\n01000000\n00000000\n00000000\n00000100\n00100000\n00000000\n00000000\n00000010\nNAME door\nDLG ITM_2".to_string();
assert_eq!(output, expected);
}