Compare commits

..

2 commits

Author SHA1 Message Date
f35f7abce7 integrated particle system into player positions 2022-08-24 14:35:05 +08:00
f0e5f2f360 removed hannabi 2022-08-24 13:52:14 +08:00
8 changed files with 117 additions and 123 deletions

29
Cargo.lock generated
View file

@ -469,23 +469,6 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "bevy_hanabi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "371c5bd9edcf42721cd4e769354c221f3c4090e7a1dba0eb0bd6ca29e7828142"
dependencies = [
"anyhow",
"bevy",
"bitflags",
"bytemuck",
"copyless",
"rand",
"rand_pcg",
"ron",
"serde",
]
[[package]] [[package]]
name = "bevy_hierarchy" name = "bevy_hierarchy"
version = "0.8.0" version = "0.8.0"
@ -915,11 +898,12 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy-inspector-egui", "bevy-inspector-egui",
"bevy_hanabi",
"bevy_rapier2d", "bevy_rapier2d",
"cpal 0.14.0", "cpal 0.14.0",
"crossbeam-channel", "crossbeam-channel",
"hexodsp", "hexodsp",
"rand",
"rand_distr",
"ticktock", "ticktock",
] ]
@ -3131,12 +3115,13 @@ dependencies = [
] ]
[[package]] [[package]]
name = "rand_pcg" name = "rand_distr"
version = "0.3.1" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" checksum = "32cb0b9bc82b0a0876c2dd994a7e7a2683d3e7390ca40e6886785ef0c7e3ee31"
dependencies = [ dependencies = [
"rand_core", "num-traits",
"rand",
] ]
[[package]] [[package]]

View file

@ -7,13 +7,15 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.8.0" bevy = "0.8.0"
bevy_hanabi = "0.3.1" # bevy_hanabi = "0.3.1"
bevy-inspector-egui = "0.12.1" bevy-inspector-egui = "0.12.1"
bevy_rapier2d = "0.16.2" bevy_rapier2d = "0.16.2"
cpal = "0.14.0" cpal = "0.14.0"
crossbeam-channel = "0.5.6" crossbeam-channel = "0.5.6"
hexodsp = { git = "https://github.com/WeirdConstructor/HexoDSP" } hexodsp = { git = "https://github.com/WeirdConstructor/HexoDSP" }
ticktock = "0.8.0" ticktock = "0.8.0"
rand = "0.8.5"
rand_distr = "0.4.3"
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 3 opt-level = 3

View file

@ -10,7 +10,6 @@ use bevy::{
prelude::{shape::Quad, *}, prelude::{shape::Quad, *},
sprite::Mesh2dHandle, sprite::Mesh2dHandle,
}; };
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
use std::collections::BTreeSet; use std::collections::BTreeSet;
@ -32,7 +31,8 @@ impl Plugin for GamePlugin {
.add_system_set( .add_system_set(
SystemSet::on_update(AppState::Game) SystemSet::on_update(AppState::Game)
.with_system(crate::levels::post_setup_level) .with_system(crate::levels::post_setup_level)
.with_system(keyboard_input_system), .with_system(keyboard_input_system)
.with_system(character_particle_effect_system),
) )
.add_system_set(SystemSet::on_update(AppState::Win).with_system(keyboard_input_system)) .add_system_set(SystemSet::on_update(AppState::Win).with_system(keyboard_input_system))
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system); .add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
@ -101,7 +101,6 @@ fn setup(
pub fn spawn_character( pub fn spawn_character(
commands: &mut Commands, commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>, character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>, materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>, selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>, character_id_list: &mut Mut<CharacterIdList>,
@ -118,22 +117,6 @@ pub fn spawn_character(
); );
character_id_list.0.insert(character_id); character_id_list.0.insert(character_id);
let mut gradient = Gradient::new();
gradient.add_key(
0.0,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
);
gradient.add_key(
0.2,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 1.)),
);
gradient.add_key(
1.0,
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
);
commands commands
.spawn_bundle(ColorMesh2dBundle { .spawn_bundle(ColorMesh2dBundle {
mesh: character_meshes.square.clone(), mesh: character_meshes.square.clone(),
@ -156,35 +139,7 @@ pub fn spawn_character(
angular_damping: 0.5, angular_damping: 0.5,
}) })
.insert(ExternalImpulse::default()) .insert(ExternalImpulse::default())
.insert(ActiveEvents::COLLISION_EVENTS) .insert(ActiveEvents::COLLISION_EVENTS);
.with_children(|c| {
c.spawn_bundle(ParticleEffectBundle {
effect: ParticleEffect::new(
effects.add(
EffectAsset {
name: "Particles".into(),
capacity: 4096,
spawner: Spawner::rate(30.0.into())
.with_active(selected_character_id.0.is_none()),
..Default::default()
}
.init(PositionCircleModifier {
radius: 30.0,
speed: 20.0.into(),
dimension: ShapeDimension::Surface,
..Default::default()
})
.init(ParticleLifetimeModifier { lifetime: 0.8 })
.render(SizeOverLifetimeModifier {
gradient: Gradient::constant(Vec2::splat(4.0)),
})
.render(ColorOverLifetimeModifier { gradient }),
),
),
transform: Transform::from_xyz(0., 0., 0.1),
..Default::default()
});
});
// If no character is selected, then select this one // If no character is selected, then select this one
if selected_character_id.0.is_none() { if selected_character_id.0.is_none() {
@ -199,7 +154,6 @@ fn collision_event_system(
mut commands: Commands, mut commands: Commands,
character_meshes: Res<CharacterMeshes>, character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
mut effects: ResMut<Assets<EffectAsset>>,
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
character_query: Query<(&CharacterId, &CharacterColor, &Transform)>, character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>, mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
@ -235,7 +189,6 @@ fn collision_event_system(
spawn_character( spawn_character(
&mut commands, &mut commands,
&character_meshes, &character_meshes,
&mut effects,
&mut materials, &mut materials,
&mut selected_character_id, &mut selected_character_id,
&mut character_id_list, &mut character_id_list,
@ -251,27 +204,14 @@ fn collision_event_system(
fn keyboard_input_system( fn keyboard_input_system(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<(&CharacterId, &mut Velocity, &CharacterColor, &Children)>, mut characters: Query<(&CharacterId, &mut Velocity, &CharacterColor)>,
mut level_query: Query<(&mut SelectedCharacterId, &CharacterIdList)>, mut level_query: Query<(&mut SelectedCharacterId, &CharacterIdList)>,
mut effect: Query<&mut ParticleEffect>,
mut app_state: ResMut<State<AppState>>, mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>, audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) { ) {
if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() { if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() {
if keyboard_input.just_pressed(KeyCode::Tab) { if keyboard_input.just_pressed(KeyCode::Tab) {
let selected = if let Some(selected_character_id) = &mut selected_character_id.0 { let selected = if let Some(selected_character_id) = &mut selected_character_id.0 {
if let Some((_character_id, _velocity, _color, children)) = characters
.iter_mut()
.find(|(character_id, _velocity, _color, _children)| {
*character_id == selected_character_id
}) {
effect
.get_mut(children[0])
.unwrap()
.maybe_spawner()
.unwrap()
.set_active(false);
}
*selected_character_id = *character_id_list *selected_character_id = *character_id_list
.0 .0
@ -284,16 +224,10 @@ fn keyboard_input_system(
CharacterId(0) CharacterId(0)
}; };
if let Some((_character_id, _velocity, color, children)) = characters if let Some((_character_id, _velocity, color)) = characters
.iter_mut() .iter_mut()
.find(|(character_id, _velocity, _color, _children)| **character_id == selected) .find(|(character_id, _velocity, _color)| **character_id == selected)
{ {
effect
.get_mut(children[0])
.unwrap()
.maybe_spawner()
.unwrap()
.set_active(true);
audio audio
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()])) .send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
.ok(); .ok();
@ -306,9 +240,9 @@ fn keyboard_input_system(
keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A); keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);
if let Some(selected_character_id) = &selected_character_id.0 { if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, mut velocity, _color, _children)) = characters if let Some((_character_id, mut velocity, _color)) = characters
.iter_mut() .iter_mut()
.find(|(character_id, _velocity, _color, _children)| { .find(|(character_id, _velocity, _color)| {
*character_id == selected_character_id *character_id == selected_character_id
}) { }) {
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32; velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
@ -326,6 +260,25 @@ fn keyboard_input_system(
} }
} }
fn character_particle_effect_system(
mut characters: Query<(&CharacterId, &Transform, &CharacterColor)>,
mut particle_effect: ResMut<crate::particle_effect::ParticleEffectResource>,
mut level_query: Query<(&SelectedCharacterId)>,
) {
if let Ok(selected_character_id) = level_query.get_single_mut() {
if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, transform, color)) = characters
.iter_mut()
.find(|(character_id, _transform, _color)| {
*character_id == selected_character_id
}) {
particle_effect.translation = transform.translation;
particle_effect.color = color.0;
}
}
}
}
fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) { fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.get_handle("UacariLegacy-Thin.ttf"); let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
commands commands

View file

@ -7,7 +7,6 @@ mod level1;
use crate::game::*; use crate::game::*;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_hanabi::*;
use std::collections::BTreeSet; use std::collections::BTreeSet;
pub fn setup_level( pub fn setup_level(
@ -37,7 +36,6 @@ pub fn despawn_level(mut commands: Commands, level_query: Query<Entity, With<Lev
pub fn post_setup_level( pub fn post_setup_level(
mut commands: Commands, mut commands: Commands,
character_meshes: Res<CharacterMeshes>, character_meshes: Res<CharacterMeshes>,
mut effects: ResMut<Assets<EffectAsset>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
current_level: Res<CurrentLevel>, current_level: Res<CurrentLevel>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>, mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
@ -54,7 +52,6 @@ pub fn post_setup_level(
0 => level0::setup( 0 => level0::setup(
&mut commands, &mut commands,
&character_meshes, &character_meshes,
&mut effects,
&mut materials, &mut materials,
&mut selected_character_id, &mut selected_character_id,
&mut character_id_list, &mut character_id_list,
@ -63,7 +60,6 @@ pub fn post_setup_level(
1 => level1::setup( 1 => level1::setup(
&mut commands, &mut commands,
&character_meshes, &character_meshes,
&mut effects,
&mut materials, &mut materials,
&mut selected_character_id, &mut selected_character_id,
&mut character_id_list, &mut character_id_list,

View file

@ -1,13 +1,11 @@
use crate::game::*; use crate::game::*;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
pub fn setup( pub fn setup(
commands: &mut Commands, commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>, character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>, materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>, selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>, character_id_list: &mut Mut<CharacterIdList>,
@ -21,7 +19,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,
@ -32,7 +29,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,
@ -43,7 +39,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,

View file

@ -1,13 +1,11 @@
use crate::game::*; use crate::game::*;
use bevy::prelude::*; use bevy::prelude::*;
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
pub fn setup( pub fn setup(
commands: &mut Commands, commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>, character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>, materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>, selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>, character_id_list: &mut Mut<CharacterIdList>,
@ -27,7 +25,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,
@ -38,7 +35,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,
@ -49,7 +45,6 @@ pub fn setup(
spawn_character( spawn_character(
commands, commands,
character_meshes, character_meshes,
effects,
materials, materials,
selected_character_id, selected_character_id,
character_id_list, character_id_list,

View file

@ -9,7 +9,6 @@ use bevy::{
prelude::*, prelude::*,
render::settings::{WgpuFeatures, WgpuSettings}, render::settings::{WgpuFeatures, WgpuSettings},
}; };
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -33,11 +32,11 @@ fn main() {
.insert_resource(audio_event_sender) .insert_resource(audio_event_sender)
.add_state(AppState::Menu) .add_state(AppState::Menu)
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(HanabiPlugin)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0)) .add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0))
.add_plugin(RapierDebugRenderPlugin::default()) .add_plugin(RapierDebugRenderPlugin::default())
.add_plugin(menu::MenuPlugin) .add_plugin(menu::MenuPlugin)
.add_plugin(game::GamePlugin) .add_plugin(game::GamePlugin)
.add_plugin(particle_effect::ParticleEffectPlugin)
.add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new()) .add_plugin(bevy_inspector_egui::WorldInspectorPlugin::new())
.add_startup_system(setup) .add_startup_system(setup)
.run(); .run();

View file

@ -1,4 +1,13 @@
use bevy::{prelude::*, sprite::Mesh2dHandle}; use bevy::{prelude::*, sprite::Mesh2dHandle};
use rand::Rng;
use rand_distr::{UnitCircle, Distribution};
pub const POOL_COUNT: usize = 100;
pub const MIN_VELOCITY: f32 = 10.0;
pub const MAX_VELOCITY: f32 = 100.0;
pub const VELOCITY_SCALE: f32 = 0.1;
pub const PARTICLE_EFFECT_RADIUS: f32 = 70.0;
pub struct ParticleEffectPlugin; pub struct ParticleEffectPlugin;
@ -6,8 +15,9 @@ impl Plugin for ParticleEffectPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app app
.init_resource::<ParticleMesh>() .init_resource::<ParticleMesh>()
.insert_resource(PoolCount(1000)) .insert_resource(ParticleEffectResource::new(Vec3::new(10000.0, 10000.0, 0.0)))
.add_startup_system(particle_effect_startup); .add_startup_system(particle_effect_startup)
.add_system(particle_effect_system);
} }
} }
@ -18,6 +28,7 @@ pub struct ParticleMesh {
impl FromWorld for ParticleMesh { impl FromWorld for ParticleMesh {
fn from_world(world: &mut World) -> Self { fn from_world(world: &mut World) -> Self {
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap(); let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
Self { Self {
square: meshes square: meshes
.add(Mesh::from(shape::Quad { .add(Mesh::from(shape::Quad {
@ -29,29 +40,87 @@ impl FromWorld for ParticleMesh {
} }
} }
pub struct PoolCount(usize); #[derive(bevy_inspector_egui::Inspectable)]
pub struct ParticleEffectResource {
pub translation: Vec3,
pub prev_translation: Vec3,
pub radius_squared: f32,
pub color: Color,
}
#[derive(Component)] impl ParticleEffectResource {
pub struct ParticleComponent; fn new(init_translation: Vec3) -> Self {
Self {
translation: init_translation,
prev_translation: init_translation,
radius_squared: PARTICLE_EFFECT_RADIUS * PARTICLE_EFFECT_RADIUS,
color: Color::WHITE,
}
}
}
#[derive(Default, Component)]
pub struct ParticleComponent {
pub velocity: Vec3,
}
impl ParticleComponent {
pub fn new() -> Self {
let mut particle_component: Self = Self::default();
particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY);
return particle_component;
}
pub fn randomize_velocity(&mut self, min_velocity: f32, max_velocity: f32) {
let random_direction: [f32; 2] = UnitCircle.sample(&mut rand::thread_rng());
let random_magnitude: f32 = rand::thread_rng().gen_range(min_velocity .. max_velocity);
self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude;
}
}
fn particle_effect_startup( fn particle_effect_startup(
mut commands: Commands, mut commands: Commands,
particle_mesh: Res<ParticleMesh>, particle_mesh: Res<ParticleMesh>,
pool_count: ResMut<PoolCount>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
for _p in 0 .. pool_count.0 { for _p in 0 .. POOL_COUNT {
let color_mesh = ColorMesh2dBundle { let color_mesh = ColorMesh2dBundle {
mesh: particle_mesh.square.clone(), mesh: particle_mesh.square.clone(),
material: materials.add(ColorMaterial::from(Color::WHITE)), material: materials.add(ColorMaterial::from(Color::WHITE)),
// default to invisible
visibility: Visibility {is_visible: false},
..default() ..default()
}; };
commands commands
.spawn_bundle(color_mesh) .spawn_bundle(color_mesh)
.insert(ParticleComponent); .insert(ParticleComponent::new());
} }
}
fn particle_effect_system(
mut materials: ResMut<Assets<ColorMaterial>>,
mut query: Query<(&mut Transform, &mut ParticleComponent, &Handle<ColorMaterial>)>,
mut particle_effect: ResMut<ParticleEffectResource>,
time: Res<Time>,
) {
let delta_seconds: f32 = f32::max(0.001, time.delta_seconds());
let delta_position: Vec3 = particle_effect.translation - particle_effect.prev_translation;
// let overall_velocity: Vec3 = delta_position / delta_seconds;
particle_effect.prev_translation = particle_effect.translation;
for (mut transform, mut particle_component, color_material) in query.iter_mut() {
// particle_component.velocity -= overall_velocity * VELOCITY_SCALE;
transform.translation += particle_component.velocity * delta_seconds + delta_position;
let squared_distance: f32 = transform.translation.distance_squared(particle_effect.translation);
if squared_distance > particle_effect.radius_squared {
transform.translation = particle_effect.translation;
particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY);
}
if let Some(material) = materials.get_mut(color_material) {
material.color = particle_effect.color;
material.color.set_a((particle_effect.radius_squared - squared_distance) / particle_effect.radius_squared);
}
}
} }