void MoveDown() { // Move brick into field if (!isPlaced) { isPlaced = true; MoveToField(currentPosition); } else { Brick brick = field[currentPosition.x, currentPosition.y]; // Move down if brick is not at the bottom or ontop of another if (currentPosition.y > 0 && field[currentPosition.x, currentPosition.y - 1] == null) { field[currentPosition.x, currentPosition.y] = null; currentPosition.y--; field[currentPosition.x, currentPosition.y] = brick; brick.GetComponent <RectTransform>().anchoredPosition = GetBrickPosition(new Vector2(currentPosition.x, currentPosition.y)); SaveGame(); } else { isAnimating = true; landingSfx.Play(); brick.DoLandingAnimation( () => { isAnimating = false; Merge( new List <Vector2Int> { currentPosition }, () => { isFalling = false; lastPlacementPosition = currentPosition.x; currentPosition = new Vector2Int(lastPlacementPosition, 0); SpawnPlacement(currentPosition.x, previewBrick.Number); previewBrick.Number = GetRandomNumber(); SaveGame(); } ); } ); } } }
void MoveDown() { NumberedBrick brick = field[currentBrick.x, currentBrick.y]; if (currentBrick.y > 0 && field[currentBrick.x, currentBrick.y - 1] == null) { field[currentBrick.x, currentBrick.y] = null; currentBrick.y--; field[currentBrick.x, currentBrick.y] = brick; brick.GetComponent <RectTransform> ().anchoredPosition = GetBrickPosition(new Vector2(currentBrick.x, currentBrick.y)); SaveGame(); } else { isAnimating = true; landingSfx.Play(); brick.DoLandingAnimation( () => { isAnimating = false; Merge( new List <Vector2Int> { currentBrick }, () => { isFalling = false; currentBrick = new Vector2Int(bricksCount.x / 2, bricksCount.y - 1); if (field[currentBrick.x, currentBrick.y] != null) { isAnimating = true; gameState.SetField(new int[0]); UserProgress.Current.SaveGameState(name); OnGameOver(); return; } Spawn(currentBrick, nextBrick.Number); nextBrick.Number = GetRandomNumber(); nextBrick.ColorIndex = GetColorIndex(nextBrick.Number); SaveGame(); } ); } ); } }
void SpawnNewBricks(List <int> numbers, Action onComplete) { List <Vector2Int> emptyCoords = new List <Vector2Int>(); for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { if (field[x, y] == null) { emptyCoords.Add(new Vector2Int(x, y)); } } } if (numbers.Count == 0 || emptyCoords.Count == 0) { onComplete?.Invoke(); return; } int rand = Random.Range(0, emptyCoords.Count - 1); int number = numbers[0]; numbers.RemoveAt(0); Spawn(emptyCoords[rand], number) .DoLandingAnimation( () => Merge(emptyCoords[rand], true, () => SpawnNewBricks(numbers, onComplete)) ); landingSfx.Play(); }
void OnNumberedBrickClick(Brick brick) { if (isAnimating || brick == currentBrick) { return; } if (currentBrick != null) { currentBrick.DoStopBlinking(); } for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { if (field[x, y] != brick) { continue; } field[x, y].DoBlinkingAnimation(); currentBrick = field[x, y]; currentBrickCoords = new Vector2Int(x, y); clickSfx.Play(); return; } } }
public void PlayWinSound() { GameObject ap = Instantiate(audioPack); PlaySfx audioScript = ap.GetComponent <PlaySfx>(); audioScript.soundFile = winSound; audioScript.Play(); }
public void PlayButtonClick() { GameObject ap = Instantiate(audioPack); PlaySfx audioScript = ap.GetComponent <PlaySfx>(); audioScript.soundFile = buttonClick; audioScript.Play(); }
public void Spawn(GameObject prefabToSpawn) { GameObject spawned = Instantiate(prefabToSpawn, transform.position, prefabToSpawn.transform.rotation); EnemyMovement enemySpawned = spawned.GetComponent <EnemyMovement>(); enemySpawned.SetDirection(directionToSpawn); // Change speed according to level. int difficulty = gm.GetDifficulty(); enemySpawned.SetSpeed(difficulty > 1 ? enemySpawned.GetSpeed() + (difficulty - 1) * 0.75f : enemySpawned.GetSpeed()); // Play Spawn sfx PlaySfx sfx = spawned.GetComponent <PlaySfx>(); sfx.SetPitch(0, Random.Range(0.85f, 1.15f)); sfx.Play(0); }
void Merge(List <Vector2Int> toMerge, Action onComplete) { isAnimating = true; List <Vector2Int> newCoords = new List <Vector2Int> (); int animationsLeft = 0; foreach (Vector2Int coords in toMerge) { if (field[coords.x, coords.y] == null) { continue; } NumberedBrick brick = field[coords.x, coords.y]; List <Vector2Int> area = WaveAlgorithm.GetArea( field, coords, GetAdjacentCoords, b => b != null && b.Number == brick.Number ); if (area.Count < 2) { continue; } newCoords.AddRange(area); List <BrickPath> paths = new List <BrickPath> (); foreach (Vector2Int toMove in area) { if (toMove == coords) { continue; } BrickPath brickPath = new BrickPath { brick = field[toMove.x, toMove.y], path = WaveAlgorithm.GetPath( field, toMove, coords, GetAdjacentCoords, b => b != null && b.Number == brick.Number ) }; brickPath.path.RemoveAt(0); paths.Add(brickPath); } foreach (Vector2Int toMove in area) { if (toMove != coords) { field[toMove.x, toMove.y] = null; } } animationsLeft++; int areaSize = area.Count; AnimateMerge( paths, () => { animationsLeft--; if (animationsLeft > 0) { return; } mergingSfx.Play(); brick.Number *= Mathf.ClosestPowerOfTwo(areaSize); brick.ColorIndex = GetColorIndex(brick.Number); brick.DoMergingAnimation( () => { if (Random.Range(0f, 1f) < coinProbability) { UserProgress.Current.Coins++; GameObject vfx = Resources.Load <GameObject> ("CoinVFX"); vfx = Instantiate(vfx, fieldTransform.parent); vfx.transform.position = brick.transform.position; Destroy(vfx, 1.5f); } if (newCoords.Count > 0) { Normalize( normalized => { newCoords.AddRange(normalized); Merge(newCoords, onComplete); } ); } } ); gameState.Score += brick.Number; speed = GetSpeed(); // Debug.Log(speed); } ); } if (newCoords.Count > 0) { return; } isAnimating = false; onComplete.Invoke(); }
void Merge(List <Vector2Int> toMerge, Action onComplete) { isAnimating = true; List <Vector2Int> newCoords = new List <Vector2Int>(); int animationsLeft = 0; foreach (Vector2Int coords in toMerge) { if (field[coords.x, coords.y] == null) { continue; } Brick brick = field[coords.x, coords.y]; List <Vector2Int> area = WaveAlgorithm.GetArea( field, coords, b => b != null && b.Number == brick.Number ); if (area.Count < 2) { continue; } newCoords.AddRange(area); List <BrickPath> paths = new List <BrickPath>(); foreach (Vector2Int toMove in area) { if (toMove == coords) { continue; } BrickPath brickPath = new BrickPath { brick = field[toMove.x, toMove.y], path = WaveAlgorithm.GetPath( field, toMove, coords, b => b != null && b.Number == brick.Number ) }; brickPath.path.RemoveAt(0); paths.Add(brickPath); } foreach (Vector2Int toMove in area) { if (toMove != coords) { field[toMove.x, toMove.y] = null; } } animationsLeft++; int areaSize = area.Count; AnimateMerge( paths, () => { animationsLeft--; if (animationsLeft > 0) { return; } mergingSfx.Play(); brick.Number *= Mathf.ClosestPowerOfTwo(areaSize); brick.DoMergingAnimation( () => { if (newCoords.Count > 0) { Normalize( normalized => { newCoords.AddRange(normalized); Merge(newCoords, onComplete); } ); } } ); UserProgress.Current.Score += brick.Number; } ); } if (newCoords.Count > 0) { return; } isAnimating = false; onComplete.Invoke(); }
void CheckLines() { List <Vector2Int> bricksToDestroy = new List <Vector2Int>(); for (int x = 0; x < bricksCount.x; x++) { bool line = true; for (int y = 0; y < bricksCount.y; y++) { if (field[x, y] != null) { continue; } line = false; break; } if (!line) { continue; } for (int y = 0; y < bricksCount.y; y++) { Vector2Int coords = new Vector2Int(x, y); if (bricksToDestroy.Contains(coords)) { continue; } bricksToDestroy.Add(coords); } } for (int y = 0; y < bricksCount.y; y++) { bool line = true; for (int x = 0; x < bricksCount.x; x++) { if (field[x, y] != null) { continue; } line = false; break; } if (!line) { continue; } for (int x = 0; x < bricksCount.x; x++) { Vector2Int coords = new Vector2Int(x, y); if (bricksToDestroy.Contains(coords)) { continue; } bricksToDestroy.Add(coords); } } if (bricksToDestroy.Count > 0) { mergingSfx.Play(); } foreach (Vector2Int c in bricksToDestroy) { NumberedBrick brick = field[c.x, c.y]; brick.DoMergingAnimation(() => Destroy(brick.gameObject)); field[c.x, c.y] = null; gameState.Score++; } }
void FigureOnPointerUp(FigureController figureController) { Vector2Int[] coords = new Vector2Int[figureController.bricks.Count]; for (int i = 0; i < figureController.bricks.Count; i++) { Brick brick = figureController.bricks[i]; Vector2 pivot = brick.GetComponent <RectTransform>().pivot; coords[i] = BrickPositionToCoords(brick.transform.position, pivot); if (coords[i].x < 0 || coords[i].y < 0 || coords[i].x >= bricksCount.x || coords[i].y >= bricksCount.y || field[coords[i].x, coords[i].y] != null) { return; } } for (int i = 0; i < figureController.bricks.Count; i++) { Brick brick = figureController.bricks[i]; RectTransform rectTransform = brick.GetComponent <RectTransform>(); rectTransform.SetParent(fieldTransform, false); rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.zero; rectTransform.anchoredPosition = GetBrickPosition(coords[i]); field[coords[i].x, coords[i].y] = brick as NumberedBrick; gameState.Score++; } if (Random.Range(0f, 1f) < coinProbability) { UserProgress.Current.Coins++; GameObject vfx = Resources.Load <GameObject>("CoinVFX"); vfx = Instantiate(vfx, fieldTransform.parent); vfx.transform.position = figureController.transform.position; Destroy(vfx, 1.5f); } landingSfx.Play(); int index = Array.IndexOf(figureControllers, figureController); figures[index] = -1; figureController.bricks.Clear(); figureController.ResetPosition(); CheckLines(); if (figureControllers.All(c => c.bricks.Count == 0)) { SpawnNewFigures(); } SaveGame(); CheckGameOver(); }