implement From for Image
This commit is contained in:
parent
2ec01ebfce
commit
163fddda5a
33
src/main.rs
33
src/main.rs
|
@ -424,23 +424,24 @@ fn example_game_default() -> Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn image_from_string(string: String) -> Image {
|
impl From<String> for Image {
|
||||||
let string = string.replace("\n", "");
|
#[inline]
|
||||||
let pixels: Vec<&str> = string.split("").collect();
|
fn from(string: String) -> Image {
|
||||||
// the above seems to add an extra "" at the start and end of the vec, so strip them below
|
let string = string.replace("\n", "");
|
||||||
let pixels = &pixels[1..(pixels.len() - 1)];
|
let pixels: Vec<&str> = string.split("").collect();
|
||||||
let pixels: Vec<u8> = pixels.iter().map(|&pixel| {
|
// the above seems to add an extra "" at the start and end of the vec, so strip them below
|
||||||
pixel.parse::<u8>().unwrap()
|
let pixels = &pixels[1..(pixels.len() - 1)];
|
||||||
}).collect();
|
let pixels: Vec<u8> = pixels.iter().map(|&pixel| {
|
||||||
|
pixel.parse::<u8>().unwrap()
|
||||||
|
}).collect();
|
||||||
|
|
||||||
Image { pixels }
|
Image { pixels }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_image_from_string() {
|
fn test_image_from_string() {
|
||||||
let output = image_from_string(
|
let output = Image::from(include_str!("../test/resources/image").to_string());
|
||||||
include_str!("../test/resources/image").to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let expected = Image {
|
let expected = Image {
|
||||||
pixels: vec![
|
pixels: vec![
|
||||||
|
@ -532,7 +533,7 @@ fn tile_from_string(string: String) -> Tile {
|
||||||
let animation_frames = lines[1..].join("");
|
let animation_frames = lines[1..].join("");
|
||||||
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(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Tile {id, name, wall, animation_frames}
|
Tile {id, name, wall, animation_frames}
|
||||||
|
@ -751,7 +752,7 @@ fn avatar_from_string(string: String) -> Avatar {
|
||||||
let animation_frames: String = lines.join("\n");
|
let animation_frames: String = lines.join("\n");
|
||||||
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(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Avatar { animation_frames, room, position }
|
Avatar { animation_frames, room, position }
|
||||||
|
@ -816,7 +817,7 @@ fn sprite_from_string(string: String) -> Sprite {
|
||||||
let animation_frames = lines[1..].join("");
|
let animation_frames = lines[1..].join("");
|
||||||
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(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Sprite { id, name, animation_frames, dialogue, room, position }
|
Sprite { id, name, animation_frames, dialogue, room, position }
|
||||||
|
@ -876,7 +877,7 @@ fn item_from_string(string: String) -> Item {
|
||||||
let animation_frames = lines[1..].join("");
|
let animation_frames = lines[1..].join("");
|
||||||
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(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Item { id, name, animation_frames, dialogue }
|
Item { id, name, animation_frames, dialogue }
|
||||||
|
|
Loading…
Reference in New Issue