Skip to content

Commit

Permalink
Particle system for fire breath
Browse files Browse the repository at this point in the history
  • Loading branch information
cnmorales committed Nov 18, 2023
1 parent bc161c9 commit f9570f1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
64 changes: 57 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ bevy = { version = "0.12.0", default-features = false, features = [
"vorbis",
"x11",
] }

bevy_rapier2d = { version = "0.23.0", features = ["debug-render-2d"] }
bevy_particle_systems = "0.11.0"
noise = "0.8.2"
rand = "0.7.3"

Expand Down
Binary file added assets/textures/fire_breath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub use constants::*;
pub use enemy::Enemy;
pub use hitpoints::Hitpoints;
pub use level::{BorderTile, Tile};
pub use player::Player;
pub use player::{Player, SpawnFireBreathEvent};
pub use plugin::GamePlugin;
48 changes: 48 additions & 0 deletions src/game/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_particle_systems::*;
use bevy_rapier2d::prelude::Collider;

use crate::{
Expand All @@ -12,6 +13,9 @@ pub(super) struct PlayerPlugin;

impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.add_event::<SpawnFireBreathEvent>();
app.add_plugins(ParticleSystemPlugin::default());
app.add_systems(Update, spawn_fire_breath);
app.add_systems(OnEnter(AppState::InGame), spawn_player);
}
}
Expand All @@ -29,6 +33,18 @@ pub struct PlayerBundle {
#[derive(Component)]
pub struct Player;

#[derive(Event)]
pub struct SpawnFireBreathEvent {
damage: i16,
position: Vec2,
}

impl SpawnFireBreathEvent {
pub fn new(damage: i16, position: Vec2) -> Self {
Self { damage, position }
}
}

fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture = asset_server
.get_handle("textures/dragon.png")
Expand All @@ -50,3 +66,35 @@ fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
},
});
}

fn spawn_fire_breath(
mut commands: Commands,
mut spawn_fire_breath_event_reader: EventReader<SpawnFireBreathEvent>,
asset_server: Res<AssetServer>,
) {
for &SpawnFireBreathEvent { damage, position } in spawn_fire_breath_event_reader.read() {
commands
.spawn(ParticleSystemBundle {
transform: Transform::from_translation(position.extend(1.0)),

particle_system: ParticleSystem {
max_particles: 10_000,
texture: ParticleTexture::Sprite(asset_server.load("textures/fire_breath.png")),
spawn_rate_per_second: 10.0.into(),
initial_speed: JitteredValue::jittered(3.0, -1.0..1.0),
lifetime: JitteredValue::jittered(4.0, -1.0..1.0),
/* color: ColorOverTime::Gradient(Gradient::new(vec![
ColorPoint::new(Color::WHITE, 0.0),
ColorPoint::new(Color::rgba(0.0, 0.0, 1.0, 0.0), 1.0),
])), */
looping: false,
despawn_on_finish: true,
system_duration_seconds: 1.0,
..ParticleSystem::default()
},
..ParticleSystemBundle::default()
})
// Add the playing component so it starts playing. This can be added later as well.
.insert(Playing);
}
}
18 changes: 16 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::f32::consts::FRAC_PI_2;

use bevy::{ecs::system::SystemParam, prelude::*, window::PrimaryWindow};

use crate::{game::Player, playing};
use crate::{game::Player, game::SpawnFireBreathEvent, playing};

pub struct InputPlugin;

impl Plugin for InputPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, mouse_input.run_if(playing()));
app.add_systems(PreUpdate, (mouse_input, keyboard_input).run_if(playing()));
}
}

Expand Down Expand Up @@ -64,3 +64,17 @@ fn mouse_input(
}
}
}

fn keyboard_input(
keys: Res<Input<KeyCode>>,
mut spawn_fire_breath_event_writer: EventWriter<SpawnFireBreathEvent>,
query: Query<&Transform, With<Player>>,
) {
if keys.pressed(KeyCode::Key1) {
// Key number 1 is being held down
let player_transform = query.single();
let player_position = player_transform.translation.truncate();

spawn_fire_breath_event_writer.send(SpawnFireBreathEvent::new(1000, player_position));
}
}

0 comments on commit f9570f1

Please sign in to comment.