-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
Pushback
level, for usage when developing #12
- Loading branch information
Showing
8 changed files
with
333 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use bevy::{ecs::system::EntityCommands, prelude::*}; | ||
|
||
use crate::levels_setup::{IsPlayer, LevelObject}; | ||
|
||
#[derive(Component)] | ||
pub struct Cannon { | ||
pub timer: Timer, | ||
pub cmd: Box<dyn Send + Sync + Fn(&mut EntityCommands)>, | ||
} | ||
|
||
pub struct CannonPlugin; | ||
|
||
impl Plugin for CannonPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Update, (shoot, handle_collision)); | ||
} | ||
} | ||
|
||
fn shoot( | ||
time: Res<Time>, | ||
mut query: Query<(&mut Cannon, &GlobalTransform, Option<&Name>)>, | ||
mut commands: Commands, | ||
) { | ||
for (mut cannon, cannon_transform, cannon_name) in query.iter_mut() { | ||
if cannon.timer.tick(time.delta()).just_finished() { | ||
let mut cmd = commands.spawn(LevelObject); | ||
if let Some(cannon_name) = cannon_name.as_ref() { | ||
cmd.insert(Name::new(format!("{cannon_name} projectile"))); | ||
} | ||
#[cfg(feature = "rapier3d")] | ||
cmd.insert(bevy_rapier3d::geometry::ActiveEvents::COLLISION_EVENTS); | ||
(cannon.cmd)(&mut cmd); | ||
cmd.insert(TransformBundle::from_transform( | ||
Transform::from_translation(cannon_transform.translation()), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
#[allow(clippy::type_complexity)] | ||
pub struct CannonBullet { | ||
effect: Box<dyn Send + Sync + Fn(&mut EntityCommands)>, | ||
} | ||
|
||
impl CannonBullet { | ||
pub fn new_with_effect(effect: impl 'static + Send + Sync + Fn(&mut EntityCommands)) -> Self { | ||
Self { | ||
effect: Box::new(effect), | ||
} | ||
} | ||
} | ||
|
||
fn handle_collision( | ||
#[cfg(feature = "avian3d")] mut avian_reader: EventReader<avian3d::prelude::CollisionStarted>, | ||
#[cfg(feature = "rapier3d")] mut rapier_reader: EventReader< | ||
bevy_rapier3d::prelude::CollisionEvent, | ||
>, | ||
bullets_query: Query<&CannonBullet>, | ||
player_query: Query<(), With<IsPlayer>>, | ||
mut commands: Commands, | ||
) { | ||
let events = std::iter::empty::<(Entity, Entity)>(); | ||
#[cfg(feature = "avian3d")] | ||
let events = events.chain(avian_reader.read().map(|event| (event.0, event.1))); | ||
#[cfg(feature = "rapier3d")] | ||
let events = events.chain(rapier_reader.read().filter_map(|event| { | ||
use bevy_rapier3d::pipeline::CollisionEvent; | ||
if let CollisionEvent::Started(e1, e2, _) = event { | ||
Some((*e1, *e2)) | ||
} else { | ||
None | ||
} | ||
})); | ||
|
||
let events = events.flat_map(|(e1, e2)| [(e1, e2), (e2, e1)]); | ||
|
||
for (bullet_entity, player_entity) in events { | ||
let Ok(bullet) = bullets_query.get(bullet_entity) else { | ||
continue; | ||
}; | ||
if !player_query.contains(player_entity) { | ||
continue; | ||
} | ||
(bullet.effect)(&mut commands.entity(player_entity)); | ||
commands.entity(bullet_entity).despawn_recursive(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
mod cannon; | ||
mod moving_platform; | ||
mod push_effect; | ||
mod time_to_despawn; | ||
|
||
use bevy::prelude::*; | ||
|
||
pub use cannon::{Cannon, CannonBullet}; | ||
pub use moving_platform::MovingPlatform; | ||
pub use push_effect::PushEffect; | ||
pub use time_to_despawn::TimeToDespawn; | ||
|
||
pub struct LevelMechanicsPlugin; | ||
|
||
impl Plugin for LevelMechanicsPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugins(moving_platform::MovingPlatformPlugin); | ||
app.add_plugins(cannon::CannonPlugin); | ||
app.add_plugins(push_effect::PushEffectPlugin); | ||
app.add_plugins(time_to_despawn::TimeToDespawnPlugin); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use bevy::{ecs::query::QueryData, prelude::*}; | ||
use bevy_tnua::math::Vector3; | ||
|
||
pub struct PushEffectPlugin; | ||
|
||
impl Plugin for PushEffectPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Update, apply_push_effect); | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
pub enum PushEffect { | ||
Impulse(Vector3), | ||
} | ||
|
||
#[derive(QueryData)] | ||
#[query_data(mutable)] | ||
struct VelocityQuery { | ||
#[cfg(feature = "rapier2d")] | ||
rapier2d_velocity: Option<&'static mut bevy_rapier2d::prelude::Velocity>, | ||
|
||
#[cfg(feature = "rapier3d")] | ||
rapier3d_velocity: Option<&'static mut bevy_rapier3d::prelude::Velocity>, | ||
|
||
#[cfg(feature = "avian2d")] | ||
avian2d_linear_velocity: Option<&'static mut avian2d::prelude::LinearVelocity>, | ||
|
||
#[cfg(feature = "avian3d")] | ||
avian3d_linear_velocity: Option<&'static mut avian3d::prelude::LinearVelocity>, | ||
} | ||
|
||
impl VelocityQueryItem<'_> { | ||
fn apply_impulse(&mut self, impulse: Vector3) { | ||
#[cfg(feature = "rapier2d")] | ||
if let Some(velocity) = self.rapier2d_velocity.as_mut() { | ||
velocity.linvel += impulse.truncate(); | ||
} | ||
|
||
#[cfg(feature = "rapier3d")] | ||
if let Some(velocity) = self.rapier3d_velocity.as_mut() { | ||
velocity.linvel += impulse; | ||
} | ||
|
||
#[cfg(feature = "avian2d")] | ||
if let Some(velocity) = self.avian2d_linear_velocity.as_mut() { | ||
velocity.0 += impulse.truncate(); | ||
} | ||
|
||
#[cfg(feature = "avian3d")] | ||
if let Some(velocity) = self.avian3d_linear_velocity.as_mut() { | ||
velocity.0 += impulse; | ||
} | ||
} | ||
} | ||
|
||
fn apply_push_effect( | ||
mut query: Query<(Entity, &PushEffect, VelocityQuery)>, | ||
mut commands: Commands, | ||
) { | ||
for (entity, push_effect, mut velocity) in query.iter_mut() { | ||
match push_effect { | ||
PushEffect::Impulse(impulse) => { | ||
velocity.apply_impulse(*impulse); | ||
commands.entity(entity).remove::<PushEffect>(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use bevy::prelude::*; | ||
|
||
pub struct TimeToDespawnPlugin; | ||
|
||
impl Plugin for TimeToDespawnPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Update, handle_despawn); | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
pub struct TimeToDespawn(Timer); | ||
|
||
impl TimeToDespawn { | ||
pub fn from_seconds(duration: f32) -> Self { | ||
Self(Timer::from_seconds(duration, TimerMode::Once)) | ||
} | ||
} | ||
|
||
fn handle_despawn( | ||
time: Res<Time>, | ||
mut query: Query<(Entity, &mut TimeToDespawn)>, | ||
mut commands: Commands, | ||
) { | ||
for (entity, mut ttd) in query.iter_mut() { | ||
if ttd.0.tick(time.delta()).just_finished() { | ||
commands.entity(entity).despawn_recursive(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.