Moved normalization of direction vectors from physics to character state system

This commit is contained in:
Sam 2022-09-26 21:34:44 -04:00 committed by Joshua Yanovski
parent 7c5917e9a9
commit 8383a72818
6 changed files with 32 additions and 26 deletions

View File

@ -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<f32>,
dir: Option<Dir>,
accel: f32,
},
Swim {
dir: Vec3<f32>,
dir: Option<Dir>,
accel: f32,
},
Leap {
dir: Vec2<f32>,
dir: Option<Dir>,
vertical: f32,
forward: f32,
progress: f32,
},
Ground {
dir: Vec2<f32>,
dir: Option<Dir>,
accel: f32,
},
Climb {
dir: Vec3<f32>,
dir: Option<Dir>,
accel: f32,
},
Teleport {
pos: Vec3<f32>,
},
Boost {
dir: Vec3<f32>,
dir: Option<Dir>,
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 } => {

View File

@ -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),

View File

@ -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 {

View File

@ -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

View File

@ -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 }
});

View File

@ -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 });