Skip to content

Commit

Permalink
speargun
Browse files Browse the repository at this point in the history
  • Loading branch information
stillonearth committed May 11, 2024
1 parent 704da58 commit 87d5f99
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn setup_gameplay_music(mut commands: Commands, audio_assets: Res<AudioAssets>,
let handle = audio
.play(audio_assets.mexico.clone())
.looped()
.with_volume(0.1)
.with_volume(0.001)
.handle();
commands.insert_resource(GameplayMusic(handle));
}
Expand Down
16 changes: 11 additions & 5 deletions src/entities/characters/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ pub fn create_enemy_bundle(
// ------

#[derive(Event, Clone)]
pub struct EnemyHitEvent(pub Entity);
pub struct EnemyHitEvent {
pub entity: Entity,
pub damage: u8,
}

#[derive(Event, Clone)]
pub struct SpawnEnemyEvent {
Expand Down Expand Up @@ -278,23 +281,23 @@ pub fn handle_enemy_hit(
let mut hit_sound_played = false;

for event in ev_enemy_hit.read() {
if commands.get_entity(event.0).is_none() {
if commands.get_entity(event.entity).is_none() {
continue;
}

for (player_transform, _) in q_player.iter() {
let player_position = player_transform.translation;

let (enemy_entity, mierda_transform, mut enemy_velocity, mut enemy) =
enemies.get_mut(event.0).unwrap();
enemies.get_mut(event.entity).unwrap();
let enemy_position = mierda_transform.translation;
let vector_attack = (enemy_position - player_position).normalize();
enemy_velocity.linvel.x += vector_attack.x * 200.;
enemy_velocity.linvel.y += vector_attack.y * 200.;

let damage = match enemy.enemy_type {
EnemyType::Mierda => 100,
EnemyType::Pendejo => 50,
EnemyType::Mierda => (1.0 * event.damage as f32) as u8,
EnemyType::Pendejo => (0.5 * event.damage as f32) as u8,
};

let timer = Timer::new(std::time::Duration::from_millis(200), TimerMode::Once);
Expand Down Expand Up @@ -344,6 +347,9 @@ pub fn despawn_dead_enemies(
state.asyn().timeout(0.3)
}))
.then(asyn!(state, mut commands: Commands => {
if commands.get_entity(state.value).is_none() {
return;
}
commands.entity(state.value).despawn_recursive();
}));
}
Expand Down
31 changes: 2 additions & 29 deletions src/entities/characters/mierda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ use bevy_rapier2d::prelude::Velocity;
use bevy_rapier2d::prelude::*;

use crate::{
entities::player::Player,
physics::ColliderBundle,
sprites::{AnimatedCharacterSprite, AnimationTimer, CharacterAnimation},
GameState,
entities::player::Player,
};

use super::{
enemy::{create_enemy_bundle, DirectionUpdateTime, Enemy, EnemyType},
};

use super::enemy::{create_enemy_bundle, DirectionUpdateTime, Enemy, EnemyType};

// -----------
// Compontents
Expand Down Expand Up @@ -123,28 +120,6 @@ pub fn update_mierdas_move_direction(
}
}

// ---------
// Physics
// ---------

pub fn handle_mierda_wall_collisions(
mut collision_events: EventReader<CollisionEvent>,
mut q_los_mierdas: Query<(Entity, &mut Velocity, &Enemy)>,
) {
for event in collision_events.read() {
for (e, mut v, _) in q_los_mierdas
.iter_mut()
.filter(|(_, _, m)| m.enemy_type == EnemyType::Mierda)
{
if let CollisionEvent::Started(e1, e2, _) = event {
if e1.index() == e.index() || e2.index() == e.index() {
v.linvel *= -1.;
}
}
}
}
}

// ---
// Plugin
// --
Expand All @@ -160,8 +135,6 @@ impl Plugin for MierdaPlugin {
// AI
mierda_activity,
update_mierdas_move_direction,
// Physics, Collisions
handle_mierda_wall_collisions,
)
.run_if(in_state(GameState::GamePlay)),
);
Expand Down
3 changes: 2 additions & 1 deletion src/entities/level_objects/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ pub fn setup_light(
commands.spawn((
SkylightLight2D {
color: Color::rgb_u8(249, 143, 33),
intensity: 0.003,
// intensity: 0.003,
intensity: 0.03,
},
Name::new("global_skylight"),
));
Expand Down
45 changes: 28 additions & 17 deletions src/entities/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ pub fn event_player_attack(
continue;
}

ev_enemy_hit.send(EnemyHitEvent(entity));
ev_enemy_hit.send(EnemyHitEvent {
entity,
damage: 100,
});
}
}
}
Expand Down Expand Up @@ -215,30 +218,38 @@ pub fn event_player_hit(
}
}

// ---------
// -------
// Physics
// ---------
// -------

pub fn handle_player_enemy_collisions(
mut collision_events: EventReader<CollisionEvent>,
mut q_player: Query<(Entity, &mut Player)>,
q_enemies: Query<(Entity, &mut Velocity, &Enemy)>,
q_player: Query<(Entity, &mut Player)>,
q_enemies: Query<(Entity, &Enemy)>,
mut ev_player_hit: EventWriter<PlayerHitEvent>,
) {
for event in collision_events.read() {
for (e, _) in q_player.iter_mut() {
if let CollisionEvent::Started(e1, e2, _) = event {
if !(e1.index() == e.index() || e2.index() == e.index()) {
continue;
}

let other_entity = if e1.index() == e.index() { *e2 } else { *e1 };
if q_enemies.get(other_entity).is_err() {
continue;
}

ev_player_hit.send(PlayerHitEvent { entity: e });
if let CollisionEvent::Started(e1, e2, _) = event {
let contact_1_player = q_player.get(*e1);
let contact_2_player = q_player.get(*e2);
let is_contact_player = contact_1_player.is_ok() || contact_2_player.is_ok();

let contact_1_enemy = q_enemies.get(*e1);
let contact_2_enemy = q_enemies.get(*e2);
let is_contact_enemy = contact_1_enemy.is_ok() || contact_2_enemy.is_ok();

if !(is_contact_player && is_contact_enemy) {
continue;
}

let player_entity = match contact_1_player.is_ok() {
true => contact_1_player.unwrap().0,
false => contact_2_player.unwrap().0,
};

ev_player_hit.send(PlayerHitEvent {
entity: player_entity,
});
}
}
}
Expand Down
Loading

0 comments on commit 87d5f99

Please sign in to comment.