~starkingdoms/starkingdoms

ref: 265fc540b0a0a8ba38d66066fa6c570968f59e37 starkingdoms/crates/unified/src/particles.rs -rw-r--r-- 1.9 KiB
265fc540 — core chore: newtype linearsplines for particle system 5 months ago
                                                                                
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
use bevy::color::LinearRgba;
use bevy::math::cubic_splines::LinearSpline;
use bevy::math::Vec2;
use rand::Rng;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
pub struct ParticleEffect {
    // -- lifetime / spawning -- //

    /// Particle lifetime in seconds
    pub lifetime_seconds: RandF32,
    /// Delay inbetween each batch of particles spawned
    pub batch_spawn_delay_seconds: RandF32,
    /// Number of distinct particles spawned per batch
    pub particles_in_batch: RandF32,

    // -- velocity -- //

    /// Initial linear velocity added to the particle's velocity when it is spawned
    pub initial_linear_velocity: RandVec2,
    /// Initial angular velocity added to the particle's rotation when it is spawned
    pub initial_angular_velocity: RandF32,

    // -- scale -- //

    // Scale curve over the lifetime of the particle
    pub scale: ScaleSpline,

    // -- color -- //

    // Color curve over the lifetime of the particle
    pub color: ColorSpline,
}

#[derive(Deserialize, Serialize)]
pub struct ScaleSpline(Vec<f32>);
impl From<ScaleSpline> for LinearSpline<f32> {
    fn from(value: ScaleSpline) -> Self {
        Self::new(value.0)
    }
}
#[derive(Deserialize, Serialize)]
pub struct ColorSpline(Vec<LinearRgba>);
impl From<ColorSpline> for LinearSpline<LinearRgba> {
    fn from(value: ColorSpline) -> Self {
        Self::new(value.0)
    }
}


#[derive(Deserialize, Serialize)]
pub struct RandF32 {
    pub value: f32,
    pub randomness: f32
}
impl RandF32 {
    pub fn sample(&self, rng: &mut impl Rng) -> f32 {
        rng.random_range(self.value-self.randomness .. self.value+self.randomness)
    }
}

#[derive(Deserialize, Serialize)]
pub struct RandVec2 {
    pub x: RandF32,
    pub y: RandF32,
}
impl RandVec2 {
    pub fn sample(&self, rng: &mut impl Rng) -> Vec2 {
        Vec2::new(self.x.sample(rng), self.y.sample(rng))
    }
}