From e4cc85c413eed01950bf9dddf0bdac2afd8ab479 Mon Sep 17 00:00:00 2001 From: uwee Date: Tue, 14 Jul 2020 12:44:52 -0400 Subject: [PATCH] fix: Misc code smells (#2094) * fix: code smell - condensed if statement * fix: code smell - dont throw generic Exception --- .../Experimental/NetworkTransformBase.cs | 22 +++++++------------ Assets/Mirror/Components/NetworkAnimator.cs | 5 ++--- .../Examples/Tanks/Scripts/TankGameManager.cs | 10 ++++----- Assets/Mirror/Runtime/NetworkIdentity.cs | 2 +- .../Runtime/Transport/MultiplexTransport.cs | 4 ++-- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/Assets/Mirror/Components/Experimental/NetworkTransformBase.cs b/Assets/Mirror/Components/Experimental/NetworkTransformBase.cs index 10107c731..bd33d08b3 100644 --- a/Assets/Mirror/Components/Experimental/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/Experimental/NetworkTransformBase.cs @@ -112,13 +112,10 @@ public struct DataPoint void FixedUpdate() { // if server then always sync to others. - if (isServer) + // let the clients know that this has moved + if (isServer && HasEitherMovedRotatedScaled()) { - // let the clients know that this has moved - if (HasEitherMovedRotatedScaled()) - { - RpcMove(targetTransform.localPosition, targetTransform.localRotation, targetTransform.localScale); - } + RpcMove(targetTransform.localPosition, targetTransform.localRotation, targetTransform.localScale); } if (isClient) @@ -127,15 +124,12 @@ void FixedUpdate() // -> only if connectionToServer has been initialized yet too if (IsOwnerWithClientAuthority) { - if (!isServer) + if (!isServer && HasEitherMovedRotatedScaled()) { - if (HasEitherMovedRotatedScaled()) - { - // serialize - // local position/rotation for VR support - // send to server - CmdClientToServerSync(targetTransform.localPosition, targetTransform.localRotation, targetTransform.localScale); - } + // serialize + // local position/rotation for VR support + // send to server + CmdClientToServerSync(targetTransform.localPosition, targetTransform.localRotation, targetTransform.localScale); } } else if (goal.isValid) diff --git a/Assets/Mirror/Components/NetworkAnimator.cs b/Assets/Mirror/Components/NetworkAnimator.cs index 23905d80d..912f8d61c 100644 --- a/Assets/Mirror/Components/NetworkAnimator.cs +++ b/Assets/Mirror/Components/NetworkAnimator.cs @@ -189,10 +189,9 @@ void HandleAnimMsg(int stateHash, float normalizedTime, int layerId, NetworkRead // usually transitions will be triggered by parameters, if not, play anims directly. // NOTE: this plays "animations", not transitions, so any transitions will be skipped. // NOTE: there is no API to play a transition(?) - if (stateHash != 0) + if (stateHash != 0 && animator.enabled) { - if (animator.enabled) - animator.Play(stateHash, layerId, normalizedTime); + animator.Play(stateHash, layerId, normalizedTime); } ReadParameters(reader); diff --git a/Assets/Mirror/Examples/Tanks/Scripts/TankGameManager.cs b/Assets/Mirror/Examples/Tanks/Scripts/TankGameManager.cs index abb2cbcf5..2bb3a4051 100644 --- a/Assets/Mirror/Examples/Tanks/Scripts/TankGameManager.cs +++ b/Assets/Mirror/Examples/Tanks/Scripts/TankGameManager.cs @@ -66,13 +66,11 @@ void GameReadyCheck() foreach (KeyValuePair kvp in NetworkIdentity.spawned) { Tank comp = kvp.Value.GetComponent(); - if (comp != null) + + //Add if new + if (comp != null && !players.Contains(comp)) { - if (!players.Contains(comp)) - { - //Add if new - players.Add(comp); - } + players.Add(comp); } } diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index a463a53f1..f0d74e85f 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -425,7 +425,7 @@ void AssignSceneID() // => throw an exception to cancel the build and let the user // know how to fix it! if (BuildPipeline.isBuildingPlayer) - throw new Exception("Scene " + gameObject.scene.path + " needs to be opened and resaved before building, because the scene object " + name + " has no valid sceneId yet."); + throw new InvalidOperationException("Scene " + gameObject.scene.path + " needs to be opened and resaved before building, because the scene object " + name + " has no valid sceneId yet."); // if we generate the sceneId then we MUST be sure to set dirty // in order to save the scene object properly. otherwise it diff --git a/Assets/Mirror/Runtime/Transport/MultiplexTransport.cs b/Assets/Mirror/Runtime/Transport/MultiplexTransport.cs index 47ccccda0..f329e948d 100644 --- a/Assets/Mirror/Runtime/Transport/MultiplexTransport.cs +++ b/Assets/Mirror/Runtime/Transport/MultiplexTransport.cs @@ -80,7 +80,7 @@ public override void ClientConnect(string address) return; } } - throw new Exception("No transport suitable for this platform"); + throw new ArgumentException("No transport suitable for this platform"); } public override void ClientConnect(Uri uri) @@ -101,7 +101,7 @@ public override void ClientConnect(Uri uri) } } } - throw new Exception("No transport suitable for this platform"); + throw new ArgumentException("No transport suitable for this platform"); } public override bool ClientConnected()