From c7f1d7220c5923dd65375383c4e1019aec5f7994 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 13 Apr 2020 14:43:23 +0100 Subject: [PATCH] implement ToBase36 for u64; room id to u64; sprite id to u64; tile id to u64; sprite.dialogue -> sprite.dialogue_id --- src/exit.rs | 4 ++-- src/lib.rs | 17 ++++++++++++++--- src/mock.rs | 14 +++++++------- src/palette.rs | 4 ++-- src/room.rs | 6 +++--- src/sprite.rs | 28 ++++++++++++++-------------- src/tile.rs | 12 ++++++------ 7 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/exit.rs b/src/exit.rs index 4d7b2c4..3e4b7e1 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -1,4 +1,4 @@ -use crate::{Position, from_base36, to_base36}; +use crate::{Position, from_base36, ToBase36}; #[derive(Debug, Eq, PartialEq)] pub enum Transition { @@ -74,7 +74,7 @@ impl ToString for Exit { fn to_string(&self) -> String { format!( "{} {}{}", - to_base36(self.room_id), + self.room_id.to_base36(), self.position.to_string(), self.effect.to_string() ) diff --git a/src/lib.rs b/src/lib.rs index 177f5e4..301e8b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,11 +76,22 @@ fn test_from_base36() { assert_eq!(from_base36("11"), 37); } -fn to_base36(input: u64) -> String { - format!("{}", radix_36(input)) +/// this doesn't work inside ToBase36 for some reason +fn to_base36(int: u64) -> String { + format!("{}", radix_36(int)) +} + +pub trait ToBase36 { + fn to_base36(&self) -> String; +} + +impl ToBase36 for u64 { + fn to_base36(&self) -> String { + to_base36(*self) + } } #[test] fn test_to_base36() { - assert_eq!(to_base36(37), "11"); + assert_eq!((37 as u64).to_base36(), "11"); } diff --git a/src/mock.rs b/src/mock.rs index ef356e1..19a87d9 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -69,7 +69,7 @@ pub fn avatar() -> Avatar { pub fn sprite() -> Sprite { Sprite { - id: "a".to_string(), + id: 10, name: Some("hatch".to_string()), animation_frames: vec![ Image { @@ -85,8 +85,8 @@ pub fn sprite() -> Sprite { ] } ], - dialogue: Some("SPR_0".to_string()), - room: "4".to_string(), + dialogue_id: Some("SPR_0".to_string()), + room_id: 4, position: Position { x: 9, y: 7 @@ -208,7 +208,7 @@ pub fn game_default() -> Game { ], tiles: vec![ Tile { - id: "a".to_string(), + id: 10, name: None, wall: false, animation_frames: vec![ @@ -247,7 +247,7 @@ pub fn game_default() -> Game { }, sprites: vec![ Sprite { - id: "a".to_string(), + id: 10, name: None, animation_frames: vec![ Image { @@ -263,8 +263,8 @@ pub fn game_default() -> Game { ] } ], - dialogue: Some("SPR_0".to_string()), - room: "0".to_string(), + dialogue_id: Some("SPR_0".to_string()), + room_id: 0, position: Position { x: 8, y: 12 } } ], diff --git a/src/palette.rs b/src/palette.rs index 0795be9..5efe889 100644 --- a/src/palette.rs +++ b/src/palette.rs @@ -1,5 +1,5 @@ use crate::colour::Colour; -use crate::{from_base36, to_base36}; +use crate::{from_base36, ToBase36}; #[derive(Debug, Eq, PartialEq)] pub struct Palette { @@ -44,7 +44,7 @@ impl ToString for Palette { } colours.pop(); - format!("PAL {}\n{}{}", to_base36(self.id), name, colours) + format!("PAL {}\n{}{}", self.id.to_base36(), name, colours) } } diff --git a/src/room.rs b/src/room.rs index 900b198..2662cfd 100644 --- a/src/room.rs +++ b/src/room.rs @@ -1,4 +1,4 @@ -use crate::{Exit, ExitInstance, Instance, mock, Position, from_base36, to_base36}; +use crate::{Exit, ExitInstance, Instance, mock, Position, from_base36, ToBase36}; #[derive(Debug, Eq, PartialEq)] pub struct Room { @@ -123,13 +123,13 @@ impl ToString for Room { format!( "ROOM {}\n{}{}{}{}{}\nPAL {}", - to_base36(self.id), + self.id.to_base36(), tiles, if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, items, exits, endings, - to_base36(self.palette_id) + self.palette_id.to_base36() ) } } diff --git a/src/sprite.rs b/src/sprite.rs index c7ee46d..44df374 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,12 +1,12 @@ -use crate::{AnimationFrames, Image, Position, mock}; +use crate::{AnimationFrames, Image, Position, mock, from_base36, ToBase36}; #[derive(Debug, Eq, PartialEq)] pub struct Sprite { - pub id: String, // lowercase base36 + pub id: u64, pub name: Option, pub animation_frames: Vec, - pub dialogue: Option, /// dialogue id - pub room: String, /// room id + pub dialogue_id: Option, + pub room_id: u64, pub position: Position, } @@ -14,10 +14,10 @@ impl From for Sprite { fn from(string: String) -> Sprite { let mut lines: Vec<&str> = string.lines().collect(); - let id = lines[0].replace("SPR ", ""); + let id = from_base36(&lines[0].replace("SPR ", "")); let mut name = None; - let mut dialogue = None; - let mut room: Option = None; + let mut dialogue_id: Option = None; + let mut room_id: Option = None; let mut position: Option = None; for _ in 0..3 { @@ -26,11 +26,11 @@ impl From for Sprite { if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); } else if last_line.starts_with("DLG") { - dialogue = Some(last_line.replace("DLG ", "").to_string()); + dialogue_id = Some(last_line.replace("DLG ", "").to_string()); } else if last_line.starts_with("POS") { let last_line = last_line.replace("POS ", ""); let room_position: Vec<&str> = last_line.split(' ').collect(); - room = Some(room_position[0].to_string()); + room_id = Some(from_base36(&room_position[0])); position = Some(Position::from(room_position[1].to_string())); } else { lines.push(last_line); @@ -38,7 +38,7 @@ impl From for Sprite { } } - let room = room.unwrap(); + let room_id = room_id.unwrap(); let position = position.unwrap(); // todo dedupe @@ -48,7 +48,7 @@ impl From for Sprite { Image::from(frame.to_string()) }).collect(); - Sprite { id, name, animation_frames, dialogue, room, position } + Sprite { id, name, animation_frames, dialogue_id, room_id, position } } } @@ -57,11 +57,11 @@ impl ToString for Sprite { fn to_string(&self) -> String { format!( "SPR {}\n{}{}{}\nPOS {} {}", - self.id, + self.id.to_base36(), self.animation_frames.to_string(), if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() }, - if self.dialogue.as_ref().is_some() { format!("\nDLG {}", self.dialogue.as_ref().unwrap()) } else { "".to_string() }, - self.room, + if self.dialogue_id.as_ref().is_some() { format!("\nDLG {}", self.dialogue_id.as_ref().unwrap()) } else { "".to_string() }, + self.room_id.to_base36(), self.position.to_string(), ) } diff --git a/src/tile.rs b/src/tile.rs index 6f9d2e1..60942fe 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,10 +1,10 @@ -use crate::AnimationFrames; +use crate::{AnimationFrames, from_base36, ToBase36}; use crate::image::Image; use crate::mock; #[derive(Debug, Eq, PartialEq)] pub struct Tile { - pub id: String, // base36 string + pub id: u64, pub name: Option, pub wall: bool, pub animation_frames: Vec, @@ -15,7 +15,7 @@ impl From for Tile { fn from(string: String) -> Tile { let mut lines: Vec<&str> = string.lines().collect(); - let id = lines[0].replace("TIL ", ""); + let id = from_base36(&lines[0].replace("TIL ", "")); let last_line = lines.pop().unwrap(); let wall = match last_line == "WAL true" { @@ -50,7 +50,7 @@ impl ToString for Tile { fn to_string(&self) -> String { format!( "TIL {}\n{}{}{}", - self.id, + self.id.to_base36(), self.animation_frames.to_string(), if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap())} else {"".to_string() }, if self.wall {"\nWAL true"} else {""} @@ -63,7 +63,7 @@ fn test_tile_from_string() { let output = Tile::from(include_str!("../test/resources/tile").to_string()); let expected = Tile { - id: "z".to_string(), + id: 35, name: Some("concrete 1".to_string()), wall: true, animation_frames: vec![ @@ -79,7 +79,7 @@ fn test_tile_from_string() { #[test] fn test_tile_to_string() { let output = Tile { - id: "7a".to_string(), + id: 262, name: Some("chequers".to_string()), wall: false, animation_frames: vec![