Skip to content

Commit

Permalink
Merge pull request #2 from cBournhonesque/cb/projectiles
Browse files Browse the repository at this point in the history
Adds basic projectiles
  • Loading branch information
cBournhonesque authored Jan 18, 2025
2 parents 914c428 + 4a72331 commit 78bfd69
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
3 changes: 2 additions & 1 deletion renderer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod player;
mod weapon;
mod projectiles;

use bevy::prelude::*;
use lightyear::client::interpolation::VisualInterpolationPlugin;
Expand All @@ -12,6 +12,7 @@ impl Plugin for RendererPlugin {
// TODO: add option to disable inspector
app.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::new());
app.add_plugins(player::PlayerPlugin);
app.add_plugins(projectiles::ProjectilesPlugin);
app.insert_resource(AmbientLight {
brightness: 0.0,
..default()
Expand Down
1 change: 0 additions & 1 deletion renderer/src/player.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bevy::color::palettes::basic::BLUE;
use bevy::core_pipeline::prepass::DepthPrepass;
use bevy::prelude::*;
use bevy::render::camera::Exposure;
use lightyear::prelude::client::{Confirmed, Predicted, VisualInterpolateStatus};
use lightyear::shared::replication::components::Controlled;
use shared::player::Player;
Expand Down
34 changes: 34 additions & 0 deletions renderer/src/projectiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use bevy::color::palettes::basic::BLUE;
use bevy::prelude::*;
use shared::projectiles::Projectile;

pub(crate) struct ProjectilesPlugin;

impl Plugin for ProjectilesPlugin {
fn build(&self, app: &mut App) {
// SYSTEMS
app.add_observer(spawn_visuals);
}
}

/// When a projectile is spawn, add visuals to it
fn spawn_visuals(
trigger: Trigger<OnAdd, Projectile>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.entity(trigger.entity()).with_child(
(
Mesh3d(meshes.add(Mesh::from(Sphere {
// TODO: must match the collider size
radius: 0.1,
..default()
}))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: BLUE.into(),
..Default::default()
})),
)
);
}
Empty file removed renderer/src/weapon.rs
Empty file.
4 changes: 2 additions & 2 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod map;
mod states;
mod physics;
pub mod player;
mod weapons;
pub mod projectiles;

pub mod prelude {
pub use crate::network::{protocol::*, settings::*};
Expand All @@ -27,6 +27,6 @@ impl Plugin for SharedPlugin {
app.add_plugins(map::MapPlugin { headless: self.headless});
app.add_plugins(physics::PhysicsPlugin);
app.add_plugins(player::PlayerPlugin);
app.add_plugins(weapons::WeaponsPlugin);
app.add_plugins(projectiles::ProjectilesPlugin);
}
}
24 changes: 18 additions & 6 deletions shared/src/weapons.rs → shared/src/projectiles.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use avian3d::prelude::Position;
use avian3d::prelude::{Collider, LinearVelocity, Position, RigidBody};
use bevy::prelude::*;
use leafwing_input_manager::action_state::ActionState;
use lightyear::client::prediction::Predicted;
Expand All @@ -7,40 +7,52 @@ use lightyear::prelude::server::{Replicate, SyncTarget};
use crate::player::Player;
use crate::prelude::{PlayerInput, PREDICTION_REPLICATION_GROUP_ID};

pub(crate) struct WeaponsPlugin;
pub(crate) struct ProjectilesPlugin;

impl Plugin for WeaponsPlugin {
impl Plugin for ProjectilesPlugin {
fn build(&self, app: &mut App) {
// SYSTEMS
app.add_systems(FixedUpdate, shoot_projectiles);
}
}

// TODO: maybe make this an enum with the type of projectile?
#[derive(Component, Debug, Clone)]
pub struct Projectile;


/// Shoot projectiles from the current weapon when the shoot action is pressed
pub(crate) fn shoot_projectiles(
mut commands: Commands,
identity: NetworkIdentity,
query: Query<
(
&Player,
&Position,
&Transform,
&ActionState<PlayerInput>,
),
Or<(With<Predicted>, With<Replicating>)>,
>,
) {
for (player, position, action) in query.iter() {
for (player, transform, action) in query.iter() {

// NOTE: pressed lets you shoot many bullets, which can be cool
if action.just_pressed(&PlayerInput::ShootPrimary) {
let direction = transform.forward().as_vec3();

// offset a little bit from the player
let mut new_transform = *transform;
new_transform.translation += 0.5 * direction;
let projectile = (
Transform::from_translation(position.0),
new_transform,
Projectile,
// TODO: change projectile speed
LinearVelocity(direction * 5.0),
// TODO: change projectile shape
Collider::sphere(0.1),
// the projectile will be spawned on both client (in the predicted timeline) and the server
PreSpawnedPlayerObject::default(),
RigidBody::Dynamic,
);

// on the server, spawn and replicate the projectile
Expand Down

0 comments on commit 78bfd69

Please sign in to comment.