Split crate into shared logic library and binary crate (#52)
This commit is contained in:
17
crates/shared/src/utils/auto_rotate.rs
Normal file
17
crates/shared/src/utils/auto_rotate.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[reflect(Component)]
|
||||
pub struct AutoRotation(pub Quat);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.register_type::<AutoRotation>();
|
||||
|
||||
app.add_systems(FixedUpdate, update_auto_rotation);
|
||||
}
|
||||
|
||||
fn update_auto_rotation(mut query: Query<(&AutoRotation, &mut Transform)>) {
|
||||
for (auto_rotation, mut transform) in query.iter_mut() {
|
||||
transform.rotate_local(auto_rotation.0);
|
||||
}
|
||||
}
|
||||
74
crates/shared/src/utils/billboards.rs
Normal file
74
crates/shared/src/utils/billboards.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use crate::camera::MainCamera;
|
||||
use bevy::prelude::*;
|
||||
use bevy_sprite3d::Sprite3dPlugin;
|
||||
|
||||
#[derive(Component, Reflect, Default, PartialEq, Eq)]
|
||||
#[reflect(Component)]
|
||||
pub enum Billboard {
|
||||
#[default]
|
||||
All,
|
||||
XZ,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
if !app.is_plugin_added::<Sprite3dPlugin>() {
|
||||
app.add_plugins(Sprite3dPlugin);
|
||||
}
|
||||
|
||||
app.register_type::<Billboard>();
|
||||
app.add_systems(Update, (face_camera, face_camera_no_parent));
|
||||
}
|
||||
|
||||
fn face_camera(
|
||||
cam_query: Query<&GlobalTransform, With<MainCamera>>,
|
||||
mut query: Query<
|
||||
(&mut Transform, &ChildOf, &InheritedVisibility, &Billboard),
|
||||
Without<MainCamera>,
|
||||
>,
|
||||
parent_transform: Query<&GlobalTransform>,
|
||||
) {
|
||||
let Ok(cam_transform) = cam_query.single() else {
|
||||
return;
|
||||
};
|
||||
|
||||
for (mut transform, parent, visible, billboard) in query.iter_mut() {
|
||||
if !matches!(*visible, InheritedVisibility::VISIBLE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let Ok(parent_global) = parent_transform.get(parent.parent()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let target = cam_transform.reparented_to(parent_global);
|
||||
|
||||
let target = match *billboard {
|
||||
Billboard::All => target.translation,
|
||||
Billboard::XZ => Vec3::new(
|
||||
target.translation.x,
|
||||
transform.translation.y,
|
||||
target.translation.z,
|
||||
),
|
||||
};
|
||||
|
||||
transform.look_at(target, Vec3::Y);
|
||||
}
|
||||
}
|
||||
|
||||
fn face_camera_no_parent(
|
||||
cam_query: Query<&GlobalTransform, With<MainCamera>>,
|
||||
mut query: Query<(&mut Transform, &Billboard), (Without<MainCamera>, Without<ChildOf>)>,
|
||||
) {
|
||||
let Ok(cam_transform) = cam_query.single() else {
|
||||
return;
|
||||
};
|
||||
for (mut transform, billboard) in query.iter_mut() {
|
||||
let target = cam_transform.translation();
|
||||
let target = match *billboard {
|
||||
Billboard::All => cam_transform.translation(),
|
||||
Billboard::XZ => Vec3::new(target.x, transform.translation.y, target.z),
|
||||
};
|
||||
|
||||
transform.look_at(target, Vec3::Y);
|
||||
}
|
||||
}
|
||||
40
crates/shared/src/utils/explosions.rs
Normal file
40
crates/shared/src/utils/explosions.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::{global_observer, hitpoints::Hit, physics_layers::GameLayer};
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Event, Debug)]
|
||||
pub struct Explosion {
|
||||
pub position: Vec3,
|
||||
pub radius: f32,
|
||||
pub damage: u32,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
global_observer!(app, on_explosion);
|
||||
}
|
||||
|
||||
fn on_explosion(
|
||||
explosion: Trigger<Explosion>,
|
||||
mut commands: Commands,
|
||||
spatial_query: SpatialQuery,
|
||||
) {
|
||||
let explosion = explosion.event();
|
||||
let intersections = {
|
||||
spatial_query.shape_intersections(
|
||||
&Collider::sphere(explosion.radius),
|
||||
explosion.position,
|
||||
Quat::default(),
|
||||
&SpatialQueryFilter::default().with_mask(LayerMask(
|
||||
GameLayer::Npc.to_bits() | GameLayer::Player.to_bits(),
|
||||
)),
|
||||
)
|
||||
};
|
||||
|
||||
for entity in intersections.iter() {
|
||||
if let Ok(mut e) = commands.get_entity(*entity) {
|
||||
e.trigger(Hit {
|
||||
damage: explosion.damage,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
9
crates/shared/src/utils/mod.rs
Normal file
9
crates/shared/src/utils/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub mod auto_rotate;
|
||||
pub mod billboards;
|
||||
pub mod explosions;
|
||||
pub mod observers;
|
||||
pub mod sprite_3d_animation;
|
||||
pub mod squish_animation;
|
||||
pub mod trail;
|
||||
|
||||
pub(crate) use observers::global_observer;
|
||||
35
crates/shared/src/utils/observers.rs
Normal file
35
crates/shared/src/utils/observers.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! global_observer {
|
||||
($app:expr,$system:expr) => {{
|
||||
$app.world_mut()
|
||||
.add_observer($system)
|
||||
.insert(Name::new(stringify!($system)))
|
||||
}};
|
||||
}
|
||||
|
||||
pub use global_observer;
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Update, global_observers);
|
||||
}
|
||||
|
||||
fn global_observers(
|
||||
mut cmds: Commands,
|
||||
query: Query<Entity, (With<Observer>, Without<Children>, Added<Observer>)>,
|
||||
mut root: Local<Option<Entity>>,
|
||||
) {
|
||||
if root.is_none() {
|
||||
let new_root = cmds.spawn(Name::new("Observers")).id();
|
||||
*root = Some(new_root);
|
||||
}
|
||||
|
||||
let Some(root) = *root else {
|
||||
return;
|
||||
};
|
||||
|
||||
for o in query.iter() {
|
||||
cmds.entity(root).add_child(o);
|
||||
}
|
||||
}
|
||||
36
crates/shared/src/utils/sprite_3d_animation.rs
Normal file
36
crates/shared/src/utils/sprite_3d_animation.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_sprite3d::Sprite3d;
|
||||
|
||||
#[derive(Component, Reflect, Deref, DerefMut)]
|
||||
#[reflect(Component)]
|
||||
pub struct AnimationTimer(Timer);
|
||||
|
||||
impl AnimationTimer {
|
||||
pub fn new(t: Timer) -> Self {
|
||||
Self(t)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Update, animate_sprite);
|
||||
}
|
||||
|
||||
fn animate_sprite(
|
||||
mut commands: Commands,
|
||||
time: Res<Time>,
|
||||
mut query: Query<(Entity, &mut AnimationTimer, &mut Sprite3d)>,
|
||||
) {
|
||||
for (e, mut timer, mut sprite_3d) in query.iter_mut() {
|
||||
timer.tick(time.delta());
|
||||
if timer.just_finished() {
|
||||
let length = sprite_3d.texture_atlas_keys.as_ref().unwrap().len();
|
||||
let atlas = sprite_3d.texture_atlas.as_mut().unwrap();
|
||||
|
||||
if atlas.index < length - 1 {
|
||||
atlas.index = atlas.index.saturating_add(1) % length;
|
||||
} else {
|
||||
commands.entity(e).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
crates/shared/src/utils/squish_animation.rs
Normal file
20
crates/shared/src/utils/squish_animation.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use bevy::prelude::*;
|
||||
use ops::sin;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[reflect(Component)]
|
||||
pub struct SquishAnimation(pub f32);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Update, update);
|
||||
}
|
||||
|
||||
fn update(mut query: Query<(&mut Transform, &SquishAnimation)>, time: Res<Time>) {
|
||||
for (mut transform, keymesh) in query.iter_mut() {
|
||||
transform.scale = Vec3::new(
|
||||
keymesh.0,
|
||||
keymesh.0 + (sin(time.elapsed_secs() * 6.) * 0.2),
|
||||
keymesh.0,
|
||||
);
|
||||
}
|
||||
}
|
||||
70
crates/shared/src/utils/trail.rs
Normal file
70
crates/shared/src/utils/trail.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use crate::GameState;
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Trail {
|
||||
points: Vec<Vec3>,
|
||||
col_start: LinearRgba,
|
||||
col_end: LinearRgba,
|
||||
}
|
||||
|
||||
impl Trail {
|
||||
pub fn new(points: usize, col_start: LinearRgba, col_end: LinearRgba) -> Self {
|
||||
Self {
|
||||
points: Vec::with_capacity(points),
|
||||
col_start,
|
||||
col_end,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_pos(self, pos: Vec3) -> Self {
|
||||
let mut trail = self;
|
||||
trail.add(pos);
|
||||
trail
|
||||
}
|
||||
|
||||
pub fn add(&mut self, pos: Vec3) {
|
||||
if self.points.len() >= self.points.capacity() {
|
||||
self.points.pop();
|
||||
}
|
||||
self.points.insert(0, pos);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
update_trail.run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
}
|
||||
|
||||
fn update_trail(
|
||||
mut query: Query<(Entity, &mut Trail, &Gizmo, &GlobalTransform)>,
|
||||
global_transform: Query<&GlobalTransform>,
|
||||
mut gizmo_assets: ResMut<Assets<GizmoAsset>>,
|
||||
) -> Result {
|
||||
for (e, mut trail, gizmo, pos) in query.iter_mut() {
|
||||
trail.add(pos.translation());
|
||||
|
||||
let parent_transform = global_transform.get(e)?;
|
||||
|
||||
let Some(gizmo) = gizmo_assets.get_mut(gizmo.handle.id()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
gizmo.clear();
|
||||
|
||||
let lerp_denom = trail.points.len() as f32;
|
||||
|
||||
gizmo.linestrip_gradient(trail.points.iter().enumerate().map(|(i, pos)| {
|
||||
(
|
||||
GlobalTransform::from_translation(*pos)
|
||||
.reparented_to(parent_transform)
|
||||
.translation,
|
||||
trail.col_start.mix(&trail.col_end, i as f32 / lerp_denom),
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user