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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::math::*;
use core::{
    assets::protocol::{AssetLoadResult, AssetProtocol},
    Scalar,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::from_utf8};

fn default_speed() -> Scalar {
    1.0
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SpriteAnimationRegion {
    None,
    All,
    From(usize),
    To(usize),
    /// (from inclusive, to exclusive)
    Range(usize, usize),
}

impl Default for SpriteAnimationRegion {
    fn default() -> Self {
        Self::All
    }
}

impl SpriteAnimationRegion {
    pub fn contains(self, position: usize) -> bool {
        match self {
            Self::None => false,
            Self::All => true,
            Self::From(index) => position >= index,
            Self::To(index) => position < index,
            Self::Range(from, to) => position >= from && position < to,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum SpriteAnimationValue {
    Bool(bool),
    Integer(i32),
    Scalar(Scalar),
}

impl SpriteAnimationValue {
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_integer(&self) -> Option<i32> {
        match self {
            Self::Integer(v) => Some(*v),
            _ => None,
        }
    }

    pub fn as_scalar(&self) -> Option<Scalar> {
        match self {
            Self::Scalar(v) => Some(*v),
            _ => None,
        }
    }
}

impl From<bool> for SpriteAnimationValue {
    fn from(v: bool) -> Self {
        Self::Bool(v)
    }
}

impl From<i32> for SpriteAnimationValue {
    fn from(v: i32) -> Self {
        Self::Integer(v)
    }
}

impl From<Scalar> for SpriteAnimationValue {
    fn from(v: Scalar) -> Self {
        Self::Scalar(v)
    }
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum SpriteAnimationCondition {
    Bool(bool),
    IntegerEquals(i32),
    IntegerNotEquals(i32),
    IntegerGreater(i32),
    IntegerLess(i32),
    /// (from inclusive, to inclusive)
    IntegerRange(i32, i32),
    /// (value, threshold)
    ScalarNearlyEquals(Scalar, Scalar),
    /// (value, threshold)
    ScalarNotNearlyEquals(Scalar, Scalar),
    ScalarGreater(Scalar),
    ScalarLess(Scalar),
    /// (from inclusive, to inclusive)
    ScalarRange(Scalar, Scalar),
}

impl SpriteAnimationCondition {
    pub fn validate(&self, value: &SpriteAnimationValue) -> bool {
        match (self, value) {
            (Self::Bool(c), SpriteAnimationValue::Bool(v)) => c == v,
            (Self::IntegerEquals(c), SpriteAnimationValue::Integer(v)) => c == v,
            (Self::IntegerNotEquals(c), SpriteAnimationValue::Integer(v)) => c != v,
            (Self::IntegerGreater(c), SpriteAnimationValue::Integer(v)) => v > c,
            (Self::IntegerLess(c), SpriteAnimationValue::Integer(v)) => v < c,
            (Self::IntegerRange(a, b), SpriteAnimationValue::Integer(v)) => v >= a && v <= b,
            (Self::ScalarNearlyEquals(c, t), SpriteAnimationValue::Scalar(v)) => (c - v).abs() < *t,
            (Self::ScalarNotNearlyEquals(c, t), SpriteAnimationValue::Scalar(v)) => {
                (c - v).abs() >= *t
            }
            (Self::ScalarGreater(c), SpriteAnimationValue::Scalar(v)) => v > c,
            (Self::ScalarLess(c), SpriteAnimationValue::Scalar(v)) => v < c,
            (Self::ScalarRange(a, b), SpriteAnimationValue::Scalar(v)) => v >= a && v <= b,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpriteAnimationBlendState {
    pub target_state: String,
    pub axis_values: Vec<Scalar>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SpriteAnimationRule {
    Single {
        target_state: String,
        #[serde(default)]
        conditions: HashMap<String, SpriteAnimationCondition>,
        #[serde(default)]
        region: SpriteAnimationRegion,
    },
    BlendSpace {
        axis_scalars: Vec<String>,
        blend_states: Vec<SpriteAnimationBlendState>,
        #[serde(default)]
        conditions: HashMap<String, SpriteAnimationCondition>,
    },
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SpriteAnimationSignal {
    pub time: Scalar,
    pub id: String,
    #[serde(default)]
    pub params: HashMap<String, SpriteAnimationValue>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SpriteAnimationState {
    pub frames: Vec<String>,
    #[serde(default)]
    pub signals: Vec<SpriteAnimationSignal>,
    #[serde(default = "default_speed")]
    pub speed: Scalar,
    #[serde(default)]
    pub looping: bool,
    #[serde(default)]
    pub bounce: bool,
    #[serde(default)]
    pub rules: Vec<SpriteAnimationRule>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SpriteAnimationAsset {
    #[serde(default)]
    pub default_state: Option<String>,
    #[serde(default = "default_speed")]
    pub speed: Scalar,
    #[serde(default)]
    pub states: HashMap<String, SpriteAnimationState>,
    #[serde(default)]
    pub rules: Vec<SpriteAnimationRule>,
}

pub struct SpriteAnimationAssetProtocol;

impl AssetProtocol for SpriteAnimationAssetProtocol {
    fn name(&self) -> &str {
        "spriteanim"
    }

    fn on_load_with_path(&mut self, path: &str, data: Vec<u8>) -> AssetLoadResult {
        let data = if path.ends_with(".json") {
            let data = from_utf8(&data).unwrap();
            serde_json::from_str::<SpriteAnimationAsset>(data).unwrap()
        } else {
            bincode::deserialize::<SpriteAnimationAsset>(&data).unwrap()
        };
        AssetLoadResult::Data(Box::new(data))
    }

    // on_load_with_path() handles loading so this is not needed, so we just make it unreachable.
    fn on_load(&mut self, _data: Vec<u8>) -> AssetLoadResult {
        unreachable!()
    }
}