Skip to content

Commit 074288a

Browse files
committedJul 25, 2024·
Add projectile fade out tween
1 parent f1bdcd1 commit 074288a

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed
 

‎src/game/combat/projectile.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::time::Duration;
2+
13
use avian2d::prelude::*;
24
use bevy::ecs::system::EntityCommand;
35
use bevy::prelude::*;
46
use bevy::utils::HashMap;
7+
use bevy_tweening::*;
58
use serde::Deserialize;
69
use serde::Serialize;
710

@@ -54,14 +57,17 @@ pub struct Projectile {
5457
#[serde(skip)]
5558
pub texture: Handle<Image>,
5659

57-
/// In seconds, not beats.
60+
/// Lifetime in seconds, not beats.
5861
pub lifetime: f32,
62+
/// Hitbox radius.
5963
pub radius: f32,
6064
pub speed: f32,
6165
pub damage: f32,
6266
pub knockback: f32,
6367
}
6468

69+
const FADE_SECS: f32 = 0.2;
70+
6571
pub fn projectile(
6672
key: impl Into<String>,
6773
power: f32,
@@ -84,11 +90,26 @@ pub fn projectile(
8490
.insert((
8591
Name::new(projectile.name.replace(' ', "")),
8692
// Appearance:
87-
SpriteBundle {
88-
sprite: Sprite { color, ..default() },
89-
texture: projectile.texture.clone(),
90-
..default()
91-
},
93+
(
94+
SpriteBundle {
95+
sprite: Sprite { color, ..default() },
96+
texture: projectile.texture.clone(),
97+
..default()
98+
},
99+
Animator::new(
100+
Delay::new(Duration::from_secs_f32(
101+
(projectile.lifetime - FADE_SECS).max(0.001),
102+
))
103+
.then(Tween::new(
104+
EaseMethod::Linear,
105+
Duration::from_secs_f32(projectile.lifetime.clamp(0.001, FADE_SECS)),
106+
lens::SpriteColorLens {
107+
start: color,
108+
end: Color::NONE,
109+
},
110+
)),
111+
),
112+
),
92113
// Physics:
93114
(
94115
RigidBody::Kinematic,

0 commit comments

Comments
 (0)
Please sign in to comment.