Example #1
0
        public LiteNetLibIdentity NetworkSpawnScene(uint objectId, Vector3 position, Quaternion rotation)
        {
            if (!Manager.IsNetworkActive)
            {
                Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawnScene - Network is not active cannot spawn");
                return(null);
            }

            LiteNetLibIdentity sceneObject = null;

            if (SceneObjects.TryGetValue(objectId, out sceneObject))
            {
                sceneObject.gameObject.SetActive(true);
                sceneObject.transform.position = position;
                sceneObject.transform.rotation = rotation;
                sceneObject.Initial(Manager, true, objectId);
                SpawnedObjects[sceneObject.ObjectId] = sceneObject;
                return(sceneObject);
            }
            else if (Manager.LogWarn)
            {
                Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawnScene - Object Id: " + objectId + " is not registered.");
            }
            return(null);
        }
 public void NotifyNewObject(LiteNetLibIdentity newObject)
 {
     if (!IsServer)
     {
         // Notifies by server only
         return;
     }
     // Subscribe old objects, if it should
     if (newObject.ConnectionId >= 0 && newObject.Player.IsReady)
     {
         foreach (LiteNetLibIdentity target in Manager.Assets.GetSpawnedObjects())
         {
             // Subscribe, it may subscribe or may not, up to how subscribe function implemented
             Subscribe(newObject, target);
         }
     }
     // Notify to other players
     foreach (LiteNetLibPlayer player in Manager.GetPlayers())
     {
         if (!player.IsReady || player.ConnectionId == newObject.ConnectionId)
         {
             // Don't subscribe if player not ready or the object is owned by the player
             continue;
         }
         foreach (LiteNetLibIdentity subscriber in player.GetSpawnedObjects())
         {
             // Subscribe, it may subscribe or may not, up to how subscribe function implemented
             Subscribe(subscriber, newObject);
         }
     }
 }
        public override bool OnRebuildSubscribers(HashSet <LiteNetLibPlayer> subscribers, bool initialize)
        {
            // find players within range
            switch (checkMethod)
            {
            case CheckMethod.Physics3D:
            {
                colliderLength = Physics.OverlapSphereNonAlloc(transform.position, range, colliders, layerMask.value);
                for (int i = 0; i < colliderLength; ++i)
                {
                    tempIdentity = colliders[i].GetComponent <LiteNetLibIdentity>();
                    if (tempIdentity != null && tempIdentity.Player != null)
                    {
                        subscribers.Add(tempIdentity.Player);
                    }
                }
                return(true);
            }

            case CheckMethod.Physics2D:
            {
                colliderLength = Physics2D.OverlapCircleNonAlloc(transform.position, range, colliders2D, layerMask.value);
                for (int i = 0; i < colliderLength; ++i)
                {
                    tempIdentity = colliders2D[i].GetComponent <LiteNetLibIdentity>();
                    if (tempIdentity != null && tempIdentity.Player != null)
                    {
                        subscribers.Add(tempIdentity.Player);
                    }
                }
                return(true);
            }
            }
            return(false);
        }
Example #4
0
        public LiteNetLibIdentity NetworkSpawn(LiteNetLibIdentity identity, uint objectId = 0, long connectionId = -1)
        {
            if (identity == null)
            {
                if (Manager.LogWarn)
                {
                    Logging.LogWarning(LogTag, "NetworkSpawn - identity is null.");
                }
                return(null);
            }

            identity.gameObject.SetActive(true);
            identity.Initial(Manager, false, objectId, connectionId);
            identity.InitTransform(identity.transform.position, identity.transform.rotation);
            identity.OnSetOwnerClient(connectionId >= 0 && connectionId == Manager.ClientConnectionId);
            if (Manager.IsServer)
            {
                identity.OnStartServer();
            }
            if (Manager.IsClient)
            {
                identity.OnStartClient();
            }
            if (connectionId >= 0 && connectionId == Manager.ClientConnectionId)
            {
                identity.OnStartOwnerClient();
            }
            if (onObjectSpawn != null)
            {
                onObjectSpawn.Invoke(identity);
            }

            return(identity);
        }
        public override bool OnRebuildSubscribers(HashSet <LiteNetLibPlayer> subscribers, bool initialize)
        {
            // find players within range
            switch (checkMethod)
            {
            case CheckMethod.Physics3D:
            {
                Collider[] hits = Physics.OverlapSphere(transform.position, range, layerMask.value);
                foreach (Collider hit in hits)
                {
                    LiteNetLibIdentity identity = hit.GetComponent <LiteNetLibIdentity>();
                    if (identity != null && identity.Player != null)
                    {
                        subscribers.Add(identity.Player);
                    }
                }
                return(true);
            }

            case CheckMethod.Physics2D:
            {
                Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, range, layerMask.value);
                foreach (Collider2D hit in hits)
                {
                    LiteNetLibIdentity identity = hit.GetComponent <LiteNetLibIdentity>();
                    if (identity != null && identity.Player != null)
                    {
                        subscribers.Add(identity.Player);
                    }
                }
                return(true);
            }
            }
            return(false);
        }
Example #6
0
        public void InitPoolingObject(int hashAssetId)
        {
            if (!GuidToPrefabs.ContainsKey(hashAssetId))
            {
                Debug.LogWarning($"Cannot init prefab: {hashAssetId}, can't find the registered prefab.");
                return;
            }

            // Already init pool for the prefab
            if (PooledObjects.ContainsKey(hashAssetId))
            {
                return;
            }

            LiteNetLibIdentity prefab = GuidToPrefabs[hashAssetId];

            // Don't create an instance for this prefab, if pooling size < 0
            if (prefab.PoolingSize <= 0)
            {
                return;
            }

            Queue <LiteNetLibIdentity> queue = new Queue <LiteNetLibIdentity>();
            LiteNetLibIdentity         tempInstance;

            for (int i = 0; i < prefab.PoolingSize; ++i)
            {
                tempInstance = Instantiate(prefab);
                tempInstance.IsPooledInstance = true;
                tempInstance.gameObject.SetActive(false);
                queue.Enqueue(tempInstance);
            }

            PooledObjects[hashAssetId] = queue;
        }
Example #7
0
        public void PushInstanceBack(LiteNetLibIdentity instance)
        {
            if (instance == null)
            {
                Debug.LogWarning($"[PoolSystem] Cannot push back ({instance.gameObject}). The instance's is empty.");
                return;
            }
            if (!instance.IsPooledInstance)
            {
                Debug.LogWarning($"[PoolSystem] Cannot push back ({instance.gameObject}). The instance is not pooled instance.");
                return;
            }
            Queue <LiteNetLibIdentity> queue;

            if (!PooledObjects.TryGetValue(instance.HashAssetId, out queue))
            {
                Debug.LogWarning($"[PoolSystem] Cannot push back ({instance.gameObject}). The instance's prefab does not initailized yet.");
                return;
            }
            if (queue.Count >= instance.PoolingSize)
            {
                Destroy(instance.gameObject);
            }
            else
            {
                instance.gameObject.SetActive(false);
                queue.Enqueue(instance);
            }
        }
Example #8
0
        public virtual void SetPlayerReady(long connectionId, NetDataReader reader)
        {
            if (!IsServer)
            {
                return;
            }

            LiteNetLibPlayer player = Players[connectionId];

            if (player.IsReady)
            {
                return;
            }

            player.IsReady = true;
            LiteNetLibIdentity playerIdentity = SpawnPlayer(connectionId);

            DeserializeClientReadyExtra(playerIdentity, connectionId, reader);
            foreach (LiteNetLibIdentity spawnedObject in Assets.SpawnedObjects.Values)
            {
                if (spawnedObject.ConnectionId == player.ConnectionId)
                {
                    continue;
                }

                if (spawnedObject.ShouldAddSubscriber(player))
                {
                    spawnedObject.AddSubscriber(player);
                }
            }
        }
Example #9
0
 protected LiteNetLibIdentity SpawnPlayer(long connectionId, LiteNetLibIdentity prefab)
 {
     if (prefab == null)
     {
         return(null);
     }
     return(SpawnPlayer(connectionId, prefab.HashAssetId));
 }
Example #10
0
 public bool IsHideFrom(LiteNetLibIdentity identity)
 {
     if (identity == null)
     {
         return(true);
     }
     return(IsHide && !HideExceptions.Contains(ConnectionId) && ConnectionId != identity.ConnectionId);
 }
Example #11
0
        internal void RemoveSubscribing(LiteNetLibIdentity identity, bool destroyObjectsOnPeer)
        {
            SubscribingObjects.Remove(identity);

            if (destroyObjectsOnPeer)
            {
                Manager.SendServerDestroyObject(ConnectionId, identity.ObjectId, LiteNetLibGameManager.DestroyObjectReasons.RemovedFromSubscribing);
            }
        }
Example #12
0
 public void Clear()
 {
     CacheSpawnPoints.Clear();
     GuidToPrefabs.Clear();
     SceneObjects.Clear();
     ClearSpawnedObjects();
     LiteNetLibIdentity.ResetObjectId();
     ResetSpawnPositionCounter();
 }
 public virtual bool Subscribe(LiteNetLibIdentity subscriber, LiteNetLibIdentity target)
 {
     if (ShouldSubscribe(subscriber, target))
     {
         subscriber.AddSubscribing(target.ObjectId);
         return(true);
     }
     return(false);
 }
Example #14
0
        /// <summary>
        /// This function will be called on start server and when network scene loaded to spawn scene objects
        /// So each scene objects connection id will = -1 (No owning client)
        /// </summary>
        public void SpawnSceneObjects()
        {
            List <LiteNetLibIdentity> sceneObjects = new List <LiteNetLibIdentity>(SceneObjects.Values);

            for (int i = 0; i < sceneObjects.Count; ++i)
            {
                LiteNetLibIdentity sceneObject = sceneObjects[i];
                NetworkSpawnScene(sceneObject.ObjectId, sceneObject.transform.position, sceneObject.transform.rotation);
            }
        }
Example #15
0
        protected LiteNetLibIdentity SpawnPlayer(long connectionId, int hashAssetId)
        {
            LiteNetLibIdentity spawnedObject = Assets.NetworkSpawn(hashAssetId, Assets.GetPlayerSpawnPosition(), Quaternion.identity, 0, connectionId);

            if (spawnedObject != null)
            {
                return(spawnedObject);
            }
            return(null);
        }
Example #16
0
 public void SendServerSpawnObject(LiteNetLibIdentity identity)
 {
     if (!IsServer)
     {
         return;
     }
     foreach (long connectionId in ConnectionIds)
     {
         SendServerSpawnObject(connectionId, identity);
     }
 }
Example #17
0
 public void Clear(bool doNotResetObjectId = false)
 {
     ClearSpawnedObjects();
     CacheSpawnPoints.Clear();
     SceneObjects.Clear();
     ResetSpawnPositionCounter();
     if (!doNotResetObjectId)
     {
         LiteNetLibIdentity.ResetObjectId();
     }
 }
Example #18
0
        public LiteNetLibIdentity NetworkSpawn(GameObject gameObject, uint objectId = 0, long connectionId = -1)
        {
            if (gameObject == null)
            {
                if (Manager.LogWarn)
                {
                    Logging.LogWarning(LogTag, "NetworkSpawn - gameObject is null.");
                }
                return(null);
            }

            LiteNetLibIdentity identity = gameObject.GetComponent <LiteNetLibIdentity>();

            if (identity == null)
            {
                if (Manager.LogWarn)
                {
                    Logging.LogWarning(LogTag, "NetworkSpawn - identity is null.");
                }
                return(null);
            }

            identity.gameObject.SetActive(true);
            identity.Initial(Manager, false, objectId, connectionId);
            identity.InitTransform(gameObject.transform.position, gameObject.transform.rotation);
            identity.SetOwnerClient(connectionId >= 0 && connectionId == Manager.ClientConnectionId);
            if (onObjectSpawn != null)
            {
                onObjectSpawn.Invoke(identity);
            }
            if (Manager.IsServer)
            {
                identity.OnStartServer();
            }
            if (Manager.IsClient)
            {
                identity.OnStartClient();
            }
            if (connectionId >= 0 && connectionId == Manager.ClientConnectionId)
            {
                identity.OnStartOwnerClient();
            }
            SpawnedObjects[identity.ObjectId] = identity;

            // Add to player spawned objects dictionary
            LiteNetLibPlayer player;

            if (Manager.TryGetPlayer(connectionId, out player))
            {
                player.SpawnedObjects[identity.ObjectId] = identity;
            }

            return(identity);
        }
Example #19
0
 public LiteNetLibIdentity NetworkSpawn(LiteNetLibIdentity identity, Vector3 position, Quaternion rotation, uint objectId = 0, long connectId = 0)
 {
     if (identity == null)
     {
         if (Manager.LogWarn)
         {
             Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawn - identity is null.");
         }
         return(null);
     }
     return(NetworkSpawn(identity.AssetId, position, rotation, objectId, connectId));
 }
 public virtual bool ShouldSubscribe(LiteNetLibIdentity subscriber, LiteNetLibIdentity target, bool checkRange = true)
 {
     if (subscriber == null || target == null || subscriber.ConnectionId < 0)
     {
         return(false);
     }
     if (subscriber.ConnectionId == target.ConnectionId)
     {
         return(true);
     }
     return(!target.IsHideFrom(subscriber) && (!checkRange || target.AlwaysVisible || Vector3.Distance(subscriber.transform.position, target.transform.position) <= GetVisibleRange(target)));
 }
 internal void RemoveSubscribing(LiteNetLibIdentity identity, bool destroyObjectsOnPeer)
 {
     SubscribingObjects.Remove(identity);
     if (destroyObjectsOnPeer)
     {
         if (Manager.IsClientConnected && Manager.ClientConnectionId == identity.ConnectionId)
         {
             identity.OnServerSubscribingRemoved();
         }
         Manager.SendServerDestroyObject(ConnectionId, identity.ObjectId, DestroyObjectReasons.RemovedFromSubscribing);
     }
 }
Example #22
0
 public void RegisterPrefabs()
 {
     for (int i = 0; i < spawnablePrefabs.Length; ++i)
     {
         LiteNetLibIdentity registeringPrefab = spawnablePrefabs[i];
         RegisterPrefab(registeringPrefab);
     }
     if (playerPrefab != null)
     {
         PlayerPrefab = playerPrefab;
         RegisterPrefab(playerPrefab);
     }
 }
Example #23
0
 public void RegisterSceneObjects()
 {
     SceneObjects.Clear();
     LiteNetLibIdentity[] sceneObjects = FindObjectsOfType <LiteNetLibIdentity>();
     for (int i = 0; i < sceneObjects.Length; ++i)
     {
         LiteNetLibIdentity sceneObject = sceneObjects[i];
         if (sceneObject.ObjectId > 0)
         {
             sceneObject.gameObject.SetActive(false);
             SceneObjects[sceneObject.ObjectId] = sceneObject;
         }
     }
 }
        public LiteNetLibIdentity NetworkSpawn(int hashAssetId, Vector3 position, Quaternion rotation, uint objectId = 0, long connectionId = -1)
        {
            LiteNetLibIdentity spawningObject = null;

            if (GuidToPrefabs.TryGetValue(hashAssetId, out spawningObject))
            {
                return(NetworkSpawn(Instantiate(spawningObject.gameObject, position, rotation), objectId, connectionId));
            }
            // If object with hash asset id not exists
            if (Manager.LogWarn)
            {
                Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawn - Asset Id: " + hashAssetId + " is not registered.");
            }
            return(null);
        }
Example #25
0
 public void RegisterPrefab(LiteNetLibIdentity prefab)
 {
     if (prefab == null)
     {
         if (Manager.LogWarn)
         {
             Logging.LogWarning(LogTag, "RegisterPrefab - prefab is null.");
         }
         return;
     }
     if (Manager.LogDev)
     {
         Logging.Log(LogTag, "RegisterPrefab [" + prefab.HashAssetId + "] name [" + prefab.name + "]");
     }
     GuidToPrefabs[prefab.HashAssetId] = prefab;
 }
Example #26
0
 public void RegisterPrefab(LiteNetLibIdentity prefab)
 {
     if (prefab == null)
     {
         if (Manager.LogWarn)
         {
             Debug.LogWarning("[" + name + "] LiteNetLibAssets::RegisterPrefab - prefab is null.");
         }
         return;
     }
     if (Manager.LogDev)
     {
         Debug.Log("[" + name + "] LiteNetLibAssets::RegisterPrefab [" + prefab.AssetId + "]");
     }
     GuidToPrefabs[prefab.AssetId] = prefab;
 }
Example #27
0
 public bool UnregisterPrefab(LiteNetLibIdentity prefab)
 {
     if (prefab == null)
     {
         if (Manager.LogWarn)
         {
             Debug.LogWarning("[" + name + "] LiteNetLibAssets::UnregisterPrefab - prefab is null.");
         }
         return(false);
     }
     if (Manager.LogDev)
     {
         Debug.Log("[" + name + "] LiteNetLibAssets::UnregisterPrefab [" + prefab.AssetId + "]");
     }
     return(GuidToPrefabs.Remove(prefab.AssetId));
 }
Example #28
0
 public bool UnregisterPrefab(LiteNetLibIdentity prefab)
 {
     if (prefab == null)
     {
         if (Manager.LogWarn)
         {
             Logging.LogWarning(LogTag, "UnregisterPrefab - prefab is null.");
         }
         return(false);
     }
     if (Manager.LogDev)
     {
         Logging.Log(LogTag, "UnregisterPrefab [" + prefab.HashAssetId + "] name [" + prefab.name + "]");
     }
     return(GuidToPrefabs.Remove(prefab.HashAssetId));
 }
Example #29
0
        public void SendServerSpawnObjectWithData(long connectionId, LiteNetLibIdentity identity)
        {
            if (identity == null)
            {
                return;
            }

            if (Assets.ContainsSceneObject(identity.ObjectId))
            {
                SendServerSpawnSceneObject(connectionId, identity);
            }
            else
            {
                SendServerSpawnObject(connectionId, identity);
            }
            identity.SendInitSyncFields(connectionId);
            identity.SendInitSyncLists(connectionId);
        }
Example #30
0
        public LiteNetLibIdentity NetworkSpawn(string assetId, Vector3 position, Quaternion rotation, uint objectId = 0, long connectId = 0)
        {
            if (!Manager.IsNetworkActive)
            {
                Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawn - Network is not active cannot spawn");
                return(null);
            }

            // If it's scene object use network spawn scene function to spawn it
            if (SceneObjects.ContainsKey(objectId))
            {
                return(NetworkSpawnScene(objectId, position, rotation));
            }

            // Spawned objects cannot spawning again
            if (SpawnedObjects.ContainsKey(objectId))
            {
                return(null);
            }

            LiteNetLibIdentity spawningObject = null;

            if (GuidToPrefabs.TryGetValue(assetId, out spawningObject))
            {
                var spawnedObject = Instantiate(spawningObject);
                spawnedObject.gameObject.SetActive(true);
                spawnedObject.transform.position = position;
                spawnedObject.transform.rotation = rotation;
                spawnedObject.Initial(Manager, false, objectId, connectId);
                SpawnedObjects[spawnedObject.ObjectId] = spawnedObject;
                // Add to player spawned objects dictionary
                LiteNetLibPlayer player;
                if (Manager.Players.TryGetValue(connectId, out player))
                {
                    player.SpawnedObjects[spawnedObject.ObjectId] = spawnedObject;
                }
                return(spawnedObject);
            }
            else if (Manager.LogWarn)
            {
                Debug.LogWarning("[" + name + "] LiteNetLibAssets::NetworkSpawn - Asset Id: " + assetId + " is not registered.");
            }
            return(null);
        }