public void TestScaleFloatToByte() { Assert.That(FloatBytePacker.ScaleFloatToByte(-1f, -1f, 1f, byte.MinValue, byte.MaxValue), Is.EqualTo(0)); Assert.That(FloatBytePacker.ScaleFloatToByte(0f, -1f, 1f, byte.MinValue, byte.MaxValue), Is.EqualTo(127)); Assert.That(FloatBytePacker.ScaleFloatToByte(0.5f, -1f, 1f, byte.MinValue, byte.MaxValue), Is.EqualTo(191)); Assert.That(FloatBytePacker.ScaleFloatToByte(1f, -1f, 1f, byte.MinValue, byte.MaxValue), Is.EqualTo(255)); }
public void ScaleByteToFloat() { Assert.That(FloatBytePacker.ScaleByteToFloat(0, byte.MinValue, byte.MaxValue, -1, 1), Is.EqualTo(-1).Within(0.0001f)); Assert.That(FloatBytePacker.ScaleByteToFloat(127, byte.MinValue, byte.MaxValue, -1, 1), Is.EqualTo(-0.003921569f).Within(0.0001f)); Assert.That(FloatBytePacker.ScaleByteToFloat(191, byte.MinValue, byte.MaxValue, -1, 1), Is.EqualTo(0.4980392f).Within(0.0001f)); Assert.That(FloatBytePacker.ScaleByteToFloat(255, byte.MinValue, byte.MaxValue, -1, 1), Is.EqualTo(1).Within(0.0001f)); }
static Vector3 UncompressDirection(byte xRotation, byte yRotation) { float x = FloatBytePacker.ScaleByteToFloat(xRotation, byte.MinValue, byte.MaxValue, 0, 360); float y = FloatBytePacker.ScaleByteToFloat(yRotation, byte.MinValue, byte.MaxValue, 0, 360); Quaternion rotation = Quaternion.Euler(x, y, 0); return(rotation * Vector3.forward); }
static void CompressDirection(Vector3 direction, out byte xRotation, out byte yRotation) { Quaternion rotation = Quaternion.LookRotation(direction); Vector3 euler = rotation.eulerAngles; xRotation = FloatBytePacker.ScaleFloatToByte(euler.x, 0, 360, byte.MinValue, byte.MaxValue); yRotation = FloatBytePacker.ScaleFloatToByte(euler.y, 0, 360, byte.MinValue, byte.MaxValue); }
public void TestPackFloatToUShort() { ushort packed = FloatBytePacker.PackThreeFloatsIntoUShort(15, 95, 170, 0, 360); Vector3 unpacked = FloatBytePacker.UnpackUShortIntoThreeFloats(packed, 0, 360); Assert.That(unpacked.x, Is.EqualTo(15).Within(10f)); Assert.That(unpacked.y, Is.EqualTo(95).Within(10f)); Assert.That(unpacked.z, Is.EqualTo(170).Within(10f)); }
void FixedUpdate() { if (isLocalPlayer) { Vector2 inputDir = GetInputDirection(); Vector3 desiredDir = GetDesiredDirection(inputDir); Debug.DrawLine(transform.position, transform.position + desiredDir, Color.cyan); if (state == MoveState.IDLE) { state = UpdateIDLE(inputDir, desiredDir); } else if (state == MoveState.WALKING) { state = UpdateWALKINGandRUNNING(inputDir, desiredDir); } else if (state == MoveState.RUNNING) { state = UpdateWALKINGandRUNNING(inputDir, desiredDir); } else if (state == MoveState.CROUCHING) { state = UpdateCROUCHING(inputDir, desiredDir); } else if (state == MoveState.CRAWLING) { state = UpdateCRAWLING(inputDir, desiredDir); } else if (state == MoveState.AIRBORNE) { state = UpdateAIRBORNE(inputDir, desiredDir); } else if (state == MoveState.CLIMBING) { state = UpdateCLIMBING(inputDir, desiredDir); } else if (state == MoveState.SWIMMING) { state = UpdateSWIMMING(inputDir, desiredDir); } else if (state == MoveState.DEAD) { state = UpdateDEAD(inputDir, desiredDir); } else { Debug.LogError("Unhandled Movement State: " + state); } if (!controller.isGrounded) { lastFall = controller.velocity; } controller.Move(moveDir * Time.fixedDeltaTime); velocity = controller.velocity; byte rotationByte = FloatBytePacker.ScaleFloatToByte(transform.rotation.eulerAngles.y, 0, 360, byte.MinValue, byte.MaxValue); CmdFixedMove(new Move(route, state, transform.position, rotationByte)); float runCycle = Mathf.Repeat(animator.GetCurrentAnimatorStateInfo(0).normalizedTime + runCycleLegOffset, 1); jumpLeg = (runCycle < 0.5f ? 1 : -1); jumpKeyPressed = false; crawlKeyPressed = false; crouchKeyPressed = false; } else { if (lastState != state) { AdjustControllerCollider(); } if (!controller.isGrounded) { lastFall = velocity; } if (pendingMoves.Count > 0 && pendingMoves.Count >= minMoveBuffer) { if (isServer && pendingMoves.Count >= maxMoveBuffer) { Warp(transform.position); } else if (pendingMoves.Count >= 2 && pendingMoves.Count >= combineMovesAfter) { Move first = pendingMoves.Dequeue(); Move second = pendingMoves.Dequeue(); state = second.state; Vector3 move = second.position - transform.position; velocity = second.position - first.position; float yRotation = FloatBytePacker.ScaleByteToFloat(second.yRotation, byte.MinValue, byte.MaxValue, 0, 360); transform.rotation = Quaternion.Euler(0, yRotation, 0); controller.Move(move); ++combinedMoves; if (isServer) { RubberbandCheck(second.position); } } else { Move next = pendingMoves.Dequeue(); state = next.state; Vector3 move = next.position - transform.position; float yRotation = FloatBytePacker.ScaleByteToFloat(next.yRotation, byte.MinValue, byte.MaxValue, 0, 360); transform.rotation = Quaternion.Euler(0, yRotation, 0); controller.Move(move); velocity = controller.velocity; if (isServer) { RubberbandCheck(next.position); } } } } if (isServer) { if (lastState == MoveState.AIRBORNE && state != MoveState.AIRBORNE) { ApplyFallDamage(); } } lastState = state; }