particles: fade out transparency over time

This commit is contained in:
yuni 2024-09-16 01:17:43 +02:00
parent 4f71f833fb
commit 7e7b8e7432
2 changed files with 48 additions and 5 deletions

View file

@ -430,6 +430,7 @@ pub fn apply_input_to_player(
(&Position, &LinearVelocity),
(
Without<hud::IsTargeted>,
Without<visual::IsEffect>,
Without<actor::PlayerCamera>,
Without<actor::Player>,
Without<actor::PlayerDrivesThis>,
@ -571,7 +572,7 @@ pub fn apply_input_to_player(
let thruster_direction = acceleration_total.normalize();
let thruster_v = v.0 - 10.0 * thruster_direction;
ew_effect.send(visual::SpawnEffectEvent {
duration: 1.0,
duration: 2.0,
class: visual::Effects::ThrusterParticle(pos.clone(), LinearVelocity::from(thruster_v)),
});
}

View file

@ -27,6 +27,7 @@ impl Plugin for VisualPlugin {
app.add_systems(Update, spawn_effects);
app.add_systems(Update, update_fadein);
app.add_systems(Update, update_fadeout);
app.add_systems(Update, update_fade_material);
app.add_systems(Update, update_grow);
app.add_systems(Update, play_animations);
// Blackout disabled for now
@ -45,12 +46,19 @@ pub enum Effects {
// Blackout disabled for now
//#[derive(Component)] pub struct BlackOutOverlay;
#[derive(Component)]
pub struct IsEffect;
#[derive(Component)]
pub struct FadeInSprite;
#[derive(Component)]
pub struct FadeOutSprite;
#[derive(Component)]
pub struct FadeOut3DObject;
pub struct FadeMaterial {
pub start_time: f64,
pub duration: f64,
pub value_start: f32,
pub value_end: f32,
}
#[derive(Component)]
pub struct Grow3DObject {
pub start_time: f64,
@ -131,6 +139,7 @@ pub fn spawn_effects(
match effect.class {
Effects::FadeIn(color) => {
commands.spawn((
IsEffect,
Effect {
class: effect.class.clone(),
duration: effect.duration,
@ -146,6 +155,7 @@ pub fn spawn_effects(
}
Effects::FadeOut(color) => {
commands.spawn((
IsEffect,
Effect {
class: effect.class.clone(),
duration: effect.duration,
@ -162,6 +172,7 @@ pub fn spawn_effects(
Effects::ThrusterParticle(pos, v) => {
let texture = asset_server.load("textures/exhaust.png");
commands.spawn((
IsEffect,
RigidBody::Kinematic,
bevy::pbr::NotShadowCaster,
pos,
@ -170,9 +181,15 @@ pub fn spawn_effects(
start_time: now,
duration: effect.duration,
scale_start: 1.0,
scale_end: 10.0,
scale_end: 20.0,
},
world::DespawnAt(now + effect.duration * 3.0),
FadeMaterial {
start_time: now,
duration: effect.duration,
value_start: 0.1,
value_end: 0.0,
},
world::DespawnAt(now + effect.duration),
world::DespawnOnPlayerDeath,
actor::WantsToLookAt(cmd::ID_SPECIAL_PLAYERCAM.into()),
PbrBundle {
@ -180,6 +197,7 @@ pub fn spawn_effects(
material: materials.add(StandardMaterial {
base_color_texture: Some(texture),
perceptual_roughness: 1.0,
metallic: 0.5,
alpha_mode: AlphaMode::Blend,
..Default::default()
}),
@ -223,6 +241,31 @@ pub fn update_fadeout(
}
}
pub fn update_fade_material(
mut commands: Commands,
mut q_object: Query<(Entity, &Handle<StandardMaterial>, &FadeMaterial)>,
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let now = time.elapsed_seconds_f64();
for (entity, material_handle, data) in q_object.iter_mut() {
let end_time = data.start_time + data.duration;
let material = materials.get_mut(material_handle).unwrap();
if now > end_time {
material.base_color.set_alpha(data.value_end);
commands.entity(entity).remove::<FadeMaterial>();
}
else if now < data.start_time {
material.base_color.set_alpha(data.value_start);
}
else {
let progress = ((now - data.start_time) / data.duration) as f32;
let value = data.value_start + progress * (data.value_end - data.value_start);
material.base_color.set_alpha(value);
}
}
}
pub fn update_grow(
mut commands: Commands,
mut q_object: Query<(Entity, &mut Transform, &Grow3DObject)>,
@ -242,7 +285,6 @@ pub fn update_grow(
let progress = ((now - data.start_time) / data.duration) as f32;
let scale = data.scale_start + progress * (data.scale_end - data.scale_start);
trans.scale = Vec3::splat(scale);
dbg!(progress, scale, trans.scale);
}
}
}