Skip to content

Commit

Permalink
add crosshair
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Sep 3, 2024
1 parent 2341e1d commit 4cade00
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
4 changes: 3 additions & 1 deletion crates/unavi-player/src/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use avian3d::prelude::*;
use bevy::{prelude::*, render::view::RenderLayers};
use bevy_tnua::prelude::*;
use bevy_tnua_avian3d::TnuaAvian3dSensorShape;
use bevy_vrm::{
first_person::{FirstPersonFlag, SetupFirstPerson, RENDER_LAYERS},
loader::Vrm,
Expand Down Expand Up @@ -48,10 +49,11 @@ pub(crate) fn spawn_player(asset_server: Res<AssetServer>, mut commands: Command
memberships: LOCAL_PLAYER_LAYER,
..default()
},
LinearVelocity::default(),
LocalPlayer::default(),
RigidBody::Dynamic,
TnuaControllerBundle::default(),
TnuaAvian3dSensorShape(Collider::cylinder((PLAYER_WIDTH / 2.0) * 0.95, 0.0)),
LockedAxes::ROTATION_LOCKED,
SpatialBundle {
global_transform: GlobalTransform::from_translation(SPAWN),
..default()
Expand Down
14 changes: 4 additions & 10 deletions crates/unavi-player/src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@ pub fn move_player(
time: Res<Time>,
) {
for (transform, mut player, mut controller) in players.iter_mut() {
let dir_forward = transform.rotation.mul_vec3(Vec3 {
x: 0.0,
y: 0.0,
z: -1.0,
});
let dir_left = transform.rotation.mul_vec3(Vec3 {
x: -1.0,
y: 0.0,
z: 0.0,
});
let dir_forward = transform.rotation.mul_vec3(Vec3::NEG_Z);
let dir_left = transform.rotation.mul_vec3(Vec3::NEG_X);

let mut move_direction = Vec3::ZERO;

Expand Down Expand Up @@ -89,6 +81,8 @@ pub fn void_teleport(
linvel.x = 0.0;
linvel.y = 0.0;
linvel.z = 0.0;

// TODO: Reset camera rotation
}
}
}
52 changes: 33 additions & 19 deletions crates/unavi-player/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,49 @@ pub fn read_keyboard_input(
}
}

const RAYCAST_DISTANCE: f32 = 10.0;
const RAYCAST_DISTANCE: f32 = 5.0;
const CROSSHAIR_RADIUS: f32 = 0.008;

pub fn handle_raycast_input(
camera: Query<&GlobalTransform, With<PlayerCamera>>,
input_handlers: Query<(Entity, &InputHandlerSender)>,
mouse: Res<ButtonInput<MouseButton>>,
nodes: Query<(Entity, &InputHandlerSender)>,
mut gizmos: Gizmos,
query: SpatialQuery,
) {
if camera.is_empty() {
return;
}

if mouse.just_pressed(MouseButton::Left) {
let transform = camera.single();
let (_, rotation, translation) = transform.to_scale_rotation_translation();
let transform = camera.single();
let (_, rotation, translation) = transform.to_scale_rotation_translation();

let direction = rotation.normalize() * Dir3::NEG_Z;
let direction = rotation.normalize() * Dir3::NEG_Z;

if let Some(hit) = query.cast_ray(
translation,
direction,
RAYCAST_DISTANCE,
false,
SpatialQueryFilter {
mask: OTHER_PLAYER_LAYER | WORLD_LAYER,
..default()
},
) {
for (ent, handler) in nodes.iter() {
if let Some(hit) = query.cast_ray(
translation,
direction,
RAYCAST_DISTANCE,
false,
SpatialQueryFilter {
mask: OTHER_PLAYER_LAYER | WORLD_LAYER,
..default()
},
) {
// Crosshair.
// TODO: Showing double on non-60hz monitors
if let Ok(normal) = Dir3::from_xyz(hit.normal.x, hit.normal.y, hit.normal.z) {
gizmos.circle(
translation + direction * hit.time_of_impact,
normal,
CROSSHAIR_RADIUS,
Color::WHITE,
);
}

// Script input.
if mouse.just_pressed(MouseButton::Left) {
for (ent, handler) in input_handlers.iter() {
// TODO: Recursive check if children were hit.

if hit.entity == ent {
Expand All @@ -106,6 +120,6 @@ pub fn handle_raycast_input(
break;
}
}
};
}
}
};
}
14 changes: 8 additions & 6 deletions crates/unavi-player/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ impl Plugin for PlayerPlugin {
body::id_player_bones,
body::set_avatar_head,
body::setup_first_person,
input::handle_raycast_input,
input::read_keyboard_input,
look::grab_mouse,
(
(look::read_mouse_input, look::apply_camera_look).chain(),
(
input::read_keyboard_input,
(look::read_mouse_input, look::apply_camera_look).chain(),
),
(
(controls::void_teleport, controls::move_player).chain(),
(
controls::void_teleport,
controls::move_player.before(input::read_keyboard_input),
)
.chain(),
body::rotate_avatar_head,
input::handle_raycast_input,
),
)
.chain(),
Expand Down
2 changes: 1 addition & 1 deletion crates/unavi-world-server/src/update_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use tokio::sync::mpsc::{error::SendError, UnboundedReceiver, UnboundedSender};
use tracing::debug;

pub const TICKRATE: f32 = 1.0 / 20.0;
pub const TICKRATE: f32 = 1.0 / 30.0;

#[derive(Debug)]
pub struct IncomingEvent {
Expand Down
7 changes: 5 additions & 2 deletions wasm/unavi-vscreen/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
module::Module,
};

const ANIMATION_DURATION_SECONDS: f32 = 0.25;
const ANIMATION_DURATION_SECONDS: f32 = 0.4;
const ARM_RADIUS: f32 = 0.03; // TODO: Get from avatar.
const SCREEN_HEIGHT: f32 = 0.002;
const SCREEN_RADIUS: f32 = 0.04;
Expand All @@ -29,7 +29,10 @@ pub struct Screen {

impl GuestScreen for Screen {
fn new() -> Self {
let root = Cylinder::new(SCREEN_RADIUS, SCREEN_HEIGHT).to_node();
let cylinder = Cylinder::new(SCREEN_RADIUS, SCREEN_HEIGHT);
cylinder.set_resolution(32);

let root = cylinder.to_node();
root.set_transform(Transform {
translation: Vec3::new(SCREEN_RADIUS * 2.0, ARM_RADIUS, -ARM_RADIUS / 3.0),
rotation: Quat::default(),
Expand Down

0 comments on commit 4cade00

Please sign in to comment.