Exemple #1
0
    // Retourne le nom de la PoolingScript si celle-ci correspond au type recherché
    public string RandomPoolName(string subname, string index = null)
    {
        OrderedPools list;

        if (null == index || !indexedPools.TryGetValue (index, out list)) {
            // Si on n'a pas spécifié d'index ou qu'on a pas trouvé la liste correspondante à l'index, on prend dans toutes les pools
            list = new OrderedPools (poolCollection);

            // On choisit un élément au hasard correspondant au subname et le retourne
            int random = Random.Range (0, list.pools.Count);

            if (list.pools [random].poolName.ToLower ().Contains (subname.ToLower ())) {
                Debug.Log ("New block (non index) from: " + list.pools [random].poolName);
                return list.pools [random].poolName;
            }
        } else {
            // Si on trouve une liste dans le test précédent

            // On prend le prochain élément dans la liste
            // ex : index = difficulty_1, subname = block -- on prend dans les pools du dico indexedPools avec key = difficulty_1
            string listElement;
            listElement = list.pools [list.currentIndex].poolName;
            list.currentIndex++; // On passe à l'élément suivant

            // Mélange de la liste si on a atteint le bout
            // On aurait pu le faire à la prochaine itération, mais en le faisant ici on permet d'éviter une surcharge du CPU si le shuffle intervient au mauvais moment
            // Ici, un block est déjà créé
            if (list.currentIndex >= list.pools.Count) {
                ShuffleList (list.pools);
                list.currentIndex = 0;
            }

            Debug.Log ("New block from: " + listElement);
            return listElement;
        }

        return "";
    }
Exemple #2
0
    void Awake()
    {
        if (current == null) {
            current = this;
            pooledObjectParent = GameObject.Find ("PooledObjects").transform;
            //DontDestroyOnLoad (gameObject);
        } else if (current != this) {
            Destroy (gameObject);
        }

        indexedPools.Clear ();
        pools.Clear ();

        // On nettoie tout pour éviter qu'il y ait des blocs actifs quand on reboot
        foreach (Transform obj in pooledObjectParent.GetComponentInChildren<Transform> ()) {
            obj.gameObject.SetActive (false);
        }

        foreach(PoolingScript pool in poolCollection)
        {
            pool.Init();

            pools.Add(pool.poolName, pool);

            // S'il y a un index, on ajoute dans un dictionnaire de listes
            if( "" != pool.poolIndex ) {
                OrderedPools orderedPools;

                // On vérifie si la valeur existe déjà, et on ajoute dans la bonne key
                if( !indexedPools.TryGetValue( pool.poolIndex, out orderedPools ) ) {
                    orderedPools = new OrderedPools (new List<PoolingScript>());
                    //indexedPools.Add( pool.poolIndex, list );
                    indexedPools.Add( pool.poolIndex, orderedPools );
                }
                orderedPools.pools.Add (pool);
            }
        }

        // On mélange une première fois les listes indexées
        foreach (KeyValuePair<string, OrderedPools> entry in indexedPools) {
            ShuffleList (entry.Value.pools, true);
        }
    }