/// <summary> Used recursivly to move non-destroyed elements dowards</summary> public static bool MoveMatchedElementUpwards(int collum, int row, BoardManager board) { bool didSwapOccur = false; //AlexDebugger.GetInstance().AddMessage("Moving upwards " + boardInstance.elementsPositions[collumn, row].GetAttachedGameObject().name + " from position: " + collumn + ", " + row, AlexDebugger.tags.UpwardMovement); // swap with next non matched element on the collum if not on top of collum int newRow = row; if (row == 0) { // if element reached top or there are no non-matched elements above, mark this element as destroyed and non-matched //board.positionsDestroyed[collum, newRow] = true; board.matchedElemPositions[collum, newRow] = false; return(false); } Queue <int> destroyedElementsInRow = new Queue <int>(); if (board.matchedElemPositions[collum, row]) { destroyedElementsInRow.Enqueue(row); } for (int i = row - 1; i > -1; i--) { // if non-matched element found above, swap if (board.matchedElemPositions[collum, i] == false && destroyedElementsInRow.Count > 0) { //AlexDebugger.GetInstance().AddMessage("Swapping " + boardInstance.elementsPositions[collumn, row].GetAttachedGameObject().name + " with " + boardInstance.elementsPositions[collumn, i].GetAttachedGameObject().name + " at position " + collumn + ", " + i, AlexDebugger.tags.UpwardMovement); int nextEmptyPosition = destroyedElementsInRow.Dequeue(); BoardFunctions.SwapElements(board.elementsPositions[collum, nextEmptyPosition], board.elementsPositions[collum, i], board, false, ConstantValues.swappingSpeed * 2.5f); board.matchedElemPositions[collum, nextEmptyPosition] = false; board.changedPotitions[collum, nextEmptyPosition] = true; board.matchedElemPositions[collum, i] = true; destroyedElementsInRow.Enqueue(i); didSwapOccur = true; } else if (board.matchedElemPositions[collum, i] == true) { destroyedElementsInRow.Enqueue(i); } } return(didSwapOccur); }
// Update is called once per frame void Update() { // Do nothing. if (areAnimationsPlaying) { return; } switch (currentStep) { case GameStep.WaitingInput: //OnDragEvent.hasDragBegin = false; break; // Step1: check if input create matches and if true move to step2 case GameStep.CheckingInput: BoardFunctions.SwapElements(firstElement, secondElement, this, rewire: false, ConstantValues.swappingSpeed / 2); //Debug.Log("Checking for matches based on input"); // check if input create matches if (HandleInputForElement(firstElement, secondElement)) { if (firstElement.GetElementClassType() != typeof(BellBoardElement)) { HandleInputForElement(secondElement, firstElement); } //AlexDebugger.GetInstance().AddMessage("Step1 finished with matches, going to Step2: -play effects for matched elements-, input1:" + firstElement.GetAttachedGameObject().transform.name + ", input2: " + secondElement.GetAttachedGameObject().transform.name, AlexDebugger.tags.Step1); // Remove tokens MoneyManager.ChangeBalanceBy(-MoneyManager.GetSwapCost()); // Swap the elements on the board // Allow Update() to check if matches are created AddWaitMessage(); currentMessageID += 1; Server.GetServerInstance().SendMessageToClient(new Messages.ServerStatusMessage(currentMessageID, -1, false)); currentStep = GameStep.PlayingEffects; } else if (HandleInputForElement(secondElement, firstElement)) { //AlexDebugger.GetInstance().AddMessage("Step1 finished with matches, going to Step2: -play effects for matched elements-, input1:" + firstElement.GetAttachedGameObject().transform.name + ", input2: " + secondElement.GetAttachedGameObject().transform.name, AlexDebugger.tags.Step1); // Remove tokens MoneyManager.ChangeBalanceBy(-MoneyManager.GetSwapCost()); // Swap the elements on the board AddWaitMessage(); Server.GetServerInstance().SendMessageToClient(new Messages.ServerStatusMessage(currentMessageID, -1, false)); currentStep = GameStep.PlayingEffects; } else { BoardFunctions.SwapElements(firstElement, secondElement, this, rewire: false); AddWaitMessage(); AlexDebugger.GetInstance().AddMessage("No matches found, when finish, go back to step0: -waiting for input-", AlexDebugger.tags.Step1); SendMessagesToClient(); currentMessageID += 1; Server.GetServerInstance().SendMessageToClient(new Messages.ServerStatusMessage(currentMessageID, -1, true)); currentStep = GameStep.WaitingInput; } firstElement = null; secondElement = null; break; // Step2: -Play effects for matched elements- and move to step3 case GameStep.PlayingEffects: AlexDebugger.GetInstance().AddMessage("Entering Step2: -play effects for matched elements-", AlexDebugger.tags.Step2); if (PlayEffectsStep()) { // AlexDebugger.GetInstance().AddMessage("Sending total animations to client: " + playingAnimations.Count, AlexDebugger.tags.Step2); AddWaitMessage(); currentStep = GameStep.OrientingElements; } break; // Step3: repeat until non-destroyed elements have droped to lower position and then move to step4 case GameStep.OrientingElements: AlexDebugger.GetInstance().AddMessage("Entering Step3: -reorienting board-", AlexDebugger.tags.Step3); ReorientElements(); AddWaitMessage(); //AlexDebugger.GetInstance().AddMessage("Step3: -reorienting board- has finished, moving to Step4: -replace destroyed elements", AlexDebugger.tags.Step3); currentStep = GameStep.GeneratingElements; break; // Step4: -Replace destroyed elements-, by assigning new color, move to step5 case GameStep.GeneratingElements: //AlexDebugger.GetInstance().AddMessage("Entering Step4: -replace destroyed elements-", AlexDebugger.tags.Step4); GenerateNewElemetns(); //AlexDebugger.GetInstance().AddMessage("Sending total animations to client: " + playingAnimations.Count, AlexDebugger.tags.Step4); AddWaitMessage(); // AlexDebugger.GetInstance().AddMessage("Element: " + positions[bestInputCollum, bestInputRow].GetAttachedGameObject().name + " has the best score possible: " + maxOutput, AlexDebugger.tags.Step4); //AlexDebugger.GetInstance().AddMessage("Step4 -Replace destroyed elements- has finished, moving to Step5 -Aftermatch-", AlexDebugger.tags.Step4); currentStep = GameStep.CheckingBoardForMatches; break; // Step5: check for new matches case GameStep.CheckingBoardForMatches: AlexDebugger.GetInstance().AddMessage("Entering Step5: -Aftermatch-", AlexDebugger.tags.Step5); //Debug.Log("### AfterMatch ### Checking board for combos"); int newTotalMatches = CheckBoardForMatches(); // Go back to step2 if (newTotalMatches > 0) { //Debug.Log("### AfterMatch ### Total new matches found: " + newTotalMatches); AlexDebugger.GetInstance().AddMessage("Total new matches found on board: " + newTotalMatches, AlexDebugger.tags.Step5); AlexDebugger.GetInstance().AddMessage("Step5 -Aftermatch- has finished, with state -new matches found-, moving to Step2 -Play effects for matched elements-", AlexDebugger.tags.Step5); currentStep = GameStep.PlayingEffects; } // Check for possible inputs else { AlexDebugger.GetInstance().AddMessage("No new matches found, flagging potential input", AlexDebugger.tags.Step5); currentStep = GameStep.MarkingPossibleInputs; } AddWaitMessage(); break; case GameStep.MarkingPossibleInputs: //Debug.Log("### AfterMatch ### No new matches found"); if (!CheckForPossibleInputs()) { currentMessageID += 1; messagesToClient.Add(new Messages.AnimationMessage.PopBoxMessage(currentMessageID, currentMessageID - 1, "No possible inputs found. Regenerating board.", -1)); //Debug.Log("### AfterMatch ### No possible inputs found, re-generating board"); //AlexDebugger.GetInstance().AddMessage("No potential input found, all elements are flagged as destroyed", AlexDebugger.tags.Step5); MarkAllElementsAsDestroyed(); //AlexDebugger.GetInstance().AddMessage("Step5 -Aftermatch- has finished, with state -No available input-, moving to Step4 -Replace destroyed elements-", AlexDebugger.tags.Step5); currentStep = GameStep.GeneratingElements; } else { AlexDebugger.GetInstance().AddMessage("Step5 -Aftermatch- has finished, with state -Has available input-, moving to Step0 -Wait for input-", AlexDebugger.tags.Step5); currentMessageID += 1; Server.GetServerInstance().SendMessageToClient(new Messages.ServerStatusMessage(currentMessageID, -1, true)); currentStep = GameStep.WaitingInput; } AddWaitMessage(); SendMessagesToClient(); break; } }