yog_registry/
particles.rs1use yog_data::kinds::Texture;
6use yog_data::with::{List, RefTo};
7
8mod game {
10 pub const FRICTION: f32 = 0.98;
12 pub const LIFETIME: u32 = 20;
14}
15
16#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
18pub struct ParticleDef {
19 pub id: String,
20 pub look: Particle,
21}
22
23#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
25pub struct Particle {
26 #[data(with = "List<RefTo<Texture>>")]
29 pub textures: Vec<String>,
30 #[data(default)]
32 pub frames: Frames,
33 #[data(default = "one")]
35 pub frame_speed: f32,
36 #[data(default)]
39 pub frame_settle: bool,
40 #[data(default = "lifetime")]
42 pub lifetime: Lifetime,
43 #[data(default = "one")]
45 pub size: f32,
46 #[data(default = "one")]
48 pub size_per_tick: f32,
49 #[data(default = "one", range = "0..=1")]
51 pub alpha: f32,
52 #[data(default)]
54 pub fade: bool,
55 #[data(default = "friction")]
57 pub friction: f32,
58 #[data(default)]
60 pub gravity: f32,
61 #[data(default)]
63 pub gravity_needs_motion: bool,
64 #[data(default)]
66 pub physics: bool,
67 #[data(default)]
69 pub slide_when_blocked: bool,
70 #[data(default)]
72 pub random_roll: bool,
73 #[data(default)]
75 pub render: ParticleRender,
76}
77
78#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq, Default)]
80pub enum Frames {
81 Random,
83 #[default]
85 Age,
86}
87
88#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq)]
90pub struct Lifetime {
91 pub min: u32,
92 pub max: u32,
93}
94
95#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq)]
97pub struct ParticleRender {
98 #[data(default)]
99 pub blend: Blend,
100 #[data(default = "yes")]
102 pub depth_write: bool,
103 #[data(default)]
105 pub glow: bool,
106}
107
108impl Default for ParticleRender {
109 fn default() -> Self {
110 ParticleRender {
111 blend: Blend::default(),
112 depth_write: true,
113 glow: false,
114 }
115 }
116}
117
118#[derive(yog_data::Schema, Debug, Clone, Copy, PartialEq, Eq, Default)]
120pub enum Blend {
121 Opaque,
123 #[default]
125 Translucent,
126 Additive,
128}
129
130fn one() -> f32 {
131 1.0
132}
133
134fn yes() -> bool {
135 true
136}
137
138fn friction() -> f32 {
139 game::FRICTION
140}
141
142fn lifetime() -> Lifetime {
143 Lifetime {
144 min: game::LIFETIME,
145 max: game::LIFETIME,
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[test]
154 fn a_particle_says_only_what_isnt_the_games() {
155 let p = yog_data::from_text::<Particle>(
156 r#"{ textures: ["@texture:m:particle/cloud"], render: { blend: "additive" } }"#,
157 )
158 .unwrap()
159 .value;
160 assert_eq!(p.friction, game::FRICTION);
161 assert_eq!(p.lifetime, lifetime());
162 assert_eq!(p.frames, Frames::Age);
163 assert_eq!(p.render.blend, Blend::Additive);
164 assert!(p.render.depth_write && !p.render.glow);
165 }
166}