Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
fixed some glaring issues, disabled camera logic for now, added separ…
Browse files Browse the repository at this point in the history
…ate mouse sensitivity, fixed inverted y axis, added controller support to the menu
  • Loading branch information
doomy committed Dec 13, 2023
1 parent 55704a9 commit ce863d0
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 78 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ leafwing-input-manager = "0.11.1"
serde = "1.0.193"
image = { version = "0.24", features = ["png"] }
bevy_wasm_window_resize = "0.2.1"
bevy-egui-kbgp = "0.16.0"
7 changes: 4 additions & 3 deletions assets/default.settings.ron
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ SettingsAsset(
influence_radius: 5.0,
),
camera: (
sensitivity: (3.0, 1.5),
controller_sensitivity: (3.0, 1.5),
mouse_sensitivity: (5.0, 5.0),
fov: 90,
fov_multiplier_range: RangeInclusive( start: 1.2, end: 0.9 ),
angle_range: RangeInclusive( start: 160, end: 80 ),
distance: 7,
angle_range: RangeInclusive( start: 200, end: 270 ),
distance: 7.0,
)
),
ui: (
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{prelude::*, window::WindowMode};
use bevy_blast_ultra::prelude::*;
use bevy_easings::EasingsPlugin;
use bevy_egui::EguiPlugin;
use bevy_egui_kbgp::KbgpPlugin;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_wasm_window_resize::WindowResizePlugin;
use bevy_xpbd_3d::prelude::*;
Expand All @@ -25,6 +26,7 @@ fn main() {
PhysicsDebugPlugin::default(),
WorldInspectorPlugin::new().run_if(|debug: Res<DebugMode>| *debug == DebugMode::On),
WindowResizePlugin,
KbgpPlugin,
))
.insert_resource(DebugMode::Off)
.insert_resource(bevy_xpbd_3d::resources::Gravity(Vec3::NEG_Z * 50f32))
Expand Down
86 changes: 44 additions & 42 deletions src/plugins/camera.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::{input::mouse::MouseMotion, prelude::*};
use bevy_xpbd_3d::{
components::CollisionLayers,
plugins::spatial_query::{SpatialQuery, SpatialQueryFilter},
};
use bevy_xpbd_3d::prelude::*;
use leafwing_input_manager::prelude::*;

use crate::prelude::*;
Expand Down Expand Up @@ -68,17 +65,20 @@ fn orbit_camera_input(
if let Ok((mut orbit_camera, mut projection)) = camera.get_single_mut() {
if let Projection::Perspective(projection) = projection.as_mut() {
if let Some(look) = actions.clamped_axis_pair(Action::Look) {
orbit_camera.add_view_angle_normalized(
look.y() * camera_settings.sensitivity.y * time.delta_seconds(),
);
orbit_camera.add_view_angle_normalized(
mouse_motion.y.to_radians() * camera_settings.sensitivity.y,
);

orbit_camera.rotation -=
look.x() * camera_settings.sensitivity.x * time.delta_seconds();
orbit_camera.rotation -=
mouse_motion.x.to_radians() * camera_settings.sensitivity.x;
let sensitivity = if mouse_motion == Vec2::ZERO {
camera_settings.controller_sensitivity
} else {
camera_settings.mouse_sensitivity
};

orbit_camera
.add_view_angle_normalized(look.y() * sensitivity.y * time.delta_seconds());
orbit_camera
.add_view_angle_normalized(-mouse_motion.y.to_radians() * sensitivity.y);

orbit_camera.rotation -= look.x() * sensitivity.x * time.delta_seconds();

orbit_camera.rotation -= mouse_motion.x.to_radians() * sensitivity.x;
}
}
}
Expand All @@ -96,11 +96,13 @@ fn orbit_camera_movement(
if let (Ok(focus_transform), Ok((camera_entity, camera_transform, orbit_camera))) =
(focus.get_single(), camera.get_single())
{
// We have the focus translation, so we need the desired camera
// translation. Then, we will find the direction from the focus to the
// camera, and cast a ray. If the ray intersects with anything before
// the desired length, the camera will instead be placed there as to
// avoid clipping into things
// // TODO: Camera casting logic is probably hurting more than helping. Can
// // we instead fade objects in and out nicely when they're close to the camera?
// // We have the focus translation, so we need the desired camera
// // translation. Then, we will find the direction from the focus to the
// // camera, and cast a ray. If the ray intersects with anything before
// // the desired length, the camera will instead be placed there as to
// // avoid clipping into things
let mut cast = *focus_transform;
cast.rotation = Quat::from_axis_angle(Vec3::Z, orbit_camera.rotation);
cast.rotate_local_y(
Expand All @@ -110,28 +112,28 @@ fn orbit_camera_movement(
);

let mut new_camera_position = cast.forward();
// Cast a ray to the camera
if let Some(hit) = spatial.cast_ray(
cast.translation,
cast.forward(),
camera_settings.distance,
true,
SpatialQueryFilter::default().with_masks([Layer::Environment]),
) {
// Ensure we are not in a solid object
// TODO: This is not working :((((((
if hit.time_of_impact != 0.0 {
new_camera_position *= hit.time_of_impact;
// take off a little distance to keep the camera from intersecting
// the floor
// TODO: shape cast and take the middle so that this is not necessary
new_camera_position *= 0.80f32;
} else {
new_camera_position *= camera_settings.distance;
}
} else {
new_camera_position *= camera_settings.distance;
}
// // Cast a ray to the camera
// if let Some(hit) = spatial.cast_shape(
// &Collider::ball(0.1),
// cast.translation,
// default(),
// cast.forward(),
// camera_settings.distance,
// false,
// SpatialQueryFilter::default().with_masks([Layer::Environment]),
// ) {
// // Ensure we are not in a solid object
// // TODO: This is not working :((((((
// if hit.time_of_impact != 0.0 {
// new_camera_position *= hit.time_of_impact;
// } else {
// new_camera_position *= camera_settings.distance;
// }
// } else {
// new_camera_position *= camera_settings.distance;
// }

new_camera_position *= camera_settings.distance;

let mut camera_transform = *focus_transform;
camera_transform.translation += new_camera_position;
Expand Down
19 changes: 13 additions & 6 deletions src/plugins/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ fn spawn(
composite_mode: BloomCompositeMode::Additive,
..default()
},
ScreenSpaceAmbientOcclusionSettings {
quality_level: ScreenSpaceAmbientOcclusionQualityLevel::High,
},
));

// Get the map to spawn
Expand Down Expand Up @@ -219,8 +222,12 @@ fn process_colliders(
children: Query<&Children>,
mut cmd: Commands,
mut proxy_colliders: Query<
(Entity, &Collider, &Name, &mut Visibility),
(Without<XpbdCollider>, Added<Collider>, Without<Player>),
(Entity, &blender::Collider, &Name, &mut Visibility),
(
Without<XpbdCollider>,
Added<blender::Collider>,
Without<Player>,
),
>,
) {
// Replace physics entities
Expand All @@ -236,21 +243,21 @@ fn process_colliders(

let mut xpbd_collider: XpbdCollider;
match collider_proxy {
Collider::Ball(radius) => {
blender::Collider::Ball(radius) => {
xpbd_collider = XpbdCollider::ball(*radius);
cmd.entity(entity)
.insert((xpbd_collider, collision_layers, friction))
//.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes (also is there something like that in xpbd ?) !!!
;
}
Collider::Cuboid(size) => {
blender::Collider::Cuboid(size) => {
xpbd_collider = XpbdCollider::cuboid(size.x, size.y, size.z);
cmd.entity(entity)
.insert((xpbd_collider, collision_layers, friction))
//.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes (also is there something like that in xpbd ?) !!!
;
}
Collider::Capsule(a, b, radius) => {
blender::Collider::Capsule(a, b, radius) => {
// FIXME: temp
let height = Vec3::distance(*a, *b);
xpbd_collider = XpbdCollider::capsule(height, *radius);
Expand All @@ -260,7 +267,7 @@ fn process_colliders(
//.insert(ActiveEvents::COLLISION_EVENTS) // FIXME: this is just for demo purposes (also is there something like that in xpbd ?) !!!
;
}
Collider::Mesh => {
blender::Collider::Mesh => {
for (_, collider_mesh) in
Mesh::search_in_children(entity, &children, &meshes, &mesh_handles)
{
Expand Down
44 changes: 24 additions & 20 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl Plugin for InitPlugin {
fn build(&self, app: &mut App) {
app.add_state::<GameState>() // Register all types intended to be used either in Blender or to be saved
.register_type::<RigidBody>()
.register_type::<Collider>()
.register_type::<AutoCollider>()
.register_type::<blender::Collider>()
.register_type::<blender::AutoCollider>()
.register_type::<Player>()
.register_type::<PlayerInfluenceRadius>()
.register_type::<OrbitCamera>()
Expand All @@ -72,23 +72,27 @@ impl Plugin for InitPlugin {
}
}

/// Marker component for Blender compatibility. Replaced via [`physics_replace_proxies`].
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
pub enum Collider {
Ball(f32),
Cuboid(Vec3),
Capsule(Vec3, Vec3, f32),
#[default]
Mesh,
}
pub(crate) mod blender {
use super::*;

/// Marker component for Blender compatibility. Replaced via [`physics_replace_proxies`].
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
pub enum Collider {
Ball(f32),
Cuboid(Vec3),
Capsule(Vec3, Vec3, f32),
#[default]
Mesh,
}

/// Marker component for Blender compatibility. Replaced via [`physics_replace_proxies`].
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
pub enum AutoCollider {
#[default]
Cuboid,
Ball,
Capsule,
/// Marker component for Blender compatibility. Replaced via [`physics_replace_proxies`].
#[derive(Component, Reflect, Default, Debug)]
#[reflect(Component)]
pub enum AutoCollider {
#[default]
Cuboid,
Ball,
Capsule,
}
}
45 changes: 39 additions & 6 deletions src/plugins/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy_egui::{
egui::{self, epaint::Shadow, Color32, FontId, Frame, Stroke},
EguiContexts, EguiSettings,
};
use bevy_egui_kbgp::prelude::*;
use bevy_gltf_blueprints::GameWorldTag;
use bevy_xpbd_3d::plugins::debug::PhysicsDebugConfig;
use leafwing_input_manager::action_state::ActionState;
Expand All @@ -13,6 +14,11 @@ use super::input::Action;

pub struct UiPlugin;

#[derive(Clone)]
pub enum UiActions {
ToggleMenu,
}

impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, toggle_debug)
Expand All @@ -23,7 +29,18 @@ impl Plugin for UiPlugin {
)
.add_systems(Update, (game_ui,).run_if(in_state(GameState::Main)))
.add_systems(Update, post_game_ui.run_if(in_state(GameState::Post)))
.add_systems(Update, pause_menu_ui.run_if(in_state(GameState::Pause)));
.add_systems(Update, pause_menu_ui.run_if(in_state(GameState::Pause)))
.insert_resource(KbgpSettings {
bindings: {
bevy_egui_kbgp::KbgpNavBindings::default()
.with_wasd_navigation()
.with_arrow_keys_navigation()
.with_gamepad_dpad_navigation_and_south_button_activation()
.with_key(KeyCode::Escape, KbgpNavCommand::user(UiActions::ToggleMenu))
// .with_gamepad_button(GamepadButtonType::Start, KbgpNavCommand::user(UiActions::Menu))
},
..default()
});
}
}

Expand Down Expand Up @@ -127,12 +144,18 @@ fn menu_ui(
selected_level.0 == i,
format!("Level {i}"),
))
.kbgp_navigation()
.clicked()
{
selected_level.0 = i
}
});
if ui.button("Start").clicked() {
if ui
.button("Start")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
cmd.insert_resource(CurrentLevelIndex(selected_level.0));
state.set(GameState::LevelTransition {
level: level_paths[selected_level.0].clone(),
Expand Down Expand Up @@ -244,7 +267,12 @@ fn post_game_ui(
.font(FontId::proportional(font.size_title()))
.color(Color32::GREEN),
);
if ui.button("Next level").clicked() {
if ui
.button("Next level")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
current_level.0 += 1;
state.set(GameState::LevelTransition {
level: level.clone(),
Expand Down Expand Up @@ -296,17 +324,22 @@ fn pause_menu_ui(
),
|ui| {
ui.vertical_centered_justified(|ui| {
if ui.button("Resume").clicked() {
if ui
.button("Resume")
.kbgp_navigation()
.kbgp_initial_focus()
.clicked()
{
state.set(GameState::Main);
}

if ui.button("Restart level").clicked() {
if ui.button("Restart level").kbgp_navigation().clicked() {
state.set(GameState::LevelTransition {
level: assets.levels[current_level.0].clone(),
});
}

if ui.button("Main menu").clicked() {
if ui.button("Main menu").kbgp_navigation().clicked() {
state.set(GameState::Menu);
}
})
Expand Down
3 changes: 2 additions & 1 deletion src/resources/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub struct CameraSettings {
/// Permitted camera angles in degrees
pub angle_range: RangeInclusive<f32>,
pub distance: f32,
pub sensitivity: Vec2,
pub mouse_sensitivity: Vec2,
pub controller_sensitivity: Vec2,
pub fov: f32,
pub fov_multiplier_range: RangeInclusive<f32>,
}
Expand Down

0 comments on commit ce863d0

Please sign in to comment.