Look in the direction of the camera

This commit is contained in:
timokoesters 2019-08-24 19:58:28 +02:00
parent 39bd888a7c
commit e90f95bc75
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
3 changed files with 19 additions and 16 deletions

View File

@ -5,6 +5,7 @@ use vek::*;
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Controller {
pub move_dir: Vec2<f32>,
pub look_dir: Vec3<f32>,
pub jump: bool,
pub attack: bool,
pub block: bool,

View File

@ -94,11 +94,18 @@ impl<'a> System<'a> for Sys {
};
// Set direction based on move direction when on the ground
let ori_dir = if character.movement == Glide || character.movement.is_roll() {
let ori_dir = if controller
.look_dir
.map(|n| !n.is_normal() || n.abs() < std::f32::EPSILON)
.reduce_or()
|| character.movement == Glide
|| character.movement.is_roll()
{
Vec2::from(vel.0)
} else {
controller.move_dir
Vec2::from(controller.look_dir).normalized()
};
if ori_dir.magnitude_squared() > 0.0001
&& (ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared()
> 0.001

View File

@ -105,19 +105,19 @@ impl PlayState for SessionState {
self.client.borrow_mut().send_chat(cmd.to_string());
}
}
// Compute camera data
let get_cam_data = |camera: &Camera, client: &Client| {
let (view_mat, _, cam_pos) = camera.compute_dependents(client);
let cam_dir: Vec3<f32> = Vec3::from(view_mat.inverted() * -Vec4::unit_z());
(cam_dir, cam_pos)
};
// Game loop
let mut current_client_state = self.client.borrow().get_client_state();
while let ClientState::Pending | ClientState::Character | ClientState::Dead =
current_client_state
{
// Compute camera data
let (view_mat, _, cam_pos) = self
.scene
.camera()
.compute_dependents(&self.client.borrow());
let cam_dir: Vec3<f32> = Vec3::from(view_mat.inverted() * -Vec4::unit_z());
// Handle window events.
for event in global_state.window.fetch_events() {
// Pass all events to the ui first.
@ -142,8 +142,6 @@ impl PlayState for SessionState {
.get(client.entity())
.is_some()
{
let (cam_dir, cam_pos) = get_cam_data(&self.scene.camera(), &client);
let (d, b) = {
let terrain = client.state().terrain();
let ray = terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast();
@ -169,8 +167,6 @@ impl PlayState for SessionState {
.get(client.entity())
.is_some()
{
let (cam_dir, cam_pos) = get_cam_data(&self.scene.camera(), &client);
let (d, b) = {
let terrain = client.state().terrain();
let ray = terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast();
@ -194,9 +190,6 @@ impl PlayState for SessionState {
.is_some()
{
if state {
let (cam_dir, cam_pos) =
get_cam_data(&self.scene.camera(), &client);
if let Ok(Some(block)) = client
.state()
.terrain()
@ -268,6 +261,8 @@ impl PlayState for SessionState {
let dir_vec = self.key_state.dir_vec();
self.controller.move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1];
self.controller.look_dir = cam_dir;
// Perform an in-game tick.
if let Err(err) = self.tick(clock.get_avg_delta()) {
error!("Failed to tick the scene: {:?}", err);