public static void GetCubeState(Rigidbody rigidBody, NetworkInfo networkInfo, ref CubeState cubeState, ref Vector3 origin)
    {
        cubeState.active = !rigidBody.IsSleeping();

        cubeState.authorityIndex    = networkInfo.GetAuthorityIndex();
        cubeState.authoritySequence = networkInfo.GetAuthoritySequence();
        cubeState.ownershipSequence = networkInfo.GetOwnershipSequence();

        Vector3 position = rigidBody.position - origin;

        cubeState.position_x = (int)Math.Floor(position.x * Constants.UnitsPerMeter + 0.5f);
        cubeState.position_y = (int)Math.Floor(position.y * Constants.UnitsPerMeter + 0.5f);
        cubeState.position_z = (int)Math.Floor(position.z * Constants.UnitsPerMeter + 0.5f);

        Snapshot.QuaternionToSmallestThree(rigidBody.rotation,
                                           out cubeState.rotation_largest,
                                           out cubeState.rotation_a,
                                           out cubeState.rotation_b,
                                           out cubeState.rotation_c);

        cubeState.linear_velocity_x = (int)Math.Floor(rigidBody.velocity.x * Constants.UnitsPerMeter + 0.5f);
        cubeState.linear_velocity_y = (int)Math.Floor(rigidBody.velocity.y * Constants.UnitsPerMeter + 0.5f);
        cubeState.linear_velocity_z = (int)Math.Floor(rigidBody.velocity.z * Constants.UnitsPerMeter + 0.5f);

        cubeState.angular_velocity_x = (int)Math.Floor(rigidBody.angularVelocity.x * Constants.UnitsPerMeter + 0.5f);
        cubeState.angular_velocity_y = (int)Math.Floor(rigidBody.angularVelocity.y * Constants.UnitsPerMeter + 0.5f);
        cubeState.angular_velocity_z = (int)Math.Floor(rigidBody.angularVelocity.z * Constants.UnitsPerMeter + 0.5f);

        ClampPosition(ref cubeState.position_x, ref cubeState.position_y, ref cubeState.position_z);

        ClampLinearVelocity(ref cubeState.linear_velocity_x, ref cubeState.linear_velocity_y, ref cubeState.linear_velocity_z);

        ClampAngularVelocity(ref cubeState.angular_velocity_x, ref cubeState.angular_velocity_y, ref cubeState.angular_velocity_z);
    }
    public void ResetAuthorityForClientCubes(int clientIndex)
    {
        int authorityIndex = clientIndex + 1;

        for (int i = 0; i < Constants.NumCubes; ++i)
        {
            NetworkInfo networkInfo = cubes[i].GetComponent <NetworkInfo>();

            if (networkInfo.GetAuthorityIndex() == authorityIndex)
            {
                Debug.Log("Returning cube " + i + " to default authority");

                networkInfo.DetachCubeFromPlayer();

                networkInfo.SetAuthorityIndex(0);
                networkInfo.SetAuthoritySequence(0);
                networkInfo.IncreaseOwnershipSequence();

                var rigidBody = cubes[i].GetComponent <Rigidbody>();

                if (rigidBody.IsSleeping())
                {
                    rigidBody.WakeUp();
                }

                ResetCubeRingBuffer(i);
            }
        }
    }
    void UpdateAuthorityMaterials()
    {
        for (int i = 0; i < Constants.NumCubes; i++)
        {
            NetworkInfo networkInfo = cubes[i].GetComponent <NetworkInfo>();

            Renderer renderer = networkInfo.smoothed.GetComponent <Renderer>();

            int authorityIndex = networkInfo.GetAuthorityIndex();

            renderer.material.Lerp(renderer.material, authorityMaterials[authorityIndex], authorityIndex != 0 ? 0.3f : 0.04f);
        }
    }
    public void TakeAuthorityOverObject(NetworkInfo networkInfo)
    {
        Assert.IsTrue(networkInfo.GetAuthorityIndex() == 0);
#if DEBUG_AUTHORITY
        Debug.Log("client " + clientIndex + " took authority over cube " + networkInfo.GetCubeId());
#endif // #if DEBUG_AUTHORITY
        networkInfo.SetAuthorityIndex(authorityIndex);
        networkInfo.IncreaseAuthoritySequence();
        if (!IsServer())
        {
            networkInfo.ClearConfirmed();
        }
        else
        {
            networkInfo.SetConfirmed();
        }
    }
示例#5
0
    private void OnTriggerEnter(Collider other)
    {
        //hit by a cube
        NetworkInfo cubeInfo = other.GetComponent <NetworkInfo>();

        if (cubeInfo)
        {
            int authorityIndex = cubeInfo.GetAuthorityIndex();
            //if not default authority, and not my authority
            if (authorityIndex != 0 && authorityIndex != goalIndex + 1)
            {
                if (host.IsClientConnected(goalIndex))
                {
                    //register a scored point (from authIndex - 1 (is senderId), to this clientId (same as goalId))
                    EventManager.playerScored(PacketSerializer.GameEvent.SCORE, (ushort)(authorityIndex - 1), (ushort)goalIndex);
                }
            }
        }
    }