Skip to content

Commit

Permalink
damage indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
stillonearth committed Jan 17, 2024
1 parent 81f77ba commit 50261a5
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 107 deletions.
111 changes: 70 additions & 41 deletions src/entities/mierda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::Rng;

use crate::{loading::load_texture_atlas, physics::ColliderBundle, sprites::*, utils::*};

use super::player::Player;
use super::{player::Player, text_indicator::SpawnTextIndicatorEvent};

// -----------
// Compontents
Expand All @@ -28,6 +28,7 @@ pub struct Mierda {
pub health: u8,
pub hit_at: Option<Timer>,
pub is_dummy: bool,
pub marked_for_despawn: bool,
}

#[derive(Clone, Default, Bundle)]
Expand Down Expand Up @@ -93,14 +94,15 @@ pub fn create_mierda_bundle(
};

let mierda = Mierda {
health: 100,
health: 10,
move_direction: Vec2 {
x: rand::random::<f32>() * 2.0 - 1.0,
y: rand::random::<f32>() * 2.0 - 1.0,
}
.normalize(),
hit_at: None,
is_dummy,
marked_for_despawn: false,
};

MierdaBundle {
Expand Down Expand Up @@ -275,9 +277,10 @@ pub fn handle_spawn_mierda(
let new_entity = new_entity.unwrap();
commands.entity(new_entity).insert(Mierda {
is_dummy: false,
health: 100,
health: 30,
move_direction: Vec2::ZERO,
hit_at: None,
marked_for_despawn: false,
});

commands.add(CloneEntity {
Expand All @@ -295,10 +298,11 @@ pub fn handle_spawn_mierda(

pub fn handle_mierda_hit(
mut commands: Commands,
asset_server: Res<AssetServer>,
q_player: Query<(&Transform, &Player)>,
mut los_mierdas: Query<(Entity, &Transform, &mut Velocity, &mut Mierda)>,
mut ev_mierda_hit: EventReader<MierdaHitEvent>,
// mut ev_mierda_spawn: EventWriter<SpawnMierdaEvent>,
mut ev_spawn_text_indicator: EventWriter<SpawnTextIndicatorEvent>, // mut ev_mierda_spawn: EventWriter<SpawnMierdaEvent>,
) {
for event in ev_mierda_hit.iter() {
for (player_transform, _) in q_player.iter() {
Expand All @@ -311,51 +315,74 @@ pub fn handle_mierda_hit(
mierda_velocity.linvel.x += vector_attack.x * 200.;
mierda_velocity.linvel.y += vector_attack.y * 200.;

let distance = mierda_position.distance(player_position).abs();
let damage = distance as u8;

let timer = Timer::new(std::time::Duration::from_millis(200), TimerMode::Once);
mierda.hit_at = Some(timer.clone());
mierda.health -= u8::min(damage, mierda.health);

commands.entity(mierda_entity).insert(FlashingTimer {
timer: timer.clone(),
});

// despawn mierda async
commands
.promise(|| (mierda_entity))
.then(asyn!(state => {
state.asyn().timeout(0.3)
}))
.then(
asyn!(state, mut commands: Commands, asset_server: Res<AssetServer>, q_mierdas: Query<(Entity, &GlobalTransform)> => {
let mierda_transform = *q_mierdas.get(state.value).unwrap().1;
commands.spawn((
ParticleSystemBundle {
transform: (mierda_transform).into(),
particle_system: ParticleSystem {
spawn_rate_per_second: 0.0.into(),
texture: ParticleTexture::Sprite(asset_server.load("px.png")),
max_particles: 1_00,
initial_speed: (0.0..10.0).into(),
scale: 1.0.into(),
velocity_modifiers: vec![
VelocityModifier::Drag(0.001.into()),
VelocityModifier::Vector(Vec3::new(0.0, -100.0, 0.0).into()),
],
color: (Color::BLUE..Color::AQUAMARINE).into(),
bursts: vec![ParticleBurst {
time: 0.0,
count: 20,
}],
looping: false,
..ParticleSystem::default()
},
..default()
},
Playing,
));
commands.entity(state.value).despawn_recursive();
}),
);
ev_spawn_text_indicator.send(SpawnTextIndicatorEvent {
text: format!("-{}", damage),
entity: mierda_entity,
});

commands.spawn((
ParticleSystemBundle {
transform: *mierda_transform,
particle_system: ParticleSystem {
spawn_rate_per_second: 0.0.into(),
texture: ParticleTexture::Sprite(asset_server.load("px.png")),
max_particles: 1_00,
initial_speed: (0.0..10.0).into(),
scale: 1.0.into(),
velocity_modifiers: vec![
VelocityModifier::Drag(0.001.into()),
VelocityModifier::Vector(Vec3::new(0.0, -100.0, 0.0).into()),
],
color: (Color::BLUE..Color::AQUAMARINE).into(),
bursts: vec![ParticleBurst {
time: 0.0,
count: 20,
}],
looping: false,
..ParticleSystem::default()
},
..default()
},
Playing,
));
}
}
}

pub fn despawn_dead_mierdas(
mut commands: Commands,
mut los_mierdas: Query<(Entity, &Transform, &mut Velocity, &mut Mierda)>,
) {
for (e, _, _, mut m) in los_mierdas.iter_mut() {
if m.health != 0 {
continue;
}

if m.marked_for_despawn {
continue;
}

m.marked_for_despawn = true;

commands
.promise(|| (e))
.then(asyn!(state => {
state.asyn().timeout(0.3)
}))
.then(asyn!(state, mut commands: Commands => {
commands.entity(state.value).despawn_recursive();
}));
}
}

Expand Down Expand Up @@ -383,6 +410,8 @@ impl Plugin for EnemyPlugin {
// Events
handle_mierda_hit,
handle_spawn_mierda,
// Rest
despawn_dead_mierdas,
),
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod mierda;
pub mod pendejo;
pub mod pizza;
pub mod player;
pub mod text_indicator;

pub struct EntitiesPlugin;

Expand All @@ -25,6 +26,7 @@ impl Plugin for EntitiesPlugin {
player::PlayerPlugin,
pendejo::PendejoPlugin,
biboran::BiboranPlugin,
text_indicator::TextIndicatorPlugin,
));
}
}
130 changes: 130 additions & 0 deletions src/entities/text_indicator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use bevy::{prelude::*};



use crate::{loading::FontAssets};

// -----------
// Compontents
// -----------

#[derive(Clone, Eq, PartialEq, Debug, Default, Component, Reflect)]
pub struct TextIndicator {
pub timer: Timer,
}

// --------
// Systems
// --------

// pub fn spawn_text_indicator(
// mut commands: Commands,
// q_player: Query<(Entity, &Player)>,
// q_text_indicator: Query<(Entity, &mut TextIndicator)>,
// font_assets: Res<FontAssets>,
// ) {
// if q_text_indicator.iter().count() > 0 {
// return;
// }

// for (entity, player) in q_player.iter() {
// let timer = Timer::from_seconds(2.0, TimerMode::Once);

// let text_indicator = TextIndicator { timer };

// println!("text indicator spawned");

// commands.entity(entity).with_children(|parent| {
// let text_style = TextStyle {
// font: font_assets.pixeloid_mono.clone(),
// font_size: 10.0,
// color: Color::WHITE,
// };
// let text_alignment = TextAlignment::Center;

// parent.spawn((
// Text2dBundle {
// text: Text::from_section("rotation", text_style.clone())
// .with_alignment(text_alignment),
// transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
// ..default()
// },
// text_indicator,
// ));
// });
// }
// }

pub fn update_text_indicator(
mut commands: Commands,
time: Res<Time>,
mut query: Query<(Entity, &mut Transform, &mut TextIndicator)>,
) {
for (entity, mut transform, mut text_indicator) in query.iter_mut() {
text_indicator.timer.tick(time.delta());

transform.translation.y += 50.0 * f32::sin(time.delta().as_secs_f32());

if text_indicator.timer.just_finished() {
commands.entity(entity).despawn_recursive();
}
}
}

// ------
// Events
// ------

#[derive(Event, Clone)]
pub struct SpawnTextIndicatorEvent {
pub text: String,
pub entity: Entity,
}

// --------------
// Event Handlers
// --------------

pub fn event_spawn_text_indicator(
mut commands: Commands,
mut ev_spawn_text_indicator: EventReader<SpawnTextIndicatorEvent>,
font_assets: Res<FontAssets>,
) {
for ev in ev_spawn_text_indicator.iter() {
let timer = Timer::from_seconds(2.0, TimerMode::Once);

let text_indicator = TextIndicator { timer };

commands.entity(ev.entity).with_children(|parent| {
let text_style = TextStyle {
font: font_assets.pixeloid_mono.clone(),
font_size: 5.0,
color: Color::WHITE,
};
let text_alignment = TextAlignment::Center;

parent.spawn((
Text2dBundle {
text: Text::from_section(ev.text.clone(), text_style.clone())
.with_alignment(text_alignment),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..default()
},
text_indicator,
));
});
}
}

// ------
// Plugin
// ------

pub struct TextIndicatorPlugin;

impl Plugin for TextIndicatorPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (update_text_indicator, event_spawn_text_indicator))
.add_event::<SpawnTextIndicatorEvent>();
}
}
62 changes: 31 additions & 31 deletions src/gameplay/waves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,39 @@ pub fn ui_wave_info_text(
pub fn get_level_1_waves() -> Vec<Wave> {
vec![
Wave {
events: vec![WaveEntry::Mierda { count: 100 }],
events: vec![WaveEntry::Mierda { count: 10 }],
event_duration: Duration::from_secs(10),
wave_duration: Duration::from_secs(30),
},
Wave {
events: vec![
WaveEntry::Mierda { count: 100 },
WaveEntry::Pizza { count: 3 },
WaveEntry::Mierda { count: 100 },
WaveEntry::Biboran { count: 1 },
WaveEntry::Mierda { count: 100 },
],
event_duration: Duration::from_secs(20),
wave_duration: Duration::from_secs(80),
},
Wave {
events: vec![
WaveEntry::Mierda { count: 200 },
WaveEntry::Pizza { count: 3 },
WaveEntry::Pendejo { count: 200 },
WaveEntry::Mierda { count: 200 },
WaveEntry::Pizza { count: 3 },
WaveEntry::Biboran { count: 1 },
WaveEntry::Pendejo { count: 200 },
WaveEntry::Mierda { count: 200 },
WaveEntry::Pizza { count: 3 },
WaveEntry::Biboran { count: 1 },
WaveEntry::Pendejo { count: 200 },
WaveEntry::Biboran { count: 1 },
WaveEntry::Pizza { count: 3 },
],
event_duration: Duration::from_secs(20),
wave_duration: Duration::from_secs(260),
},
// Wave {
// events: vec![
// WaveEntry::Mierda { count: 100 },
// WaveEntry::Pizza { count: 3 },
// WaveEntry::Mierda { count: 100 },
// WaveEntry::Biboran { count: 1 },
// WaveEntry::Mierda { count: 100 },
// ],
// event_duration: Duration::from_secs(20),
// wave_duration: Duration::from_secs(80),
// },
// Wave {
// events: vec![
// WaveEntry::Mierda { count: 200 },
// WaveEntry::Pizza { count: 3 },
// WaveEntry::Pendejo { count: 200 },
// WaveEntry::Mierda { count: 200 },
// WaveEntry::Pizza { count: 3 },
// WaveEntry::Biboran { count: 1 },
// WaveEntry::Pendejo { count: 200 },
// WaveEntry::Mierda { count: 200 },
// WaveEntry::Pizza { count: 3 },
// WaveEntry::Biboran { count: 1 },
// WaveEntry::Pendejo { count: 200 },
// WaveEntry::Biboran { count: 1 },
// WaveEntry::Pizza { count: 3 },
// ],
// event_duration: Duration::from_secs(20),
// wave_duration: Duration::from_secs(260),
// },
]
}
Loading

0 comments on commit 50261a5

Please sign in to comment.