Skip to content

Commit

Permalink
Transition from hanabi particles to shader vfx
Browse files Browse the repository at this point in the history
  • Loading branch information
haihala committed Jul 28, 2024
1 parent c2da6e5 commit 137f71f
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 338 deletions.
74 changes: 0 additions & 74 deletions client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ opt-level = 3

[workspace.dependencies]
bevy = "0.14"
bevy_hanabi = "0.12"
bevy-inspector-egui = "0.25"

rand = "0.8"
Expand Down
1 change: 0 additions & 1 deletion client/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["Eero Häihälä <[email protected]>"]

[dependencies]
bevy = { workspace = true, features = ["mp3"] }
bevy_hanabi = { workspace = true }
bevy-inspector-egui = { workspace = true }
rand = { workspace = true }
strum = { workspace = true }
Expand Down
141 changes: 27 additions & 114 deletions client/lib/src/assets/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use bevy::{prelude::*, utils::HashMap};
use bevy_hanabi::*;

use wag_core::{Animation, Icon, Model, SoundEffect, VisualEffect};

use super::{
animations::animation_paths,
materials::{BlockEffectMaterial, ClashSparkMaterial, HitSparkMaterial},
models::model_paths,
sounds::{get_sound_paths, Sounds},
Animations, AssetsLoading, Fonts, Icons, Models, Particles,
Animations, AssetsLoading, Fonts, Icons, Models, Vfx,
};

pub fn fonts(
Expand Down Expand Up @@ -101,116 +101,29 @@ pub fn sounds(
);
}

// Typed like this so it can be ignored in unit tests
pub fn particles(mut commands: Commands, effects: Option<ResMut<Assets<EffectAsset>>>) {
if let Some(mut effects) = effects {
let handles = vec![
(
VisualEffect::Block,
block_entity(&mut commands, &mut effects),
),
(VisualEffect::Hit, hit_entity(&mut commands, &mut effects)),
(
VisualEffect::Clash,
clash_entity(&mut commands, &mut effects),
),
]
.into_iter()
.collect();

commands.insert_resource(Particles::new(handles));
};
}

fn block_entity(commands: &mut Commands, effects: &mut Assets<EffectAsset>) -> Entity {
particle_explosion(
commands,
effects,
"block",
Gradient::constant(Vec4::new(0.1, 0.2, 1.0, 1.0)),
vanishing_size_gradient(Vec2::new(0.1, 0.1), 0.1),
2.0,
50.0,
)
}

fn hit_entity(commands: &mut Commands, effects: &mut Assets<EffectAsset>) -> Entity {
particle_explosion(
commands,
effects,
"hit",
Gradient::constant(Vec4::new(1.0, 0.7, 0.0, 1.0)),
vanishing_size_gradient(Vec2::new(0.1, 0.1), 0.2),
3.0,
100.0,
)
}

fn clash_entity(commands: &mut Commands, effects: &mut Assets<EffectAsset>) -> Entity {
particle_explosion(
commands,
effects,
"clash",
Gradient::constant(Vec4::new(0.2, 0.3, 0.5, 1.0)),
vanishing_size_gradient(Vec2::new(0.1, 0.1), 0.1),
8.0,
30.0,
)
}

fn vanishing_size_gradient(start: Vec2, duration: f32) -> Gradient<Vec2> {
let mut size_gradient = Gradient::new();
size_gradient.add_key(0.0, start);
size_gradient.add_key(duration, Vec2::splat(0.0));
size_gradient
}

fn particle_explosion(
commands: &mut Commands,
effects: &mut Assets<EffectAsset>,
name: &'static str,
color_gradient: Gradient<Vec4>,
size_gradient: Gradient<Vec2>,
speed: f32,
amount: f32,
) -> Entity {
let spawner = Spawner::once(amount.into(), false);
let mut module = Module::default();

let position_modifier = SetPositionSphereModifier {
dimension: ShapeDimension::Surface,
radius: module.lit(0.2),
center: module.lit(Vec3::ZERO),
};

let velocity_modifier = SetVelocitySphereModifier {
center: module.lit(Vec3::ZERO),
speed: module.lit(speed),
};

let lifetime = SetAttributeModifier::new(Attribute::LIFETIME, module.lit(1.0));

let gravity = AccelModifier::new(module.lit(Vec3::new(0.0, -2.0, 0.0)));

let effect = effects.add(
EffectAsset::new(vec![1000], spawner, module)
.init(position_modifier)
.init(velocity_modifier)
.init(lifetime)
.update(gravity)
.render(ColorOverLifetimeModifier {
gradient: color_gradient,
})
.render(SizeOverLifetimeModifier {
gradient: size_gradient,
..default()
}),
);

commands
.spawn((
ParticleEffectBundle::new(effect),
Name::new(format!("Particle system '{name}'")),
))
.id()
pub fn vfx(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut clash_spark_materials: ResMut<Assets<ClashSparkMaterial>>,
mut block_effect_materials: ResMut<Assets<BlockEffectMaterial>>,
mut hit_spark_materials: ResMut<Assets<HitSparkMaterial>>,
) {
let mesh_handles = vec![
(VisualEffect::Block, meshes.add(Rectangle::new(1.1, 2.0))),
(VisualEffect::Hit, meshes.add(Rectangle::new(1.5, 1.5))),
(VisualEffect::Clash, meshes.add(Rectangle::new(1.5, 1.5))),
]
.into_iter()
.collect();

let clash_spark_material = clash_spark_materials.add(ClashSparkMaterial::default());
let block_effect_material = block_effect_materials.add(BlockEffectMaterial::default());
let hit_spark_material = hit_spark_materials.add(HitSparkMaterial::default());

commands.insert_resource(Vfx::new(
mesh_handles,
clash_spark_material,
block_effect_material,
hit_spark_material,
));
}
Loading

0 comments on commit 137f71f

Please sign in to comment.