sprite from string

This commit is contained in:
Max Bradbury 2020-04-05 23:58:10 +01:00
parent 894889afa7
commit 6d8f8fa9b4
2 changed files with 69 additions and 5 deletions

View File

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

View File

@ -49,7 +49,7 @@ struct Sprite {
id: String, // lowercase base36 id: String, // lowercase base36
name: Option<String>, name: Option<String>,
animation_frames: Vec<Image>, animation_frames: Vec<Image>,
dialogue: Option<Dialogue>, dialogue: Option<String>, /// dialogue id
position: Position, position: Position,
} }
@ -396,9 +396,73 @@ fn test_position_to_string() {
) )
} }
// fn get_avatar(game: Game) -> Sprite { fn sprite_from_string(string: String) -> Sprite {
// // get the sprite with an id of A let mut lines: Vec<&str> = string.split("\n").collect();
// }
let id = lines[0].replace("SPR ", "");
let mut name = None;
let mut dialogue = None;
let mut position: Option<Position> = None;
for _ in 0..3 {
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 if last_line.starts_with("POS") {
position = Some(position_from_string(
last_line.replace("POS ", "").to_string()
));
} else {
lines.push(last_line); break;
}
}
let position = position.unwrap();
// 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();
Sprite { id, name, animation_frames, dialogue, position }
}
#[test]
fn test_sprite_from_string() {
let output = sprite_from_string("SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string());
let expected = Sprite {
id: "a".to_string(),
name: Some("hatch".to_string()),
animation_frames: vec![
Image {
pixels: vec![
0,0,0,0,0,0,0,0,
0,1,1,1,1,0,0,0,
0,1,0,0,1,0,0,0,
0,0,1,1,1,1,0,0,
0,0,1,1,1,1,0,0,
0,1,0,1,1,1,1,0,
0,1,0,1,1,1,1,0,
0,1,1,0,1,1,1,1,
]
}
],
dialogue: Some("SPR_0".to_string()),
position: Position {
room: "4".to_string(),
x: 9,
y: 7
}
};
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