Пример #1
0
 /// <summary>
 /// This method adds to the score (the value is read from the collectable),
 /// removes the collectable from the collectables list,
 /// destroys the collectable and
 /// updates the score text.
 /// </summary>
 /// <param name="collectable"></param> The collectable that calls this method.
 public void CollectableCollected(Collectable collectable)
 {
     _score += collectable.Score;
     RemoveCollectableFromList(collectable);
     Destroy(collectable.gameObject);
     UpdateScore();
 }
Пример #2
0
 void Update()
 {
     if (_collectables.Count < _maximumCollectableItems)
     {
         _collectableSpawnCountdown -= Time.deltaTime;
         if (_collectableSpawnCountdown < 0f)
         {
             _collectableSpawnCountdown = _collectableSpawnRate;
             Collectable collectable = Instantiate(_collectablePrefab);
             RandomizeCollectablePosition(collectable);
             _collectables.Add(collectable);
         }
     }
 }
Пример #3
0
        private IEnumerator Spawn()
        {
            while (!GameManager.IsClosing)
            {
                yield return(new WaitForSeconds(_spawnInterval));

                // Actually checks if the max number is already active.
                if (_store.Count > 0)
                {
                    Collectable c = _store.Pop();
                    c.transform.position = GetRandomPosition();
                    c.gameObject.SetActive(true);
                }
            }
        }
Пример #4
0
        // Use this for initialization
        void Awake()
        {
            _store    = new Stack <Collectable>();
            _instance = this;

            // Pregenerate the objects as inactive.
            for (int i = 1; i <= maxAtOnce; i++)
            {
                Collectable c = Instantiate(_prefab);
                c.gameObject.SetActive(false);
                _store.Push(c);
            }

            StartCoroutine(Spawn());
        }
Пример #5
0
        /// <summary>
        /// Spawns a collectable at a random position within the spawn area.
        /// </summary>
        private void SpawnItem()
        {
            Collectable item = collItemPool.GetPooledObject(true);

            if (item != null)
            {
                // Gets random x- and z-coordinates
                float randX = Random.Range(ItemSpawnAreaCorner1.x, ItemSpawnAreaCorner2.x);
                float randZ = Random.Range(ItemSpawnAreaCorner1.z, ItemSpawnAreaCorner2.z);

                item.transform.position = new Vector3(randX, 0, randZ);
                item.InitDefaults();
                item.SetHandler(this);

                collectables.Add(item);
            }
        }
Пример #6
0
 internal void Recycle(Collectable collectable)
 {
     collectable.gameObject.SetActive(false);
     _store.Push(collectable);
 }
Пример #7
0
 /// <summary>
 /// Returns a collectable to the pool.
 /// </summary>
 /// <param name="item">A collectable</param>
 public void ReturnItemToPool(Collectable item)
 {
     collItemPool.ReturnObject(item);
 }
Пример #8
0
 /// <summary>
 /// This method randomizes the location of the parameter collectable to within
 /// a rectangle formed between two points in the scene.
 /// </summary>
 /// <param name="collectable"></param> The collectable that is to be moved.
 private void RandomizeCollectablePosition(Collectable collectable)
 {
     collectable.transform.position = new Vector3(Random.Range(_upperLeftCorner.position.x, _lowerRightCorner.position.x),
                                                  _collectablePositionY,
                                                  Random.Range(_upperLeftCorner.position.z, _lowerRightCorner.position.z));
 }
Пример #9
0
 /// <summary>
 /// When a collectable is collected, it calls this method so it can be removed from the collectables list.
 /// </summary>
 /// <param name="collectable"></param> The collectable that is to be removed.
 public void RemoveCollectableFromList(Collectable collectable)
 {
     _collectables.Remove(collectable);
 }