Пример #1
0
 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));
 }
Пример #2
0
    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);
    }
Пример #3
0
    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;
    }