Struct FireworkConfig

Source
pub struct FireworkConfig {
    pub gravity_scale: f32,
    pub ar_scale: f32,
    pub additional_force: Box<dyn Fn(&Particle) -> Vec2>,
    pub gradient_scale: fn(f32) -> f32,
    pub enable_gradient: bool,
}
Expand description

Struct representing the configuration of a single Firework

This applies to all Particle in the Firework

Fields§

§gravity_scale: f32

Larger gravity_scale tends to pull particles down

§ar_scale: f32

Air resistance scale Warning: too large or too small ar_scale may lead to unexpected behavior of Particles

§additional_force: Box<dyn Fn(&Particle) -> Vec2>§gradient_scale: fn(f32) -> f32

This field is a function that takes a float between 0 and 1, returns a float representing all Particles’ gradient

Particles’ gradient changes according to its elapsed time and lifetime The input f32 equals to time_elapsed/life_time, which returns a f32 affecting its color gradient gradient_scale returns 1. meansParticle will have the same colors as defined all over its lifetime

§enable_gradient: bool

Set wheter or not firework has color gradient

§Notes

  • It is recommanded that your terminal window is non-transparent and has black bg color to get better visual effects
  • Otherwise set it to false

Implementations§

Source§

impl FireworkConfig

Source

pub fn with_gradient_scale(self, f: fn(f32) -> f32) -> Self

Set gradient_scale

Examples found in repository?
examples/fountain.rs (line 96)
74fn gen_fountain_firework(center: Vec2) -> Firework {
75    let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
76    let mut particles = Vec::new();
77    for v in gen_points_fan(
78        300.,
79        45,
80        5 as f32 / 12 as f32 * PI,
81        7 as f32 / 12 as f32 * PI,
82    )
83    .iter()
84    {
85        particles.push(ParticleConfig::new(
86            center,
87            *v,
88            thread_rng().gen_range(28..38),
89            Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
90            *colors.iter().choose(&mut thread_rng()).unwrap(),
91        ));
92    }
93    let mut config = FireworkConfig::default()
94        .with_ar_scale(0.15)
95        .with_gravity_scale(0.5)
96        .with_gradient_scale(gradient);
97    config.set_enable_gradient(true);
98    Firework {
99        init_time: SystemTime::now(),
100        spawn_after: Duration::ZERO,
101        center,
102        particles,
103        config,
104        form: ExplosionForm::Sustained {
105            lasts: Duration::from_secs(5),
106            time_interval: Duration::from_secs_f32(0.08),
107            timer: Duration::ZERO,
108        },
109        ..Default::default()
110    }
111}
More examples
Hide additional examples
examples/vortex.rs (line 94)
73fn gen_vortex_firework(center: Vec2) -> Firework {
74    let colors = vec![
75        (6, 55, 63),
76        (24, 90, 96),
77        (47, 123, 119),
78        (92, 174, 166),
79        (200, 255, 255),
80    ];
81    let mut particles = Vec::new();
82    for p in gen_points_circle(30, 45).iter() {
83        particles.push(ParticleConfig::new(
84            center + *p,
85            Vec2::new((*p).y, -(*p).x).normalize() * 15.,
86            thread_rng().gen_range(28..40),
87            Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
88            *colors.iter().choose(&mut thread_rng()).unwrap(),
89        ));
90    }
91    let mut config = FireworkConfig::default()
92        .with_ar_scale(0.05)
93        .with_gravity_scale(0.)
94        .with_gradient_scale(gradient)
95        .with_additional_force(move |particle| {
96            (center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
97        });
98    config.set_enable_gradient(true);
99    Firework {
100        init_time: SystemTime::now(),
101        spawn_after: Duration::ZERO,
102        center,
103        particles,
104        config,
105        form: ExplosionForm::Sustained {
106            lasts: Duration::from_secs(10),
107            time_interval: Duration::from_secs_f32(0.01),
108            timer: Duration::ZERO,
109        },
110        ..Default::default()
111    }
112}
examples/heart.rs (line 106)
74fn gen_heart_firework(center: Vec2) -> Firework {
75    let colors = vec![
76        (233, 232, 237),
77        (254, 142, 130),
78        (200, 27, 72),
79        (86, 18, 31),
80    ];
81    let mut particles = Vec::new();
82    let trail_length = thread_rng().gen_range(100..105);
83    let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
84    let init_pos = center - Vec2::NEG_Y * 15.;
85    for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
86        particles.push(ParticleConfig::new(
87            init_pos,
88            *v,
89            trail_length,
90            life_time,
91            *colors.iter().choose(&mut thread_rng()).unwrap(),
92        ));
93    }
94    for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
95        particles.push(ParticleConfig::new(
96            init_pos,
97            *v,
98            trail_length,
99            life_time,
100            *colors.iter().choose(&mut thread_rng()).unwrap(),
101        ));
102    }
103    let mut config = FireworkConfig::default()
104        .with_ar_scale(0.1)
105        .with_gravity_scale(0.1)
106        .with_gradient_scale(gradient)
107        .with_additional_force(move |particle| (center - particle.pos) * 2.);
108    config.set_enable_gradient(true);
109    Firework {
110        init_time: SystemTime::now(),
111        spawn_after: Duration::ZERO,
112        center,
113        particles,
114        config,
115        form: ExplosionForm::Instant { used: false },
116        ..Default::default()
117    }
118}
Source

pub fn with_gravity_scale(self, s: f32) -> Self

Set gravity_scale

Examples found in repository?
examples/fountain.rs (line 95)
74fn gen_fountain_firework(center: Vec2) -> Firework {
75    let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
76    let mut particles = Vec::new();
77    for v in gen_points_fan(
78        300.,
79        45,
80        5 as f32 / 12 as f32 * PI,
81        7 as f32 / 12 as f32 * PI,
82    )
83    .iter()
84    {
85        particles.push(ParticleConfig::new(
86            center,
87            *v,
88            thread_rng().gen_range(28..38),
89            Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
90            *colors.iter().choose(&mut thread_rng()).unwrap(),
91        ));
92    }
93    let mut config = FireworkConfig::default()
94        .with_ar_scale(0.15)
95        .with_gravity_scale(0.5)
96        .with_gradient_scale(gradient);
97    config.set_enable_gradient(true);
98    Firework {
99        init_time: SystemTime::now(),
100        spawn_after: Duration::ZERO,
101        center,
102        particles,
103        config,
104        form: ExplosionForm::Sustained {
105            lasts: Duration::from_secs(5),
106            time_interval: Duration::from_secs_f32(0.08),
107            timer: Duration::ZERO,
108        },
109        ..Default::default()
110    }
111}
More examples
Hide additional examples
examples/vortex.rs (line 93)
73fn gen_vortex_firework(center: Vec2) -> Firework {
74    let colors = vec![
75        (6, 55, 63),
76        (24, 90, 96),
77        (47, 123, 119),
78        (92, 174, 166),
79        (200, 255, 255),
80    ];
81    let mut particles = Vec::new();
82    for p in gen_points_circle(30, 45).iter() {
83        particles.push(ParticleConfig::new(
84            center + *p,
85            Vec2::new((*p).y, -(*p).x).normalize() * 15.,
86            thread_rng().gen_range(28..40),
87            Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
88            *colors.iter().choose(&mut thread_rng()).unwrap(),
89        ));
90    }
91    let mut config = FireworkConfig::default()
92        .with_ar_scale(0.05)
93        .with_gravity_scale(0.)
94        .with_gradient_scale(gradient)
95        .with_additional_force(move |particle| {
96            (center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
97        });
98    config.set_enable_gradient(true);
99    Firework {
100        init_time: SystemTime::now(),
101        spawn_after: Duration::ZERO,
102        center,
103        particles,
104        config,
105        form: ExplosionForm::Sustained {
106            lasts: Duration::from_secs(10),
107            time_interval: Duration::from_secs_f32(0.01),
108            timer: Duration::ZERO,
109        },
110        ..Default::default()
111    }
112}
examples/heart.rs (line 105)
74fn gen_heart_firework(center: Vec2) -> Firework {
75    let colors = vec![
76        (233, 232, 237),
77        (254, 142, 130),
78        (200, 27, 72),
79        (86, 18, 31),
80    ];
81    let mut particles = Vec::new();
82    let trail_length = thread_rng().gen_range(100..105);
83    let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
84    let init_pos = center - Vec2::NEG_Y * 15.;
85    for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
86        particles.push(ParticleConfig::new(
87            init_pos,
88            *v,
89            trail_length,
90            life_time,
91            *colors.iter().choose(&mut thread_rng()).unwrap(),
92        ));
93    }
94    for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
95        particles.push(ParticleConfig::new(
96            init_pos,
97            *v,
98            trail_length,
99            life_time,
100            *colors.iter().choose(&mut thread_rng()).unwrap(),
101        ));
102    }
103    let mut config = FireworkConfig::default()
104        .with_ar_scale(0.1)
105        .with_gravity_scale(0.1)
106        .with_gradient_scale(gradient)
107        .with_additional_force(move |particle| (center - particle.pos) * 2.);
108    config.set_enable_gradient(true);
109    Firework {
110        init_time: SystemTime::now(),
111        spawn_after: Duration::ZERO,
112        center,
113        particles,
114        config,
115        form: ExplosionForm::Instant { used: false },
116        ..Default::default()
117    }
118}
Source

pub fn with_ar_scale(self, s: f32) -> Self

Set ar_scale

Examples found in repository?
examples/fountain.rs (line 94)
74fn gen_fountain_firework(center: Vec2) -> Firework {
75    let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
76    let mut particles = Vec::new();
77    for v in gen_points_fan(
78        300.,
79        45,
80        5 as f32 / 12 as f32 * PI,
81        7 as f32 / 12 as f32 * PI,
82    )
83    .iter()
84    {
85        particles.push(ParticleConfig::new(
86            center,
87            *v,
88            thread_rng().gen_range(28..38),
89            Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
90            *colors.iter().choose(&mut thread_rng()).unwrap(),
91        ));
92    }
93    let mut config = FireworkConfig::default()
94        .with_ar_scale(0.15)
95        .with_gravity_scale(0.5)
96        .with_gradient_scale(gradient);
97    config.set_enable_gradient(true);
98    Firework {
99        init_time: SystemTime::now(),
100        spawn_after: Duration::ZERO,
101        center,
102        particles,
103        config,
104        form: ExplosionForm::Sustained {
105            lasts: Duration::from_secs(5),
106            time_interval: Duration::from_secs_f32(0.08),
107            timer: Duration::ZERO,
108        },
109        ..Default::default()
110    }
111}
More examples
Hide additional examples
examples/vortex.rs (line 92)
73fn gen_vortex_firework(center: Vec2) -> Firework {
74    let colors = vec![
75        (6, 55, 63),
76        (24, 90, 96),
77        (47, 123, 119),
78        (92, 174, 166),
79        (200, 255, 255),
80    ];
81    let mut particles = Vec::new();
82    for p in gen_points_circle(30, 45).iter() {
83        particles.push(ParticleConfig::new(
84            center + *p,
85            Vec2::new((*p).y, -(*p).x).normalize() * 15.,
86            thread_rng().gen_range(28..40),
87            Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
88            *colors.iter().choose(&mut thread_rng()).unwrap(),
89        ));
90    }
91    let mut config = FireworkConfig::default()
92        .with_ar_scale(0.05)
93        .with_gravity_scale(0.)
94        .with_gradient_scale(gradient)
95        .with_additional_force(move |particle| {
96            (center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
97        });
98    config.set_enable_gradient(true);
99    Firework {
100        init_time: SystemTime::now(),
101        spawn_after: Duration::ZERO,
102        center,
103        particles,
104        config,
105        form: ExplosionForm::Sustained {
106            lasts: Duration::from_secs(10),
107            time_interval: Duration::from_secs_f32(0.01),
108            timer: Duration::ZERO,
109        },
110        ..Default::default()
111    }
112}
examples/heart.rs (line 104)
74fn gen_heart_firework(center: Vec2) -> Firework {
75    let colors = vec![
76        (233, 232, 237),
77        (254, 142, 130),
78        (200, 27, 72),
79        (86, 18, 31),
80    ];
81    let mut particles = Vec::new();
82    let trail_length = thread_rng().gen_range(100..105);
83    let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
84    let init_pos = center - Vec2::NEG_Y * 15.;
85    for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
86        particles.push(ParticleConfig::new(
87            init_pos,
88            *v,
89            trail_length,
90            life_time,
91            *colors.iter().choose(&mut thread_rng()).unwrap(),
92        ));
93    }
94    for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
95        particles.push(ParticleConfig::new(
96            init_pos,
97            *v,
98            trail_length,
99            life_time,
100            *colors.iter().choose(&mut thread_rng()).unwrap(),
101        ));
102    }
103    let mut config = FireworkConfig::default()
104        .with_ar_scale(0.1)
105        .with_gravity_scale(0.1)
106        .with_gradient_scale(gradient)
107        .with_additional_force(move |particle| (center - particle.pos) * 2.);
108    config.set_enable_gradient(true);
109    Firework {
110        init_time: SystemTime::now(),
111        spawn_after: Duration::ZERO,
112        center,
113        particles,
114        config,
115        form: ExplosionForm::Instant { used: false },
116        ..Default::default()
117    }
118}
Source

pub fn with_additional_force( self, af: impl Fn(&Particle) -> Vec2 + 'static, ) -> Self

Set additional_force

Examples found in repository?
examples/vortex.rs (lines 95-97)
73fn gen_vortex_firework(center: Vec2) -> Firework {
74    let colors = vec![
75        (6, 55, 63),
76        (24, 90, 96),
77        (47, 123, 119),
78        (92, 174, 166),
79        (200, 255, 255),
80    ];
81    let mut particles = Vec::new();
82    for p in gen_points_circle(30, 45).iter() {
83        particles.push(ParticleConfig::new(
84            center + *p,
85            Vec2::new((*p).y, -(*p).x).normalize() * 15.,
86            thread_rng().gen_range(28..40),
87            Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
88            *colors.iter().choose(&mut thread_rng()).unwrap(),
89        ));
90    }
91    let mut config = FireworkConfig::default()
92        .with_ar_scale(0.05)
93        .with_gravity_scale(0.)
94        .with_gradient_scale(gradient)
95        .with_additional_force(move |particle| {
96            (center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
97        });
98    config.set_enable_gradient(true);
99    Firework {
100        init_time: SystemTime::now(),
101        spawn_after: Duration::ZERO,
102        center,
103        particles,
104        config,
105        form: ExplosionForm::Sustained {
106            lasts: Duration::from_secs(10),
107            time_interval: Duration::from_secs_f32(0.01),
108            timer: Duration::ZERO,
109        },
110        ..Default::default()
111    }
112}
More examples
Hide additional examples
examples/heart.rs (line 107)
74fn gen_heart_firework(center: Vec2) -> Firework {
75    let colors = vec![
76        (233, 232, 237),
77        (254, 142, 130),
78        (200, 27, 72),
79        (86, 18, 31),
80    ];
81    let mut particles = Vec::new();
82    let trail_length = thread_rng().gen_range(100..105);
83    let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
84    let init_pos = center - Vec2::NEG_Y * 15.;
85    for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
86        particles.push(ParticleConfig::new(
87            init_pos,
88            *v,
89            trail_length,
90            life_time,
91            *colors.iter().choose(&mut thread_rng()).unwrap(),
92        ));
93    }
94    for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
95        particles.push(ParticleConfig::new(
96            init_pos,
97            *v,
98            trail_length,
99            life_time,
100            *colors.iter().choose(&mut thread_rng()).unwrap(),
101        ));
102    }
103    let mut config = FireworkConfig::default()
104        .with_ar_scale(0.1)
105        .with_gravity_scale(0.1)
106        .with_gradient_scale(gradient)
107        .with_additional_force(move |particle| (center - particle.pos) * 2.);
108    config.set_enable_gradient(true);
109    Firework {
110        init_time: SystemTime::now(),
111        spawn_after: Duration::ZERO,
112        center,
113        particles,
114        config,
115        form: ExplosionForm::Instant { used: false },
116        ..Default::default()
117    }
118}
Source

pub fn set_enable_gradient(&mut self, enable_gradient: bool)

Set enable_gradient

Examples found in repository?
examples/fountain.rs (line 97)
74fn gen_fountain_firework(center: Vec2) -> Firework {
75    let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
76    let mut particles = Vec::new();
77    for v in gen_points_fan(
78        300.,
79        45,
80        5 as f32 / 12 as f32 * PI,
81        7 as f32 / 12 as f32 * PI,
82    )
83    .iter()
84    {
85        particles.push(ParticleConfig::new(
86            center,
87            *v,
88            thread_rng().gen_range(28..38),
89            Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
90            *colors.iter().choose(&mut thread_rng()).unwrap(),
91        ));
92    }
93    let mut config = FireworkConfig::default()
94        .with_ar_scale(0.15)
95        .with_gravity_scale(0.5)
96        .with_gradient_scale(gradient);
97    config.set_enable_gradient(true);
98    Firework {
99        init_time: SystemTime::now(),
100        spawn_after: Duration::ZERO,
101        center,
102        particles,
103        config,
104        form: ExplosionForm::Sustained {
105            lasts: Duration::from_secs(5),
106            time_interval: Duration::from_secs_f32(0.08),
107            timer: Duration::ZERO,
108        },
109        ..Default::default()
110    }
111}
More examples
Hide additional examples
examples/vortex.rs (line 98)
73fn gen_vortex_firework(center: Vec2) -> Firework {
74    let colors = vec![
75        (6, 55, 63),
76        (24, 90, 96),
77        (47, 123, 119),
78        (92, 174, 166),
79        (200, 255, 255),
80    ];
81    let mut particles = Vec::new();
82    for p in gen_points_circle(30, 45).iter() {
83        particles.push(ParticleConfig::new(
84            center + *p,
85            Vec2::new((*p).y, -(*p).x).normalize() * 15.,
86            thread_rng().gen_range(28..40),
87            Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
88            *colors.iter().choose(&mut thread_rng()).unwrap(),
89        ));
90    }
91    let mut config = FireworkConfig::default()
92        .with_ar_scale(0.05)
93        .with_gravity_scale(0.)
94        .with_gradient_scale(gradient)
95        .with_additional_force(move |particle| {
96            (center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
97        });
98    config.set_enable_gradient(true);
99    Firework {
100        init_time: SystemTime::now(),
101        spawn_after: Duration::ZERO,
102        center,
103        particles,
104        config,
105        form: ExplosionForm::Sustained {
106            lasts: Duration::from_secs(10),
107            time_interval: Duration::from_secs_f32(0.01),
108            timer: Duration::ZERO,
109        },
110        ..Default::default()
111    }
112}
examples/heart.rs (line 108)
74fn gen_heart_firework(center: Vec2) -> Firework {
75    let colors = vec![
76        (233, 232, 237),
77        (254, 142, 130),
78        (200, 27, 72),
79        (86, 18, 31),
80    ];
81    let mut particles = Vec::new();
82    let trail_length = thread_rng().gen_range(100..105);
83    let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
84    let init_pos = center - Vec2::NEG_Y * 15.;
85    for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
86        particles.push(ParticleConfig::new(
87            init_pos,
88            *v,
89            trail_length,
90            life_time,
91            *colors.iter().choose(&mut thread_rng()).unwrap(),
92        ));
93    }
94    for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
95        particles.push(ParticleConfig::new(
96            init_pos,
97            *v,
98            trail_length,
99            life_time,
100            *colors.iter().choose(&mut thread_rng()).unwrap(),
101        ));
102    }
103    let mut config = FireworkConfig::default()
104        .with_ar_scale(0.1)
105        .with_gravity_scale(0.1)
106        .with_gradient_scale(gradient)
107        .with_additional_force(move |particle| (center - particle.pos) * 2.);
108    config.set_enable_gradient(true);
109    Firework {
110        init_time: SystemTime::now(),
111        spawn_after: Duration::ZERO,
112        center,
113        particles,
114        config,
115        form: ExplosionForm::Instant { used: false },
116        ..Default::default()
117    }
118}

Trait Implementations§

Source§

impl Default for FireworkConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V