arrow ability (#29)
This commit is contained in:
BIN
assets/sfx/abilities/crossbow.ogg
Normal file
BIN
assets/sfx/abilities/crossbow.ogg
Normal file
Binary file not shown.
111
src/abilities/arrow.rs
Normal file
111
src/abilities/arrow.rs
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
use super::TriggerArrow;
|
||||||
|
use crate::{
|
||||||
|
GameState, billboards::Billboard, global_observer, hitpoints::Hit, loading_assets::GameAssets,
|
||||||
|
physics_layers::GameLayer, sounds::PlaySound, utils::sprite_3d_animation::AnimationTimer,
|
||||||
|
};
|
||||||
|
use avian3d::prelude::*;
|
||||||
|
use bevy::{pbr::NotShadowCaster, prelude::*};
|
||||||
|
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
|
||||||
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct ArrowProjectile;
|
||||||
|
|
||||||
|
#[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, update.run_if(in_state(GameState::Playing)));
|
||||||
|
|
||||||
|
global_observer!(app, on_trigger_arrow);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_arrow(
|
||||||
|
trigger: Trigger<TriggerArrow>,
|
||||||
|
mut commands: Commands,
|
||||||
|
query_transform: Query<&Transform>,
|
||||||
|
) {
|
||||||
|
let state = trigger.0;
|
||||||
|
|
||||||
|
commands.trigger(PlaySound::Crossbow);
|
||||||
|
|
||||||
|
let rotation = if let Some(target) = state.target {
|
||||||
|
let t = query_transform
|
||||||
|
.get(target)
|
||||||
|
.expect("target must have transform");
|
||||||
|
Transform::from_translation(state.pos)
|
||||||
|
.looking_at(t.translation, Vec3::Y)
|
||||||
|
.rotation
|
||||||
|
} else {
|
||||||
|
state.rot.mul_quat(Quat::from_rotation_y(PI))
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut t = Transform::from_translation(state.pos).with_rotation(rotation);
|
||||||
|
t.translation += (t.forward().as_vec3() * 2.) + (Vec3::Y * 0.6);
|
||||||
|
|
||||||
|
commands.spawn((Name::new("projectile-arrow"), ArrowProjectile, t));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
mut cmds: Commands,
|
||||||
|
query: Query<(Entity, &Transform), With<ArrowProjectile>>,
|
||||||
|
spatial_query: SpatialQuery,
|
||||||
|
assets: Res<ShotAssets>,
|
||||||
|
mut sprite_params: Sprite3dParams,
|
||||||
|
) {
|
||||||
|
for (e, t) in query.iter() {
|
||||||
|
let filter = SpatialQueryFilter::from_mask(LayerMask(
|
||||||
|
GameLayer::Level.to_bits() | GameLayer::Npc.to_bits(),
|
||||||
|
));
|
||||||
|
|
||||||
|
if let Some(first_hit) = spatial_query.cast_shape(
|
||||||
|
&Collider::sphere(0.5),
|
||||||
|
t.translation,
|
||||||
|
t.rotation,
|
||||||
|
t.forward(),
|
||||||
|
&ShapeCastConfig::from_max_distance(80.),
|
||||||
|
&filter,
|
||||||
|
) {
|
||||||
|
cmds.entity(first_hit.entity).trigger(Hit { damage: 50 });
|
||||||
|
|
||||||
|
cmds.spawn(
|
||||||
|
Sprite3dBuilder {
|
||||||
|
image: assets.image.clone(),
|
||||||
|
pixels_per_metre: 128.,
|
||||||
|
alpha_mode: AlphaMode::Blend,
|
||||||
|
unlit: true,
|
||||||
|
..default()
|
||||||
|
}
|
||||||
|
.bundle_with_atlas(
|
||||||
|
&mut sprite_params,
|
||||||
|
TextureAtlas {
|
||||||
|
layout: assets.layout.clone(),
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.insert((
|
||||||
|
Billboard,
|
||||||
|
Transform::from_translation(first_hit.point1),
|
||||||
|
NotShadowCaster,
|
||||||
|
AnimationTimer::new(Timer::from_seconds(0.005, TimerMode::Repeating)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
cmds.entity(e).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
mod arrow;
|
||||||
mod gun;
|
mod gun;
|
||||||
mod thrown;
|
mod thrown;
|
||||||
|
|
||||||
@@ -73,11 +74,14 @@ impl TriggerData {
|
|||||||
#[derive(Event, Reflect)]
|
#[derive(Event, Reflect)]
|
||||||
pub struct TriggerGun(pub TriggerData);
|
pub struct TriggerGun(pub TriggerData);
|
||||||
#[derive(Event, Reflect)]
|
#[derive(Event, Reflect)]
|
||||||
|
pub struct TriggerArrow(pub TriggerData);
|
||||||
|
#[derive(Event, Reflect)]
|
||||||
pub struct TriggerThrow(pub TriggerData);
|
pub struct TriggerThrow(pub TriggerData);
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_plugins(gun::plugin);
|
app.add_plugins(gun::plugin);
|
||||||
app.add_plugins(thrown::plugin);
|
app.add_plugins(thrown::plugin);
|
||||||
|
app.add_plugins(arrow::plugin);
|
||||||
|
|
||||||
app.add_systems(Update, enemy_hit.run_if(in_state(GameState::Playing)));
|
app.add_systems(Update, enemy_hit.run_if(in_state(GameState::Playing)));
|
||||||
|
|
||||||
@@ -155,7 +159,8 @@ fn on_trigger_state(
|
|||||||
match ability {
|
match ability {
|
||||||
HeadAbility::Thrown => commands.trigger(TriggerThrow(trigger_state)),
|
HeadAbility::Thrown => commands.trigger(TriggerThrow(trigger_state)),
|
||||||
HeadAbility::Gun => commands.trigger(TriggerGun(trigger_state)),
|
HeadAbility::Gun => commands.trigger(TriggerGun(trigger_state)),
|
||||||
_ => (),
|
HeadAbility::Arrow => commands.trigger(TriggerArrow(trigger_state)),
|
||||||
|
HeadAbility::None => (),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ pub struct AudioAssets {
|
|||||||
pub key_collect: Handle<AudioSource>,
|
pub key_collect: Handle<AudioSource>,
|
||||||
#[asset(path = "sfx/abilities/gun.ogg")]
|
#[asset(path = "sfx/abilities/gun.ogg")]
|
||||||
pub gun: Handle<AudioSource>,
|
pub gun: Handle<AudioSource>,
|
||||||
|
#[asset(path = "sfx/abilities/crossbow.ogg")]
|
||||||
|
pub crossbow: Handle<AudioSource>,
|
||||||
#[asset(path = "sfx/effects/gate.ogg")]
|
#[asset(path = "sfx/effects/gate.ogg")]
|
||||||
pub gate: Handle<AudioSource>,
|
pub gate: Handle<AudioSource>,
|
||||||
#[asset(path = "sfx/effects/cash.ogg")]
|
#[asset(path = "sfx/effects/cash.ogg")]
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ pub enum PlaySound {
|
|||||||
Invalid,
|
Invalid,
|
||||||
Reloaded,
|
Reloaded,
|
||||||
CashHeal,
|
CashHeal,
|
||||||
|
Crossbow,
|
||||||
Backpack { open: bool },
|
Backpack { open: bool },
|
||||||
Head(String),
|
Head(String),
|
||||||
}
|
}
|
||||||
@@ -42,6 +43,7 @@ fn on_spawn_sounds(
|
|||||||
}
|
}
|
||||||
PlaySound::KeyCollect => assets.key_collect.clone(),
|
PlaySound::KeyCollect => assets.key_collect.clone(),
|
||||||
PlaySound::Gun => assets.gun.clone(),
|
PlaySound::Gun => assets.gun.clone(),
|
||||||
|
PlaySound::Crossbow => assets.crossbow.clone(),
|
||||||
PlaySound::Gate => assets.gate.clone(),
|
PlaySound::Gate => assets.gate.clone(),
|
||||||
PlaySound::CashCollect => assets.cash.clone(),
|
PlaySound::CashCollect => assets.cash.clone(),
|
||||||
PlaySound::Selection => assets.selection.clone(),
|
PlaySound::Selection => assets.selection.clone(),
|
||||||
|
|||||||
Reference in New Issue
Block a user