implement From for Tile
This commit is contained in:
parent
163fddda5a
commit
7b941b5d99
58
src/main.rs
58
src/main.rs
|
@ -507,43 +507,43 @@ impl AnimationFrames for Vec<Image> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tile_from_string(string: String) -> Tile {
|
impl From<String> for Tile {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
fn from(string: String) -> Tile {
|
||||||
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
|
|
||||||
let id = lines[0].replace("TIL ", "");
|
let id = lines[0].replace("TIL ", "");
|
||||||
|
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
let wall = match last_line == "WAL true" {
|
let wall = match last_line == "WAL true" {
|
||||||
true => true,
|
true => true,
|
||||||
false => {
|
false => {
|
||||||
lines.push(last_line);
|
lines.push(last_line);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
let name = match last_line.starts_with("NAME") {
|
let name = match last_line.starts_with("NAME") {
|
||||||
true => Some(last_line.replace("NAME ", "").to_string()),
|
true => Some(last_line.replace("NAME ", "").to_string()),
|
||||||
false => {
|
false => {
|
||||||
lines.push(last_line);
|
lines.push(last_line);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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(frame.to_string())
|
Image::from(frame.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Tile {id, name, wall, animation_frames}
|
Tile { id, name, wall, animation_frames }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tile_from_string() {
|
fn test_tile_from_string() {
|
||||||
let output = tile_from_string(
|
let output = Tile::from(include_str!("../test/resources/tile").to_string());
|
||||||
include_str!("../test/resources/tile").to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let expected = Tile {
|
let expected = Tile {
|
||||||
id: "z".to_string(),
|
id: "z".to_string(),
|
||||||
|
@ -1237,7 +1237,7 @@ fn game_from_string(string: String ) -> Game {
|
||||||
} else if segment.starts_with("ROOM") {
|
} else if segment.starts_with("ROOM") {
|
||||||
rooms.push(room_from_string(segment));
|
rooms.push(room_from_string(segment));
|
||||||
} else if segment.starts_with("TIL") {
|
} else if segment.starts_with("TIL") {
|
||||||
tiles.push(tile_from_string(segment));
|
tiles.push(Tile::from(segment));
|
||||||
} else if segment.starts_with("SPR A") {
|
} else if segment.starts_with("SPR A") {
|
||||||
avatar = Some(avatar_from_string(segment));
|
avatar = Some(avatar_from_string(segment));
|
||||||
} else if segment.starts_with("SPR") {
|
} else if segment.starts_with("SPR") {
|
||||||
|
|
Loading…
Reference in New Issue