handle bitsy version as major and minor versions (uint); helper functions for game version/format

This commit is contained in:
Max Bradbury 2020-04-17 09:37:03 +01:00
parent c13fd1dcdc
commit f77c8b2d4e
2 changed files with 29 additions and 8 deletions

View File

@ -1,10 +1,11 @@
use crate::{Avatar, Dialogue, Ending, Item, Palette, Room, Sprite, Tile, Variable, mock}; use crate::{Avatar, Dialogue, Ending, Item, Palette, Room, Sprite, Tile, Variable, mock, optional_data_line};
use std::fs; use std::fs;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Game { pub struct Game {
pub name: String, pub name: String,
pub version: f64, pub major_version: u8,
pub minor_version: u8,
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
pub palettes: Vec<Palette>, pub palettes: Vec<Palette>,
pub rooms: Vec<Room>, pub rooms: Vec<Room>,
@ -70,7 +71,8 @@ impl From<String> for Game {
let segments: Vec<&str> = main.split("\n\n").collect(); let segments: Vec<&str> = main.split("\n\n").collect();
let name = segments[0].to_string(); let name = segments[0].to_string();
let mut version: f64 = 1.0; let mut major_version: u8 = 1;
let mut minor_version: u8 = 0;
let mut room_format: u8 = 1; let mut room_format: u8 = 1;
let mut palettes: Vec<Palette> = Vec::new(); let mut palettes: Vec<Palette> = Vec::new();
let mut rooms: Vec<Room> = Vec::new(); let mut rooms: Vec<Room> = Vec::new();
@ -83,7 +85,10 @@ impl From<String> for Game {
let segment = segment.to_string(); let segment = segment.to_string();
if segment.starts_with("# BITSY VERSION") { if segment.starts_with("# BITSY VERSION") {
version = segment.replace("# BITSY VERSION ", "").parse().unwrap(); let segment = segment.replace("# BITSY VERSION ", "");
let parts = Game::version_from_string(segment);
major_version = parts.0;
minor_version = parts.1;
} else if segment.starts_with("! ROOM_FORMAT") { } else if segment.starts_with("! ROOM_FORMAT") {
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
} else if segment.starts_with("PAL") { } else if segment.starts_with("PAL") {
@ -106,7 +111,8 @@ impl From<String> for Game {
Game { Game {
name, name,
version, major_version,
minor_version,
room_format, room_format,
palettes, palettes,
rooms, rooms,
@ -163,9 +169,9 @@ impl ToString for Game {
} }
format!( format!(
"{}\n\n# BITSY VERSION {}\n\n! ROOM_FORMAT {}\n\n{}\n\n", "{}\n{}\n\n! ROOM_FORMAT {}\n\n{}\n\n",
&self.name, &self.name,
&self.version, &self.version_line(),
&self.room_format, &self.room_format,
segments.join("\n\n"), segments.join("\n\n"),
) )
@ -203,6 +209,20 @@ impl Game {
self.tiles.push(tile); self.tiles.push(tile);
new_id new_id
} }
fn version_line(&self) -> String {
format!("\n# BITSY VERSION {}.{}", self.major_version, self.minor_version)
}
fn room_format_line(&self) -> String {
optional_data_line("! ROOM_FORMAT", Some(self.room_format))
}
fn version_from_string(string: String) -> (u8, u8) {
let parts: Vec<&str> = string.split(".").collect();
assert_eq!(parts.len(), 2);
(parts[0].parse().unwrap(),parts[1].parse().unwrap())
}
} }
#[test] #[test]

View File

@ -188,7 +188,8 @@ pub fn room() -> Room {
pub fn game_default() -> Game { pub fn game_default() -> Game {
Game { Game {
name: "Write your game's title here".to_string(), name: "Write your game's title here".to_string(),
version: 6.5, major_version: 6,
minor_version: 5,
room_format: 1, room_format: 1,
palettes: vec![ palettes: vec![
Palette { Palette {