room to string
This commit is contained in:
parent
0ebe3babe6
commit
a7d9354d9d
|
@ -29,12 +29,12 @@ a library for parsing Bitsy game data.
|
||||||
* variable from
|
* variable from
|
||||||
* variable to
|
* variable to
|
||||||
* room from
|
* room from
|
||||||
|
* room to
|
||||||
|
|
||||||
## todo
|
## todo
|
||||||
|
|
||||||
### functions
|
### functions
|
||||||
|
|
||||||
* room to
|
|
||||||
* game from
|
* game from
|
||||||
* game to
|
* game to
|
||||||
|
|
||||||
|
|
97
src/main.rs
97
src/main.rs
|
@ -30,12 +30,24 @@ struct Tile {
|
||||||
animation_frames: Vec<Image>,
|
animation_frames: Vec<Image>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
struct Position {
|
struct Position {
|
||||||
x: u8,
|
x: u8,
|
||||||
y: u8,
|
y: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
struct Instance {
|
||||||
|
position: Position,
|
||||||
|
id: String, // item / ending id
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
struct ExitInstance {
|
||||||
|
position: Position,
|
||||||
|
exit: Exit,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
struct Dialogue {
|
struct Dialogue {
|
||||||
id: String,
|
id: String,
|
||||||
|
@ -88,9 +100,9 @@ struct Room {
|
||||||
palette: String, // id
|
palette: String, // id
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
tiles: Vec<String>, // tile ids
|
tiles: Vec<String>, // tile ids
|
||||||
items: HashMap<Position, String>, // item id
|
items: Vec<Instance>,
|
||||||
exits: HashMap<Position, Exit>,
|
exits: Vec<ExitInstance>,
|
||||||
endings: HashMap<Position, String>, // ending id
|
endings: Vec<Instance>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
@ -189,24 +201,6 @@ fn test_item() -> Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_room() -> Room {
|
fn test_room() -> Room {
|
||||||
// todo can I instantiate these inline?
|
|
||||||
let mut items = HashMap::new();
|
|
||||||
let mut exits = HashMap::new();
|
|
||||||
let mut endings = HashMap::new();
|
|
||||||
|
|
||||||
items.insert(Position { x: 11, y: 5}, "d".to_string());
|
|
||||||
items.insert(Position { x: 8, y: 3}, "e".to_string());
|
|
||||||
items.insert(Position { x: 1, y: 0}, "5".to_string());
|
|
||||||
items.insert(Position { x: 2, y: 1}, "6".to_string());
|
|
||||||
items.insert(Position { x: 3, y: 2}, "6".to_string());
|
|
||||||
|
|
||||||
exits.insert(
|
|
||||||
Position { x: 3, y: 3},
|
|
||||||
Exit { room: "3".to_string(), position: Position { x: 10, y: 6}}
|
|
||||||
);
|
|
||||||
|
|
||||||
endings.insert(Position { x: 8, y: 7 }, "undefined".to_string());
|
|
||||||
|
|
||||||
Room {
|
Room {
|
||||||
id: "a".to_string(),
|
id: "a".to_string(),
|
||||||
palette: "9".to_string(),
|
palette: "9".to_string(),
|
||||||
|
@ -229,9 +223,22 @@ fn test_room() -> Room {
|
||||||
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),
|
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),
|
||||||
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string()
|
"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string(),"0".to_string()
|
||||||
],
|
],
|
||||||
items,
|
items: vec![
|
||||||
exits,
|
Instance {position: Position { x: 11, y: 5}, id: "d".to_string()},
|
||||||
endings
|
Instance {position: Position { x: 8, y: 3}, id: "e".to_string()},
|
||||||
|
Instance {position: Position { x: 1, y: 0}, id: "5".to_string()},
|
||||||
|
Instance {position: Position { x: 2, y: 1}, id: "6".to_string()},
|
||||||
|
Instance {position: Position { x: 3, y: 2}, id: "6".to_string()},
|
||||||
|
],
|
||||||
|
exits: vec![
|
||||||
|
ExitInstance {
|
||||||
|
position: Position { x: 3, y: 3},
|
||||||
|
exit: Exit { room: "3".to_string(), position: Position { x: 10, y: 6}}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
endings: vec![
|
||||||
|
Instance{position: Position { x: 8, y: 7 }, id: "undefined".to_string()},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -760,9 +767,9 @@ fn room_from_string(string: String) -> Room {
|
||||||
let id = lines[0].replace("ROOM ", "");
|
let id = lines[0].replace("ROOM ", "");
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
let mut palette = "0".to_string();
|
let mut palette = "0".to_string();
|
||||||
let mut items: HashMap<Position, String> = HashMap::new();
|
let mut items: Vec<Instance> = Vec::new();
|
||||||
let mut exits: HashMap<Position, Exit> = HashMap::new();
|
let mut exits: Vec<ExitInstance> = Vec::new();
|
||||||
let mut endings: HashMap<Position, String> = HashMap::new();
|
let mut endings: Vec<Instance> = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let last_line = lines.pop().unwrap();
|
let last_line = lines.pop().unwrap();
|
||||||
|
@ -778,14 +785,14 @@ fn room_from_string(string: String) -> Room {
|
||||||
let position = item_position[1];
|
let position = item_position[1];
|
||||||
let position = position_from_string(position.to_string());
|
let position = position_from_string(position.to_string());
|
||||||
|
|
||||||
items.insert(position, 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_string(parts[0].to_string());
|
||||||
let exit = exit_from_string(format!("{} {}", parts[1], parts[2]));
|
let exit = exit_from_string(format!("{} {}", parts[1], parts[2]));
|
||||||
|
|
||||||
exits.insert(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();
|
||||||
|
@ -793,7 +800,7 @@ fn room_from_string(string: String) -> Room {
|
||||||
let position = ending_position[1].to_string();
|
let position = ending_position[1].to_string();
|
||||||
let position = position_from_string(position);
|
let position = position_from_string(position);
|
||||||
|
|
||||||
endings.insert(position, ending);
|
endings.push(Instance { position, id: ending });
|
||||||
} else {
|
} else {
|
||||||
lines.push(last_line); break;
|
lines.push(last_line); break;
|
||||||
}
|
}
|
||||||
|
@ -838,18 +845,26 @@ fn room_to_string(room: Room) -> String {
|
||||||
}
|
}
|
||||||
tiles.pop(); // remove trailing newline
|
tiles.pop(); // remove trailing newline
|
||||||
|
|
||||||
for (position, item) in room.items {
|
for instance in room.items {
|
||||||
items.push_str(&format!("\nITM {} {}", item, position_to_string(position)));
|
items.push_str(
|
||||||
}
|
&format!("\nITM {} {}", instance.id, position_to_string(instance.position))
|
||||||
|
|
||||||
for (position, exit) in room.exits {
|
|
||||||
exits.push_str(
|
|
||||||
&format!("\nEXT {} {}", position_to_string(position), exit_to_string(exit))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (position, ending) in room.endings {
|
for instance in room.exits {
|
||||||
endings.push_str(&format!("\nEND {} {}", ending, position_to_string(position)));
|
exits.push_str(
|
||||||
|
&format!(
|
||||||
|
"\nEXT {} {}",
|
||||||
|
position_to_string(instance.position),
|
||||||
|
exit_to_string(instance.exit)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for instance in room.endings {
|
||||||
|
endings.push_str(
|
||||||
|
&format!("\nEND {} {}", instance.id, position_to_string(instance.position))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
|
@ -866,7 +881,7 @@ fn room_to_string(room: Room) -> String {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_room_to_string() {
|
fn test_room_to_string() {
|
||||||
assert_eq!( room_to_string(test_room()), test_room_string());
|
assert_eq!(room_to_string(test_room()), test_room_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn game_from_string(game: String ) -> Game {
|
// fn game_from_string(game: String ) -> Game {
|
||||||
|
|
Loading…
Reference in New Issue