From cf48db6050e4fd2cf8b0f103f2eda06bb91e127b Mon Sep 17 00:00:00 2001
From: Imbris <imbrisf@gmail.com>
Date: Wed, 25 Mar 2020 18:40:07 -0400
Subject: [PATCH] Replace uses of normalized() to avoid setting ori to NaN
 values, tweak triple strike code

---
 common/src/states/dash_melee.rs    |  4 ++-
 common/src/states/triple_strike.rs | 43 ++++++++++++++----------------
 common/src/sys/projectile.rs       |  7 +++--
 3 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/common/src/states/dash_melee.rs b/common/src/states/dash_melee.rs
index c027b9f113..b3d67a8256 100644
--- a/common/src/states/dash_melee.rs
+++ b/common/src/states/dash_melee.rs
@@ -28,7 +28,9 @@ impl CharacterBehavior for Data {
 
         if self.initialize {
             update.vel.0 = data.inputs.look_dir * 20.0;
-            update.ori.0 = Vec3::from(data.vel.0.xy()).normalized();
+            if let Some(dir) = Vec3::from(data.vel.0.xy()).try_normalized() {
+                update.ori.0 = dir;
+            }
         }
 
         if self.buildup_duration != Duration::default() && data.physics.touch_entity.is_none() {
diff --git a/common/src/states/triple_strike.rs b/common/src/states/triple_strike.rs
index 01b12be422..72e80ef453 100644
--- a/common/src/states/triple_strike.rs
+++ b/common/src/states/triple_strike.rs
@@ -54,13 +54,14 @@ impl CharacterBehavior for Data {
         let should_transition = data.inputs.primary.is_pressed() && self.should_transition;
 
         if !self.initialized {
-            update.ori.0 = data.inputs.look_dir.normalized();
             update.vel.0 = Vec3::zero();
+            if let Some(dir) = data.inputs.look_dir.try_normalized() {
+                update.ori.0 = dir;
+            }
         }
         let initialized = true;
 
         // Handling movement
-
         if stage_time_active < Duration::from_millis(STAGE_DURATION / 3) {
             let adjusted_accel = match (self.stage, data.physics.touch_entity.is_none()) {
                 (Stage::First, true) => INITIAL_ACCEL,
@@ -69,7 +70,7 @@ impl CharacterBehavior for Data {
                 (_, _) => 0.0,
             };
 
-            // Move player forward while in first third of first stage
+            // Move player forward while in first third of each stage
             if update.vel.0.magnitude_squared() < BASE_SPEED.powf(2.0) {
                 update.vel.0 =
                     update.vel.0 + Vec2::broadcast(data.dt.0) * data.ori.0 * adjusted_accel;
@@ -111,26 +112,22 @@ impl CharacterBehavior for Data {
                 initialized,
             })
         } else if stage_time_active > Duration::from_millis(STAGE_DURATION) {
-            if should_transition {
-                if let Stage::Third = self.stage {
-                    // Make sure attack component is removed
-                    data.updater.remove::<Attacking>(data.entity);
-                    // Done
-                    CharacterState::Wielding
-                } else {
-                    CharacterState::TripleStrike(Data {
-                        base_damage: self.base_damage,
-                        stage: match self.stage {
-                            Stage::First => Stage::Second,
-                            Stage::Second => Stage::Third,
-                            Stage::Third => Stage::First,
-                        },
-                        stage_time_active: Duration::default(),
-                        stage_exhausted: false,
-                        should_transition,
-                        initialized,
-                    })
-                }
+            let next_stage = match self.stage {
+                _ if !should_transition => None,
+                Stage::First => Some(Stage::Second),
+                Stage::Second => Some(Stage::Third),
+                Stage::Third => None,
+            };
+
+            if let Some(stage) = next_stage {
+                CharacterState::TripleStrike(Data {
+                    base_damage: self.base_damage,
+                    stage,
+                    stage_time_active: Duration::default(),
+                    stage_exhausted: false,
+                    should_transition,
+                    initialized,
+                })
             } else {
                 // Make sure attack component is removed
                 data.updater.remove::<Attacking>(data.entity);
diff --git a/common/src/sys/projectile.rs b/common/src/sys/projectile.rs
index 4a8b15dd7b..e09050cc0f 100644
--- a/common/src/sys/projectile.rs
+++ b/common/src/sys/projectile.rs
@@ -110,8 +110,11 @@ impl<'a> System<'a> for Sys {
                     }
                 }
             } else {
-                if let Some(vel) = velocities.get(entity) {
-                    ori.0 = vel.0.normalized();
+                if let Some(dir) = velocities
+                    .get(entity)
+                    .and_then(|vel| vel.0.try_normalized())
+                {
+                    ori.0 = dir;
                 }
             }