Replicate explosion effects (#56)
This commit is contained in:
@@ -1,21 +1,19 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
abilities::TriggerCurver,
|
abilities::{BuildExplosionSprite, TriggerCurver},
|
||||||
billboards::Billboard,
|
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
hitpoints::Hit,
|
hitpoints::Hit,
|
||||||
loading_assets::GameAssets,
|
|
||||||
physics_layers::GameLayer,
|
physics_layers::GameLayer,
|
||||||
protocol::GltfSceneRoot,
|
protocol::GltfSceneRoot,
|
||||||
tb_entities::EnemySpawn,
|
tb_entities::EnemySpawn,
|
||||||
utils::{
|
utils::{
|
||||||
auto_rotate::AutoRotation, commands::CommandExt, global_observer,
|
auto_rotate::AutoRotation,
|
||||||
sprite_3d_animation::AnimationTimer,
|
commands::{CommandExt, EntityCommandExt},
|
||||||
|
global_observer,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::{pbr::NotShadowCaster, prelude::*};
|
use bevy::prelude::*;
|
||||||
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
|
|
||||||
use lightyear::prelude::{NetworkTarget, Replicate};
|
use lightyear::prelude::{NetworkTarget, Replicate};
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
@@ -27,14 +25,7 @@ struct CurverProjectile {
|
|||||||
damage: u32,
|
damage: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct ShotAssets {
|
|
||||||
image: Handle<Image>,
|
|
||||||
layout: Handle<TextureAtlasLayout>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(shot_collision, enemy_hit).run_if(in_state(GameState::Playing)),
|
(shot_collision, enemy_hit).run_if(in_state(GameState::Playing)),
|
||||||
@@ -47,16 +38,6 @@ pub fn plugin(app: &mut App) {
|
|||||||
global_observer!(app, on_trigger_missile);
|
global_observer!(app, on_trigger_missile);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Sprite3dParams) {
|
|
||||||
let layout = TextureAtlasLayout::from_grid(UVec2::splat(256), 7, 6, None, None);
|
|
||||||
let texture_atlas_layout = sprite_params.atlas_layouts.add(layout);
|
|
||||||
|
|
||||||
commands.insert_resource(ShotAssets {
|
|
||||||
image: assets.impact_atlas.clone(),
|
|
||||||
layout: texture_atlas_layout,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_trigger_missile(
|
fn on_trigger_missile(
|
||||||
trigger: Trigger<TriggerCurver>,
|
trigger: Trigger<TriggerCurver>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
@@ -156,8 +137,6 @@ fn shot_collision(
|
|||||||
mut collision_event_reader: EventReader<CollisionStarted>,
|
mut collision_event_reader: EventReader<CollisionStarted>,
|
||||||
query_shot: Query<&Transform, With<CurverProjectile>>,
|
query_shot: Query<&Transform, With<CurverProjectile>>,
|
||||||
sensors: Query<(), With<Sensor>>,
|
sensors: Query<(), With<Sensor>>,
|
||||||
assets: Res<ShotAssets>,
|
|
||||||
mut sprite_params: Sprite3dParams,
|
|
||||||
) {
|
) {
|
||||||
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
||||||
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
||||||
@@ -180,27 +159,10 @@ fn shot_collision(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let texture_atlas = TextureAtlas {
|
commands.trigger_server(BuildExplosionSprite {
|
||||||
layout: assets.layout.clone(),
|
pos: shot_pos,
|
||||||
index: 0,
|
pixels_per_meter: 128.,
|
||||||
};
|
time: 0.01,
|
||||||
|
});
|
||||||
commands
|
|
||||||
.spawn(
|
|
||||||
Sprite3dBuilder {
|
|
||||||
image: assets.image.clone(),
|
|
||||||
pixels_per_metre: 128.,
|
|
||||||
alpha_mode: AlphaMode::Blend,
|
|
||||||
unlit: true,
|
|
||||||
..default()
|
|
||||||
}
|
|
||||||
.bundle_with_atlas(&mut sprite_params, texture_atlas),
|
|
||||||
)
|
|
||||||
.insert((
|
|
||||||
Billboard::All,
|
|
||||||
Transform::from_translation(shot_pos),
|
|
||||||
NotShadowCaster,
|
|
||||||
AnimationTimer::new(Timer::from_seconds(0.01, TimerMode::Repeating)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
use super::TriggerMissile;
|
use super::TriggerMissile;
|
||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
billboards::Billboard,
|
abilities::BuildExplosionSprite,
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
loading_assets::GameAssets,
|
|
||||||
physics_layers::GameLayer,
|
physics_layers::GameLayer,
|
||||||
protocol::GltfSceneRoot,
|
protocol::GltfSceneRoot,
|
||||||
sounds::PlaySound,
|
sounds::PlaySound,
|
||||||
utils::{
|
utils::{
|
||||||
commands::CommandExt, explosions::Explosion, global_observer,
|
commands::{CommandExt, EntityCommandExt},
|
||||||
sprite_3d_animation::AnimationTimer, trail::Trail,
|
explosions::Explosion,
|
||||||
|
global_observer,
|
||||||
|
trail::Trail,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::{pbr::NotShadowCaster, prelude::*};
|
use bevy::prelude::*;
|
||||||
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
|
|
||||||
use lightyear::prelude::{NetworkTarget, Replicate};
|
use lightyear::prelude::{NetworkTarget, Replicate};
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
@@ -27,14 +27,7 @@ struct MissileProjectile {
|
|||||||
damage: u32,
|
damage: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct ShotAssets {
|
|
||||||
image: Handle<Image>,
|
|
||||||
layout: Handle<TextureAtlasLayout>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
|
||||||
app.add_systems(Update, shot_collision.run_if(in_state(GameState::Playing)));
|
app.add_systems(Update, shot_collision.run_if(in_state(GameState::Playing)));
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
FixedUpdate,
|
FixedUpdate,
|
||||||
@@ -44,16 +37,6 @@ pub fn plugin(app: &mut App) {
|
|||||||
global_observer!(app, on_trigger_missile);
|
global_observer!(app, on_trigger_missile);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Sprite3dParams) {
|
|
||||||
let layout = TextureAtlasLayout::from_grid(UVec2::splat(256), 7, 6, None, None);
|
|
||||||
let texture_atlas_layout = sprite_params.atlas_layouts.add(layout);
|
|
||||||
|
|
||||||
commands.insert_resource(ShotAssets {
|
|
||||||
image: assets.impact_atlas.clone(),
|
|
||||||
layout: texture_atlas_layout,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_trigger_missile(
|
fn on_trigger_missile(
|
||||||
trigger: Trigger<TriggerMissile>,
|
trigger: Trigger<TriggerMissile>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
@@ -144,8 +127,6 @@ fn shot_collision(
|
|||||||
mut collision_event_reader: EventReader<CollisionStarted>,
|
mut collision_event_reader: EventReader<CollisionStarted>,
|
||||||
query_shot: Query<(&MissileProjectile, &Transform)>,
|
query_shot: Query<(&MissileProjectile, &Transform)>,
|
||||||
sensors: Query<(), With<Sensor>>,
|
sensors: Query<(), With<Sensor>>,
|
||||||
assets: Res<ShotAssets>,
|
|
||||||
mut sprite_params: Sprite3dParams,
|
|
||||||
) {
|
) {
|
||||||
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
||||||
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
||||||
@@ -175,27 +156,10 @@ fn shot_collision(
|
|||||||
radius: 6.,
|
radius: 6.,
|
||||||
});
|
});
|
||||||
|
|
||||||
let texture_atlas = TextureAtlas {
|
commands.trigger_server(BuildExplosionSprite {
|
||||||
layout: assets.layout.clone(),
|
pos: shot_pos,
|
||||||
index: 0,
|
pixels_per_meter: 16.,
|
||||||
};
|
time: 0.01,
|
||||||
|
});
|
||||||
commands
|
|
||||||
.spawn(
|
|
||||||
Sprite3dBuilder {
|
|
||||||
image: assets.image.clone(),
|
|
||||||
pixels_per_metre: 16.,
|
|
||||||
alpha_mode: AlphaMode::Blend,
|
|
||||||
unlit: true,
|
|
||||||
..default()
|
|
||||||
}
|
|
||||||
.bundle_with_atlas(&mut sprite_params, texture_atlas),
|
|
||||||
)
|
|
||||||
.insert((
|
|
||||||
Billboard::All,
|
|
||||||
Transform::from_translation(shot_pos),
|
|
||||||
NotShadowCaster,
|
|
||||||
AnimationTimer::new(Timer::from_seconds(0.01, TimerMode::Repeating)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
mod arrow;
|
pub mod arrow;
|
||||||
mod curver;
|
pub mod curver;
|
||||||
mod gun;
|
pub mod gun;
|
||||||
mod healing;
|
pub mod healing;
|
||||||
mod missile;
|
pub mod missile;
|
||||||
mod thrown;
|
pub mod thrown;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
@@ -13,11 +13,14 @@ use crate::{
|
|||||||
head::ActiveHead,
|
head::ActiveHead,
|
||||||
heads::ActiveHeads,
|
heads::ActiveHeads,
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
|
loading_assets::GameAssets,
|
||||||
physics_layers::GameLayer,
|
physics_layers::GameLayer,
|
||||||
player::{Player, PlayerBodyMesh},
|
player::{Player, PlayerBodyMesh},
|
||||||
sounds::PlaySound,
|
sounds::PlaySound,
|
||||||
|
utils::{billboards::Billboard, sprite_3d_animation::AnimationTimer},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::{pbr::NotShadowCaster, prelude::*};
|
||||||
|
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
|
||||||
pub use healing::Healing;
|
pub use healing::Healing;
|
||||||
use healing::HealingStateChanged;
|
use healing::HealingStateChanged;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -109,12 +112,14 @@ pub fn plugin(app: &mut App) {
|
|||||||
app.add_plugins(healing::plugin);
|
app.add_plugins(healing::plugin);
|
||||||
app.add_plugins(curver::plugin);
|
app.add_plugins(curver::plugin);
|
||||||
|
|
||||||
|
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||||
app.add_systems(
|
app.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(update, update_heal_ability).run_if(in_state(GameState::Playing)),
|
(update, update_heal_ability).run_if(in_state(GameState::Playing)),
|
||||||
);
|
);
|
||||||
|
|
||||||
global_observer!(app, on_trigger_state);
|
global_observer!(app, on_trigger_state);
|
||||||
|
global_observer!(app, build_explosion_sprite);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_trigger_state(
|
fn on_trigger_state(
|
||||||
@@ -222,3 +227,57 @@ fn update_heal_ability(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
struct ShotAssets {
|
||||||
|
image: Handle<Image>,
|
||||||
|
layout: Handle<TextureAtlasLayout>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Sprite3dParams) {
|
||||||
|
let layout = TextureAtlasLayout::from_grid(UVec2::splat(256), 7, 6, None, None);
|
||||||
|
let texture_atlas_layout = sprite_params.atlas_layouts.add(layout);
|
||||||
|
|
||||||
|
commands.insert_resource(ShotAssets {
|
||||||
|
image: assets.impact_atlas.clone(),
|
||||||
|
layout: texture_atlas_layout,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Event, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct BuildExplosionSprite {
|
||||||
|
pos: Vec3,
|
||||||
|
pixels_per_meter: f32,
|
||||||
|
time: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_explosion_sprite(
|
||||||
|
trigger: Trigger<BuildExplosionSprite>,
|
||||||
|
mut commands: Commands,
|
||||||
|
assets: Res<ShotAssets>,
|
||||||
|
mut sprite_params: Sprite3dParams,
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
Transform::from_translation(trigger.event().pos),
|
||||||
|
Sprite3dBuilder {
|
||||||
|
image: assets.image.clone(),
|
||||||
|
pixels_per_metre: trigger.event().pixels_per_meter,
|
||||||
|
alpha_mode: AlphaMode::Blend,
|
||||||
|
unlit: true,
|
||||||
|
..default()
|
||||||
|
}
|
||||||
|
.bundle_with_atlas(
|
||||||
|
&mut sprite_params,
|
||||||
|
TextureAtlas {
|
||||||
|
layout: assets.layout.clone(),
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Billboard::All,
|
||||||
|
NotShadowCaster,
|
||||||
|
AnimationTimer::new(Timer::from_seconds(
|
||||||
|
trigger.event().time,
|
||||||
|
TimerMode::Repeating,
|
||||||
|
)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
use super::TriggerThrow;
|
use super::TriggerThrow;
|
||||||
use crate::{
|
use crate::{
|
||||||
GameState,
|
GameState,
|
||||||
billboards::Billboard,
|
abilities::BuildExplosionSprite,
|
||||||
heads_database::HeadsDatabase,
|
heads_database::HeadsDatabase,
|
||||||
loading_assets::GameAssets,
|
|
||||||
physics_layers::GameLayer,
|
physics_layers::GameLayer,
|
||||||
protocol::GltfSceneRoot,
|
protocol::GltfSceneRoot,
|
||||||
sounds::PlaySound,
|
sounds::PlaySound,
|
||||||
utils::{
|
utils::{
|
||||||
auto_rotate::AutoRotation, commands::CommandExt, explosions::Explosion, global_observer,
|
auto_rotate::AutoRotation,
|
||||||
sprite_3d_animation::AnimationTimer,
|
commands::{CommandExt, EntityCommandExt},
|
||||||
|
explosions::Explosion,
|
||||||
|
global_observer,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::{pbr::NotShadowCaster, prelude::*};
|
use bevy::prelude::*;
|
||||||
use bevy_ballistic::launch_velocity;
|
use bevy_ballistic::launch_velocity;
|
||||||
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
|
|
||||||
use lightyear::prelude::{NetworkTarget, Replicate};
|
use lightyear::prelude::{NetworkTarget, Replicate};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
@@ -26,29 +26,12 @@ pub struct ThrownProjectile {
|
|||||||
damage: u32,
|
damage: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct ShotAssets {
|
|
||||||
image: Handle<Image>,
|
|
||||||
layout: Handle<TextureAtlasLayout>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
|
||||||
app.add_systems(Update, shot_collision.run_if(in_state(GameState::Playing)));
|
app.add_systems(Update, shot_collision.run_if(in_state(GameState::Playing)));
|
||||||
|
|
||||||
global_observer!(app, on_trigger_thrown);
|
global_observer!(app, on_trigger_thrown);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Sprite3dParams) {
|
|
||||||
let layout = TextureAtlasLayout::from_grid(UVec2::splat(256), 7, 6, None, None);
|
|
||||||
let texture_atlas_layout = sprite_params.atlas_layouts.add(layout);
|
|
||||||
|
|
||||||
commands.insert_resource(ShotAssets {
|
|
||||||
image: assets.impact_atlas.clone(),
|
|
||||||
layout: texture_atlas_layout,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_trigger_thrown(
|
fn on_trigger_thrown(
|
||||||
trigger: Trigger<TriggerThrow>,
|
trigger: Trigger<TriggerThrow>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
@@ -113,8 +96,6 @@ fn shot_collision(
|
|||||||
mut collision_event_reader: EventReader<CollisionStarted>,
|
mut collision_event_reader: EventReader<CollisionStarted>,
|
||||||
query_shot: Query<(&ThrownProjectile, &Transform)>,
|
query_shot: Query<(&ThrownProjectile, &Transform)>,
|
||||||
sensors: Query<(), With<Sensor>>,
|
sensors: Query<(), With<Sensor>>,
|
||||||
assets: Res<ShotAssets>,
|
|
||||||
mut sprite_params: Sprite3dParams,
|
|
||||||
) {
|
) {
|
||||||
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
for CollisionStarted(e1, e2) in collision_event_reader.read() {
|
||||||
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
|
||||||
@@ -156,29 +137,11 @@ fn shot_collision(
|
|||||||
|
|
||||||
//TODO: support different impact animations
|
//TODO: support different impact animations
|
||||||
if animation {
|
if animation {
|
||||||
commands
|
commands.trigger_server(BuildExplosionSprite {
|
||||||
.spawn(
|
pos: shot_pos,
|
||||||
Sprite3dBuilder {
|
pixels_per_meter: 32.,
|
||||||
image: assets.image.clone(),
|
time: 0.02,
|
||||||
pixels_per_metre: 32.,
|
});
|
||||||
alpha_mode: AlphaMode::Blend,
|
|
||||||
unlit: true,
|
|
||||||
..default()
|
|
||||||
}
|
|
||||||
.bundle_with_atlas(
|
|
||||||
&mut sprite_params,
|
|
||||||
TextureAtlas {
|
|
||||||
layout: assets.layout.clone(),
|
|
||||||
index: 0,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.insert((
|
|
||||||
Billboard::All,
|
|
||||||
Transform::from_translation(shot_pos),
|
|
||||||
NotShadowCaster,
|
|
||||||
AnimationTimer::new(Timer::from_seconds(0.02, TimerMode::Repeating)),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
use crate::{global_observer, loading_assets::GameAssets};
|
use crate::{
|
||||||
|
abilities::BuildExplosionSprite, global_observer, loading_assets::GameAssets,
|
||||||
|
utils::triggers::TriggerAppExt,
|
||||||
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use lightyear::prelude::AppComponentExt;
|
use lightyear::prelude::{ActionsChannel, AppComponentExt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.register_component::<Transform>();
|
app.register_component::<Transform>();
|
||||||
app.register_component::<GltfSceneRoot>();
|
app.register_component::<GltfSceneRoot>();
|
||||||
|
|
||||||
|
app.replicate_trigger::<BuildExplosionSprite, ActionsChannel>();
|
||||||
|
|
||||||
global_observer!(app, spawn_gltf_scene_roots);
|
global_observer!(app, spawn_gltf_scene_roots);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,49 @@
|
|||||||
use bevy::ecs::{
|
use bevy::ecs::{
|
||||||
bundle::Bundle, resource::Resource, system::EntityCommands, world::EntityWorldMut,
|
bundle::Bundle,
|
||||||
|
event::Event,
|
||||||
|
resource::Resource,
|
||||||
|
system::{Commands, EntityCommands},
|
||||||
|
world::{EntityWorldMut, World},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Default, Resource)]
|
#[derive(Default, Resource)]
|
||||||
pub struct IsServer;
|
pub struct IsServer;
|
||||||
|
|
||||||
pub trait CommandExt {
|
pub trait CommandExt {
|
||||||
fn insert_server(&mut self, bundle: impl Bundle) -> &mut Self;
|
fn trigger_server(&mut self, event: impl Event) -> &mut Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'w> CommandExt for EntityCommands<'w> {
|
impl<'w, 's> CommandExt for Commands<'w, 's> {
|
||||||
fn insert_server(&mut self, bundle: impl Bundle) -> &mut Self {
|
fn trigger_server(&mut self, event: impl Event) -> &mut Self {
|
||||||
self.queue(|mut entity: EntityWorldMut| {
|
self.queue(|world: &mut World| {
|
||||||
if entity.world().contains_resource::<IsServer>() {
|
if world.contains_resource::<IsServer>() {
|
||||||
entity.insert(bundle);
|
world.trigger(event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait EntityCommandExt {
|
||||||
|
fn insert_server(&mut self, bundle: impl Bundle) -> &mut Self;
|
||||||
|
|
||||||
|
fn trigger_server(&mut self, event: impl Event) -> &mut Self;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'w> EntityCommandExt for EntityCommands<'w> {
|
||||||
|
fn insert_server(&mut self, bundle: impl Bundle) -> &mut Self {
|
||||||
|
self.queue(|mut entity: EntityWorldMut| {
|
||||||
|
if entity.world().contains_resource::<IsServer>() {
|
||||||
|
entity.insert(bundle);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn trigger_server(&mut self, event: impl Event) -> &mut Self {
|
||||||
|
self.queue(|mut entity: EntityWorldMut| {
|
||||||
|
if entity.world().contains_resource::<IsServer>() {
|
||||||
|
entity.trigger(event);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ pub mod observers;
|
|||||||
pub mod sprite_3d_animation;
|
pub mod sprite_3d_animation;
|
||||||
pub mod squish_animation;
|
pub mod squish_animation;
|
||||||
pub mod trail;
|
pub mod trail;
|
||||||
|
pub mod triggers;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
pub(crate) use observers::global_observer;
|
pub(crate) use observers::global_observer;
|
||||||
|
|||||||
55
crates/shared/src/utils/triggers.rs
Normal file
55
crates/shared/src/utils/triggers.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use crate::utils::commands::IsServer;
|
||||||
|
use bevy::{ecs::system::SystemParam, prelude::*};
|
||||||
|
use lightyear::prelude::{AppTriggerExt, Channel, NetworkDirection, RemoteTrigger, TriggerSender};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(SystemParam)]
|
||||||
|
pub struct ServerMultiTriggerSender<'w, 's, M: Event + Clone> {
|
||||||
|
senders: Query<'w, 's, &'static mut TriggerSender<M>>,
|
||||||
|
is_server: Option<Res<'w, IsServer>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'w, 's, M: Event + Clone> ServerMultiTriggerSender<'w, 's, M> {
|
||||||
|
pub fn server_trigger_targets<C: Channel>(&mut self, trigger: M, target: &[Entity]) {
|
||||||
|
if self.is_server.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for mut sender in self.senders.iter_mut() {
|
||||||
|
sender.trigger_targets::<C>(trigger.clone(), target.iter().copied());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TriggerAppExt {
|
||||||
|
fn replicate_trigger<M: Event + Clone + Serialize + for<'de> Deserialize<'de>, C: Channel>(
|
||||||
|
&mut self,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TriggerAppExt for App {
|
||||||
|
fn replicate_trigger<M: Event + Clone + Serialize + for<'de> Deserialize<'de>, C: Channel>(
|
||||||
|
&mut self,
|
||||||
|
) {
|
||||||
|
self.add_trigger::<M>()
|
||||||
|
.add_direction(NetworkDirection::ServerToClient);
|
||||||
|
self.add_observer(replicate_trigger_to_clients::<M, C>);
|
||||||
|
self.add_observer(remote_to_local_trigger::<M>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replicate_trigger_to_clients<M: Event + Clone, C: Channel>(
|
||||||
|
trigger: Trigger<M>,
|
||||||
|
mut sender: ServerMultiTriggerSender<M>,
|
||||||
|
) {
|
||||||
|
let targets: &[Entity] = if trigger.target() == Entity::PLACEHOLDER {
|
||||||
|
&[]
|
||||||
|
} else {
|
||||||
|
&[trigger.target()]
|
||||||
|
};
|
||||||
|
sender.server_trigger_targets::<C>(trigger.event().clone(), targets);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remote_to_local_trigger<M: Event + Clone>(trigger: Trigger<RemoteTrigger<M>>, mut c: Commands) {
|
||||||
|
c.trigger_targets(trigger.event().trigger.clone(), trigger.target());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user