private void CreateToken(int gridX, int gridY, bool avoidMatches, bool withMove) { Vector3 newPos; if (withMove) { bool isDown = (collapseDirection == MoveDirection.Down); float columnsVerticalOffset = -Mathf.Abs(gridX - gridSize.x / 2) * tokenSize.y * generationTokensRelativeDistance; newPos = GridToWorldPosition( gridX, gridY + (isDown ? -1 : 1) * (gridSize.y)) + Vector3.up * (isDown ? -1 : 1) * columnsVerticalOffset; } else { newPos = GridToWorldPosition(gridX, gridY); } GameObject tokenObj = Instantiate(Settings.main.tokens.prefab, newPos, Quaternion.identity, transform); MaskToken token = tokenObj.GetComponent <MaskToken>(); token.previousGridPosition = new Vector2Int(-1, -1); SetTokenInGrid(token, gridX, gridY); UpdateTokenPosition(token, Settings.main.tokens.collapseTime); token.wasMoved = false; if (avoidMatches) { List <int> unwantedElements = new List <int>(); if (gridY > 1) { if (tokens[gridY - 1, gridX].elementId == tokens[gridY - 2, gridX].elementId) { unwantedElements.Add(tokens[gridY - 1, gridX].elementId); } } if (gridX > 1) { if (tokens[gridY, gridX - 1].elementId == tokens[gridY, gridX - 2].elementId) { unwantedElements.Add(tokens[gridY, gridX - 1].elementId); } } token.SetRandomElement(unwantedElements.ToArray()); } else { token.SetRandomElement(); } token.UpdateSpriteImmediate(); tokenObj.name = Settings.main.elements[token.elementId].name + "(" + gridX + ", " + gridY + ")"; }
private void RefillColumn(int col, int nullCount, bool isDown) { // create new tokens and collapse them for (int p = 0; p < nullCount; p++) { int initialRow = isDown ? (-p - 1) : (gridSize.y + p); int finalRow = initialRow + (isDown ? 1 : -1) * nullCount; GameObject tokenObj = Instantiate(Settings.main.tokens.prefab, GridToWorldPosition(col, initialRow), Quaternion.identity, transform); MaskToken token = tokenObj.GetComponent <MaskToken>(); token.previousGridPosition = new Vector2Int(-1, -1); token.SetRandomElement(); tokenObj.name = Settings.main.elements[token.elementId].name + "(" + col + ", " + finalRow + ")"; SetTokenInGrid(token, col, finalRow); token.BeginMovingToPosition(GridToWorldPosition(token.gridPosition), Settings.main.tokens.collapseTime); token.wasMoved = false; } }