Split crate into shared logic library and binary crate (#52) (#53)

This commit is contained in:
extrawurst
2025-06-29 12:45:25 +02:00
committed by GitHub
parent 5d4c7630ef
commit 7996d632f7
65 changed files with 497 additions and 82 deletions

View File

@@ -0,0 +1,184 @@
use super::TriggerThrow;
use crate::{
GameState,
billboards::Billboard,
heads_database::HeadsDatabase,
loading_assets::GameAssets,
physics_layers::GameLayer,
sounds::PlaySound,
utils::{
auto_rotate::AutoRotation, explosions::Explosion, global_observer,
sprite_3d_animation::AnimationTimer,
},
};
use avian3d::prelude::*;
use bevy::{pbr::NotShadowCaster, prelude::*};
use bevy_ballistic::launch_velocity;
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
use std::f32::consts::PI;
#[derive(Component)]
struct ThrownProjectile {
impact_animation: bool,
damage: u32,
}
#[derive(Resource)]
struct ShotAssets {
image: Handle<Image>,
layout: Handle<TextureAtlasLayout>,
}
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)));
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(
trigger: Trigger<TriggerThrow>,
mut commands: Commands,
query_transform: Query<&Transform>,
assets: Res<GameAssets>,
gltf_assets: Res<Assets<Gltf>>,
heads_db: Res<HeadsDatabase>,
) {
let state = trigger.event().0;
commands.trigger(PlaySound::Throw);
const SPEED: f32 = 35.;
let pos = state.pos;
let vel = if let Some(target) = state.target {
let t = query_transform
.get(target)
.expect("target must have transform");
launch_velocity(pos, t.translation, SPEED, 9.81)
.map(|(low, _)| low)
.unwrap()
} else {
state.rot.mul_quat(Quat::from_rotation_y(-PI / 2.))
* (Vec3::new(2., 1., 0.).normalize() * SPEED)
};
let head = heads_db.head_stats(state.head);
let mesh = assets.projectiles[format!("{}.glb", head.projectile).as_str()].clone();
let asset = gltf_assets.get(&mesh).unwrap();
//TODO: projectile db?
let explosion_animation = !matches!(state.head, 8 | 16);
commands
.spawn((
Transform::from_translation(pos),
Name::new("projectile-thrown"),
ThrownProjectile {
impact_animation: explosion_animation,
damage: head.damage,
},
Collider::sphere(0.4),
CollisionLayers::new(
LayerMask(GameLayer::Projectile.to_bits()),
LayerMask(state.target_layer.to_bits() | GameLayer::Level.to_bits()),
),
RigidBody::Dynamic,
CollisionEventsEnabled,
Mass(0.01),
LinearVelocity(vel),
Visibility::default(),
Sensor,
))
.with_child((
AutoRotation(Quat::from_rotation_x(0.4) * Quat::from_rotation_z(0.3)),
SceneRoot(asset.scenes[0].clone()),
));
}
fn shot_collision(
mut commands: Commands,
mut collision_event_reader: EventReader<CollisionStarted>,
query_shot: Query<(&ThrownProjectile, &Transform)>,
sensors: Query<(), With<Sensor>>,
assets: Res<ShotAssets>,
mut sprite_params: Sprite3dParams,
) {
for CollisionStarted(e1, e2) in collision_event_reader.read() {
if !query_shot.contains(*e1) && !query_shot.contains(*e2) {
continue;
}
if sensors.contains(*e1) && sensors.contains(*e2) {
continue;
}
let shot_entity = if query_shot.contains(*e1) { *e1 } else { *e2 };
let Ok((shot_pos, animation, damage)) =
query_shot.get(shot_entity).map(|(projectile, t)| {
(
t.translation,
projectile.impact_animation,
projectile.damage,
)
})
else {
continue;
};
if let Ok(mut entity) = commands.get_entity(shot_entity) {
entity.try_despawn();
} else {
continue;
}
commands.trigger(PlaySound::ThrowHit);
commands.trigger(Explosion {
damage,
position: shot_pos,
//TODO: should be around 1 grid in distance
radius: 5.,
});
//TODO: support different impact animations
if animation {
commands
.spawn(
Sprite3dBuilder {
image: assets.image.clone(),
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)),
));
}
}
}