convert palette id to int

This commit is contained in:
Max Bradbury 2020-04-13 13:30:08 +01:00
parent 48e479c189
commit 1928334b27
4 changed files with 31 additions and 7 deletions

View File

@ -12,3 +12,4 @@ keywords = ["gamedev"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
radix_fmt = "1.0.0"

View File

@ -1,3 +1,5 @@
use radix_fmt::radix_36;
pub mod avatar; pub mod avatar;
pub mod colour; pub mod colour;
pub mod dialogue; pub mod dialogue;
@ -62,3 +64,23 @@ impl AnimationFrames for Vec<Image> {
string string
} }
} }
fn from_base36(str: &str) -> u64 {
u64::from_str_radix(str, 36).unwrap()
}
#[test]
fn test_from_base36() {
assert_eq!(from_base36("0"), 0);
assert_eq!(from_base36("0z"), 35);
assert_eq!(from_base36("11"), 37);
}
fn to_base36(input: u64) -> String {
format!("{}", radix_36(input))
}
#[test]
fn test_to_base36() {
assert_eq!(to_base36(37), "11");
}

View File

@ -165,7 +165,7 @@ pub fn game_default() -> Game {
room_format: 1, room_format: 1,
palettes: vec![ palettes: vec![
Palette { Palette {
id: "0".to_string(), id: 0,
name: None, name: None,
colours: vec![ colours: vec![
Colour {red: 0, green: 82, blue: 204 }, Colour {red: 0, green: 82, blue: 204 },

View File

@ -1,8 +1,9 @@
use crate::colour::Colour; use crate::colour::Colour;
use crate::{from_base36, to_base36};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Palette { pub struct Palette {
pub id: String, // base36 string (why??) pub id: u64,
pub name: Option<String>, pub name: Option<String>,
pub colours: Vec<Colour>, pub colours: Vec<Colour>,
} }
@ -11,7 +12,7 @@ impl From<String> for Palette {
fn from(string: String) -> Palette { fn from(string: String) -> Palette {
let lines: Vec<&str> = string.lines().collect(); let lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("PAL ", ""); let id = from_base36(&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()),
@ -43,7 +44,7 @@ impl ToString for Palette {
} }
colours.pop(); colours.pop();
format!("PAL {}\n{}{}", self.id, name, colours) format!("PAL {}\n{}{}", to_base36(self.id), name, colours)
} }
} }
@ -54,7 +55,7 @@ fn test_palette_from_string() {
); );
let expected = Palette { let expected = Palette {
id: "1".to_string(), id: 1,
name: Some("lamplight".to_string()), name: Some("lamplight".to_string()),
colours: vec![ colours: vec![
Colour {red: 45, green: 45, blue: 59}, Colour {red: 45, green: 45, blue: 59},
@ -73,7 +74,7 @@ fn test_palette_from_string_no_name() {
); );
let expected = Palette { let expected = Palette {
id: "9".to_string(), id: 9,
name: None, name: None,
colours: vec![ colours: vec![
Colour {red: 45, green: 45, blue: 59}, Colour {red: 45, green: 45, blue: 59},
@ -88,7 +89,7 @@ fn test_palette_from_string_no_name() {
#[test] #[test]
fn test_palette_to_string() { fn test_palette_to_string() {
let output = Palette { let output = Palette {
id: "g".to_string(), id: 16,
name: Some("moss".to_string()), name: Some("moss".to_string()),
colours: vec![ colours: vec![
Colour {red: 1, green: 2, blue: 3 }, Colour {red: 1, green: 2, blue: 3 },