Skip to content

Commit

Permalink
started working on the camera movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietrek14 committed Jul 8, 2023
1 parent 9fc5115 commit 927f586
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;

use crate::actions::game_control::{get_movement, GameControl};
Expand All @@ -19,9 +20,14 @@ impl Plugin for ActionsPlugin {
#[derive(Default, Resource)]
pub struct Actions {
pub player_movement: Option<Vec2>,
pub camera_movement: Option<Vec2>,
}

pub fn set_movement_actions(mut actions: ResMut<Actions>, keyboard_input: Res<Input<KeyCode>>) {
pub fn set_movement_actions(
mut actions: ResMut<Actions>,
keyboard_input: Res<Input<KeyCode>>,
mut mouse_motion: EventReader<MouseMotion>,
) {
let player_movement = Vec2::new(
get_movement(GameControl::Right, &keyboard_input)
- get_movement(GameControl::Left, &keyboard_input),
Expand All @@ -34,4 +40,18 @@ pub fn set_movement_actions(mut actions: ResMut<Actions>, keyboard_input: Res<In
} else {
actions.player_movement = None;
}

if mouse_motion.is_empty() {
actions.camera_movement = None;
} else {
actions.camera_movement = Some({
let mut camera_movement = Vec2::ZERO;

for motion in mouse_motion.iter() {
camera_movement += motion.delta;
}

camera_movement
});
}
}
9 changes: 7 additions & 2 deletions src/ball/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, ui::UiSystem};

use crate::util::cleanup;
use crate::GameState;
Expand All @@ -12,7 +12,12 @@ impl Plugin for BallUiPlugin {
app.register_type::<BallUi>()
.register_type::<BallUiBar>()
.add_system(setup_ball_ui.in_schedule(OnEnter(GameState::Playing)))
.add_system(update_ball_ui.in_set(OnUpdate(GameState::Playing)))
.add_system(
update_ball_ui
.run_if(in_state(GameState::Playing))
.in_base_set(CoreSet::PostUpdate)
.after(UiSystem::Stack),
)
.add_system(cleanup::<BallUi>.in_schedule(OnEnter(GameState::Playing)));
}
}
Expand Down
82 changes: 82 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use bevy::{prelude::*, window::CursorGrabMode};
use bevy_rapier3d::prelude::*;

use crate::{actions::Actions, ball::Ball, util::cleanup, GameState};

// TODO: Make this a setting if there's time left.
const SENSITIVITY: f32 = 0.2;

pub struct CameraPlugin;

impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems((setup_camera, lock_cursor).in_schedule(OnEnter(GameState::Playing)))
.add_system(look_angles.in_set(OnUpdate(GameState::Playing)))
.add_system(cleanup::<Camera>.in_schedule(OnExit(GameState::Playing)));
}
}

#[derive(Component, Reflect, Clone, Copy, Debug, Default)]
#[reflect(Component)]
struct CameraControls {
pub radius: f32,
}

fn setup_camera(mut commands: Commands) {
commands
.spawn(Camera3dBundle {
transform: Transform::from_xyz(25., 7., 0.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(CameraControls { radius: 32. });
}

fn lock_cursor(mut windows: Query<&mut Window>) {
let mut window = windows.single_mut();

window.cursor.visible = false;
window.cursor.grab_mode = CursorGrabMode::Locked;
}

fn look_angles(
mut query: Query<(&mut Transform, &CameraControls)>,
target: Query<(Entity, &Transform), (With<Ball>, Without<CameraControls>)>,
actions: Res<Actions>,
time: Res<Time>,
rapier_context: Res<RapierContext>,
) {
if let Ok((ball, target)) = target.get_single() {
let delta = actions.camera_movement.unwrap_or(Vec2::ZERO);
let delta = delta * SENSITIVITY * time.delta_seconds();

let (mut transform, camera_controls) = query.single_mut();

let yaw = Quat::from_rotation_y(-delta.x);
let pitch = Quat::from_rotation_x(-delta.y);

transform.rotation = yaw * transform.rotation;
transform.rotation = transform.rotation * pitch;

// TODO: Clamp y rotation

transform.rotation = transform.rotation.normalize();

let ray_direction = -transform.forward().normalize();

let camera_position = if let Some((_entity, toi)) = rapier_context.cast_ray(
target.translation,
ray_direction,
camera_controls.radius,
false,
QueryFilter::new().exclude_collider(ball),
) {
let hit_point = target.translation + ray_direction * toi;

hit_point
} else {
target.translation + ray_direction * camera_controls.radius
};

transform.translation = camera_position;
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod actions;
mod ball;
mod camera;
mod level;
mod light;
mod loading;
Expand All @@ -21,6 +22,7 @@ use bevy_rapier3d::{
prelude::{NoUserData, RapierPhysicsPlugin},
render::RapierDebugRenderPlugin,
};
use camera::CameraPlugin;
use level::LevelPlugin;
use light::LightPlugin;

Expand All @@ -47,6 +49,7 @@ impl Plugin for GamePlugin {
.add_plugin(ActionsPlugin)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(LevelPlugin)
.add_plugin(CameraPlugin)
.add_plugin(AudioPlugin)
.add_plugin(LightPlugin)
.add_plugin(MenuPlugin)
Expand Down
8 changes: 3 additions & 5 deletions src/menu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::loading::FontAssets;
use crate::GameState;
use crate::util::cleanup;
use bevy::prelude::*;

pub struct MenuPlugin;
Expand All @@ -11,7 +12,7 @@ impl Plugin for MenuPlugin {
app.init_resource::<ButtonColors>()
.add_system(setup_menu.in_schedule(OnEnter(GameState::Menu)))
.add_system(click_play_button.in_set(OnUpdate(GameState::Menu)))
.add_system(cleanup_menu.in_schedule(OnExit(GameState::Menu)));
.add_systems((cleanup_menu, cleanup::<Camera2d>).in_schedule(OnExit(GameState::Menu)));
}
}

Expand All @@ -35,10 +36,7 @@ fn setup_menu(
font_assets: Res<FontAssets>,
button_colors: Res<ButtonColors>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 6., 25.).looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
..default()
});
commands.spawn(Camera2dBundle::default());
commands
.spawn(ButtonBundle {
style: Style {
Expand Down

0 comments on commit 927f586

Please sign in to comment.