implement From<String>
This commit is contained in:
parent
b09b9dbece
commit
cd82c075be
529
src/main.rs
529
src/main.rs
|
@ -624,28 +624,30 @@ fn test_colour_to_string() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn palette_from_string(palette: String) -> Palette {
|
impl From<String> for Palette {
|
||||||
let lines: Vec<&str> = palette.split('\n').collect();
|
fn from(string: String) -> Palette {
|
||||||
|
let lines: Vec<&str> = string.lines().collect();
|
||||||
|
|
||||||
let id = lines[0].replace("PAL ", "");
|
let id = lines[0].replace("PAL ", "");
|
||||||
|
|
||||||
let name = match lines[1].starts_with("NAME") {
|
let name = match lines[1].starts_with("NAME") {
|
||||||
true => Some(lines[1].replace("NAME ", "").to_string()),
|
true => Some(lines[1].replace("NAME ", "").to_string()),
|
||||||
false => None,
|
false => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let colour_start_index = if name.is_some() {2} else {1};
|
let colour_start_index = if name.is_some() { 2 } else { 1 };
|
||||||
|
|
||||||
let colours = lines[colour_start_index..].iter().map(|&line| {
|
let colours = lines[colour_start_index..].iter().map(|&line| {
|
||||||
Colour::from(line.to_string())
|
Colour::from(line.to_string())
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
Palette { id, name, colours }
|
Palette { id, name, colours }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_palette_from_string() {
|
fn test_palette_from_string() {
|
||||||
let output = palette_from_string(
|
let output = Palette::from(
|
||||||
"PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string()
|
"PAL 1\nNAME lamplight\n45,45,59\n66,60,39\n140,94,1".to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -664,7 +666,7 @@ fn test_palette_from_string() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_palette_from_string_no_name() {
|
fn test_palette_from_string_no_name() {
|
||||||
let output = palette_from_string(
|
let output = Palette::from(
|
||||||
"PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string()
|
"PAL 9\n45,45,59\n66,60,39\n140,94,1".to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -715,21 +717,20 @@ fn test_palette_to_string() {
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn position_from_string(string: String) -> Position {
|
impl From<String> for Position {
|
||||||
// e.g. "2,5"
|
fn from(string: String) -> Position {
|
||||||
let xy: Vec<&str> = string.split(',').collect();
|
// e.g. "2,5"
|
||||||
let x = xy[0].parse().unwrap();
|
let xy: Vec<&str> = string.split(',').collect();
|
||||||
let y = xy[1].parse().unwrap();
|
let x = xy[0].parse().unwrap();
|
||||||
|
let y = xy[1].parse().unwrap();
|
||||||
|
|
||||||
Position {x, y}
|
Position { x, y }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_position_from_string() {
|
fn test_position_from_string() {
|
||||||
assert_eq!(
|
assert_eq!(Position::from("4,12".to_string()), Position { x: 4, y: 12 });
|
||||||
position_from_string("4,12".to_string()),
|
|
||||||
Position { x: 4, y: 12 }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Position {
|
impl ToString for Position {
|
||||||
|
@ -744,27 +745,30 @@ fn test_position_to_string() {
|
||||||
assert_eq!(Position { x: 4, y: 12 }.to_string(), "4,12".to_string())
|
assert_eq!(Position { x: 4, y: 12 }.to_string(), "4,12".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn avatar_from_string(string: String) -> Avatar {
|
impl From<String> for Avatar {
|
||||||
let string = string.replace("SPR A\n", "");
|
fn from(string: String) -> Avatar {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
let string = string.replace("SPR A\n", "");
|
||||||
let room_pos = lines.pop().unwrap().replace("POS ", "");
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
let room_pos: Vec<&str> = room_pos.split_whitespace().collect();
|
let room_pos = lines.pop().unwrap().replace("POS ", "");
|
||||||
let room = room_pos[0].to_string();
|
let room_pos: Vec<&str> = room_pos.split_whitespace().collect();
|
||||||
let position = position_from_string(room_pos[1].to_string());
|
let room = room_pos[0].to_string();
|
||||||
let animation_frames: String = lines.join("\n");
|
let position = Position::from(room_pos[1].to_string());
|
||||||
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
let animation_frames: String = lines.join("\n");
|
||||||
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
||||||
Image::from(frame.to_string())
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
||||||
}).collect();
|
Image::from(frame.to_string())
|
||||||
|
}).collect();
|
||||||
|
|
||||||
Avatar { animation_frames, room, position }
|
Avatar { animation_frames, room, position }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_avatar_from_string() {
|
fn test_avatar_from_string() {
|
||||||
let output = avatar_from_string(
|
let output = Avatar::from(
|
||||||
include_str!("../test/resources/avatar").to_string()
|
include_str!("../test/resources/avatar").to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
let expected = example_avatar();
|
let expected = example_avatar();
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
@ -786,48 +790,51 @@ fn test_avatar_to_string() {
|
||||||
assert_eq!(example_avatar().to_string(), include_str!("../test/resources/avatar"));
|
assert_eq!(example_avatar().to_string(), include_str!("../test/resources/avatar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sprite_from_string(string: String) -> Sprite {
|
impl From<String> for Sprite {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
fn from(string: String) -> Sprite {
|
||||||
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
|
|
||||||
let id = lines[0].replace("SPR ", "");
|
let id = lines[0].replace("SPR ", "");
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
let mut dialogue = None;
|
let mut dialogue = None;
|
||||||
let mut room: Option<String> = None;
|
let mut room: Option<String> = None;
|
||||||
let mut position: Option<Position> = None;
|
let mut position: Option<Position> = None;
|
||||||
|
|
||||||
for _ in 0..3 {
|
for _ in 0..3 {
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
|
|
||||||
if last_line.starts_with("NAME") {
|
if last_line.starts_with("NAME") {
|
||||||
name = Some(last_line.replace("NAME ", "").to_string());
|
name = Some(last_line.replace("NAME ", "").to_string());
|
||||||
} else if last_line.starts_with("DLG") {
|
} else if last_line.starts_with("DLG") {
|
||||||
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
||||||
} else if last_line.starts_with("POS") {
|
} else if last_line.starts_with("POS") {
|
||||||
let last_line = last_line.replace("POS ", "");
|
let last_line = last_line.replace("POS ", "");
|
||||||
let room_position: Vec<&str> = last_line.split(' ').collect();
|
let room_position: Vec<&str> = last_line.split(' ').collect();
|
||||||
room = Some(room_position[0].to_string());
|
room = Some(room_position[0].to_string());
|
||||||
position = Some(position_from_string(room_position[1].to_string()));
|
position = Some(Position::from(room_position[1].to_string()));
|
||||||
} else {
|
} else {
|
||||||
lines.push(last_line); break;
|
lines.push(last_line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let room = room.unwrap();
|
||||||
|
let position = position.unwrap();
|
||||||
|
|
||||||
|
// todo dedupe
|
||||||
|
let animation_frames = lines[1..].join("");
|
||||||
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
||||||
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
||||||
|
Image::from(frame.to_string())
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
Sprite { id, name, animation_frames, dialogue, room, position }
|
||||||
}
|
}
|
||||||
|
|
||||||
let room = room.unwrap();
|
|
||||||
let position = position.unwrap();
|
|
||||||
|
|
||||||
// todo dedupe
|
|
||||||
let animation_frames = lines[1..].join("");
|
|
||||||
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
|
||||||
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
|
||||||
Image::from(frame.to_string())
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
Sprite { id, name, animation_frames, dialogue, room, position }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sprite_from_string() {
|
fn test_sprite_from_string() {
|
||||||
let output = sprite_from_string(
|
let output = Sprite::from(
|
||||||
include_str!("../test/resources/sprite").to_string()
|
include_str!("../test/resources/sprite").to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -856,43 +863,42 @@ fn test_sprite_to_string() {
|
||||||
assert_eq!(example_sprite().to_string(), include_str!("../test/resources/sprite").to_string());
|
assert_eq!(example_sprite().to_string(), include_str!("../test/resources/sprite").to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn item_from_string(string: String) -> Item {
|
impl From<String> for Item {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
fn from(string: String) -> Item {
|
||||||
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
|
|
||||||
let id = lines[0].replace("ITM ", "");
|
let id = lines[0].replace("ITM ", "");
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
let mut dialogue = None;
|
let mut dialogue = None;
|
||||||
|
|
||||||
for _ in 0..2 {
|
for _ in 0..2 {
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
|
|
||||||
if last_line.starts_with("NAME") {
|
if last_line.starts_with("NAME") {
|
||||||
name = Some(last_line.replace("NAME ", "").to_string());
|
name = Some(last_line.replace("NAME ", "").to_string());
|
||||||
} else if last_line.starts_with("DLG") {
|
} else if last_line.starts_with("DLG") {
|
||||||
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
dialogue = Some(last_line.replace("DLG ", "").to_string());
|
||||||
} else {
|
} else {
|
||||||
lines.push(last_line); break;
|
lines.push(last_line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo dedupe
|
||||||
|
let animation_frames = lines[1..].join("");
|
||||||
|
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
||||||
|
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
||||||
|
Image::from(frame.to_string())
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
Item { id, name, animation_frames, dialogue }
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo dedupe
|
|
||||||
let animation_frames = lines[1..].join("");
|
|
||||||
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
|
|
||||||
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
|
|
||||||
Image::from(frame.to_string())
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
Item { id, name, animation_frames, dialogue }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_item_from_string() {
|
fn test_item_from_string() {
|
||||||
let output = item_from_string(
|
let output = Item::from(include_str!("../test/resources/item").to_string());
|
||||||
include_str!("../test/resources/item").to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let expected = example_item();
|
let expected = example_item();
|
||||||
|
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -916,19 +922,21 @@ fn test_item_to_string() {
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_from_string(string: String) -> Exit {
|
impl From<String> for Exit {
|
||||||
// e.g. "4 3,3"
|
fn from(string: String) -> Exit {
|
||||||
let room_position: Vec<&str> = string.split(' ').collect();
|
// e.g. "4 3,3"
|
||||||
let room = room_position[0].to_string();
|
let room_position: Vec<&str> = string.split(' ').collect();
|
||||||
let position = position_from_string(room_position[1].to_string());
|
let room = room_position[0].to_string();
|
||||||
|
let position = Position::from(room_position[1].to_string());
|
||||||
|
|
||||||
Exit { room, position }
|
Exit { room, position }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_exit_from_string() {
|
fn test_exit_from_string() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
exit_from_string("a 12,13".to_string()),
|
Exit::from("a 12,13".to_string()),
|
||||||
Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
|
Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -947,19 +955,21 @@ fn test_exit_to_string() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ending_from_string(string: String) -> Ending {
|
impl From<String> for Ending {
|
||||||
let string = string.replace("END ", "");
|
fn from(string: String) -> Ending {
|
||||||
let id_dialogue: Vec<&str> = string.split('\n').collect();
|
let string = string.replace("END ", "");
|
||||||
let id = id_dialogue[0].to_string();
|
let id_dialogue: Vec<&str> = string.lines().collect();
|
||||||
let dialogue = id_dialogue[1].to_string();
|
let id = id_dialogue[0].to_string();
|
||||||
|
let dialogue = id_dialogue[1].to_string();
|
||||||
|
|
||||||
Ending { id, dialogue }
|
Ending { id, dialogue }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ending_from_string() {
|
fn test_ending_from_string() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
ending_from_string(include_str!("../test/resources/ending").to_string()),
|
Ending::from(include_str!("../test/resources/ending").to_string()),
|
||||||
Ending {
|
Ending {
|
||||||
id: "a".to_string(),
|
id: "a".to_string(),
|
||||||
dialogue: "This is a long line of dialogue. Blah blah blah".to_string()
|
dialogue: "This is a long line of dialogue. Blah blah blah".to_string()
|
||||||
|
@ -985,18 +995,20 @@ fn test_ending_to_string() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dialogue_from_string(string: String) -> Dialogue {
|
impl From<String> for Dialogue {
|
||||||
let lines: Vec<&str> = string.lines().collect();
|
fn from(string: String) -> Dialogue {
|
||||||
let id = lines[0].replace("DLG ", "").to_string();
|
let lines: Vec<&str> = string.lines().collect();
|
||||||
let contents = lines[1..].join("\n");
|
let id = lines[0].replace("DLG ", "").to_string();
|
||||||
|
let contents = lines[1..].join("\n");
|
||||||
|
|
||||||
Dialogue { id, contents }
|
Dialogue { id, contents }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dialogue_from_string() {
|
fn test_dialogue_from_string() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
dialogue_from_string("DLG h\nhello\ngoodbye".to_string()),
|
Dialogue::from("DLG h\nhello\ngoodbye".to_string()),
|
||||||
Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()}
|
Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1019,18 +1031,20 @@ fn test_dialogue_to_string() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variable_from_string(string: String) -> Variable {
|
impl From<String> for Variable {
|
||||||
let id_value: Vec<&str> = string.split('\n').collect();
|
fn from(string: String) -> Variable {
|
||||||
let id = id_value[0].replace("VAR ", "").to_string();
|
let id_value: Vec<&str> = string.split('\n').collect();
|
||||||
let initial_value = id_value[1].to_string();
|
let id = id_value[0].replace("VAR ", "").to_string();
|
||||||
|
let initial_value = id_value[1].to_string();
|
||||||
|
|
||||||
Variable { id, initial_value }
|
Variable { id, initial_value }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_variable_from_string() {
|
fn test_variable_from_string() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
variable_from_string("VAR a\n42".to_string()),
|
Variable::from("VAR a\n42".to_string()),
|
||||||
Variable { id: "a".to_string(), initial_value: "42".to_string()}
|
Variable { id: "a".to_string(), initial_value: "42".to_string()}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1049,72 +1063,72 @@ fn test_variable_to_string() {
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn room_from_string(string: String) -> Room {
|
impl From<String> for Room {
|
||||||
// todo handle room_format?
|
fn from(string: String) -> Room {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
// todo handle room_format?
|
||||||
let id = lines[0].replace("ROOM ", "");
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
let mut name = None;
|
let id = lines[0].replace("ROOM ", "");
|
||||||
let mut palette = "0".to_string();
|
let mut name = None;
|
||||||
let mut items: Vec<Instance> = Vec::new();
|
let mut palette = "0".to_string();
|
||||||
let mut exits: Vec<ExitInstance> = Vec::new();
|
let mut items: Vec<Instance> = Vec::new();
|
||||||
let mut endings: Vec<Instance> = Vec::new();
|
let mut exits: Vec<ExitInstance> = Vec::new();
|
||||||
|
let mut endings: Vec<Instance> = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
|
|
||||||
if last_line.starts_with("NAME") {
|
if last_line.starts_with("NAME") {
|
||||||
name = Some(last_line.replace("NAME ", "").to_string());
|
name = Some(last_line.replace("NAME ", "").to_string());
|
||||||
} else if last_line.starts_with("PAL") {
|
} else if last_line.starts_with("PAL") {
|
||||||
palette = last_line.replace("PAL ", "").to_string();
|
palette = last_line.replace("PAL ", "").to_string();
|
||||||
} else if last_line.starts_with("ITM") {
|
} else if last_line.starts_with("ITM") {
|
||||||
let last_line = last_line.replace("ITM ", "");
|
let last_line = last_line.replace("ITM ", "");
|
||||||
let item_position: Vec<&str> = last_line.split(' ').collect();
|
let item_position: Vec<&str> = last_line.split(' ').collect();
|
||||||
let item_id = item_position[0];
|
let item_id = item_position[0];
|
||||||
let position = item_position[1];
|
let position = item_position[1];
|
||||||
let position = position_from_string(position.to_string());
|
let position = Position::from(position.to_string());
|
||||||
|
|
||||||
items.push(Instance { position, id: item_id.to_string() });
|
items.push(Instance { position, id: item_id.to_string() });
|
||||||
} else if last_line.starts_with("EXT") {
|
} else if last_line.starts_with("EXT") {
|
||||||
let last_line = last_line.replace("EXT ", "");
|
let last_line = last_line.replace("EXT ", "");
|
||||||
let parts: Vec<&str> = last_line.split(' ').collect();
|
let parts: Vec<&str> = last_line.split(' ').collect();
|
||||||
let position = position_from_string(parts[0].to_string());
|
let position = Position::from(parts[0].to_string());
|
||||||
let exit = exit_from_string(format!("{} {}", parts[1], parts[2]));
|
let exit = Exit::from(format!("{} {}", parts[1], parts[2]));
|
||||||
|
|
||||||
exits.push(ExitInstance { position, exit });
|
exits.push(ExitInstance { position, exit });
|
||||||
} else if last_line.starts_with("END") {
|
} else if last_line.starts_with("END") {
|
||||||
let last_line = last_line.replace("END ", "");
|
let last_line = last_line.replace("END ", "");
|
||||||
let ending_position: Vec<&str> = last_line.split(' ').collect();
|
let ending_position: Vec<&str> = last_line.split(' ').collect();
|
||||||
let ending= ending_position[0].to_string();
|
let ending = ending_position[0].to_string();
|
||||||
let position = ending_position[1].to_string();
|
let position = ending_position[1].to_string();
|
||||||
let position = position_from_string(position);
|
let position = Position::from(position);
|
||||||
|
|
||||||
endings.push(Instance { position, id: ending });
|
endings.push(Instance { position, id: ending });
|
||||||
} else {
|
} else {
|
||||||
lines.push(last_line); break;
|
lines.push(last_line);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let lines= &lines[1..];
|
let lines = &lines[1..];
|
||||||
|
|
||||||
let mut tiles : Vec<String> = Vec::new();
|
let mut tiles: Vec<String> = Vec::new();
|
||||||
|
|
||||||
for line in lines.into_iter() {
|
for line in lines.into_iter() {
|
||||||
let line: Vec<&str> = line.split(",").collect();
|
let line: Vec<&str> = line.split(",").collect();
|
||||||
|
|
||||||
for tile_id in line {
|
for tile_id in line {
|
||||||
tiles.push(tile_id.to_string());
|
tiles.push(tile_id.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Room { id, palette, name, tiles, items, exits, endings }
|
Room { id, palette, name, tiles, items, exits, endings }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_room_from_string() {
|
fn test_room_from_string() {
|
||||||
let output = room_from_string(example_room_string());
|
assert_eq!(Room::from(example_room_string()), example_room());
|
||||||
let expected = example_room();
|
|
||||||
|
|
||||||
assert_eq!(output, expected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Room {
|
impl ToString for Room {
|
||||||
|
@ -1174,105 +1188,108 @@ fn test_room_to_string() {
|
||||||
assert_eq!(example_room().to_string(), example_room_string());
|
assert_eq!(example_room().to_string(), example_room_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn game_from_string(string: String ) -> Game {
|
impl From<String> for Game {
|
||||||
// dialogues and endings can have 2+ line breaks inside, so deal with these separately
|
fn from(string: String) -> Game {
|
||||||
// otherwise, everything can be split on a double line break (\n\n)
|
// dialogues and endings can have 2+ line breaks inside, so deal with these separately
|
||||||
let mut dialogues: Vec<Dialogue> = Vec::new();
|
// otherwise, everything can be split on a double line break (\n\n)
|
||||||
let mut endings: Vec<Ending> = Vec::new();
|
let mut dialogues: Vec<Dialogue> = Vec::new();
|
||||||
let mut variables: Vec<Variable> = Vec::new();
|
let mut endings: Vec<Ending> = Vec::new();
|
||||||
let main_split: Vec<&str> = string.split("\n\nDLG").collect();
|
let mut variables: Vec<Variable> = Vec::new();
|
||||||
let main = main_split[0].to_string();
|
let main_split: Vec<&str> = string.split("\n\nDLG").collect();
|
||||||
let mut dialogues_endings_variables: String = main_split[1..].join("\n\nDLG");
|
let main = main_split[0].to_string();
|
||||||
|
let mut dialogues_endings_variables: String = main_split[1..].join("\n\nDLG");
|
||||||
|
|
||||||
let variable_segments = dialogues_endings_variables.clone();
|
let variable_segments = dialogues_endings_variables.clone();
|
||||||
let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect();
|
let variable_segments: Vec<&str> = variable_segments.split("\n\nVAR").collect();
|
||||||
if variable_segments.len() > 0 {
|
if variable_segments.len() > 0 {
|
||||||
dialogues_endings_variables = variable_segments[0].to_string();
|
dialogues_endings_variables = variable_segments[0].to_string();
|
||||||
let variable_segments = variable_segments[1..].to_owned();
|
let variable_segments = variable_segments[1..].to_owned();
|
||||||
|
|
||||||
for segment in variable_segments {
|
for segment in variable_segments {
|
||||||
let segment = format!("VAR{}", segment);
|
let segment = format!("VAR{}", segment);
|
||||||
variables.push(variable_from_string(segment));
|
variables.push(Variable::from(segment));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let ending_segments = dialogues_endings_variables.clone();
|
let ending_segments = dialogues_endings_variables.clone();
|
||||||
let ending_segments: Vec<&str> = ending_segments.split("\n\nEND").collect();
|
let ending_segments: Vec<&str> = ending_segments.split("\n\nEND").collect();
|
||||||
if ending_segments.len() > 0 {
|
if ending_segments.len() > 0 {
|
||||||
dialogues_endings_variables = ending_segments[0].to_string();
|
dialogues_endings_variables = ending_segments[0].to_string();
|
||||||
let ending_segments = ending_segments[1..].to_owned();
|
let ending_segments = ending_segments[1..].to_owned();
|
||||||
|
|
||||||
for segment in ending_segments {
|
for segment in ending_segments {
|
||||||
let segment = format!("END{}", segment);
|
let segment = format!("END{}", segment);
|
||||||
endings.push(ending_from_string(segment));
|
endings.push(Ending::from(segment));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let dialogue_segments = format!("\n\nDLG {}", dialogues_endings_variables.trim());
|
let dialogue_segments = format!("\n\nDLG {}", dialogues_endings_variables.trim());
|
||||||
let dialogue_segments: Vec<&str> = dialogue_segments.split("\n\nDLG").collect();
|
let dialogue_segments: Vec<&str> = dialogue_segments.split("\n\nDLG").collect();
|
||||||
for segment in dialogue_segments[1..].to_owned() {
|
for segment in dialogue_segments[1..].to_owned() {
|
||||||
let segment = format!("DLG{}", segment);
|
let segment = format!("DLG{}", segment);
|
||||||
dialogues.push(dialogue_from_string(segment));
|
dialogues.push(Dialogue::from(segment));
|
||||||
}
|
|
||||||
|
|
||||||
let segments: Vec<&str> = main.split("\n\n").collect();
|
|
||||||
|
|
||||||
let name = segments[0].to_string();
|
|
||||||
let mut version: f64 = 1.0;
|
|
||||||
let mut room_format: u8 = 1;
|
|
||||||
let mut palettes: Vec<Palette> = Vec::new();
|
|
||||||
let mut rooms: Vec<Room> = Vec::new();
|
|
||||||
let mut tiles: Vec<Tile> = Vec::new();
|
|
||||||
let mut avatar : Option<Avatar> = None; // unwrap this later
|
|
||||||
let mut sprites: Vec<Sprite> = Vec::new();
|
|
||||||
let mut items: Vec<Item> = Vec::new();
|
|
||||||
|
|
||||||
for segment in segments[1..].to_owned() {
|
|
||||||
let segment = segment.to_string();
|
|
||||||
|
|
||||||
if segment.starts_with("# BITSY VERSION") {
|
|
||||||
version = segment.replace("# BITSY VERSION ", "").parse().unwrap();
|
|
||||||
} else if segment.starts_with("! ROOM_FORMAT") {
|
|
||||||
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
|
|
||||||
} else if segment.starts_with("PAL") {
|
|
||||||
palettes.push(palette_from_string(segment));
|
|
||||||
} else if segment.starts_with("ROOM") {
|
|
||||||
rooms.push(room_from_string(segment));
|
|
||||||
} else if segment.starts_with("TIL") {
|
|
||||||
tiles.push(Tile::from(segment));
|
|
||||||
} else if segment.starts_with("SPR A") {
|
|
||||||
avatar = Some(avatar_from_string(segment));
|
|
||||||
} else if segment.starts_with("SPR") {
|
|
||||||
sprites.push(sprite_from_string(segment));
|
|
||||||
} else if segment.starts_with("ITM") {
|
|
||||||
items.push(item_from_string(segment));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
assert!(avatar.is_some());
|
let segments: Vec<&str> = main.split("\n\n").collect();
|
||||||
let avatar = avatar.unwrap();
|
|
||||||
|
|
||||||
Game {
|
let name = segments[0].to_string();
|
||||||
name,
|
let mut version: f64 = 1.0;
|
||||||
version,
|
let mut room_format: u8 = 1;
|
||||||
room_format,
|
let mut palettes: Vec<Palette> = Vec::new();
|
||||||
palettes,
|
let mut rooms: Vec<Room> = Vec::new();
|
||||||
rooms,
|
let mut tiles: Vec<Tile> = Vec::new();
|
||||||
tiles,
|
let mut avatar: Option<Avatar> = None; // unwrap this later
|
||||||
avatar,
|
let mut sprites: Vec<Sprite> = Vec::new();
|
||||||
sprites,
|
let mut items: Vec<Item> = Vec::new();
|
||||||
items,
|
|
||||||
dialogues,
|
for segment in segments[1..].to_owned() {
|
||||||
endings,
|
let segment = segment.to_string();
|
||||||
variables,
|
|
||||||
|
if segment.starts_with("# BITSY VERSION") {
|
||||||
|
version = segment.replace("# BITSY VERSION ", "").parse().unwrap();
|
||||||
|
} else if segment.starts_with("! ROOM_FORMAT") {
|
||||||
|
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
|
||||||
|
} else if segment.starts_with("PAL") {
|
||||||
|
palettes.push(Palette::from(segment));
|
||||||
|
} else if segment.starts_with("ROOM") {
|
||||||
|
rooms.push(Room::from(segment));
|
||||||
|
} else if segment.starts_with("TIL") {
|
||||||
|
tiles.push(Tile::from(segment));
|
||||||
|
} else if segment.starts_with("SPR A") {
|
||||||
|
avatar = Some(Avatar::from(segment));
|
||||||
|
} else if segment.starts_with("SPR") {
|
||||||
|
sprites.push(Sprite::from(segment));
|
||||||
|
} else if segment.starts_with("ITM") {
|
||||||
|
items.push(Item::from(segment));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(avatar.is_some());
|
||||||
|
let avatar = avatar.unwrap();
|
||||||
|
|
||||||
|
Game {
|
||||||
|
name,
|
||||||
|
version,
|
||||||
|
room_format,
|
||||||
|
palettes,
|
||||||
|
rooms,
|
||||||
|
tiles,
|
||||||
|
avatar,
|
||||||
|
sprites,
|
||||||
|
items,
|
||||||
|
dialogues,
|
||||||
|
endings,
|
||||||
|
variables,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_game_from_string() {
|
fn test_game_from_string() {
|
||||||
let output = game_from_string(
|
let output = Game::from(
|
||||||
include_str!["../test/resources/default.bitsy"].to_string()
|
include_str!["../test/resources/default.bitsy"].to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
let expected = example_game_default();
|
let expected = example_game_default();
|
||||||
|
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
|
|
Loading…
Reference in New Issue