示例#1
0
        public void Initialize()
        {
            _brickViewInstances.CollectionChanged += BrickViewInstancesOnCollectionChanged;

            _gameObjectsContainerInstance = Instantiate(GameObjectsContainerPrefab);

            _fieldViewInstance = InstantiateElement(FieldViewPrefab, _gameObjectsContainerInstance);

            _bricksContainerInstance = InstantiateElement(BricksContainerPrefab, _gameObjectsContainerInstance, new Vector3(0, 3.5f, 0));
            _bricksContainerInstance.BrickPositioningCompleted += BricksContainerInstanceOnBrickPositioningCompleted;
            var brickTypes = Enum.GetValues(typeof(BrickType)).Cast <BrickType>().Reverse();

            foreach (var brickType in brickTypes)
            {
                for (var j = 0; j < bricksCountPerType; j++)
                {
                    var brickViewInstance = InstantiateElement <BrickView>(BrickViewPrefab, _bricksContainerInstance.transform);
                    brickViewInstance.Initialize(brickType);
                    brickViewInstance.WasDestroyed += BrickViewInstanceOnWasDestroyed;

                    _brickViewInstances.Add(brickViewInstance);
                }
            }

            if (_randomLevelGeneration)
            {
                foreach (var brickViewInstance in _brickViewInstances)
                {
                    var destroyBrick = _random.Next(0, 2);
                    if (destroyBrick > 0)
                    {
                        _brickViewInstancesForDestruction.Add(brickViewInstance);
                    }
                }
            }

            if (_advantageousPositioningOfTargetBrick)
            {
                var randomBrick = _brickViewInstances.Last();
                randomBrick.Targeted = true;
            }
            else
            {
                var randomBrick = _brickViewInstances[_random.Next(_brickViewInstances.Count)];
                randomBrick.Targeted = true;
            }

            _racketViewInstance = InstantiateElement(RacketViewPrefab, _fieldViewInstance.gameObject.transform, new Vector3(0, -4.5f, 0));

            _ballViewInstance = InstantiateElement(BallViewPrefab, _fieldViewInstance.gameObject.transform, new Vector3(0, -1f, 0));
        }
示例#2
0
        public void Uninitialize()
        {
            _brickViewInstances.CollectionChanged -= BrickViewInstancesOnCollectionChanged;

            foreach (var brickViewInstance in _brickViewInstances)
            {
                brickViewInstance.WasDestroyed -= BrickViewInstanceOnWasDestroyed;
                if (brickViewInstance != null)
                {
                    LeanPool.Despawn(brickViewInstance.gameObject);
                }
            }
            _brickViewInstances.Clear();

            if (_bricksContainerInstance != null)
            {
                Destroy(_bricksContainerInstance.gameObject);
                _bricksContainerInstance = null;
            }

            if (_fieldViewInstance != null)
            {
                Destroy(_fieldViewInstance.gameObject);
                _fieldViewInstance = null;
            }

            if (_racketViewInstance != null)
            {
                Destroy(_racketViewInstance.gameObject);
                _racketViewInstance = null;
            }

            if (_ballViewInstance != null)
            {
                Destroy(_ballViewInstance.gameObject);
                _ballViewInstance = null;
            }

            if (_gameObjectsContainerInstance != null)
            {
                Destroy(_gameObjectsContainerInstance.gameObject);
                _gameObjectsContainerInstance = null;
            }
        }