particles: grow now over time

This commit is contained in:
yuni 2024-09-16 00:45:20 +02:00
parent b01823c641
commit 4f71f833fb
4 changed files with 75 additions and 14 deletions

View file

@ -447,6 +447,7 @@ pub fn apply_input_to_player(
), ),
(With<actor::PlayerCamera>, Without<Camera>), (With<actor::PlayerCamera>, Without<Camera>),
>, >,
mut ew_effect: EventWriter<visual::SpawnEffectEvent>,
) { ) {
if settings.map_active || !settings.in_control() { if settings.map_active || !settings.in_control() {
return; return;
@ -564,6 +565,16 @@ pub fn apply_input_to_player(
engine.current_warmup = engine.current_warmup =
(engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0); (engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);
play_thruster_sound = true; play_thruster_sound = true;
// Visual effect
if acceleration_total.length_squared() > 1e-4 {
let thruster_direction = acceleration_total.normalize();
let thruster_v = v.0 - 10.0 * thruster_direction;
ew_effect.send(visual::SpawnEffectEvent {
duration: 1.0,
class: visual::Effects::ThrusterParticle(pos.clone(), LinearVelocity::from(thruster_v)),
});
}
} else { } else {
engine.current_warmup = engine.current_warmup =
(engine.current_warmup - dt / engine.warmup_seconds).clamp(0.0, 1.0); (engine.current_warmup - dt / engine.warmup_seconds).clamp(0.0, 1.0);

View file

@ -476,16 +476,9 @@ fn debug(
// >, // >,
mut achievement_tracker: ResMut<var::AchievementTracker>, mut achievement_tracker: ResMut<var::AchievementTracker>,
vars: Res<var::GameVars>, vars: Res<var::GameVars>,
q_playercam: Query<(&Position, &LinearVelocity), With<actor::PlayerCamera>>,
mut ew_effect: EventWriter<visual::SpawnEffectEvent>,
// materials: Query<(Entity, Option<&Name>, &Handle<Mesh>)>, // materials: Query<(Entity, Option<&Name>, &Handle<Mesh>)>,
) { ) {
if settings.dev_mode && keyboard_input.just_pressed(KeyCode::KeyP) { if settings.dev_mode && keyboard_input.just_pressed(KeyCode::KeyP) {
let (pos, v) = q_playercam.get_single().unwrap();
ew_effect.send(visual::SpawnEffectEvent {
duration: 0.0,
class: visual::Effects::ThrusterParticle(pos.clone(), v.clone()),
});
// for (entity, _name, mesh) in &materials { // for (entity, _name, mesh) in &materials {
// dbg!(mesh); // dbg!(mesh);

View file

@ -27,6 +27,7 @@ impl Plugin for VisualPlugin {
app.add_systems(Update, spawn_effects); app.add_systems(Update, spawn_effects);
app.add_systems(Update, update_fadein); app.add_systems(Update, update_fadein);
app.add_systems(Update, update_fadeout); app.add_systems(Update, update_fadeout);
app.add_systems(Update, update_grow);
app.add_systems(Update, play_animations); app.add_systems(Update, play_animations);
// Blackout disabled for now // Blackout disabled for now
//app.add_systems(Update, update_blackout); //app.add_systems(Update, update_blackout);
@ -45,9 +46,18 @@ pub enum Effects {
//#[derive(Component)] pub struct BlackOutOverlay; //#[derive(Component)] pub struct BlackOutOverlay;
#[derive(Component)] #[derive(Component)]
pub struct FadeIn; pub struct FadeInSprite;
#[derive(Component)] #[derive(Component)]
pub struct FadeOut; pub struct FadeOutSprite;
#[derive(Component)]
pub struct FadeOut3DObject;
#[derive(Component)]
pub struct Grow3DObject {
pub start_time: f64,
pub duration: f64,
pub scale_start: f32,
pub scale_end: f32,
}
#[derive(Component)] #[derive(Component)]
pub struct Effect { pub struct Effect {
pub class: Effects, pub class: Effects,
@ -126,7 +136,7 @@ pub fn spawn_effects(
duration: effect.duration, duration: effect.duration,
start_time: now, start_time: now,
}, },
FadeIn, FadeInSprite,
NodeBundle { NodeBundle {
style: style_fullscreen(), style: style_fullscreen(),
background_color: color.into(), background_color: color.into(),
@ -141,7 +151,7 @@ pub fn spawn_effects(
duration: effect.duration, duration: effect.duration,
start_time: now, start_time: now,
}, },
FadeOut, FadeOutSprite,
NodeBundle { NodeBundle {
style: style_fullscreen(), style: style_fullscreen(),
background_color: color.with_alpha(0.0).into(), background_color: color.with_alpha(0.0).into(),
@ -156,10 +166,17 @@ pub fn spawn_effects(
bevy::pbr::NotShadowCaster, bevy::pbr::NotShadowCaster,
pos, pos,
v, v,
Grow3DObject {
start_time: now,
duration: effect.duration,
scale_start: 1.0,
scale_end: 10.0,
},
world::DespawnAt(now + effect.duration * 3.0),
world::DespawnOnPlayerDeath, world::DespawnOnPlayerDeath,
actor::WantsToLookAt(cmd::ID_SPECIAL_PLAYERCAM.into()), actor::WantsToLookAt(cmd::ID_SPECIAL_PLAYERCAM.into()),
PbrBundle { PbrBundle {
mesh: meshes.add(Mesh::from(Rectangle::new(1.0, 1.0))), mesh: meshes.add(Mesh::from(Rectangle::new(0.2, 0.2))),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color_texture: Some(texture), base_color_texture: Some(texture),
perceptual_roughness: 1.0, perceptual_roughness: 1.0,
@ -176,7 +193,7 @@ pub fn spawn_effects(
pub fn update_fadein( pub fn update_fadein(
mut commands: Commands, mut commands: Commands,
mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeIn>>, mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeInSprite>>,
time: Res<Time>, time: Res<Time>,
) { ) {
for (entity, effect, mut bgcolor) in &mut q_effect { for (entity, effect, mut bgcolor) in &mut q_effect {
@ -192,7 +209,7 @@ pub fn update_fadein(
pub fn update_fadeout( pub fn update_fadeout(
mut commands: Commands, mut commands: Commands,
mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeOut>>, mut q_effect: Query<(Entity, &Effect, &mut BackgroundColor), With<FadeOutSprite>>,
time: Res<Time>, time: Res<Time>,
) { ) {
for (entity, effect, mut bgcolor) in &mut q_effect { for (entity, effect, mut bgcolor) in &mut q_effect {
@ -206,6 +223,30 @@ pub fn update_fadeout(
} }
} }
pub fn update_grow(
mut commands: Commands,
mut q_object: Query<(Entity, &mut Transform, &Grow3DObject)>,
time: Res<Time>,
) {
let now = time.elapsed_seconds_f64();
for (entity, mut trans, data) in q_object.iter_mut() {
let end_time = data.start_time + data.duration;
if now > end_time {
trans.scale = Vec3::splat(data.scale_end);
commands.entity(entity).remove::<Grow3DObject>();
}
else if now < data.start_time {
trans.scale = Vec3::splat(data.scale_start);
}
else {
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);
}
}
}
fn play_animations( fn play_animations(
mut commands: Commands, mut commands: Commands,
mut players: Query<(Entity, &mut AnimationPlayer), Added<AnimationPlayer>>, mut players: Query<(Entity, &mut AnimationPlayer), Added<AnimationPlayer>>,

View file

@ -38,6 +38,7 @@ impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, handle_respawn.run_if(on_event::<RespawnEvent>())); app.add_systems(Update, handle_respawn.run_if(on_event::<RespawnEvent>()));
app.add_systems(Update, handle_despawn_at.run_if(any_with_component::<DespawnAt>));
app.add_plugins(PhysicsPlugins::default()); app.add_plugins(PhysicsPlugins::default());
//app.add_plugins(PhysicsDebugPlugin::default()); //app.add_plugins(PhysicsDebugPlugin::default());
app.insert_resource(Gravity(DVec3::splat(0.0))); app.insert_resource(Gravity(DVec3::splat(0.0)));
@ -65,6 +66,8 @@ struct Asteroid;
pub struct Star; pub struct Star;
#[derive(Component)] #[derive(Component)]
pub struct DespawnOnPlayerDeath; pub struct DespawnOnPlayerDeath;
#[derive(Component)]
pub struct DespawnAt(pub f64);
#[derive(Event)] #[derive(Event)]
pub struct RespawnEvent; pub struct RespawnEvent;
@ -360,6 +363,19 @@ fn handle_despawn_asteroids(
} }
} }
fn handle_despawn_at(
mut commands: Commands,
q_entity: Query<(Entity, &DespawnAt)>,
time: Res<Time>,
) {
let now = time.elapsed_seconds_f64();
for (entity, despawn_at) in &q_entity {
if despawn_at.0 < now {
commands.entity(entity).despawn();
}
}
}
pub fn handle_respawn( pub fn handle_respawn(
ew_spawn: EventWriter<cmd::SpawnEvent>, ew_spawn: EventWriter<cmd::SpawnEvent>,
mut achievement_tracker: ResMut<var::AchievementTracker>, mut achievement_tracker: ResMut<var::AchievementTracker>,