take room out of position struct as they're really separate
This commit is contained in:
parent
c38d369725
commit
95cb1d6039
33
src/main.rs
33
src/main.rs
|
@ -33,7 +33,6 @@ struct Tile {
|
|||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
struct Position {
|
||||
room: String, // id. is room id int or base36 string?
|
||||
x: u8,
|
||||
y: u8,
|
||||
}
|
||||
|
@ -50,6 +49,7 @@ struct Sprite {
|
|||
name: Option<String>,
|
||||
animation_frames: Vec<Image>,
|
||||
dialogue: Option<String>, /// dialogue id
|
||||
room: String, /// room id
|
||||
position: Position,
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,7 @@ struct Sprite {
|
|||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct Avatar {
|
||||
animation_frames: Vec<Image>,
|
||||
room: String, /// room id
|
||||
position: Position,
|
||||
}
|
||||
|
||||
|
@ -419,33 +420,31 @@ fn test_palette_from_string_no_name() {
|
|||
}
|
||||
|
||||
fn position_from_string(string: String) -> Position {
|
||||
// e.g. "0 2,5"
|
||||
let room_xy: Vec<&str> = string.split(' ').collect();
|
||||
let xy: Vec<&str> = room_xy[1].split(',').collect();
|
||||
let room = room_xy[0].to_string();
|
||||
// e.g. "2,5"
|
||||
let xy: Vec<&str> = string.split(',').collect();
|
||||
let x = xy[0].parse().unwrap();
|
||||
let y = xy[1].parse().unwrap();
|
||||
|
||||
Position {room, x, y}
|
||||
Position {x, y}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_from_string() {
|
||||
assert_eq!(
|
||||
position_from_string("5 4,12".to_string()),
|
||||
Position { room: "5".to_string(), x: 4, y: 12 }
|
||||
position_from_string("4,12".to_string()),
|
||||
Position { x: 4, y: 12 }
|
||||
)
|
||||
}
|
||||
|
||||
fn position_to_string(position: Position) -> String {
|
||||
format!("{} {},{}", position.room, position.x, position.y)
|
||||
format!("{},{}", position.x, position.y)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_to_string() {
|
||||
assert_eq!(
|
||||
position_to_string(Position { room: "5".to_string(), x: 4, y: 12 }),
|
||||
"5 4,12".to_string()
|
||||
position_to_string(Position { x: 4, y: 12 }),
|
||||
"4,12".to_string()
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -455,6 +454,7 @@ fn sprite_from_string(string: String) -> Sprite {
|
|||
let id = lines[0].replace("SPR ", "");
|
||||
let mut name = None;
|
||||
let mut dialogue = None;
|
||||
let mut room: Option<String> = None;
|
||||
let mut position: Option<Position> = None;
|
||||
|
||||
for _ in 0..3 {
|
||||
|
@ -465,25 +465,26 @@ fn sprite_from_string(string: String) -> Sprite {
|
|||
} 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()
|
||||
));
|
||||
let last_line = last_line.replace("POS ", "");
|
||||
let room_position: Vec<&str> = last_line.split(' ').collect();
|
||||
room = Some(room_position[0].to_string());
|
||||
position = Some(position_from_string(room_position[1].to_string()));
|
||||
} else {
|
||||
lines.push(last_line); break;
|
||||
}
|
||||
}
|
||||
|
||||
let room = room.unwrap();
|
||||
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 }
|
||||
Sprite { id, name, animation_frames, dialogue, room, position }
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue