Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walking and damage visual effects #70

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
188 changes: 166 additions & 22 deletions game/src/combat/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,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>>,
umut-sahin marked this conversation as resolved.
Show resolved Hide resolved
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 +188,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 +273,69 @@ pub fn damage_player_on_contact_started(
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 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 +364,69 @@ pub fn damage_enemies_on_contact(
damage,
damage_cooldown,
);

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

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
enemy_position.y + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
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 +452,22 @@ pub fn damage_enemies_on_contact_started(
damage,
damage_cooldown,
);

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

blood_spawner.reset();

pop_effect_transform.translation = Vec3::new(
enemy_position.x + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
enemy_position.y + rng.gen_range(-POP_EFFECT_SPREAD..POP_EFFECT_SPREAD),
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
Loading