From 8383a72818a836fa66ddf846b6ddf6942bc47536 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 26 Sep 2022 21:34:44 -0400 Subject: [PATCH] Moved normalization of direction vectors from physics to character state system --- common/src/comp/movement.rs | 25 +++++++++++++------------ common/src/states/boost.rs | 5 +++-- common/src/states/climb.rs | 4 ++-- common/src/states/skate.rs | 4 +++- common/src/states/spin_melee.rs | 4 +++- common/src/states/utils.rs | 16 ++++++++-------- 6 files changed, 32 insertions(+), 26 deletions(-) diff --git a/common/src/comp/movement.rs b/common/src/comp/movement.rs index 51a05b3430..aabcc4717a 100644 --- a/common/src/comp/movement.rs +++ b/common/src/comp/movement.rs @@ -2,6 +2,7 @@ use crate::{ comp::{Ori, Pos, Vel}, consts::GRAVITY, resources::DeltaTime, + util::Dir, }; use serde::{Deserialize, Serialize}; use specs::{Component, DerefFlaggedStorage}; @@ -22,32 +23,32 @@ pub enum MovementKind { }, Flight { lift: f32, - dir: Vec2, + dir: Option, accel: f32, }, Swim { - dir: Vec3, + dir: Option, accel: f32, }, Leap { - dir: Vec2, + dir: Option, vertical: f32, forward: f32, progress: f32, }, Ground { - dir: Vec2, + dir: Option, accel: f32, }, Climb { - dir: Vec3, + dir: Option, accel: f32, }, Teleport { pos: Vec3, }, Boost { - dir: Vec3, + dir: Option, accel: f32, }, ChangeSpeed { @@ -79,12 +80,12 @@ impl MovementState { vel.0.z += dt.0 * lift; }, MovementKind::Flight { lift, dir, accel } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| d.xy()).unwrap_or_default(); vel.0.z += dt.0 * lift; vel.0 += dir * accel * dt.0; }, MovementKind::Swim { dir, accel } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| *d).unwrap_or_default(); vel.0 += dir * accel * dt.0; }, MovementKind::Leap { @@ -93,7 +94,7 @@ impl MovementState { forward, progress, } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| d.xy()).unwrap_or_default(); let progress = progress.clamp(0.0, 1.0); // TODO: Make this += instead of =, will require changing magnitude of strengths // probably, and potentially other behavior too Multiplication @@ -101,17 +102,17 @@ impl MovementState { vel.0 = dir.mul(forward).with_z(vertical * progress * 2.0); }, MovementKind::Ground { dir, accel } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| d.xy()).unwrap_or_default(); vel.0 += dir * accel * dt.0; }, MovementKind::Climb { dir, accel } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| *d).unwrap_or_default(); vel.0.z += GRAVITY * dt.0; vel.0 += dir * accel * dt.0; }, MovementKind::Teleport { pos: new_pos } => pos.0 = new_pos, MovementKind::Boost { dir, accel } => { - let dir = dir.try_normalized().unwrap_or_default(); + let dir = dir.map(|d| *d).unwrap_or_default(); vel.0 += dir * accel * dt.0; }, MovementKind::ChangeSpeed { speed } => { diff --git a/common/src/states/boost.rs b/common/src/states/boost.rs index 431e96ee5b..e175f62cbe 100644 --- a/common/src/states/boost.rs +++ b/common/src/states/boost.rs @@ -5,6 +5,7 @@ use crate::{ utils::*, wielding, }, + util::Dir, }; use serde::{Deserialize, Serialize}; use std::time::Duration; @@ -37,11 +38,11 @@ impl CharacterBehavior for Data { if self.timer < self.static_data.movement_duration { // Movement - let dir = if self.static_data.only_up { + let dir = Dir::from_unnormalized(if self.static_data.only_up { Vec3::unit_z() } else { *data.inputs.look_dir - }; + }); update.movement = update.movement.with_movement(MovementKind::Boost { dir, accel: self.static_data.speed }); update.character = CharacterState::Boost(Data { timer: tick_attack_or_default(data, self.timer, None), diff --git a/common/src/states/climb.rs b/common/src/states/climb.rs index 4028f29377..11ddb85530 100644 --- a/common/src/states/climb.rs +++ b/common/src/states/climb.rs @@ -113,11 +113,11 @@ impl CharacterBehavior for Data { }; // Apply Vertical Climbing Movement - let dir = data.inputs.move_dir.with_z(match climb { + let dir = Dir::from_unnormalized(data.inputs.move_dir.with_z(match climb { Climb::Down => -1.0, Climb::Up => 1.0, Climb::Hold => 0.0, - }); + })); let accel = if data.vel.0.magnitude_squared() < self.static_data.movement_speed.powi(2) { self.static_data.movement_speed.powi(2) } else { diff --git a/common/src/states/skate.rs b/common/src/states/skate.rs index 385b59e291..717ad0857f 100644 --- a/common/src/states/skate.rs +++ b/common/src/states/skate.rs @@ -8,6 +8,7 @@ use crate::{ behavior::{CharacterBehavior, JoinData}, idle, }, + util::Dir, }; use serde::{Deserialize, Serialize}; @@ -79,7 +80,8 @@ impl CharacterBehavior for Data { if let CharacterState::Skate(skate_data) = &mut update.character { skate_data.turn = orthogonal.dot(data.vel.0.xy()); } - update.movement = update.movement.with_movement(MovementKind::Ground { dir: data.inputs.move_dir, accel: acceleration }).with_ori_update(OriUpdate::New(new_ori)); + let dir = Dir::from_unnormalized(data.inputs.move_dir.with_z(0.0)); + update.movement = update.movement.with_movement(MovementKind::Ground { dir, accel: acceleration }).with_ori_update(OriUpdate::New(new_ori)); } update diff --git a/common/src/states/spin_melee.rs b/common/src/states/spin_melee.rs index 10f21834a0..36a3997ea1 100644 --- a/common/src/states/spin_melee.rs +++ b/common/src/states/spin_melee.rs @@ -6,6 +6,7 @@ use crate::{ utils::*, wielding, }, + util::Dir, }; use serde::{Deserialize, Serialize}; use std::time::Duration; @@ -64,7 +65,8 @@ impl CharacterBehavior for Data { update.movement = update.movement.with_movement(if data.physics.on_ground.is_some() { // TODO: Just remove axehover entirely with axe rework, it's really janky // TODO: Should 5 even be used here, or should body accel be used? Maybe just call handle_move? - MovementKind::Ground { dir: data.inputs.move_dir, accel: 5.0 } + let dir = Dir::from_unnormalized(data.inputs.move_dir.with_z(0.0)); + MovementKind::Ground { dir, accel: 5.0 } } else { MovementKind::SlowFall { lift: GRAVITY * 0.5 } }); diff --git a/common/src/states/utils.rs b/common/src/states/utils.rs index 8a3283be64..38320a491c 100644 --- a/common/src/states/utils.rs +++ b/common/src/states/utils.rs @@ -370,11 +370,11 @@ fn basic_move(data: &JoinData<'_>, update: &mut StateUpdate, efficiency: f32) { } * efficiency; // Should ability to backpedal be separate from ability to strafe? - let dir = if data.body.can_strafe() { + let dir = Dir::from_unnormalized(if data.body.can_strafe() { data.inputs.move_dir } else { Vec2::from(*data.ori) - }; + }.with_z(0.0)); let accel_mod = if is_strafing(data, update) { Lerp::lerp( Vec2::from(*data.ori) @@ -393,7 +393,7 @@ fn basic_move(data: &JoinData<'_>, update: &mut StateUpdate, efficiency: f32) { data.body.reverse_move_factor(), ) } else { - data.inputs.move_dir.dot(dir).max(0.0) + data.inputs.move_dir.dot(dir.unwrap_or_default().xy()).max(0.0) }; let accel = accel * accel_mod; @@ -416,7 +416,7 @@ pub fn handle_forced_movement( // TODO: Remove * 2.0 with changes made in sword branch, added as hack for now to keep same behavior let accel = accel * strength * 2.0; // TODO: Remove move_dir portion with changes made in sword branch, added as hack for now to keep same behavior - let dir = data.inputs.move_dir * 0.5 + Vec2::from(*data.ori) * 1.5; + let dir = Dir::from_unnormalized((data.inputs.move_dir * 0.5 + Vec2::from(*data.ori) * 1.5).with_z(0.0)); update.movement = update.movement.with_movement(MovementKind::Ground { dir, accel }); } }, @@ -426,7 +426,7 @@ pub fn handle_forced_movement( progress, direction, } => { - let dir = direction.get_2d_dir(data); + let dir = Dir::from_unnormalized(direction.get_2d_dir(data).with_z(0.0)); // Control forward movement based on look direction. // This allows players to stop moving forward when they // look downward at target @@ -532,7 +532,7 @@ fn swim_move( data.inputs.move_z }; - let dir = Vec3::new(dir.x, dir.y, move_z); + let dir = Dir::from_unnormalized(Vec3::new(dir.x, dir.y, move_z)); let accel = water_accel * (submersion - 0.2).clamp(0.0, 1.0).powi(2); update.movement = update.movement.with_movement(MovementKind::Swim { dir, accel }); @@ -595,12 +595,12 @@ pub fn fly_move(data: &JoinData<'_>, update: &mut StateUpdate, efficiency: f32) _ => 0.0, }; - let dir = if data.body.can_strafe() { + let dir = Dir::from_unnormalized(if data.body.can_strafe() { data.inputs.move_dir } else { let fw = Vec2::from(*data.ori); fw * data.inputs.move_dir.dot(fw).max(0.0) - }; + }.with_z(0.0)); update.movement = update.movement.with_movement(MovementKind::Flight { lift, dir, accel });