public static void Delete(GameObject g)
        {
            if (ObjectPool.objectPool.LogFull)
            {
                Debug.Log("Deleting " + g.name);
            }
            PoolToken token  = ObjectPool.GetPoolToken(g);
            Pool      myPool = ObjectPool.GetPool(token.poolPrefabString, token.poolType);

            myPool.StoreObject(g);
        }
Example #2
0
        public static PoolToken AddPoolToken(GameObject g, string prefabString, InstantiationData i)
        {
            PoolToken token = g.GetComponent <PoolToken>();

            if (token == null)
            {
                token = g.AddComponent <PoolToken>();
            }

            token.ownerId           = i.OwnerId;
            token.tokenId           = i.TokenId;
            token.poolType          = i.PoolType;
            token.poolPrefabString  = prefabString;
            token.instantiationData = i.Data;
            token.usable            = true;
            return(token);
        }
Example #3
0
        private void deletePlayerObject(GameObject g)
        {
            PoolToken t = ObjectPool.GetPoolToken(g);

            if (playerObjects.ContainsKey(t.ownerId))
            {
                bool listContains = playerObjects[t.ownerId].Contains(g);
                if (listContains)
                {
                    playerObjects[t.ownerId].Remove(g);
                }
                else
                {
                    Debug.LogError("Tried to delete object from List but there was no GameObject g=[" + g.name + "]");
                }
            }
            else
            {
                Debug.LogError("Tried to delete object from Dictionary but there was no Key for player with ownerId=[" + t.ownerId + "]");
            }
        }
Example #4
0
        internal static void AddManagedObject(GameObject g)
        {
            PoolToken token = ObjectPool.GetPoolToken(g);

            //all
            if (AllObjects.ContainsKey(token.tokenId))
            {
                throw new Exception("Trying to add object with existing Id. New Object: " + g.name + " | Current Object: " + AllObjects[token.tokenId]);
            }
            AllObjects[token.tokenId] = g;

            //all by type
            if (!AllObjectsByType.ContainsKey(token.poolPrefabString))
            {
                AllObjectsByType[token.poolPrefabString] = new List <GameObject>();
            }
            AllObjectsByType[token.poolPrefabString].Add(g);

            //player objects
            if (!PlayerObjects.ContainsKey(token.ownerId))
            {
                PlayerObjects[token.ownerId] = new List <GameObject>();
            }
            PlayerObjects[token.ownerId].Add(g);

            //player controller
            if (token.IsPlayerController)
            {
                if (PlayerControllers.ContainsKey(token.ownerId))
                {
                    throw new Exception("Trying to add player controller for player ID " + token.ownerId + " but one already exists.");
                }
                else
                {
                    PlayerControllers[token.ownerId] = g;
                }
            }
        }
Example #5
0
        /// <summary>
        /// stores an object in the pool, removing it from the game world
        /// </summary>
        public void StoreObject(GameObject g)
        {
            PoolToken pt = ObjectPool.GetPoolToken(g);

            SendMessage("PreObjectDeactivate", g, SendMessageOptions.DontRequireReceiver);
            g.BroadcastMessage("OnObjectPoolDestroy", SendMessageOptions.DontRequireReceiver);
            g.BroadcastMessage("OnObjectPoolDelete", SendMessageOptions.DontRequireReceiver);
            if (transform.childCount > MaxPoolSize)
            {
                pt.usable = false;
                Destroy(g);
            }
            else
            {
                g.BroadcastMessage("OnDestroy", SendMessageOptions.DontRequireReceiver);
                g.SetActive(false);
                g.transform.parent   = transform;
                g.transform.position = Vector3.zero;
                g.transform.rotation = Quaternion.identity;
            }
            deletePlayerObject(g);
            ManagedObjects.RemoveManagedObject(g);
            SendMessage("PostObjectDeactivate", g, SendMessageOptions.DontRequireReceiver);
        }
Example #6
0
        /// <summary>
        /// gets an object from the pool, instantiating a new gameobject if necessary
        /// </summary>
        public GameObject GetObject(InstantiationData i)
        {
            GameObject g;

            bool newObj = false;

            if (transform.childCount > 0)
            {
                int ind = 0;
                while (ind < transform.childCount)
                {
                    GameObject go = transform.GetChild(ind).gameObject;
                    PoolToken  pt = go.GetComponent <PoolToken>();
                    if (pt == null || pt.usable == true)
                    {
                        g = go;
                        break;
                    }
                    ind++;
                }
                if (ind >= transform.childCount)
                {
                    throw new Exception("No usable objects in pool. This should never happen.");
                }
                g = transform.GetChild(ind).gameObject;
            }
            else
            {
                g      = LoadInactiveObject();
                newObj = true;
            }
            i.GameObject = g;

            SendMessage("PreObjectActivate", i, SendMessageOptions.DontRequireReceiver);

            g.transform.parent   = null;
            g.transform.position = i.Position;
            g.transform.rotation = i.Rotation;

            var rigidbody = g.GetComponent <Rigidbody>();

            if (rigidbody != null)
            {
                rigidbody.velocity        = Vector3.zero;
                rigidbody.angularVelocity = Vector3.zero;
            }

            var token = PoolToken.AddPoolToken(g, PrefabString, i);

            token.firstInstantiate = newObj;
            if (ObjectPool.objectPool.AutoNameObjects)
            {
                g.name = getPoolNameFromPrefabPath(PrefabString) + "." + i.OwnerId + "." + token.tokenId;
            }

            g.SetActive(prefabDefaultActive);
            SendMessage("PostObjectActivate", i, SendMessageOptions.DontRequireReceiver);

            if (!newObj && prefabDefaultActive)
            {
                g.BroadcastMessage("Awake", SendMessageOptions.DontRequireReceiver);
                g.BroadcastMessage("Start", SendMessageOptions.DontRequireReceiver);
            }

            addPlayerObject(i.OwnerId, g);
            ManagedObjects.AddManagedObject(g);
            g.BroadcastMessage("OnObjectPoolInstantiate", SendMessageOptions.DontRequireReceiver);
            return(g);
        }
Example #7
0
        internal static void RemoveManagedObject(GameObject g)
        {
            PoolToken token = ObjectPool.GetPoolToken(g);

            //all
            if (!AllObjects.ContainsKey(token.tokenId))
            {
                throw new Exception("Trying to delete nonexistent object with name: " + g.name);
            }
            AllObjects.Remove(token.tokenId);

            //all by type
            if (AllObjectsByType.ContainsKey(token.poolPrefabString))
            {
                bool containsG = AllObjectsByType[token.poolPrefabString].Contains(g);
                if (containsG)
                {
                    AllObjectsByType[token.poolPrefabString].Remove(g);
                    if (AllObjectsByType[token.poolPrefabString].Count == 0)
                    {
                        AllObjectsByType.Remove(token.poolPrefabString);
                    }
                }
                else
                {
                    Debug.LogError("Error removing managed game object. This should never happen, make sure you instantiate and delete everything properly.");
                }
            }
            else
            {
                Debug.LogError("Error removing managed game object. This should never happen, make sure you instantiate and delete everything properly.");
            }

            //player objects
            if (PlayerObjects.ContainsKey(token.ownerId))
            {
                bool containsG = PlayerObjects[token.ownerId].Contains(g);
                if (containsG)
                {
                    PlayerObjects[token.ownerId].Remove(g);
                    if (PlayerObjects[token.ownerId].Count == 0)
                    {
                        PlayerObjects.Remove(token.ownerId);
                    }
                }
                else
                {
                    Debug.LogError("Error removing managed game object. This should never happen, make sure you instantiate and delete everything properly.");
                }
            }
            else
            {
                Debug.LogError("Error removing managed game object. This should never happen, make sure you instantiate and delete everything properly.");
            }

            //player controller
            if (token.IsPlayerController)
            {
                if (!PlayerControllers.ContainsKey(token.ownerId))
                {
                    Debug.LogError("Error removing managed game object. This should never happen, make sure you instantiate and delete everything properly.");
                }
                else
                {
                    PlayerControllers.Remove(token.ownerId);
                }
            }
        }