From ef9d564bd55f62164041397d6abe508694b62992 Mon Sep 17 00:00:00 2001 From: Fabien JUIF Date: Sun, 12 Nov 2023 19:30:23 +0100 Subject: [PATCH] :sparkles: explosion damage --- src/castles.rs | 2 +- src/main.rs | 5 +- src/minions.rs | 72 ++++++++++++++++++------- src/physics.rs | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ src/player.rs | 11 ++-- src/racks.rs | 2 +- 6 files changed, 209 insertions(+), 27 deletions(-) create mode 100644 src/physics.rs diff --git a/src/castles.rs b/src/castles.rs index b3081d4..4e8f9e5 100644 --- a/src/castles.rs +++ b/src/castles.rs @@ -54,7 +54,7 @@ impl CastleBundle { .with_health_bar_size(Vec2::new(size.x, 5.)), rewards: Rewards { gold: 500. }, rigid_body: RigidBody::Dynamic, - collider: Collider::cuboid(size.x / 2., size.y / 2.), + collider: Collider::cuboid((size.x / 2.) * 0.98, (size.y / 2.) * 0.98), events: ActiveEvents::COLLISION_EVENTS, mass: ColliderMassProperties::Mass(0.), } diff --git a/src/main.rs b/src/main.rs index 4be8ceb..6ee0154 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod castles; mod common; mod health; mod minions; +mod physics; mod player; mod racks; mod teams; @@ -15,6 +16,7 @@ use bevy_rapier2d::prelude::*; use castles::CastlesPlugin; use health::HealthPlugin; use minions::MinionsPlugin; +use physics::PhysicsPlugin; use player::{LocalPlayer, LocalPlayerPlugin}; use racks::RacksPlugin; use teams::TeamsPlugin; @@ -28,9 +30,10 @@ fn main() { app.add_plugins(( DefaultPlugins.set(LogPlugin { level: Level::TRACE, - filter: "wgpu=error,bevy_render=warn,bevy_app=warn,bevy_ecs=warn,naga=warn,gilrs=warn" + filter: "wgpu=error,bevy_render=warn,bevy_app=warn,bevy_ecs=warn,naga=warn,gilrs=warn,game::health=info,game::racks=info" .to_string(), }), + PhysicsPlugin, TeamsPlugin, MinionsPlugin, RacksPlugin, diff --git a/src/minions.rs b/src/minions.rs index 7872313..8388265 100644 --- a/src/minions.rs +++ b/src/minions.rs @@ -1,4 +1,4 @@ -use crate::{common::*, health::Health, teams::Team}; +use crate::{common::*, health::Health, physics::CollisionEvent, teams::Team}; use bevy::{ prelude::*, sprite::MaterialMesh2dBundle, @@ -29,7 +29,12 @@ impl Plugin for MinionsPlugin { fn build(&self, app: &mut App) { app.add_systems( Update, - (update_move_minions, check_collisions_minions, decay_life), + ( + update_move_minions, + check_collisions_minions, + decay_life, + explosion_damage, + ), ) .add_systems(PostUpdate, (destroy_minions, destroy_after_timer)); } @@ -47,7 +52,6 @@ pub struct MinionBundle { // physics body: RigidBody, collider: Collider, - events: ActiveEvents, timer_destroyable: TimeDestroyable, } @@ -58,9 +62,10 @@ impl MinionBundle { translation: Vec3, team: Team, ) -> Self { + let radius = 6.0; MinionBundle { mesh: MaterialMesh2dBundle { - mesh: meshes.add(shape::Circle::new(6.).into()).into(), + mesh: meshes.add(shape::Circle::new(radius).into()).into(), material: materials.add(ColorMaterial::from(team.color)), transform: Transform::from_translation(translation), ..default() @@ -85,8 +90,7 @@ impl MinionBundle { team, // physics body: RigidBody::Dynamic, - collider: Collider::ball(6.), - events: ActiveEvents::COLLISION_EVENTS, + collider: Collider::ball(radius * 0.98), timer_destroyable: TimeDestroyable { timer: Timer::from_seconds(DESTROY_MINIONS_AFTER_SECS, bevy::time::TimerMode::Once), }, @@ -95,7 +99,9 @@ impl MinionBundle { } #[derive(Component)] -struct Explosion; +struct Explosion { + pub damage: f32, +} #[derive(Bundle)] struct ExplosionBundle { @@ -111,23 +117,26 @@ impl ExplosionBundle { pub fn new( meshes: &mut ResMut>, materials: &mut ResMut>, - translation: Vec3, + mut translation: Vec3, team: Team, ) -> Self { + let mut color = team.color; + color.set_a(0.4); let radius = 20.; + translation.z = 10.0; ExplosionBundle { mesh: MaterialMesh2dBundle { mesh: meshes.add(shape::Circle::new(radius).into()).into(), - material: materials.add(ColorMaterial::from(team.color)), + material: materials.add(ColorMaterial::from(color)), transform: Transform::from_translation(translation), ..default() }, - explosion: Explosion, + explosion: Explosion { damage: 30.0 }, team, - collider: Collider::ball(radius), + collider: Collider::ball(radius * 0.98), sensor: Sensor, timer_destroyable: TimeDestroyable { - timer: Timer::from_seconds(0.5, bevy::time::TimerMode::Once), + timer: Timer::from_seconds(0.2, bevy::time::TimerMode::Once), }, } } @@ -225,10 +234,6 @@ fn destroy_after_timer( } } -// maybe this is a bad idea to have a system per component since the collision event is having all contacts -// it makes us loop inside collision events multiple time -// TODO: We can have a more global system to handle that in one loop querying "atker" component (with a team), -// TODO: and putting reward in the team rather than in the player, or we want to stick of having reward on each entity so when a entity die its reward are huge? // FIXME: With explosion implem, it can be simplify fn check_collisions_minions( mut commands: Commands, @@ -241,7 +246,7 @@ fn check_collisions_minions( ) { for collision_event in collision_events.iter() { match collision_event { - CollisionEvent::Started(e1, e2, _) => { + CollisionEvent::Started(e1, e2) => { // between minions if query_minions.contains(*e1) && query_minions.contains(*e2) { let [(transform_a, team_a, mut minion_a), (_, team_b, _)] = @@ -306,7 +311,7 @@ fn check_collisions_minions( minion.had_exploded = true; } - CollisionEvent::Stopped(_, _, _) => {} + CollisionEvent::Stopped(_, _) => {} } } } @@ -316,3 +321,34 @@ fn decay_life(time: Res