item from string

This commit is contained in:
Max Bradbury 2020-04-06 08:54:43 +01:00
parent d14f613f7a
commit 5f531d9d1d
2 changed files with 61 additions and 4 deletions

View File

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

View File

@ -110,7 +110,7 @@ struct Sprite {
position: Position, position: Position,
} }
/// avatar is a "sprite" in the game data but with a specific ID /// avatar is a "sprite" in the game data but with a specific id
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
struct Avatar { struct Avatar {
animation_frames: Vec<Image>, animation_frames: Vec<Image>,
@ -122,7 +122,7 @@ struct Item {
id: String, id: String,
animation_frames: Vec<Image>, animation_frames: Vec<Image>,
name: Option<String>, name: Option<String>,
dialogue: Option<Dialogue>, dialogue: Option<String>, // dialogue id
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
@ -491,12 +491,69 @@ fn test_sprite_to_string() {
assert_eq!(output, expected); assert_eq!(output, expected);
} }
fn item_from_string(string: String) -> Item {
let mut lines: Vec<&str> = string.split("\n").collect();
let id = lines[0].replace("ITM ", "");
let mut name = None;
let mut dialogue = None;
for _ in 0..2 {
let last_line = lines.pop().unwrap();
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());
} else {
lines.push(last_line); break;
}
}
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}
}
#[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())
};
assert_eq!(output, expected);
}
// fn game_from_string(game: String ) -> Game { // fn game_from_string(game: String ) -> Game {
// // probably needs to split the game data into different segments starting from the end // // probably needs to split the game data into different segments starting from the end
// // e.g. VAR... then END... then DLG... // // e.g. VAR... then END... then DLG...
// // then split all these up into their individual items // // then split all these up into their individual items
// }
// //
// // no - let's try going from the beginning and popping off elements from the start?
// }
// fn game_to_string(game: Game) -> String { // fn game_to_string(game: Game) -> String {
// //
// } // }