Beispiel #1
0
        /// <summary>
        /// Returns and activates a reusable collectable
        /// </summary>
        /// <returns></returns>
        public CollectableStep NextCollectable()
        {
            ObstacleStep step = _collectablePool.Dequeue();

            step.Activate();
            return((CollectableStep)step);
        }
Beispiel #2
0
        /// <summary>
        /// Prepare a collectable to be activated
        /// </summary>
        /// <param name="collectable"></param>
        private void ResetCollectable(ObstacleStep collectable)
        {
            collectable.transform.localPosition = Vector3.zero;
            collectable.gameObject.SetActive(false);

            collectable.Init();
            _collectablePool.Enqueue(collectable);
        }
Beispiel #3
0
        /// <summary>
        /// Prepare an obstacle to be activated
        /// </summary>
        /// <param name="obstacle"></param>
        private void ResetObstacle(ObstacleStep obstacle)
        {
            // step should be the same as the obstacle param
            ObstaclePoolID FullStep = _pool.Dequeue();

            FullStep.Step.transform.localPosition = Vector3.zero;
            FullStep.Step.gameObject.SetActive(false);
            FullStep.Step.Init();
            _typedPool[FullStep.PoolId].Enqueue(FullStep);
        }
Beispiel #4
0
        /// <summary>
        /// Instanciate a configurable amount of obstacles per obstacle type
        /// to be reused, also intantiate several collectables that are reused too
        /// </summary>
        public void FillPool()
        {
            int poolIndex = 0;

            foreach (var pattern in _config.ObstaclesPatterns)
            {
                _typedPool.Add(new Queue <ObstaclePoolID>());
                while (_typedPool[poolIndex].Count < _config.MaxPoolSize)
                {
                    // Obstacles per type
                    ObstacleStep step = GameObject.Instantiate(_config.ObstaclesPatterns[poolIndex], _obstaclesParent, false);
                    step.gameObject.SetActive(false);
                    step.Init();
                    // Adding callback for when the obstacles disappear
                    step.OnDestroyEvent += ResetObstacle;
                    _typedPool[poolIndex].Enqueue(new ObstaclePoolID()
                    {
                        PoolId = poolIndex,
                        Step   = step
                    });
                }

                poolIndex++;
            }

            // We don't need more than 4 instances of collectables
            while (_collectablePool.Count < 4)
            {
                int          patternIndex = Random.Range(0, _config.CollectablesPatterns.Count);
                ObstacleStep step         = GameObject.Instantiate(_config.CollectablesPatterns[patternIndex], _obstaclesParent, false);
                step.gameObject.SetActive(false);
                step.Init();
                // Adding callback for when the collectable was not collected by the player
                step.OnDestroyEvent += ResetCollectable;
                _collectablePool.Enqueue(step);
            }
        }