Exemple #1
0
    IEnumerator Start()
    {
        var onlineDetails = FindObjectOfType <OnlineDetails>();

        if (onlineDetails != null)
        {
            if (onlineDetails.IsClient)
            {
                if (channelID < 1)
                {
                    channelID = TNManager.lastChannelID;
                }

                while (TNManager.isJoiningChannel || !TNManager.IsInChannel(channelID))
                {
                    yield return(null);
                }

                TNManager.Instantiate(channelID, "CreateAtPosition", prefabPath, persistent, transform.position, transform.rotation);
                Destroy(gameObject);
            }
        }
        else
        {
            Debug.LogWarning("No online details found");
        }
    }
Exemple #2
0
        public void DestroySelf()
        {
            if (!mDestroyed)
            {
                mDestroyed = true;

                if (TNManager.IsInChannel(channelID))
                {
                    if (TNManager.IsChannelLocked(channelID))
                    {
                        Debug.LogWarning("Trying to destroy an object in a locked channel. Call will be ignored.");
                    }
                    else
                    {
                        Invoke("EnsureDestroy", 5f);
                        BinaryWriter bw = TNManager.BeginSend(Packet.RequestDestroyObject);
                        bw.Write(channelID);
                        bw.Write(uid);
                        TNManager.EndSend(channelID, true);
                    }
                }
                else
                {
                    if (onDestroy != null)
                    {
                        onDestroy();
                    }
                    Object.Destroy(gameObject);
                }
            }
        }
Exemple #3
0
    /// <summary>
    /// Create a new object above the clicked position
    /// </summary>

    public void CreateMapByHost()
    {
        // Let's not try to create objects unless we are in this channel
        if (TNManager.isConnected && !TNManager.IsInChannel(GameCtr.Instance.ChannelID))
        {
            return;
        }
        if (!TNManager.isHosting)
        {
            return;
        }

        TNManager.Instantiate(GameCtr.Instance.ChannelID, "RCC_CreateMap", prefabName, true, Vector3.zero, Quaternion.identity);

//		var halfX = mapSizeX / 2;
//		var halfY = mapSizeY / 2;
//		for (int i = -1*halfX; i < halfX; i++)
//		{
//			for (int j = -1*halfY; j < halfY; j++)
//			{
//				// Object's position will be up in the air so that it can fall down
//				Vector3 pos = new Vector3(i + .5f, 0, j + 0.5f);
//
//				// Object's rotation is completely random
//				Quaternion rot = Quaternion.identity;
//
//				// Object's color is completely random
//				Color color = new Color(Random.value, Random.value, Random.value, 1f);
//
//				// Create the object using a custom creation function defined below.
//				// Note that passing "channelID" is optional. If you don't pass anything, TNet will pick one for you.
//				TNManager.Instantiate(GameCtr.Instance.ChannelID, "RCC_CreateMap", prefabName, true, pos, rot, color);
//			}
//		}
    }
        /// <summary>
        /// Send out an update to everyone on the network.
        /// </summary>

        public void Sync()
        {
            if (isActive && TNManager.IsInChannel(tno.channelID))
            {
                UpdateInterval();
                mWasSleeping = false;
                mLastPos     = mTrans.position;
                mLastRot     = mTrans.rotation;
                tno.Send(1, Target.OthersSaved, mLastPos, mLastRot, mRb.velocity, mRb.angularVelocity);
            }
        }
Exemple #5
0
    void UpdateRenderer()
    {
        Renderer ren = GetComponent <Renderer>();

        if (ren != null)
        {
            Color c = TNManager.IsInChannel(channelID) ? Color.green : Color.red;
            c.a = 0.25f;
            ren.material.color = c;
        }
    }
Exemple #6
0
        /// <summary>
        /// Transfer this object to another channel. Only the object's owner can perform this action.
        /// </summary>

        public void TransferToChannel(int newChannelID)
        {
            if (!mDestroyed && isMine && channelID != newChannelID && TNManager.IsInChannel(channelID))
            {
                mDestroyed = true;
                BinaryWriter writer = TNManager.BeginSend(Packet.RequestTransferObject);
                writer.Write(channelID);
                writer.Write(newChannelID);
                writer.Write(uid);
                TNManager.EndSend(channelID, true);
            }
        }
Exemple #7
0
        /// <summary>
        /// Only the host should be sending out updates. Everyone else should be simply observing the changes.
        /// </summary>

        void FixedUpdate()
        {
            if (updatesPerSecond < 0.001f)
            {
                return;
            }

            if (isActive && tno.isMine && !tno.hasBeenDestroyed && TNManager.IsInChannel(tno.channelID))
            {
                bool isSleeping = mRb.IsSleeping();
                if (isSleeping && mWasSleeping)
                {
                    return;
                }

                mNext -= Time.deltaTime;
                if (mNext > 0f)
                {
                    return;
                }
                UpdateInterval();
#if W2
                var pos = FloatingOrigin.positionOffset + mRb.position;
                var vel = FloatingOrigin.velocityOffset + mRb.velocity;
#else
                var pos = mRb.position;
                var vel = mRb.velocity;
#endif
                Quaternion rot = mTrans.rotation;

                if (mWasSleeping || pos != mLastPos || Quaternion.Dot(rot, mLastRot) < 0.99f)
                {
                    mLastPos = pos;
                    mLastRot = rot;

                    // Send the update. Note that we're using an RFC ID here instead of the function name.
                    // Using an ID speeds up the function lookup time and reduces the size of the packet.
                    // Since the target is "OthersSaved", even players that join later will receive this update.
                    // Each consecutive Send() updates the previous, so only the latest one is kept on the server.

                    if (isImportant)
                    {
                        tno.Send(1, Target.OthersSaved, pos, rot, vel, mRb.angularVelocity);
                    }
                    else
                    {
                        tno.SendQuickly(1, Target.OthersSaved, pos, rot, vel, mRb.angularVelocity);
                    }
                }
                mWasSleeping = isSleeping;
            }
        }
 IEnumerator Start()
 {
     if (channelID < 1)
     {
         channelID = TNManager.lastChannelID;
     }
     while (TNManager.isJoiningChannel || !TNManager.IsInChannel(channelID))
     {
         yield return(null);
     }
     TNManager.Instantiate(channelID, "CreateAtPosition", prefabPath, persistent, transform.position, transform.rotation);
     Destroy(gameObject);
 }
Exemple #9
0
        /// <summary>
        /// Remove a previously saved remote function call.
        /// </summary>

        static void RemoveSavedRFC(int channelID, uint objID, byte rfcID, string funcName)
        {
            if (TNManager.IsInChannel(channelID))
            {
                BinaryWriter writer = TNManager.BeginSend(Packet.RequestRemoveRFC);
                writer.Write(channelID);
                writer.Write(GetUID(objID, rfcID));
                if (rfcID == 0)
                {
                    writer.Write(funcName);
                }
                TNManager.EndSend(channelID, true);
            }
        }
        /// <summary>
        /// Immediately synchronize all data by sending current values to everyone else.
        /// </summary>

        public void Sync()
        {
            if (TNManager.IsInChannel(tno.channelID) && mList.size != 0)
            {
                if (isImportant)
                {
                    tno.Send(255, isSavedOnServer ? Target.OthersSaved : Target.Others, mCached);
                }
                else
                {
                    tno.SendQuickly(255, isSavedOnServer ? Target.OthersSaved : Target.Others, mCached);
                }
            }
        }
    void PeriodicCheck()
    {
        Vector3       myPos           = transform.position;
        ExampleRegion closestRegion   = null;
        float         closestDistance = float.MaxValue;

        // First find the closest region -- this is the region the player avatar should belong to
        for (int i = 0; i < ExampleRegion.list.size; ++i)
        {
            ExampleRegion region   = ExampleRegion.list[i];
            float         distance = Vector3.Distance(region.transform.position, myPos);

            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestRegion   = region;
            }
        }

        // Now ensure we've joined all the nearby regions in addition to the closest region
        for (int i = 0; i < ExampleRegion.list.size; ++i)
        {
            ExampleRegion region   = ExampleRegion.list[i];
            float         distance = Vector3.Distance(region.transform.position, myPos);

            if (distance < joinDistance || region == closestRegion)
            {
                // We're close -- join the region's channel
                if (!TNManager.IsInChannel(region.channelID))
                {
                    TNManager.JoinChannel(region.channelID, true);
                }
            }
            else if (distance > leaveDistance && tno.channelID != region.channelID)
            {
                // We're far away -- leave the region's channel
                if (TNManager.IsInChannel(region.channelID))
                {
                    TNManager.LeaveChannel(region.channelID);
                }
            }
        }

        // Transfer the car to the closest region's channel
        if (closestRegion != null && tno.channelID != closestRegion.channelID)
        {
            tno.TransferToChannel(closestRegion.channelID);
        }
    }
Exemple #12
0
        /// <summary>
        /// Send out an update to everyone on the network.
        /// </summary>

        public void Sync()
        {
            if (isActive && !tno.hasBeenDestroyed && TNManager.IsInChannel(tno.channelID))
            {
                UpdateInterval();
#if W2
                mLastPos = FloatingOrigin.positionOffset + mRb.position;
                var vel = FloatingOrigin.velocityOffset + mRb.velocity;
#else
                mLastPos = mRb.position;
                var vel = mRb.velocity;
#endif
                mWasSleeping = false;
                mLastRot     = mRb.rotation;
                tno.Send(1, Target.OthersSaved, mLastPos, mLastRot, vel, mRb.angularVelocity);
            }
        }
        /// <summary>
        /// Sync periodically.
        /// </summary>

        IEnumerator PeriodicSync()
        {
            for (; ;)
            {
                if (TNManager.IsInChannel(tno.channelID) && updatesPerSecond > 0f)
                {
                    if (mList.size != 0 && (!onlyOwnerCanSync || tno.isMine) && Cache())
                    {
                        Sync();
                    }
                    yield return(new WaitForSeconds(1f / updatesPerSecond));
                }
                else
                {
                    yield return(new WaitForSeconds(0.1f));
                }
            }
        }
Exemple #14
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            FindPuck();

            if (ball)
            {
                ball.SetVelocity(Vector3.forward * speed);
            }
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            FindPuck();

            if (ball)
            {
                return;
            }

            if (TNManager.isConnected && !TNManager.IsInChannel(TNManager.lastChannelID))
            {
                return;
            }

            CreateBall();
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            if (!player)
            {
                return;
            }

            if (TNManager.isConnected && !TNManager.IsInChannel(TNManager.lastChannelID))
            {
                return;
            }

            player.ChangeJerseyColorRandom();
        }
    }
Exemple #15
0
    /// <summary>
    /// Create a new object above the clicked position
    /// </summary>

    void OnClick()
    {
        // Let's not try to create objects unless we are in this channel
        if (TNManager.isConnected && !TNManager.IsInChannel(channelID))
        {
            return;
        }

        // Object's position will be up in the air so that it can fall down
        Vector3 pos = TouchHandler.worldPos + Vector3.up * 3f;

        // Object's rotation is completely random
        Quaternion rot = Quaternion.Euler(Random.value * 180f, Random.value * 180f, Random.value * 180f);

        // Object's color is completely random
        Color color = new Color(Random.value, Random.value, Random.value, 1f);

        // Create the object using a custom creation function defined below.
        // Note that passing "channelID" is optional. If you don't pass anything, TNet will pick one for you.
        TNManager.Instantiate(channelID, "ColoredObject", prefabName, true, pos, rot, color, autoDestroyDelay);
    }
Exemple #16
0
    private void Awake()
    {
        if (canvas == null)
        {
            Debug.LogError("set canvas");
            Debug.Break();
        }

        button_play = canvas.transform.FindChild("play").GetComponent <Button>();
        button_play.onClick.AddListener(button_play_onClick);

        button_connect = canvas.transform.FindChild("connect").GetComponent <Button>();
        button_connect.onClick.AddListener(button_connect_onClick);

        button_disconnect = canvas.transform.FindChild("disconnect").GetComponent <Button>();
        button_disconnect.onClick.AddListener(button_disconnect_onClick);

        //when joining a channel the scene is reloaded (if using one scene)
        if (TNManager.isConnected)
        {
            button_connect.interactable    = false;
            button_disconnect.interactable = true;

            //play button is only available if in the lobby channel
            if (TNManager.IsInChannel(1000))
            {
                button_play.interactable = true;
            }
            else
            {
                button_play.interactable = false;
            }
        }
        else
        {
            button_play.interactable       = false;
            button_connect.interactable    = true;
            button_disconnect.interactable = false;
        }
    }
Exemple #17
0
    private void InitMapByHoset()
    {
        // Let's not try to create objects unless we are in this channel
        if (TNManager.isConnected && !TNManager.IsInChannel(GameCtr.Instance.ChannelID))
        {
            return;
        }
        if (!TNManager.isHosting)
        {
            return;
        }

        var aps         = this.GetAllPlayers();
        var mapStyleDic = new Dictionary <int, int> ();

        var poses = new TNet.List <int>();

        for (int i = 0; i < MapMgr.Instance.MapCellCount; i++)
        {
            poses.Add(i);
        }

        for (int i = 0; i < aps.Count; i++)
        {
            var posIdx = Random.Range(0, poses.Count);
            mapStyleDic.Add(posIdx, (int)MapStyle.PLAIN);
            poses.Remove(posIdx);
        }

        foreach (var p in poses)
        {
            mapStyleDic.Add(p, Random.Range(0, System.Enum.GetNames(typeof(MapStyle)).Length));
        }

        tno.Send("RFC_InitMap", Target.All, mapStyleDic.ToJsonString());
    }
Exemple #18
0
        /// <summary>
        /// Send a new RFC call to the specified target.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, Target target, bool reliable, params object[] objs)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            if (mDestroyed)
            {
                return;
            }

            if ((target == Target.AllSaved || target == Target.Others) && TNManager.IsChannelLocked(channelID))
            {
#if UNITY_EDITOR
                Debug.LogError("Can't send persistent RFCs while in a locked channel");
#endif
                return;
            }

            // Some very odd special case... sending a string[] as the only parameter
            // results in objs[] being a string[] instead, when it should be object[string[]].
            if (objs != null && objs.GetType() != typeof(object[]))
            {
                objs = new object[] { objs }
            }
            ;

            bool executeLocally = false;
            bool connected      = TNManager.isConnected;

            if (target == Target.Broadcast)
            {
                if (connected)
                {
                    BinaryWriter writer = TNManager.BeginSend(Packet.Broadcast);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
                else
                {
                    executeLocally = true;
                }
            }
            else if (target == Target.Admin)
            {
                if (connected)
                {
                    BinaryWriter writer = TNManager.BeginSend(Packet.BroadcastAdmin);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
                else
                {
                    executeLocally = true;
                }
            }
            else if (target == Target.Host && TNManager.isHosting)
            {
                // We're the host, and the packet should be going to the host -- just echo it locally
                executeLocally = true;
            }
            else
            {
                if (!connected || !reliable)
                {
                    if (target == Target.All)
                    {
                        target         = Target.Others;
                        executeLocally = true;
                    }
                    else if (target == Target.AllSaved)
                    {
                        target         = Target.OthersSaved;
                        executeLocally = true;
                    }
                }

                if (connected && TNManager.IsInChannel(channelID))
                {
                    byte         packetID = (byte)((int)Packet.ForwardToAll + (int)target);
                    BinaryWriter writer   = TNManager.BeginSend(packetID);
                    writer.Write(TNManager.playerID);
                    writer.Write(channelID);
                    writer.Write(GetUID(uid, rfcID));
                    if (rfcID == 0)
                    {
                        writer.Write(rfcName);
                    }
                    writer.WriteArray(objs);
                    TNManager.EndSend(channelID, reliable);
                }
            }

            if (executeLocally)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
        }

        /// <summary>
        /// Send a new RFC call to the specified target.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, string targetName, bool reliable, params object[] objs)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            if (mDestroyed || string.IsNullOrEmpty(targetName))
            {
                return;
            }

            if (targetName == TNManager.playerName)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
            else
            {
                BinaryWriter writer = TNManager.BeginSend(Packet.ForwardByName);
                writer.Write(TNManager.playerID);
                writer.Write(targetName);
                writer.Write(channelID);
                writer.Write(GetUID(uid, rfcID));
                if (rfcID == 0)
                {
                    writer.Write(rfcName);
                }
                writer.WriteArray(objs);
                TNManager.EndSend(channelID, reliable);
            }
        }

        /// <summary>
        /// Send a new remote function call to the specified player.
        /// </summary>

        void SendRFC(byte rfcID, string rfcName, int target, bool reliable, params object[] objs)
        {
            if (mDestroyed)
            {
                return;
            }

            if (TNManager.isConnected)
            {
                BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer);
                writer.Write(TNManager.playerID);
                writer.Write(target);
                writer.Write(channelID);
                writer.Write(GetUID(uid, rfcID));
                if (rfcID == 0)
                {
                    writer.Write(rfcName);
                }
                writer.WriteArray(objs);
                TNManager.EndSend(channelID, reliable);
            }
            else if (target == TNManager.playerID)
            {
                if (rfcID != 0)
                {
                    Execute(rfcID, objs);
                }
                else
                {
                    Execute(rfcName, objs);
                }
            }
        }

        /// <summary>
        /// Broadcast a remote function call to all players on the network.
        /// </summary>

        void BroadcastToLAN(int port, byte rfcID, string rfcName, params object[] objs)
        {
            if (mDestroyed)
            {
                return;
            }
            BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll);

            writer.Write(TNManager.playerID);
            writer.Write(channelID);
            writer.Write(GetUID(uid, rfcID));
            if (rfcID == 0)
            {
                writer.Write(rfcName);
            }
            writer.WriteArray(objs);
            TNManager.EndSendToLAN(port);
        }