Skip to content

Commit

Permalink
Merge pull request #5 from MaikoVDV/2d-controls
Browse files Browse the repository at this point in the history
Apologies for the long wait. I haven't used Bevy in a while. Thank you for your contribution.
  • Loading branch information
JonahPlusPlus authored Nov 10, 2023
2 parents f4d05fd + a3984b1 commit 38d21d5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bevy = { version = "0.12", default-features = false, features = [
"bevy_core_pipeline",
"bevy_pbr",
"bevy_render",
"bevy_sprite",
"x11",
"ktx2",
"zstd",
Expand Down
26 changes: 26 additions & 0 deletions examples/2d_scene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! A simple 2D scene using orthographic controls.

use bevy::prelude::*;
use bevy_spectator::*;

fn main() {
App::new()
.insert_resource(SpectatorSettings {
orthographic: true,
..default()
})
.add_plugins((DefaultPlugins, SpectatorPlugin))
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn((Camera2dBundle::default(), Spectator));
commands.spawn(SpriteBundle {
transform: Transform {
scale: Vec3::new(10.0, 10.0, 1.0),
..default()
},
..default()
});
}
3 changes: 1 addition & 2 deletions examples/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ fn main() {
sensitivity: 0.0015,
..default()
})
.add_plugins(DefaultPlugins)
.add_plugins(SpectatorPlugin)
.add_plugins((DefaultPlugins, SpectatorPlugin))
.add_systems(Startup, setup)
.run();
}
Expand Down
44 changes: 29 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ fn spectator_update(

let mut set_focus = |focused: bool| {
*focus = focused;
let grab_mode = match focused {
true => CursorGrabMode::Confined,
false => CursorGrabMode::None,
};
window.cursor.grab_mode = grab_mode;
window.cursor.visible = !focused;
if !settings.orthographic {
let grab_mode = match focused {
true => CursorGrabMode::Confined,
false => CursorGrabMode::None,
};
window.cursor.grab_mode = grab_mode;
window.cursor.visible = !focused;
}
};

if keys.just_pressed(KeyCode::Escape) {
Expand All @@ -117,9 +119,10 @@ fn spectator_update(
set_focus(true);
}

if *focus {
// When in orthographic mode, mouse capturing is disabled. Movement should therefore always be enabled.
if *focus || settings.orthographic {
// rotation
{
if !settings.orthographic {
let mouse_delta = {
let mut total = Vec2::ZERO;
for d in motion.read() {
Expand Down Expand Up @@ -147,24 +150,30 @@ fn spectator_update(
let backward = if keys.pressed(KeyCode::S) { 1f32 } else { 0f32 };
let right = if keys.pressed(KeyCode::D) { 1f32 } else { 0f32 };
let left = if keys.pressed(KeyCode::A) { 1f32 } else { 0f32 };
let up = if keys.pressed(KeyCode::Space) {
1f32
let up_cond = if !settings.orthographic {
keys.pressed(KeyCode::Space)
} else {
0f32
keys.pressed(KeyCode::W)
};
let down = if keys.pressed(KeyCode::ControlLeft) {
1f32
let up = if up_cond { 1f32 } else { 0f32 };
let down_cond = if !settings.orthographic {
keys.pressed(KeyCode::ControlLeft)
} else {
0f32
keys.pressed(KeyCode::S)
};
let down = if down_cond { 1f32 } else { 0f32 };

let speed = if keys.pressed(KeyCode::ShiftLeft) {
settings.alt_speed
} else {
settings.base_speed
};

let delta_axial = (forward - backward) * speed;
let delta_axial = if settings.orthographic {
0.0
} else {
(forward - backward) * speed
};
let delta_lateral = (right - left) * speed;
let delta_vertical = (up - down) * speed;

Expand Down Expand Up @@ -214,6 +223,10 @@ pub struct SpectatorSettings {
///
/// Use this to control how fast the [`Spectator`] turns when you move the mouse.
pub sensitivity: f32,
/// Use a control scheme more fit for orthographic (2D) rendering
///
/// Disables mouse capturing and hiding, prevents moving along z-axis and uses W and S for y-axis movement
pub orthographic: bool,
}

impl Default for SpectatorSettings {
Expand All @@ -224,6 +237,7 @@ impl Default for SpectatorSettings {
base_speed: 10.0,
alt_speed: 50.0,
sensitivity: 0.001,
orthographic: false,
}
}
}

0 comments on commit 38d21d5

Please sign in to comment.