Esempio n. 1
0
        private void Update()
        {
            //Wait until level filling ends
            if (_levelIsFilling)
            {
                return;
            }

            //Wait until switching animation ends
            if (_firstTile != null && _secondTile != null)
            {
                if (_firstTile.Element.IsAnimating || _secondTile.Element.IsAnimating)
                {
                    return;
                }
            }

            //Perform matching
            if (_currentMatchingStrategy != null)
            {
                if (TryMatching(_currentMatchingStrategy))
                {
                    //Forget previous move
                    _firstTile  = null;
                    _secondTile = null;
                    StartCoroutine(FillInTheLevel());
                    return;
                }
                else
                {
                    //Undo last move
                    if (_firstTile != null && _secondTile != null)
                    {
                        SwitchElements(_firstTile, _secondTile);
                    }
                    _currentMatchingStrategy = null;
                    return;
                }
            }

            // if there is no animations get user interaction
            if (Input.GetMouseButtonDown(0))
            {
                var mousePosition = Input.mousePosition;
                mousePosition   = _mainCamera.ScreenToWorldPoint(mousePosition);
                mousePosition.z = -1.0f;

                Physics.Raycast(mousePosition, new Vector3(0, 0, 1), out _hit, _tileLayers);
                if (_hit.transform != null)
                {
                    var hitTile = _hit.transform.GetComponent <TileController> ();
                    if (hitTile != null)
                    {
                        if (_firstTile == null)
                        {
                            _firstTile = hitTile;
                            _firstTile.Element.IsSelected = true;
                        }
                        else
                        {
                            _secondTile = hitTile;
                            _secondTile.Element.IsSelected = true;

                            //Deselect item
                            if (_secondTile == _firstTile)
                            {
                                _secondTile = null;
                                _firstTile.Element.IsSelected = false;
                                _firstTile = null;
                            }
                        }

                        //Switch pair selected - perform switch
                        if (_firstTile != null && _secondTile != null)
                        {
                            _firstTile.Element.IsSelected  = false;
                            _secondTile.Element.IsSelected = false;
                            SwitchElements(_firstTile, _secondTile);

                            _currentMatchingStrategy = _firstTile.Element.Model.Strategy;
                            if (_currentMatchingStrategy == null)
                            {
                                _currentMatchingStrategy = _standartMatchingStategy;
                            }
                        }
                    }
                }
            }
        }
 public override bool TryMatch(FieldController field, TileController firstTile, TileController secondTile)
 {
     //TODO: destroy all the Elements in +-1,+-1 radius
     return(base.TryMatch(field, firstTile, secondTile));
 }
        public override bool TryMatch(FieldController field, TileController firstTile, TileController secondTile)
        {
            _field = field;

            //Loop through each element on the field
            for (var t = 0; t < _field.Rows.Count; t++)
            {
                for (var k = 0; k < _field.Rows [t].Tiles.Count; k++)
                {
                    if (_field.Rows [t].Tiles [k].IsEmpty)
                    {
                        continue;
                    }

                    var currentElement = _field.Rows [t].Tiles [k].Element;
                    _horizontalLine = new List <ElementController> ();
                    _verticalLine   = new List <ElementController> ();

                    GetHorizontalMatch(currentElement, t, k);
                    GetVerticalMatch(currentElement, t, k);

                    if (_horizontalLine.Count > 1)
                    {
                        currentElement.MarkedToDestroy = true;
                        foreach (var element in _horizontalLine)
                        {
                            element.MarkedToDestroy = true;
                        }
                    }

                    if (_verticalLine.Count > 1)
                    {
                        currentElement.MarkedToDestroy = true;
                        foreach (var element in _verticalLine)
                        {
                            element.MarkedToDestroy = true;
                        }
                    }
                }
            }

            bool retVal = false;

            foreach (var row in _field.Rows)
            {
                foreach (var tile in row.Tiles)
                {
                    if (!tile.IsEmpty && tile.Element.MarkedToDestroy)
                    {
                        retVal = true;
                        tile.Element.Destroy();
                        tile.Element = null;
                    }
                }
            }
            return(retVal);
        }