IEnumerator SwapTwoItemRoutine(Chip a, Chip b) { if (swaping) { yield break; } if (!a || !b) { yield break; } if (!SessionAssistant.main.CanIAnimate()) { yield break; } switch (LevelProfile.main.target) { case FieldTarget.Jelly: case FieldTarget.Score: if (SessionAssistant.main.movesCount <= 0) { yield break; } break; case FieldTarget.Timer: if (SessionAssistant.main.timeLeft <= 0) { yield break; } break; } bool mix = false; if (BombMixEffect.ContainsPair(a.chipType, b.chipType)) { mix = true; } int move = 0; SessionAssistant.main.animate++; swaping = true; Vector3 posA = a.parentSlot.transform.position; Vector3 posB = b.parentSlot.transform.position; float progress = 0; while (progress < swapDuration) { a.transform.position = Vector3.Lerp(posA, posB, progress / swapDuration); if (!mix) { b.transform.position = Vector3.Lerp(posB, posA, progress / swapDuration); } progress += Time.deltaTime; yield return(0); } a.transform.position = posB; if (!mix) { b.transform.position = posA; } a.movementID = SessionAssistant.main.GetMovementID(); b.movementID = SessionAssistant.main.GetMovementID(); if (mix) { swaping = false; BombPair pair = new BombPair(a.chipType, b.chipType); SlotForChip slot = b.parentSlot; a.HideChip(); b.HideChip(); BombMixEffect.Mix(pair, slot); SessionAssistant.main.movesCount--; SessionAssistant.main.animate--; yield break; } SlotForChip slotA = a.parentSlot; SlotForChip slotB = b.parentSlot; slotB.SetChip(a); slotA.SetChip(b); move++; int count = 0; SessionAssistant.Solution solution; solution = slotA.MatchAnaliz(); if (solution != null) { count += solution.count; } solution = slotB.MatchAnaliz(); if (solution != null) { count += solution.count; } if (count == 0 && !forceSwap) { AudioAssistant.Shot("SwapFailed"); while (progress > 0) { a.transform.position = Vector3.Lerp(posA, posB, progress / swapDuration); b.transform.position = Vector3.Lerp(posB, posA, progress / swapDuration); progress -= Time.deltaTime; yield return(0); } a.transform.position = posA; b.transform.position = posB; a.movementID = SessionAssistant.main.GetMovementID(); b.movementID = SessionAssistant.main.GetMovementID(); slotB.SetChip(b); slotA.SetChip(a); move--; } else { AudioAssistant.Shot("SwapSuccess"); } SessionAssistant.main.movesCount -= move; SessionAssistant.main.MatchingCounter(); SessionAssistant.main.animate--; swaping = false; }
// Coroutine swapping 2 chips IEnumerator SwapTwoItemRoutine(Chip a, Chip b) { // cancellation terms if (swaping) { yield break; // If the process is already running } if (!a || !b) { yield break; // If one of the chips is missing } if (a.parentSlot.slot.GetBlock() || b.parentSlot.slot.GetBlock()) { yield break; // If one of the chips is blocked } if (!SessionAssistant.main.CanIAnimate()) { yield break; // If the core prohibits animation } switch (LevelProfile.main.limitation) { case Limitation.Moves: if (SessionAssistant.main.movesCount <= 0) { yield break; } break; // If not enough moves case Limitation.Time: if (SessionAssistant.main.timeLeft <= 0) { yield break; } break; // If not enough time } bool mix = false; // The effect of mixing or not if (BombMixEffect.ContainsPair(a.chipType, b.chipType)) // Checking the possibility of mixing { mix = true; } int move = 0; // Number of points movement which will be expend SessionAssistant.main.animate++; swaping = true; Vector3 posA = a.parentSlot.transform.position; Vector3 posB = b.parentSlot.transform.position; float progress = 0; Vector3 normal = a.parentSlot.slot.x == b.parentSlot.slot.x ? Vector3.right : Vector3.up; // Animation swapping 2 chips while (progress < swapDuration) { a.transform.position = Vector3.Lerp(posA, posB, progress / swapDuration) + normal * Mathf.Sin(3.14f * progress / swapDuration) * 0.2f; if (!mix) { b.transform.position = Vector3.Lerp(posB, posA, progress / swapDuration) - normal * Mathf.Sin(3.14f * progress / swapDuration) * 0.2f; } progress += Time.deltaTime; yield return(0); } a.transform.position = posB; if (!mix) { b.transform.position = posA; } a.movementID = SessionAssistant.main.GetMovementID(); b.movementID = SessionAssistant.main.GetMovementID(); if (mix) // Scenario mix effect { swaping = false; BombPair pair = new BombPair(a.chipType, b.chipType); SlotForChip slot = b.parentSlot; a.HideChip(); b.HideChip(); BombMixEffect.Mix(pair, slot); SessionAssistant.main.movesCount--; SessionAssistant.main.animate--; yield break; } // Scenario the effect of swapping two chips SlotForChip slotA = a.parentSlot; SlotForChip slotB = b.parentSlot; slotB.SetChip(a); slotA.SetChip(b); move++; // searching for solutions of matching int count = 0; SessionAssistant.Solution solution; solution = slotA.MatchAnaliz(); if (solution != null) { count += solution.count; } solution = slotB.MatchAnaliz(); if (solution != null) { count += solution.count; } // Scenario canceling of changing places of chips if (count == 0 && !forceSwap) { AudioAssistant.Shot("SwapFailed"); while (progress > 0) { a.transform.position = Vector3.Lerp(posA, posB, progress / swapDuration) - normal * Mathf.Sin(3.14f * progress / swapDuration) * 0.2f; b.transform.position = Vector3.Lerp(posB, posA, progress / swapDuration) + normal * Mathf.Sin(3.14f * progress / swapDuration) * 0.2f; progress -= Time.deltaTime; yield return(0); } a.transform.position = posA; b.transform.position = posB; a.movementID = SessionAssistant.main.GetMovementID(); b.movementID = SessionAssistant.main.GetMovementID(); slotB.SetChip(b); slotA.SetChip(a); move--; } else { AudioAssistant.Shot("SwapSuccess"); SessionAssistant.main.swapEvent++; } SessionAssistant.main.firstChipGeneration = false; SessionAssistant.main.movesCount -= move; SessionAssistant.main.EventCounter(); SessionAssistant.main.animate--; swaping = false; }