/// <summary> /// Swap of two units. /// </summary> /// <param name="units"></param> private void SwapTwoUnits(params UnitInfo[] units) { if (units == null || units.Length != 2) { return; } SetGameState(Match3GameStates.Swap); for (int i = 0; i < 2; i++) { UnitsToMove.Add(units[i]); _unitsPossToCheckMatches.Add(units[i].MPos); //Replace links for swapped units in the common array of units. _unitsArray[units[i].MPos.X, units[i].MPos.Y] = units[i == 0 ? 1 : 0]; } //Replace positions for swapped units. Position temp = units[0].MPos; units[0].SetNewPosition(units[1].MPos); units[1].SetNewPosition(temp); }
/// <summary> /// Check for matches. /// If successful, start displaying the received changes in the units, by performing sequentially game states. /// </summary> /// <returns></returns> public bool CheckForMatches() { //Exit if there are no places to check new matches. if (_unitsPossToCheckMatches.Count == 0) { return(false); } //Received changes in the units. List <Position> matchedUnits; List <PositionVector> fallingUnits; List <UnitData> newUnits; _game.DoMatchesPipeline(out matchedUnits, out fallingUnits, out newUnits, _unitsPossToCheckMatches.ToArray()); UnitsToMove.Clear(); _unitsPossToCheckMatches.Clear(); //Exit if there are no new matches found. if (matchedUnits == null || matchedUnits.Count == 0) { return(false); } //We keep the places of the matched units and units that are going to fall, to check them for new matches in the future. foreach (PositionVector posv in fallingUnits) { _unitsPossToCheckMatches.Add(posv.From); } foreach (Position pos in matchedUnits) { _unitsPossToCheckMatches.Add(pos); } //Falling units keep their target positions and are added to the list of units to be moved. foreach (PositionVector pos in fallingUnits) { UnitInfo unit = _unitsArray[pos.From.X, pos.From.Y]; unit.SetNewPosition(pos.To); UnitsToMove.Add(unit); } //Matched units are highlighted and take on new positions and colors for rebirth. And they are added to the list of units to be moved (die and reborn). for (int i = 0; i < newUnits.Count; i++) { Position pos = matchedUnits[i]; UnitInfo unit = _unitsArray[pos.X, pos.Y]; unit.SetNewPosition(newUnits[i].Pos); unit.SetNewUnitIdAndColor(newUnits[i].Id, _colors[newUnits[i].Id]); unit.ShowShadow(); UnitsToDieAndReborn.Add(unit); } //Replacing links to units according to their new positions (in which they have to be after their movements). foreach (UnitInfo unit in UnitsToDieAndReborn) { _unitsArray[unit.MPos.X, unit.MPos.Y] = unit; } foreach (UnitInfo unit in UnitsToMove) { _unitsArray[unit.MPos.X, unit.MPos.Y] = unit; } return(true); }