1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use radix_fmt::radix_36;

pub mod avatar;
pub mod colour;
pub mod dialogue;
pub mod ending;
pub mod exit;
pub mod game;
pub mod image;
pub mod item;
pub mod mock;
pub mod palette;
pub mod position;
pub mod room;
pub mod sprite;
pub mod text;
pub mod tile;
pub mod variable;

use avatar::Avatar;
use colour::Colour;
use dialogue::Dialogue;
use ending::Ending;
use exit::{Exit, Transition};
use game::{Game, Version};
use image::Image;
use item::Item;
use palette::Palette;
use position::Position;
use room::Room;
use sprite::Sprite;
use std::fmt::Display;
use text::{Font, TextDirection};
use tile::Tile;
use variable::Variable;

#[derive(Debug, Eq, PartialEq)]
pub struct Instance {
    position: Position,
    id: String, // item / ending.rs id
}

#[derive(Debug, Eq, PartialEq)]
pub struct ExitInstance {
    position: Position,
    exit: Exit,
}

pub trait AnimationFrames {
    fn to_string(&self) -> String;
}

impl AnimationFrames for Vec<Image> {
    #[inline]
    fn to_string(&self) -> String {
        let mut string = String::new();
        let last_frame = self.len() - 1;

        for (i, frame) in self.into_iter().enumerate() {
            string.push_str(&frame.to_string());

            if i < last_frame {
                string.push_str(&"\n>\n".to_string());
            }
        }

        string
    }
}

fn from_base36(str: &str) -> u64 {
    u64::from_str_radix(str, 36).unwrap()
}

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

/// e.g. `\nNAME DLG_0`
fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
    if item.is_some() {
        format!("\n{} {}", label, item.unwrap())
    } else {
        "".to_string()
    }
}

#[cfg(test)]
mod test {
    use crate::{from_base36, ToBase36, optional_data_line, mock};

    #[test]
    fn test_from_base36() {
        assert_eq!(from_base36("0"), 0);
        assert_eq!(from_base36("0z"), 35);
        assert_eq!(from_base36("11"), 37);
    }

    #[test]
    fn test_to_base36() {
        assert_eq!((37 as u64).to_base36(), "11");
    }

    #[test]
    fn test_optional_data_line() {
        let output = optional_data_line("NAME", mock::item().name);
        assert_eq!(output, "\nNAME door".to_string());
    }
}