void SpawnNextBrick() { nextBrick = Instantiate(brickPrefab, nextBrickPoint); nextBrick.GetComponent <RectTransform> ().anchorMin = new Vector2(0.5f, 0.5f); nextBrick.GetComponent <RectTransform> ().anchorMax = new Vector2(0.5f, 0.5f); nextBrick.GetComponent <RectTransform> ().anchoredPosition = Vector2.zero; }
void Start() { field = new NumberedBrick[bricksCount.x, bricksCount.y]; for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { SpawnEmptyBrick(new Vector2Int(x, y)); } } SpawnNextBricks(); gameState = UserProgress.Current.GetGameState <GameState>(name); if (gameState == null) { gameState = new GameState(); UserProgress.Current.SetGameState(name, gameState); } UserProgress.Current.CurrentGameId = name; if (LoadGame()) { return; } gameState.Score = 0; SpawnStartingBricks(); }
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; } } }
void Normalize(Action <List <Vector2Int> > onComplete) { List <Vector2Int> normalized = new List <Vector2Int> (); for (int x = 0; x < field.GetLength(0); x++) { for (int y = 0; y < field.GetLength(1); y++) { NumberedBrick brick = field[x, y]; if (brick == null) { continue; } int yEmpty = y; while (yEmpty > 0 && field[x, yEmpty - 1] == null) { yEmpty--; } if (yEmpty == y) { continue; } field[x, y] = null; field[x, yEmpty] = brick; Vector2Int brickCoords = new Vector2Int(x, yEmpty); normalized.Add(brickCoords); bool isFirst = normalized.Count == 1; brick.DoLocalMove( GetBrickPosition(brickCoords), () => { if (isFirst) { brick.DoLandingAnimation(() => onComplete.Invoke(normalized)); landingSfx.Play(); } else { brick.DoLandingAnimation(null); } } ); } } if (normalized.Count == 0) { onComplete.Invoke(normalized); } }
void SpawnNextBricks() { nextBricks = new NumberedBrick[nextBricksCount]; for (int i = 0; i < nextBricksCount; i++) { NumberedBrick brick = Instantiate(brickPrefab, nextBricksParent); brick.Number = GetRandomNumber(); brick.ColorIndex = GetColorIndex(brick.Number); nextBricks[i] = brick; } }
void OnEmptyBrickClick(Vector2Int coords) { if (isAnimating || currentBrick == null || field[coords.x, coords.y] != null) { return; } currentBrick.DoStopBlinking(); List <Vector2Int> path = WaveAlgorithm.GetPath(field, currentBrickCoords, coords, GetAdjacentCoords, brick => brick == null); if (path.Count < 2) { currentBrick = null; NoPath?.Invoke(); return; } field[currentBrickCoords.x, currentBrickCoords.y] = null; field[coords.x, coords.y] = currentBrick; int number = currentBrick.Number; List <Vector2> localPath = new List <Vector2>(path.Count); path.ForEach(c => localPath.Add(GetBrickPosition(c))); currentBrick.DoLocalPath( localPath, () => { List <Vector2Int> area = WaveAlgorithm.GetArea(field, coords, GetAdjacentCoords, b => b != null && b.Number == number); if (area.Count < bricksToMerge) { SpawnNewBricks( () => { SaveGame(); CheckGameOver(); } ); } else { Merge(coords, true, SaveGame); } } ); currentBrick = null; }
void SpawnNewBricks(Action onComplete) { isAnimating = true; bool spawned = false; int yMin = int.MaxValue; for (int y = 0; y < bricksCount.y; y++) { for (int x = 0; x < bricksCount.x; x++) { if (field[x, y] != null) { continue; } yMin = Mathf.Min(yMin, y); Vector2Int coords = new Vector2Int(x, y); NumberedBrick brick = Spawn(coords, GetRandomNumber()); brick.GetComponent <RectTransform>().anchoredPosition = GetBrickPosition(new Vector2Int(x, bricksCount.y + y - yMin)); brick.DoLocalMove( GetBrickPosition(coords), () => brick.DoLandingAnimation( () => { isAnimating = false; if (onComplete != null) { onComplete.Invoke(); } } ) ); spawned = true; } } if (!spawned) { isAnimating = false; if (onComplete != null) { onComplete.Invoke(); } } 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 Spawn(Vector2Int coords) { NumberedBrick brick = Instantiate(brickPrefab, fieldTransform); brick.transform.SetParent(fieldTransform, false); brick.GetComponent <RectTransform>().anchorMin = Vector2.zero; brick.GetComponent <RectTransform>().anchorMax = Vector2.zero; brick.GetComponent <RectTransform>().anchoredPosition = GetBrickPosition(coords); brick.ColorIndex = Random.Range(0, 6); field[coords.x, coords.y] = brick; }
void Spawn(Vector2Int coords, int number) { NumberedBrick brick = Instantiate(brickPrefab, fieldTransform); brick.transform.SetParent(fieldTransform, false); brick.GetComponent <RectTransform> ().anchorMin = Vector2.zero; brick.GetComponent <RectTransform> ().anchorMax = Vector2.zero; brick.GetComponent <RectTransform> ().anchoredPosition = GetBrickPosition(new Vector2(coords.x, coords.y)); brick.Number = number; brick.ColorIndex = GetColorIndex(number); field[coords.x, coords.y] = brick; }
void Move(int value) { if (value < 0 || value >= field.GetLength(0) || field[value, currentBrick.y] != null) { return; } NumberedBrick brick = field[currentBrick.x, currentBrick.y]; field[currentBrick.x, currentBrick.y] = null; currentBrick.x = value; field[currentBrick.x, currentBrick.y] = brick; brick.GetComponent <RectTransform> ().anchoredPosition = GetBrickPosition(new Vector2(currentBrick.x, currentBrick.y)); }
int GetRandomNumber() { int maxNumber = 0; for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { NumberedBrick brick = field[x, y]; if (brick != null) { maxNumber = Mathf.Max(maxNumber, brick.Number); } } } return(Random.Range(1, Mathf.Clamp(maxNumber, 4, 6))); }
void Start() { inputController.gameObject.SetActive(controls == Controls.Swipe); tapInputController.gameObject.SetActive(controls == Controls.Tap); if (controls == Controls.Swipe) { InputController.Left += OnLeft; InputController.Right += OnRight; InputController.Down += OnDown; } else { tapInputController.PointerDown += OnTapMove; tapInputController.PointerDrag += OnTapMove; tapInputController.PointerUp += OnDown; SpawnColumns(); } SpawnNextBrick(); field = new NumberedBrick[bricksCount.x, bricksCount.y]; gameState = UserProgress.Current.GetGameState <GameState2048Bricks> (name); if (gameState == null) { gameState = new GameState2048Bricks(); UserProgress.Current.SetGameState(name, gameState); } UserProgress.Current.CurrentGameId = name; if (LoadGame()) { return; } gameState.Score = 0; SpawnStartingBricks(); nextBrick.Number = GetRandomNumber(); nextBrick.ColorIndex = GetColorIndex(nextBrick.Number); speed = GameDesignConstantsBehaviour.Instance.GameDesignConstants.BaseSpeed; }
void SpawnFigure(FigureController figureController, int figureIndex, float rotation) { figureController.transform.localRotation = Quaternion.identity; int colorIndex = Random.Range(0, 6); int[,] figure = Figures1010.Figures[figureIndex]; for (int i = 0; i < figure.GetLength(0); i++) { for (int j = 0; j < figure.GetLength(1); j++) { if (figure[figure.GetLength(0) - i - 1, j] == 0) { continue; } NumberedBrick brick = Instantiate(brickPrefab, figureController.transform); figureController.bricks.Add(brick); RectTransform brickRectTransform = brick.GetComponent <RectTransform>(); brickRectTransform.anchorMin = new Vector2(0.5f, 0.5f); brickRectTransform.anchorMax = new Vector2(0.5f, 0.5f); Rect rect = figureController.GetComponent <RectTransform>().rect; Vector2 brickSize = new Vector2 { x = rect.width / 4, y = rect.height / 4 }; Vector2 coords = new Vector2(j - figure.GetLength(1) / 2f, i - figure.GetLength(0) / 2f); Vector2 brickPosition = Vector2.Scale(coords, brickSize); brickPosition += Vector2.Scale(brickSize, brickRectTransform.pivot); brick.GetComponent <RectTransform>().anchoredPosition = brickPosition; brick.ColorIndex = colorIndex; } } figureController.transform.localRotation = Quaternion.Euler(0f, 0f, rotation); }
void Start() { field = new NumberedBrick[bricksCount.x, bricksCount.y]; gameState = UserProgress.Current.GetGameState <GameState>(name); if (gameState == null) { gameState = new GameState(); UserProgress.Current.SetGameState(name, gameState); } UserProgress.Current.CurrentGameId = name; if (LoadGame()) { return; } gameState.Score = 0; SpawnStartingBricks(); }
void Start() { field = new NumberedBrick[bricksCount.x, bricksCount.y]; for (int x = 0; x < bricksCount.x; x++) { for (int y = 0; y < bricksCount.y; y++) { SpawnEmptyBrick(new Vector2Int(x, y)); } } gameState = UserProgress.Current.GetGameState <GameState1010>(name); if (gameState == null) { gameState = new GameState1010(); UserProgress.Current.SetGameState(name, gameState); } UserProgress.Current.CurrentGameId = name; foreach (FigureController figureController in figureControllers) { figureController.PointerUp += FigureOnPointerUp; } if (LoadGame()) { return; } gameState.Score = 0; SpawnNewFigures(); SpawnStartingBricks(); }
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 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++; } }