Exemple #1
0
    public void RemovePos(Transform target, InputBuffer buffer)
    {
        float h = (buffer.D ? 1f : 0) + (buffer.A ? -1f : 0);

        Vector3 position = new Vector3(0, 0, h);
        Vector3 pos      = target.position - position;

        InputRender render = new InputRender();

        render.Tick     = buffer.Tick;
        render.position = pos;
        buffers.Enqueue(render);
    }
Exemple #2
0
    //20f(收到消息解析,同步影子玩家)
    public void PlacePos(Transform target, InputBuffer buffer)
    {
        //分析按键组合,按优先级屏蔽无效操作
        float y = (buffer.W ? MOVE_SPEED : 0) + (buffer.S ? -MOVE_SPEED : 0);
        float z = (buffer.D ? MOVE_SPEED : 0) + (buffer.A ? -MOVE_SPEED : 0);

        //移动
        Vector3 position = Vector3.zero;

        if (OnGround() && y == 0)
        {
            position = new Vector3(0, 0, z);
        }

        if (OnGround() && playerVelocity.y < 0)
        {
            playerVelocity.y = 0;
            playerVelocity.z = 0;
        }

        //跳跃
        if (OnGround() && y > 0)
        {
            if (z == 0)
            {
                //原地跳跃
                playerVelocity.y += Mathf.Sqrt(JUMP_HEIGHT * -3.0f * GRAVITY);//平方根
            }
            else if (z > 0)
            {
                //向前跳跃
                playerVelocity.y += Mathf.Sqrt(JUMP_HEIGHT * -3.0f * GRAVITY);
                playerVelocity.z += 0.4f;
            }
            else if (z < 0)
            {
                //向后跳跃
                playerVelocity.y += Mathf.Sqrt(JUMP_HEIGHT * -3.0f * GRAVITY);
                playerVelocity.z -= 0.4f;
            }
        }

        //衰减
        playerVelocity.y += GRAVITY;
        if (OnGround() == false)
        {
            //在空中
            if (playerVelocity.z > 0)
            {
                playerVelocity.z -= 0.05f;
            }
            else if (playerVelocity.z < 0)
            {
                playerVelocity.z += 0.05f;
            }
        }
        position += playerVelocity;

        //移动到的位置
        Vector3 pos = target.position + position;

        pos.y = Mathf.Clamp(pos.y, 0, pos.y);


        //PushBox计算
        if (rival.transform.position.z > transform.position.z)
        {
            //自己在左,对手在右
            direction        = Direction.Right;
            child.localScale = POS_SIZE;
            child.rotation   = new Quaternion(0, 0, 0, 0);

            if (z > 0 && OnGround() && y == 0)
            {
                status = MotionStatus.MoveForward;
            }
            else if (z < 0 && OnGround() && y == 0)
            {
                status = MotionStatus.MoveBackward;
            }
            else if (z == 0 && OnGround() && y == 0)
            {
                status = MotionStatus.Idle;
            }
            else if (!OnGround())
            {
                // 向前跳碰撞
                status = MotionStatus.Jump;
            }
            else
            {
                //Debug.Log($"h={h} && hash={animator.GetCurrentAnimatorStateInfo(0).shortNameHash}");
            }

            distance = rival.transform.position.z - pos.z;
            if (distance < SCALE_Z)
            {
                if ((status == MotionStatus.MoveForward || status == MotionStatus.JumpForward) && z > 0)
                {
                    Vector3 pos1 = new Vector3(0, 0, SCALE_Z - distance);
                    Debug.Log(distance + ",右推: " + pos1);
                    Push(pos1);
                }
                else if (status == MotionStatus.Idle && rival.OnGround())
                {
                    Vector3 pos1 = new Vector3(0, 0, SCALE_Z - distance);
                    Push(pos1);
                }
            }
        }
        else if (rival.transform.position.z < transform.position.z)
        {
            //对手在左,自己在右
            direction        = Direction.Left;
            child.localScale = NEG_SIZE;
            child.rotation   = new Quaternion(0, 1, 0, 0);

            if (z > 0 && OnGround() && y == 0)
            {
                status = MotionStatus.MoveBackward;
            }
            else if (z < 0 && OnGround() && y == 0)
            {
                status = MotionStatus.MoveForward;
            }
            else if (z == 0 && OnGround() && y == 0)
            {
                status = MotionStatus.Idle;
            }
            else if (!OnGround())
            {
                // 向前跳碰撞
                status = MotionStatus.Jump;
            }
            else
            {
                //Debug.Log($"h={h} && hash={animator.GetCurrentAnimatorStateInfo(0).shortNameHash}");
            }

            distance = pos.z - rival.transform.position.z;
            if (distance < SCALE_Z)
            {
                if ((status == MotionStatus.MoveForward || status == MotionStatus.JumpForward) && z < 0)
                {
                    Vector3 pos1 = new Vector3(0, 0, distance - SCALE_Z);
                    Debug.Log(distance + ",左推: " + pos1);
                    Push(pos1);//移动中推动
                }
                else if (status == MotionStatus.Idle && rival.OnGround())
                {
                    Vector3 pos1 = new Vector3(0, 0, distance - SCALE_Z);
                    Push(pos1);
                }
            }
        }
        else
        {
            direction = Direction.Error;
        }


        InputRender render = new InputRender();

        render.Tick     = buffer.Tick;
        render.position = pos;
        buffers.Enqueue(render);
    }