From 0bf0d765c25a52173939641ca78ac6464d01ab0d Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Thu, 12 Oct 2023 12:51:18 +0200 Subject: [PATCH] WIP --- crates/combat/src/laser.rs | 6 +- crates/combat/src/trail.rs | 73 ++++++++++++++++++----- crates/messages/src/lib.rs | 2 +- crates/messages/src/players/mod.rs | 4 ++ crates/messages/src/players/projectile.rs | 12 ++++ crates/multiplayer/src/lib.rs | 3 +- crates/multiplayer/src/messages.rs | 1 + crates/multiplayer/src/playermsg.rs | 10 +++- 8 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 crates/messages/src/players/projectile.rs diff --git a/crates/combat/src/laser.rs b/crates/combat/src/laser.rs index 8dda1648f..e4a5daf32 100644 --- a/crates/combat/src/laser.rs +++ b/crates/combat/src/laser.rs @@ -6,7 +6,7 @@ use parry3d::query::Ray; use crate::{ health::{HealthSet, LocalUpdateHealthEvent}, sightline::LineOfSight, - trail::TrailEvent, + trail::LocalLaserTrailEvent, AttackingSet, }; @@ -80,13 +80,13 @@ fn fire( mut fires: EventReader, sightline: LineOfSight, mut health: EventWriter, - mut trail: EventWriter, + mut trail: EventWriter, mut start_sound: EventWriter, ) { for fire in fires.iter() { let observation = sightline.sight(fire.ray(), fire.max_toi(), fire.attacker()); - trail.send(TrailEvent::new(Ray::new( + trail.send(LocalLaserTrailEvent::new(Ray::new( fire.ray().origin, observation.toi() * fire.ray().dir, ))); diff --git a/crates/combat/src/trail.rs b/crates/combat/src/trail.rs index cf311ce3d..a1849e563 100644 --- a/crates/combat/src/trail.rs +++ b/crates/combat/src/trail.rs @@ -11,7 +11,11 @@ use bevy::{ }, }, }; -use de_core::{cleanup::DespawnOnGameExit, gamestate::GameState, state::AppState}; +use de_core::{ + cleanup::DespawnOnGameExit, gamestate::GameState, gconfig::GameConfig, state::AppState, +}; +use de_messages::{NetProjectile, ToPlayers}; +use de_multiplayer::{MessagesSet, NetRecvProjectileEvent, ToPlayersEvent}; use parry3d::query::Ray; const TRAIL_LIFESPAN: Duration = Duration::from_millis(500); @@ -22,20 +26,29 @@ pub(crate) struct TrailPlugin; impl Plugin for TrailPlugin { fn build(&self, app: &mut App) { app.add_plugins(MaterialPlugin::::default()) - .add_event::() + .add_event::() + .add_event::() .add_systems(OnEnter(AppState::InGame), setup) .add_systems(OnExit(AppState::InGame), cleanup) .add_systems( PostUpdate, - (spawn, update).run_if(in_state(GameState::Playing)), + ( + local_laser_trail + .before(MessagesSet::SendMessages) + .before(laser_trail), + remote_laser_trail.before(laser_trail), + laser_trail, + update, + ) + .run_if(in_state(GameState::Playing)), ); } } #[derive(Event)] -pub(crate) struct TrailEvent(Ray); +pub(crate) struct LocalLaserTrailEvent(Ray); -impl TrailEvent { +impl LocalLaserTrailEvent { /// Send this event to spawn a new trail. The trail will automatically fade /// out and disappear. /// @@ -46,12 +59,11 @@ impl TrailEvent { pub(crate) fn new(ray: Ray) -> Self { Self(ray) } - - fn ray(&self) -> &Ray { - &self.0 - } } +#[derive(Event)] +struct LaserTrailEvent(Ray); + #[derive(Resource)] struct MeshHandle(Handle); @@ -104,12 +116,45 @@ impl Material for TrailMaterial { } } -fn spawn( +fn local_laser_trail( + config: Res, + mut in_events: EventReader, + mut out_events: EventWriter, + mut net_events: EventWriter, +) { + for event in in_events.iter() { + out_events.send(LaserTrailEvent(event.0)); + + if config.multiplayer() { + net_events.send(ToPlayersEvent::new(ToPlayers::Projectile( + NetProjectile::Laser { + origin: event.0.origin.into(), + direction: event.0.dir.into(), + }, + ))); + } + } +} + +fn remote_laser_trail( + mut in_events: EventReader, + mut out_events: EventWriter, +) { + for event in in_events.iter() { + match **event { + NetProjectile::Laser { origin, direction } => { + out_events.send(LaserTrailEvent(Ray::new(origin.into(), direction.into()))); + } + } + } +} + +fn laser_trail( mut commands: Commands, mut materials: ResMut>, time: Res