Add very basic projectile

This commit is contained in:
Felix Ableitner 2021-01-10 19:47:53 +01:00
parent 27ee782aad
commit 1f9493efb5

View file

@ -1,7 +1,16 @@
use crate::player::PlayerMarker;
use bevy::{ use bevy::{
input::{mouse::MouseButtonInput, ElementState}, input::{mouse::MouseButtonInput, ElementState},
prelude::*, prelude::*,
}; };
use bevy_rapier3d::{
na::Matrix3x1,
physics::RigidBodyHandleComponent,
rapier::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodySet},
geometry::ColliderBuilder,
},
};
use std::time::Duration; use std::time::Duration;
pub struct WeaponPlugin; pub struct WeaponPlugin;
@ -10,7 +19,8 @@ impl Plugin for WeaponPlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
app app
.add_resource(WeaponData::default()) .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>>, mouse_button_events: Res<Events<MouseButtonInput>>,
time: Res<Time>, time: Res<Time>,
mut weapon: ResMut<WeaponData>, 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 { if weapon.reloading {
weapon.reload_timer.tick(time.delta_seconds()); weapon.reload_timer.tick(time.delta_seconds());
@ -49,8 +63,56 @@ fn shoot_system(
} }
} }
if let Some(player_transform) = player_location_query.iter().next() {
if fired && !weapon.reloading { if fired && !weapon.reloading {
info!("weapon fired"); info!("weapon fired");
weapon.reloading = true; 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);
} }
} }