implement ToBase36 for u64; room id to u64; sprite id to u64; tile id to u64; sprite.dialogue -> sprite.dialogue_id

This commit is contained in:
Max Bradbury 2020-04-13 14:43:23 +01:00
parent f424c49301
commit c7f1d7220c
7 changed files with 48 additions and 37 deletions

View File

@ -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()
)

View File

@ -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");
}

View File

@ -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 }
}
],

View File

@ -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)
}
}

View File

@ -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()
)
}
}

View File

@ -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<String>,
pub animation_frames: Vec<Image>,
pub dialogue: Option<String>, /// dialogue id
pub room: String, /// room id
pub dialogue_id: Option<String>,
pub room_id: u64,
pub position: Position,
}
@ -14,10 +14,10 @@ impl From<String> 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<String> = None;
let mut dialogue_id: Option<String> = None;
let mut room_id: Option<u64> = None;
let mut position: Option<Position> = None;
for _ in 0..3 {
@ -26,11 +26,11 @@ impl From<String> 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<String> for Sprite {
}
}
let room = room.unwrap();
let room_id = room_id.unwrap();
let position = position.unwrap();
// todo dedupe
@ -48,7 +48,7 @@ impl From<String> 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(),
)
}

View File

@ -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<String>,
pub wall: bool,
pub animation_frames: Vec<Image>,
@ -15,7 +15,7 @@ impl From<String> 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![