diff --git a/src/game.rs b/src/game.rs index 29aca99..bc9b173 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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; #[derive(Debug, PartialEq)] pub struct Game { 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 palettes: Vec, pub rooms: Vec, @@ -70,7 +71,8 @@ impl From for Game { let segments: Vec<&str> = main.split("\n\n").collect(); 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 palettes: Vec = Vec::new(); let mut rooms: Vec = Vec::new(); @@ -83,7 +85,10 @@ impl From for Game { let segment = segment.to_string(); 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") { room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); } else if segment.starts_with("PAL") { @@ -106,7 +111,8 @@ impl From for Game { Game { name, - version, + major_version, + minor_version, room_format, palettes, rooms, @@ -163,9 +169,9 @@ impl ToString for Game { } 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.version, + &self.version_line(), &self.room_format, segments.join("\n\n"), ) @@ -203,6 +209,20 @@ impl Game { self.tiles.push(tile); 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] diff --git a/src/mock.rs b/src/mock.rs index eddc7c7..496630f 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -188,7 +188,8 @@ pub fn room() -> Room { pub fn game_default() -> Game { Game { name: "Write your game's title here".to_string(), - version: 6.5, + major_version: 6, + minor_version: 5, room_format: 1, palettes: vec![ Palette {