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
use crate::network::protocol::resource_types;

macro_rules! parse_resource {
    ($num:expr, $name:ident, Character, $val: tt) => {
        pub const $name: CharacterInfo = CharacterInfo {
            texture: $val.texture,
            atlas: $val.atlas,
            animation: $val.animation,
            id: $num,
        };
    };
    ($num:expr, $name:ident, $variant:ident, $val: expr) => {
        pub const $name: ResourceInfo = ResourceInfo {
            variant: ResourceVariant::$variant,
            path: $val,
            id: $num,
        };
    };
}

macro_rules! resources {
    ($(($name:ident, $variant:ident, $val:expr)),*) => {
        resources! { 0, $(($name, $variant, $val)),* }
    };
    ($num:expr, ($name:ident, $variant:ident, $val:expr), $(($name2:ident, $variant2:ident, $val2:expr)),*) => {
        parse_resource! { $num, $name, $variant, $val }
        resources! { $num+1, $(($name2, $variant2, $val2)),* }
    };
    ($num:expr, ($name:ident, $variant:ident, $val:expr)) => {
        parse_resource! { $num, $name, $variant, $val }
        pub const RESOURCE_COUNT: u32 = $num + 1;
    };
}

resources! {
    (EMPTY,             Texture,        "empty.png"              ),
    (THIEF,             Texture,        "thief.png"              ),
    (SOUND,             Sound,          "theme_song.mp3"         ),
    (CHAR,              Character,      Character {
                          texture:   "Thief/Thief.png",
                          atlas:     "Thief/Thief.atlas",
                          animation: "Thief/Thief.json"
    }                                                            )
}

trait ResourceId {
    fn get_id(&self) -> u32;
}

#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum ResourceVariant {
    Texture = resource_types::TEXTURE,
    Character = resource_types::CHARACTER,
    Sound = resource_types::SOUND,
}

#[derive(Debug, Clone, Copy)]
pub struct ResourceInfo {
    pub variant: ResourceVariant,
    pub path: &'static str,
    pub id: u32,
}

impl ResourceId for ResourceInfo {
    fn get_id(&self) -> u32 {
        self.id
    }
}

struct Character {
    pub texture: &'static str,
    pub atlas: &'static str,
    pub animation: &'static str,
}

#[derive(Debug, Clone, Copy)]
pub struct CharacterInfo {
    pub texture: &'static str,
    pub atlas: &'static str,
    pub animation: &'static str,
    pub id: u32,
}

impl ResourceId for CharacterInfo {
    fn get_id(&self) -> u32 {
        self.id
    }
}

impl std::convert::From<ResourceInfo> for usize {
    fn from(a: ResourceInfo) -> Self {
        a.get_id() as usize
    }
}
impl std::convert::From<CharacterInfo> for usize {
    fn from(a: CharacterInfo) -> Self {
        a.get_id() as usize
    }
}