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
use super::*;

#[derive(HasSchema, Clone, Debug, Default)]
#[type_data(metadata_asset("player"))]
#[repr(C)]
pub struct PlayerMeta {
    pub name: Ustr,
    pub body_size: Vec2,
    pub slide_body_size: Vec2,
    pub gravity: f32,
    pub sounds: PlayerSoundsMeta,
    pub stats: PlayerStatsMeta,
    pub layers: PlayerLayersMeta,
}

#[derive(HasSchema, Clone, Debug, Default)]
#[repr(C)]
pub struct PlayerLayersMeta {
    pub body: PlayerBodyLayerMeta,
    pub fin: PlayerLayerMeta,
    pub face: PlayerLayerMeta,
}

#[derive(HasSchema, Clone, Debug, Default)]
#[repr(C)]
pub struct PlayerBodyLayerMeta {
    pub atlas: Handle<Atlas>,
    // #[serde(deserialize_with = "deserialize_body_animations")]
    // #[asset(deserialize_only)]
    pub animations: BodyAnimationsMeta,
}

#[derive(HasSchema, Clone, Debug, Default)]
#[derive_type_data(SchemaDeserialize)]
pub struct BodyAnimationsMeta {
    // TODO: Put Animation Frames in an `Arc` to Avoid Snapshot Clone Cost.
    pub offsets: SMap<Ustr, SVec<Offsets>>,
    pub frames: SMap<Ustr, AnimatedSprite>,
}

impl<'de> Deserialize<'de> for BodyAnimationsMeta {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserialize_body_animations(deserializer)
    }
}

#[derive(Clone, Debug, Default, HasSchema)]
#[repr(C)]
pub struct Offsets {
    pub body: Vec2,
    pub head: Vec2,
}

#[derive(HasSchema, Clone, Debug, Default)]
#[repr(C)]
pub struct PlayerLayerMeta {
    pub atlas: Handle<Atlas>,
    pub offset: Vec2,
    // TODO: Put Animation Frames in an `Arc` to Avoid Snapshot Clone Cost.
    // The current blocker to this is `Arc` doesn't implement `HasSchema`, and we have to figure
    // that out.
    // TODO: Use more economic key type such as `ustr`
    // The current blocker is implementing `HasSchema` for `ustr`.
    pub animations: SMap<Ustr, AnimatedSprite>,
}

#[derive(HasSchema, Deserialize, Clone, Debug, Default)]
#[repr(C)]
pub struct PlayerStatsMeta {
    pub jump_speed: f32,
    pub slow_fall_speed: f32,
    pub air_speed: f32,
    pub accel_air_speed: f32,
    pub walk_speed: f32,
    pub slowdown: f32,
    pub accel_walk_speed: f32,
}

#[derive(HasSchema, Clone, Debug, Default)]
#[repr(C)]
pub struct PlayerSoundsMeta {
    pub land_volume: f64,
    pub land: Handle<AudioSource>,

    pub jump_volume: f64,
    pub jump: Handle<AudioSource>,

    pub grab_volume: f64,
    pub grab: Handle<AudioSource>,

    pub drop_volume: f64,
    pub drop: Handle<AudioSource>,

    pub death_volume: f64,
    pub death: Handle<AudioSource>,
}

fn deserialize_arc<'de, T: Deserialize<'de>, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Arc<T>, D::Error> {
    let item = T::deserialize(deserializer)?;
    Ok(Arc::new(item))
}
fn deserialize_arc_slice<'de, T: Deserialize<'de>, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<Arc<[T]>, D::Error> {
    let item = <Vec<T>>::deserialize(deserializer)?;
    Ok(Arc::from(item))
}
fn default_true() -> bool {
    true
}

fn deserialize_body_animations<'de, D: Deserializer<'de>>(
    deserializer: D,
) -> Result<BodyAnimationsMeta, D::Error> {
    #[derive(Clone, Deserialize)]
    #[serde(deny_unknown_fields)]
    struct AnimatedBodySprite {
        #[serde(default)]
        pub index: u32,
        #[serde(deserialize_with = "deserialize_arc_slice")]
        #[serde(serialize_with = "serialize_arc_slice")]
        pub frames: Arc<[BodyFrame]>,
        /// The frames per second to play the animation at.
        pub fps: f32,
        /// The amount of time the current frame has been playing
        #[serde(default)]
        pub timer: f32,
        /// Whether or not to repeat the animation
        #[serde(default = "default_true")]
        pub repeat: bool,
    }

    #[derive(Clone, Deserialize)]
    #[serde(deny_unknown_fields)]
    struct BodyFrame {
        pub idx: u32,
        #[serde(default)]
        pub offset: Vec2,
        #[serde(default)]
        pub head_offset: Vec2,
    }

    let body_sprite_anmations = <HashMap<String, AnimatedBodySprite>>::deserialize(deserializer)?;

    let offsets = body_sprite_anmations
        .iter()
        .map(|(k, v)| {
            (
                ustr(k),
                v.frames
                    .iter()
                    .map(|x| Offsets {
                        body: x.offset,
                        head: x.head_offset,
                    })
                    .collect::<SVec<_>>(),
            )
        })
        .collect();
    let frames = body_sprite_anmations
        .into_iter()
        .map(|(k, v)| {
            (
                ustr(&k),
                AnimatedSprite {
                    index: v.index,
                    frames: v.frames.iter().map(|x| x.idx).collect(),
                    fps: v.fps,
                    timer: v.timer,
                    repeat: v.repeat,
                },
            )
        })
        .collect();

    Ok(BodyAnimationsMeta { offsets, frames })
}

/// Metadata for a player emote.
#[derive(HasSchema, Clone, Debug, Default)]
#[type_data(metadata_asset("emote"))]
#[repr(C)]
pub struct EmoteMeta {
    pub name: Ustr,
    pub atlas: Handle<Atlas>,
    pub offset: Vec2,
    pub animation: AnimatedSprite,
}

/// Metadata for a player hat.
#[derive(HasSchema, Clone, Debug, Default)]
#[type_data(metadata_asset("hat"))]
#[repr(C)]
pub struct HatMeta {
    pub name: Ustr,
    pub atlas: Handle<Atlas>,
    pub offset: Vec2,
    pub body_size: Vec2,
}