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 to
|
||||
* room from
|
||||
* room to
|
||||
|
||||
## todo
|
||||
|
||||
### functions
|
||||
|
||||
* room to
|
||||
* game from
|
||||
* game to
|
||||
|
||||
|
|
95
src/main.rs
95
src/main.rs
|
@ -30,12 +30,24 @@ struct Tile {
|
|||
animation_frames: Vec<Image>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct Position {
|
||||
x: 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)]
|
||||
struct Dialogue {
|
||||
id: String,
|
||||
|
@ -88,9 +100,9 @@ struct Room {
|
|||
palette: String, // id
|
||||
name: Option<String>,
|
||||
tiles: Vec<String>, // tile ids
|
||||
items: HashMap<Position, String>, // item id
|
||||
exits: HashMap<Position, Exit>,
|
||||
endings: HashMap<Position, String>, // ending id
|
||||
items: Vec<Instance>,
|
||||
exits: Vec<ExitInstance>,
|
||||
endings: Vec<Instance>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
|
@ -189,24 +201,6 @@ fn test_item() -> Item {
|
|||
}
|
||||
|
||||
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 {
|
||||
id: "a".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()
|
||||
],
|
||||
items,
|
||||
exits,
|
||||
endings
|
||||
items: vec![
|
||||
Instance {position: Position { x: 11, y: 5}, id: "d".to_string()},
|
||||
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 mut name = None;
|
||||
let mut palette = "0".to_string();
|
||||
let mut items: HashMap<Position, String> = HashMap::new();
|
||||
let mut exits: HashMap<Position, Exit> = HashMap::new();
|
||||
let mut endings: HashMap<Position, String> = HashMap::new();
|
||||
let mut items: Vec<Instance> = Vec::new();
|
||||
let mut exits: Vec<ExitInstance> = Vec::new();
|
||||
let mut endings: Vec<Instance> = Vec::new();
|
||||
|
||||
loop {
|
||||
let last_line = lines.pop().unwrap();
|
||||
|
@ -778,14 +785,14 @@ fn room_from_string(string: String) -> Room {
|
|||
let position = item_position[1];
|
||||
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") {
|
||||
let last_line = last_line.replace("EXT ", "");
|
||||
let parts: Vec<&str> = last_line.split(' ').collect();
|
||||
let position = position_from_string(parts[0].to_string());
|
||||
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") {
|
||||
let last_line = last_line.replace("END ", "");
|
||||
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 = position_from_string(position);
|
||||
|
||||
endings.insert(position, ending);
|
||||
endings.push(Instance { position, id: ending });
|
||||
} else {
|
||||
lines.push(last_line); break;
|
||||
}
|
||||
|
@ -838,18 +845,26 @@ fn room_to_string(room: Room) -> String {
|
|||
}
|
||||
tiles.pop(); // remove trailing newline
|
||||
|
||||
for (position, item) in room.items {
|
||||
items.push_str(&format!("\nITM {} {}", item, position_to_string(position)));
|
||||
}
|
||||
|
||||
for (position, exit) in room.exits {
|
||||
exits.push_str(
|
||||
&format!("\nEXT {} {}", position_to_string(position), exit_to_string(exit))
|
||||
for instance in room.items {
|
||||
items.push_str(
|
||||
&format!("\nITM {} {}", instance.id, position_to_string(instance.position))
|
||||
);
|
||||
}
|
||||
|
||||
for (position, ending) in room.endings {
|
||||
endings.push_str(&format!("\nEND {} {}", ending, position_to_string(position)));
|
||||
for instance in room.exits {
|
||||
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!(
|
||||
|
|
Loading…
Reference in New Issue