private TunnelCollectable chooseCollectable()
    {
        float[] cumulativeSumOfWeights = new float[availablePool.Count];
        float   weightSum = 0;

        for (int i = 0; i < availablePool.Count; i++)
        {
            TunnelCollectable tc = availablePool[i];
            weightSum += tc.weight;
            cumulativeSumOfWeights[i] = weightSum;
        }

        //Randomly select a collectable based upon the weights calculated above
        float rand = Random.Range(0f, weightSum);

        for (int i = 0; i < availablePool.Count; i++)
        {
            if (cumulativeSumOfWeights[i] >= rand)
            {
                TunnelCollectable chosen = takeFromPool(i);
                return(chosen);
            }
        }

        //this shouldn't happen
        return(takeFromPool(0));
    }
    private TunnelCollectable takeFromPool(int index)
    {
        TunnelCollectable collectable = availablePool [index];

        availablePool.RemoveAt(index);
        return(collectable);
    }
示例#3
0
 void OnDisable()
 {
     if (currentCollectable != null)
     {
         //currentCollectable.transform.parent = null;
         TunnelCollectableSpawner.INSTANCE.returnToPool(currentCollectable);
         currentCollectable = null;
     }
 }
 //a piece can request a collectable at a specified position, and if there is one available it will be spawned in
 public void requestCollectable(TunnelCollectableSlot slot)
 {
     if (availablePool.Count > 0)
     {
         TunnelCollectable tc = chooseCollectable();
         spawnCollectable(tc, slot);
     }
     else             //unlikely but possible
     {
         Debug.Log("No available collectables left. Ignoring request.");
     }
 }
 private void initPool()
 {
     availablePool = new List <TunnelCollectable> ();
     for (int i = 0; i < collectablePrefabs.Length; i++)
     {
         TunnelCollectable template = collectablePrefabs[i];
         for (int j = 0; j < template.numInstances; j++)
         {
             spawnNewInstance(template);
         }
     }
 }
示例#6
0
    public void PlaceCollectable(TunnelCollectable collectable)
    {
        //Debug.Log ("Placing collectable");
        currentCollectable = collectable;
        //if (moveCollectableWithSelf) {
        currentCollectable.transform.parent        = this.transform;
        currentCollectable.transform.localPosition = currentCollectable.GetStartPos();
        currentCollectable.transform.localRotation = currentCollectable.GetStartRot();

        /*} else {
         *      currentCollectable.transform.position = this.transform.position;
         * }*/
    }
 public void returnToPool(TunnelCollectable collectable)
 {
     //collectable.Reset ();
     collectable.gameObject.SetActive(false);
     availablePool.Add(collectable);
 }
 private void spawnCollectable(TunnelCollectable tc, TunnelCollectableSlot slot)
 {
     tc.Reset();
     slot.PlaceCollectable(tc);
     tc.gameObject.SetActive(true);
 }
    private void spawnNewInstance(TunnelCollectable template)
    {
        TunnelCollectable instance = Instantiate <TunnelCollectable> (template);

        returnToPool(instance);
    }