Add very basic projectile
This commit is contained in:
parent
27ee782aad
commit
1f9493efb5
1 changed files with 66 additions and 4 deletions
|
@ -1,7 +1,16 @@
|
|||
use crate::player::PlayerMarker;
|
||||
use bevy::{
|
||||
input::{mouse::MouseButtonInput, ElementState},
|
||||
prelude::*,
|
||||
};
|
||||
use bevy_rapier3d::{
|
||||
na::Matrix3x1,
|
||||
physics::RigidBodyHandleComponent,
|
||||
rapier::{
|
||||
dynamics::{RigidBody, RigidBodyBuilder, RigidBodySet},
|
||||
geometry::ColliderBuilder,
|
||||
},
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct WeaponPlugin;
|
||||
|
@ -10,7 +19,8 @@ impl Plugin for WeaponPlugin {
|
|||
fn build(&self, app: &mut AppBuilder) {
|
||||
app
|
||||
.add_resource(WeaponData::default())
|
||||
.add_system(shoot_system.system());
|
||||
.add_system(shoot_system.system())
|
||||
.add_system(projectile_system.system());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,6 +43,10 @@ fn shoot_system(
|
|||
mouse_button_events: Res<Events<MouseButtonInput>>,
|
||||
time: Res<Time>,
|
||||
mut weapon: ResMut<WeaponData>,
|
||||
commands: &mut Commands,
|
||||
meshes: ResMut<Assets<Mesh>>,
|
||||
materials: ResMut<Assets<StandardMaterial>>,
|
||||
player_location_query: Query<&Transform, With<PlayerMarker>>,
|
||||
) {
|
||||
if weapon.reloading {
|
||||
weapon.reload_timer.tick(time.delta_seconds());
|
||||
|
@ -49,8 +63,56 @@ fn shoot_system(
|
|||
}
|
||||
}
|
||||
|
||||
if fired && !weapon.reloading {
|
||||
info!("weapon fired");
|
||||
weapon.reloading = true;
|
||||
if let Some(player_transform) = player_location_query.iter().next() {
|
||||
if fired && !weapon.reloading {
|
||||
info!("weapon fired");
|
||||
weapon.reloading = true;
|
||||
spawn_projectile(commands, meshes, materials, player_transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ProjectileMarker;
|
||||
|
||||
fn spawn_projectile(
|
||||
commands: &mut Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
player_transform: &Transform,
|
||||
) {
|
||||
let projectile_mesh = Mesh::from(shape::Cube { size: 0.25 });
|
||||
let projectile_material = Color::rgb(0.0, 1.0, 0.0).into();
|
||||
|
||||
let player_trans = player_transform.translation;
|
||||
let player_rot = player_transform.rotation;
|
||||
|
||||
commands
|
||||
.spawn(PbrBundle {
|
||||
mesh: meshes.add(projectile_mesh),
|
||||
material: materials.add(projectile_material),
|
||||
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
|
||||
..Default::default()
|
||||
})
|
||||
.with(ProjectileMarker::default())
|
||||
.with(
|
||||
RigidBodyBuilder::new_dynamic()
|
||||
.translation(player_trans.x, player_trans.y, player_trans.z)
|
||||
.rotation(Matrix3x1::new(player_rot.x, player_rot.y, player_rot.z))
|
||||
.mass(0.0, false)
|
||||
.lock_translations()
|
||||
.lock_rotations(),
|
||||
)
|
||||
.with(ColliderBuilder::cylinder(0.1, 0.1));
|
||||
}
|
||||
|
||||
fn projectile_system(
|
||||
mut rigid_body_set: ResMut<RigidBodySet>,
|
||||
mut query: Query<&RigidBodyHandleComponent, With<ProjectileMarker>>,
|
||||
) {
|
||||
for body_handle in query.iter_mut() {
|
||||
let body: &mut RigidBody = rigid_body_set.get_mut(body_handle.handle()).unwrap();
|
||||
|
||||
body.set_linvel(Matrix3x1::new(1.0, 0.0, 0.0), true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue