示例#1
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);
        }
示例#2
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);
            }
        }