From ad43f73027dbe8418a0688e061396a46f6aa1d50 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 09:02:52 +0100 Subject: [PATCH] item to string; dedupe --- README.md | 2 +- src/main.rs | 64 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 11e37d2..162102f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main.rs b/src/main.rs index 9472dfb..683b724 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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); }