Skip to content

Commit

Permalink
feat: walking and damage visual effects
Browse files Browse the repository at this point in the history
  • Loading branch information
distortedfuzz committed Jul 2, 2024
1 parent 3dc98d7 commit 641f02b
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 29 deletions.
1 change: 1 addition & 0 deletions game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bevy_console = { version = "0.11" }
bevy_easings = { version = "0.13" }
bevy_editor_pls = { version = "0.8", optional = true }
bevy_fluent = { git = "https://github.com/umut-sahin/bevy_fluent" }
bevy_hanabi = { version = "0.11" }
bevy_prng = { version = "0.6", features = ["rand_chacha"] }
bevy_rand = { version = "0.6" }
bevy_xpbd_2d = { version = "0.4" }
Expand Down
193 changes: 170 additions & 23 deletions game/src/combat/systems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::prelude::*;
use crate::{
effect::blood::BLOOD_EFFECT_SPREAD,
prelude::*,
};


/// Starts attack animations.
Expand Down Expand Up @@ -123,17 +126,35 @@ pub fn apply_damage(
pub fn damage_player_on_contact(
mut commands: Commands,
name_query: Query<&Name>,
mut player_query: Query<(&Name, &DodgeChance, &mut RemainingHealth), With<Player>>,
mut player_query: Query<(&Name, &Position, &DodgeChance, &mut RemainingHealth), With<Player>>,
player_hit_box_query: Query<&Parent, With<PlayerHitBox>>,
player_damage_query: Query<
(Entity, &Name, Option<&Originator>, &Damage, Option<&DamageCooldown>),
(With<Attack>, With<DamagePlayerOnContact>, Without<Cooldown<Damage>>),
>,
mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>,
mut collision_event_reader: EventReader<Collision>,
mut blood_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<BloodParticles>, Without<PopParticles>),
>,
mut pop_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<PopParticles>, Without<BloodParticles>),
>,
) {
let (mut blood_spawner, mut blood_effect_transform) = match blood_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

let (mut pop_spawner, mut pop_effect_transform) = match pop_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

for Collision(contacts) in collision_event_reader.read().cloned() {
let (player_name, player_dodge_chance, mut player_remaining_health) =
let (player_name, player_position, player_dodge_chance, mut player_remaining_health) =
match player_hit_box_query
.get(contacts.entity1)
.or_else(|_| player_hit_box_query.get(contacts.entity2))
Expand Down Expand Up @@ -170,24 +191,58 @@ pub fn damage_player_on_contact(
damage,
damage_cooldown,
);

blood_effect_transform.translation = Vec3::new(
player_position.x + rng.gen_range(-BLOOD_EFFECT_SPREAD..BLOOD_EFFECT_SPREAD),
player_position.y + rng.gen_range(-BLOOD_EFFECT_SPREAD..BLOOD_EFFECT_SPREAD),
0.0,
);

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
player_position.x + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
player_position.y + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
0.0,
);

pop_spawner.reset();
}
}

/// Damages the player on contact.
pub fn damage_player_on_contact_started(
mut commands: Commands,
name_query: Query<&Name>,
mut player_query: Query<(&Name, &DodgeChance, &mut RemainingHealth), With<Player>>,
mut player_query: Query<(&Name, &Position, &DodgeChance, &mut RemainingHealth), With<Player>>,
player_hit_box_query: Query<&Parent, With<PlayerHitBox>>,
player_damage_query: Query<
(Entity, &Name, Option<&Originator>, &Damage, Option<&DamageCooldown>),
(With<Attack>, With<DamagePlayerOnContactStarted>, Without<Cooldown<Damage>>),
>,
mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>,
mut collision_started_event_reader: EventReader<CollisionStarted>,
mut blood_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<BloodParticles>, Without<PopParticles>),
>,
mut pop_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<PopParticles>, Without<BloodParticles>),
>,
) {
let (mut blood_spawner, mut blood_effect_transform) = match blood_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

let (mut pop_spawner, mut pop_effect_transform) = match pop_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

for CollisionStarted(entity1, entity2) in collision_started_event_reader.read().cloned() {
let (player_name, player_dodge_chance, mut player_remaining_health) =
let (player_name, player_position, player_dodge_chance, mut player_remaining_health) =
match player_hit_box_query
.get(entity1)
.or_else(|_| player_hit_box_query.get(entity2))
Expand Down Expand Up @@ -221,31 +276,69 @@ pub fn damage_player_on_contact_started(
damage,
damage_cooldown,
);

blood_effect_transform.translation = Vec3::new(
player_position.x + rng.gen_range(-7.0..7.0),
player_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
player_position.x + rng.gen_range(-7.0..7.0),
player_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

pop_spawner.reset();
}
}

/// Damages the enemies on contact.
pub fn damage_enemies_on_contact(
mut commands: Commands,
name_query: Query<&Name>,
mut enemy_query: Query<(&Name, Option<&DodgeChance>, &mut RemainingHealth), With<Enemy>>,
mut enemy_query: Query<
(&Name, &Position, Option<&DodgeChance>, &mut RemainingHealth),
With<Enemy>,
>,
enemy_hit_box_query: Query<&Parent, With<EnemyHitBox>>,
enemy_damage_query: Query<
(Entity, &Name, Option<&Originator>, &Damage, Option<&DamageCooldown>),
(With<Attack>, With<DamageEnemiesOnContact>, Without<Cooldown<Damage>>),
>,
mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>,
mut collision_event_reader: EventReader<Collision>,
mut blood_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<BloodParticles>, Without<PopParticles>),
>,
mut pop_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<PopParticles>, Without<BloodParticles>),
>,
) {
let (mut blood_spawner, mut blood_effect_transform) = match blood_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

let (mut pop_spawner, mut pop_effect_transform) = match pop_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

for Collision(contacts) in collision_event_reader.read().cloned() {
let (enemy_name, enemy_dodge_chance, mut enemy_remaining_health) = match enemy_hit_box_query
.get(contacts.entity1)
.or_else(|_| enemy_hit_box_query.get(contacts.entity2))
.and_then(|parent| enemy_query.get_mut(parent.get()))
{
Ok(query_result) => query_result,
Err(_) => continue,
};
let (enemy_name, enemy_position, enemy_dodge_chance, mut enemy_remaining_health) =
match enemy_hit_box_query
.get(contacts.entity1)
.or_else(|_| enemy_hit_box_query.get(contacts.entity2))
.and_then(|parent| enemy_query.get_mut(parent.get()))
{
Ok(query_result) => query_result,
Err(_) => continue,
};

let (
damaging_entity,
Expand Down Expand Up @@ -274,31 +367,69 @@ pub fn damage_enemies_on_contact(
damage,
damage_cooldown,
);

blood_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-7.0..7.0),
enemy_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-7.0..7.0),
enemy_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

pop_spawner.reset();
}
}

/// Damages the enemies on contact started.
pub fn damage_enemies_on_contact_started(
mut commands: Commands,
name_query: Query<&Name>,
mut enemy_query: Query<(&Name, Option<&DodgeChance>, &mut RemainingHealth), With<Enemy>>,
mut enemy_query: Query<
(&Name, &Position, Option<&DodgeChance>, &mut RemainingHealth),
With<Enemy>,
>,
enemy_hit_box_query: Query<&Parent, With<EnemyHitBox>>,
enemy_damage_query: Query<
(Entity, &Name, Option<&Originator>, &Damage, Option<&DamageCooldown>),
(With<Attack>, With<DamageEnemiesOnContactStarted>, Without<Cooldown<Damage>>),
>,
mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>,
mut collision_started_event_reader: EventReader<CollisionStarted>,
mut blood_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<BloodParticles>, Without<PopParticles>),
>,
mut pop_query: Query<
(&mut EffectSpawner, &mut Transform),
(With<PopParticles>, Without<BloodParticles>),
>,
) {
let (mut blood_spawner, mut blood_effect_transform) = match blood_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

let (mut pop_spawner, mut pop_effect_transform) = match pop_query.get_single_mut() {
Ok(query_result) => query_result,
Err(_) => return,
};

for CollisionStarted(entity1, entity2) in collision_started_event_reader.read().cloned() {
let (enemy_name, enemy_dodge_chance, mut enemy_remaining_health) = match enemy_hit_box_query
.get(entity1)
.or_else(|_| enemy_hit_box_query.get(entity2))
.and_then(|parent| enemy_query.get_mut(parent.get()))
{
Ok(query_result) => query_result,
Err(_) => continue,
};
let (enemy_name, enemy_position, enemy_dodge_chance, mut enemy_remaining_health) =
match enemy_hit_box_query
.get(entity1)
.or_else(|_| enemy_hit_box_query.get(entity2))
.and_then(|parent| enemy_query.get_mut(parent.get()))
{
Ok(query_result) => query_result,
Err(_) => continue,
};

let (
damaging_entity,
Expand All @@ -324,6 +455,22 @@ pub fn damage_enemies_on_contact_started(
damage,
damage_cooldown,
);

blood_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-7.0..7.0),
enemy_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-7.0..7.0),
enemy_position.y + rng.gen_range(-7.0..7.0),
0.0,
);

pop_spawner.reset();
}
}

Expand Down
1 change: 1 addition & 0 deletions game/src/core/depths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum Depth {
Enemy,
Player,
Item,
Effect,
Projectile,

Top,
Expand Down
1 change: 1 addition & 0 deletions game/src/core/sets/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum InitializationSystems {
Hud,
GameMode,
Player,
Effect,
Market,
Leveling,
Inventory,
Expand Down
1 change: 1 addition & 0 deletions game/src/core/sets/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum RestartSystems {
Inventory,
Map,
Player,
Effect,
Leveling,
Combat,
Last,
Expand Down
Loading

0 comments on commit 641f02b

Please sign in to comment.