From 901c30154b7368f23dbe8f5fc7f783ca83cf7bbc Mon Sep 17 00:00:00 2001 From: Franklin Blanco Date: Thu, 9 Nov 2023 13:00:01 -0800 Subject: [PATCH] bullet --- src/comps/core/markers/bullet.rs | 10 ++++++ src/comps/core/markers/mod.rs | 3 +- src/logic/core/guns/shoot.rs | 59 +++++++++++++++++++++++++++----- src/ui/editor/inspector.rs | 23 ++++++++----- src/ui/editor/plugin.rs | 2 +- 5 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/comps/core/markers/bullet.rs diff --git a/src/comps/core/markers/bullet.rs b/src/comps/core/markers/bullet.rs new file mode 100644 index 0000000..83fbdef --- /dev/null +++ b/src/comps/core/markers/bullet.rs @@ -0,0 +1,10 @@ +use bevy::{prelude::Component, reflect::Reflect}; + +use crate::logic::core::guns::caliber::Caliber; + + + +#[derive(Component, Reflect)] +pub struct BulletMarker { + pub caliber: Caliber +} \ No newline at end of file diff --git a/src/comps/core/markers/mod.rs b/src/comps/core/markers/mod.rs index 90ffc00..e83341c 100644 --- a/src/comps/core/markers/mod.rs +++ b/src/comps/core/markers/mod.rs @@ -2,4 +2,5 @@ pub mod camera; pub mod firearm; pub mod holdable; pub mod player; -pub mod muzzle_flash; \ No newline at end of file +pub mod muzzle_flash; +pub mod bullet; \ No newline at end of file diff --git a/src/logic/core/guns/shoot.rs b/src/logic/core/guns/shoot.rs index 76c64f2..5c59a1b 100644 --- a/src/logic/core/guns/shoot.rs +++ b/src/logic/core/guns/shoot.rs @@ -1,8 +1,9 @@ use std::time::Duration; use bevy::{prelude::*, render::render_resource::PrimitiveTopology}; +use bevy_rapier3d::prelude::*; -use crate::comps::core::markers::muzzle_flash::MuzzleFlashMarker; +use crate::comps::core::markers::{muzzle_flash::MuzzleFlashMarker, bullet::BulletMarker}; use super::caliber::Caliber; @@ -12,7 +13,7 @@ pub fn shoot_bullet( materials: &mut ResMut>, firing_point: Transform, forward: Vec3, - _up: Vec3, + up: Vec3, caliber: Caliber ) { // Spawn muzzle flash LIGHT @@ -47,12 +48,52 @@ pub fn shoot_bullet( ..Default::default() } ); + + spawn_bullet(commands, meshes, materials, firing_point, forward, up, caliber); } -/* -meshes.add(box_2_mesh.into()), - material: materials.add(StandardMaterial { - base_color: Color::RED, - perceptual_roughness: 1.0, - ..default() - }), */ \ No newline at end of file +pub fn spawn_bullet( + commands: &mut Commands, + meshes: &mut ResMut>, + materials: &mut ResMut>, + firing_point: Transform, + forward: Vec3, + up: Vec3, + caliber: Caliber +) { + commands.spawn( + ( + Name::new("Bullet"), + BulletMarker { + caliber + }, + MaterialMeshBundle { + mesh: { + meshes.add( + shape::UVSphere { radius: 0.2, sectors: 36, stacks: 18 }.into() + ) + }, + material: materials.add(StandardMaterial { + base_color: Color::GREEN, + ..Default::default() + }), + visibility: Visibility::Visible, + transform: firing_point, + ..Default::default() + }, + RigidBody::Dynamic, + GravityScale(1.0), + Collider::ball(0.2), + Velocity::zero(), + Damping { + linear_damping: 1.0, + angular_damping: 1.0, + }, + ColliderMassProperties::Mass(0.001), + ExternalImpulse { + impulse: forward * 0.1 , + torque_impulse: Vec3::ZERO, + }, + ) + ); +} \ No newline at end of file diff --git a/src/ui/editor/inspector.rs b/src/ui/editor/inspector.rs index 5f8c2cd..2bd8da6 100644 --- a/src/ui/editor/inspector.rs +++ b/src/ui/editor/inspector.rs @@ -1,5 +1,5 @@ use bevy::{prelude::*, window::PrimaryWindow}; -use bevy_editor_pls::controls::{self, EditorControls}; +use bevy_editor_pls::{controls::{self, EditorControls}, default_windows::cameras::EditorCamera}; use bevy_inspector_egui::{egui, bevy_egui::EguiContext, bevy_inspector}; #[allow(unused)] @@ -21,7 +21,6 @@ pub fn inspector_ui(world: &mut World) { pub fn editor_controls() -> EditorControls { let mut editor_controls = EditorControls::default_bindings(); editor_controls.unbind(controls::Action::PlayPauseEditor); - editor_controls.insert( controls::Action::PlayPauseEditor, @@ -30,17 +29,23 @@ pub fn editor_controls() -> EditorControls { conditions: vec![controls::BindingCondition::ListeningForText(false)], }, ); - editor_controls } pub fn set_cam3d_controls( mut query: Query<&mut bevy_editor_pls::default_windows::cameras::camera_3d_free::FlycamControls>, + mut query_camera_transforms: Query<&mut Transform, With>, ) { - let mut controls = query.single_mut(); - controls.key_forward = KeyCode::Up; - controls.key_back = KeyCode::Down; - controls.key_left = KeyCode::Left; - controls.key_right = KeyCode::Right; - controls.key_up = KeyCode::U; + for mut controls in query.iter_mut() { + controls.key_forward = KeyCode::Up; + controls.key_back = KeyCode::Down; + controls.key_left = KeyCode::Left; + controls.key_right = KeyCode::Right; + controls.key_up = KeyCode::U; + } + // TODO: run this after the position update already in the editor plugin + for mut transform in query_camera_transforms.iter_mut() { + transform.translation = Vec3 { x: 6.5, y: 28.0, z: 41.6 }; + transform.look_at(Vec3::ZERO, Vec3::Y); + } } \ No newline at end of file diff --git a/src/ui/editor/plugin.rs b/src/ui/editor/plugin.rs index 14c5fa7..f7a8376 100644 --- a/src/ui/editor/plugin.rs +++ b/src/ui/editor/plugin.rs @@ -41,7 +41,7 @@ impl Plugin for MainEditorUiPlugin { .add_plugins(EguiPlugin) .add_plugins(EditorPlugin::default()) .insert_resource(editor_controls()) - .add_systems(Startup, set_cam3d_controls); + .add_systems(PostStartup, set_cam3d_controls); //.add_plugins(ResourceInspectorPlugin::::default()); //.add_plugins(bevy_inspector_egui::DefaultInspectorConfigPlugin) // adds default options and `InspectorEguiImpl`s //.add_systems(Update, inspector_ui);