Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# v0.3.0

- Add semantic versioning

Contributors:
- https://github.com/Leinnan

# v0.2.1

- Update to Bevy v0.16

# v0.2.0

## <ins>!Breaking Changes!<ins>
Expand Down
17 changes: 6 additions & 11 deletions src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,21 @@ pub fn orbit_gamepad(
if cam.mouse_orbit_button_enabled && !gamepad.pressed(cam.gamepad_settings.mouse_orbit_button) {
return;
}

let x_axis = gamepad.right_stick().x;
let y_axis = gamepad.right_stick().y;
let (x, y) = (gamepad.right_stick().x, gamepad.right_stick().y);

let deadzone = 0.5;
let mut rotation = Vec2::ZERO;
let (x, y) = (x_axis, y_axis);
if x.abs() > deadzone || y.abs() > deadzone {
rotation = Vec2::new(x, y);
}

if rotation.length_squared() > 0.0 {
let window = window_q.single().unwrap();
let delta_x = {
let delta = rotation.x / window.width()
* std::f32::consts::PI
* 2.0
* cam.gamepad_settings.sensitivity.x;
delta
};
let delta_x = rotation.x / window.width()
* std::f32::consts::PI
* 2.0
* cam.gamepad_settings.sensitivity.x;

let delta_y = -rotation.y / window.height() * PI * cam.gamepad_settings.sensitivity.y;
let yaw = Quat::from_rotation_y(-delta_x);
let pitch = Quat::from_rotation_x(-delta_y);
Expand Down
59 changes: 25 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use mouse::MousePlugin;
/// ```
/// use bevy::prelude::*;
/// use bevy_third_person_camera::ThirdPersonCameraPlugin;
/// fn main() {
/// App::new().add_plugins(ThirdPersonCameraPlugin);
/// }
/// App::new().add_plugins(ThirdPersonCameraPlugin);
/// ```
pub struct ThirdPersonCameraPlugin;

Expand Down Expand Up @@ -150,6 +148,13 @@ impl Default for ThirdPersonCamera {
}
}

impl ThirdPersonCamera {
pub fn with_custom_settings(mut self, gamepad_settings: CustomGamepadSettings) -> Self {
self.gamepad_settings = gamepad_settings;
self
}
}

/// Sets the zoom bounds (min & max)
pub struct Zoom {
pub min: f32,
Expand Down Expand Up @@ -195,19 +200,9 @@ impl Offset {
/// use bevy::prelude::*;
/// use bevy_third_person_camera::{CustomGamepadSettings, ThirdPersonCamera};
/// fn spawn_camera(mut commands: Commands) {
/// let gamepad = Gamepad::new(0);
/// let settings = CustomGamepadSettings::default()
/// commands.spawn((
/// ThirdPersonCamera {
/// gamepad_settings: CustomGamepadSettings {
/// aim_button: GamepadButton::new(gamepad, GamepadButtonType::LeftTrigger2),
/// mouse_orbit_button: GamepadButton::new(gamepad, GamepadButtonType::LeftTrigger),
/// offset_toggle_button: GamepadButton::new(gamepad, GamepadButtonType::DPadRight),
/// sensitivity: Vec2::new(7.0, 4.0),
/// zoom_in_button: GamepadButton::new(gamepad, GamepadButtonType::DPadUp),
/// zoom_out_button: GamepadButton::new(gamepad, GamepadButtonType::DPadDown),
/// },
/// ..default()
/// },
/// ThirdPersonCamera::default().with_custom_settings(settings)
/// Camera3dBundle::default(),
/// ));
/// }
Expand Down Expand Up @@ -299,6 +294,7 @@ fn aim_condition(cam_q: Query<&ThirdPersonCamera, With<ThirdPersonCamera>>) -> b
cam.aim_enabled
}

#[allow(clippy::type_complexity)]
fn aim(
mut cam_q: Query<
(&mut ThirdPersonCamera, &Transform),
Expand All @@ -317,10 +313,7 @@ fn aim(
return;
};

let gamepad = match gamepad_q.single() {
Ok(value) => Some(value),
Err(_) => None,
};
let gamepad = gamepad_q.single().ok();

let is_gamepad_aiming = match gamepad {
Some(gp) => gp.pressed(cam.gamepad_settings.aim_button),
Expand Down Expand Up @@ -351,30 +344,28 @@ fn aim(
} else {
cam.zoom.radius -= zoom_factor;
}
} else {
if let Some(radius_copy) = cam.zoom.radius_copy {
let zoom_factor = (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs();

// stop zooming out if current radius is greater than original radius
if cam.zoom.radius >= radius_copy || cam.zoom.radius + zoom_factor >= radius_copy {
cam.zoom.radius = radius_copy;
cam.zoom.radius_copy = None;
} else {
cam.zoom.radius += (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs();
}
} else if let Some(radius_copy) = cam.zoom.radius_copy {
let zoom_factor = (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs();

// stop zooming out if current radius is greater than original radius
if cam.zoom.radius >= radius_copy || cam.zoom.radius + zoom_factor >= radius_copy {
cam.zoom.radius = radius_copy;
cam.zoom.radius_copy = None;
} else {
cam.zoom.radius += (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs();
}
}
}

pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera, With<ThirdPersonCamera>>) -> bool {
pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera>) -> bool {
let Ok(cam) = cam_q.single() else {
return false;
};
return cam.zoom_enabled && cam.cursor_lock_active;
cam.zoom_enabled && cam.cursor_lock_active
}

// only run toggle_x_offset if `offset_toggle_enabled` is true
fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera, With<ThirdPersonCamera>>) -> bool {
fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera>) -> bool {
let Ok(cam) = cam_q.single() else {
return false;
};
Expand All @@ -383,7 +374,7 @@ fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera, With<ThirdPersonCa

// inverts the x offset. Example: left shoulder view -> right shoulder view & vice versa
fn toggle_x_offset(
mut cam_q: Query<&mut ThirdPersonCamera, With<ThirdPersonCamera>>,
mut cam_q: Query<&mut ThirdPersonCamera>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
btns: Query<&Gamepad>,
Expand Down
7 changes: 2 additions & 5 deletions src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn orbit_condition(cam_q: Query<&ThirdPersonCamera>) -> bool {
let Ok(cam) = cam_q.single() else {
return true;
};
return cam.cursor_lock_active;
cam.cursor_lock_active
}

// heavily referenced https://bevy-cheatbook.github.io/cookbook/pan-orbit-camera.html
Expand All @@ -49,10 +49,7 @@ pub fn orbit_mouse(

if rotation.length_squared() > 0.0 {
let window = window_q.single().unwrap();
let delta_x = {
let delta = rotation.x / window.width() * std::f32::consts::PI * cam.sensitivity.x;
delta
};
let delta_x = rotation.x / window.width() * std::f32::consts::PI * cam.sensitivity.x;

let delta_y = rotation.y / window.height() * PI * cam.sensitivity.y;
let yaw = Quat::from_rotation_y(-delta_x);
Expand Down