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