private void Unselect() { // Hide the selection ui this.isSelected = false; Match3Block.currentSelection = null; this.Selection.gameObject.SetActive(false); }
private void Select() { // Show the selection ui this.isSelected = true; Match3Block.currentSelection = this; this.Selection.gameObject.SetActive(true); }
public static Match3Block Create(Match3Block prefab, int x, int y, Vector3 position) { // Create a new block and setup the params Match3Block block = GameObject.Instantiate <Match3Block>(prefab, position, prefab.transform.rotation); block.BoardX = x; block.BoardY = y; return(block); }
public bool IsNextTo(Match3Block block) { // Check up, down, left, and right if ((this.BoardX == block.BoardX && (this.BoardY == block.BoardY - 1 || this.BoardY == block.BoardY + 1)) || (this.BoardY == block.BoardY && (this.BoardX == block.BoardX - 1 || this.BoardX == block.BoardX + 1))) { return(true); } return(false); }
private bool CheckPositionMatch(Match3Block toCheck, int x, int y, ref int count, ref List <Match3Block> found) { // If they match add it to the list and return true var other = DemoMatch3.Instance.GetBlockAt(x, y); if (toCheck.IsMatchTo(other)) { found.Add(other); count++; return(true); } // Not a match return(false); }
private void BeginSwap(Match3Block other) { // Ignore matching blocks since a swap would do nothing if (this.IsMatchTo(other)) { return; } Match3Block.isPerformingSwap = true; this.otherSwappingBlock = other; this.swapTime = 0; this.swapPosition1 = this.transform.position; this.swapPosition2 = other.transform.position; }
/// <summary> /// Begin the mechanism for find matches and updating the board /// </summary> /// <param name="start">One block that was just moved</param> /// <param name="other">The other block that was moved</param> public void StartFindingMatchesForSwap(Match3Block start, Match3Block other) { // Set the flag to prevent any other swaps from taking place until we are done this.IsRunningBoardChange = true; // Find matches with the first block List <Match3Block> found = new List <Match3Block>(); int xCount; int yCount; this.FindMatchesFrom(start, ref found, out xCount, out yCount); if (xCount >= 2 || yCount >= 2) { found.Add(start); this.ClearMatches(found); } // Find matches with the second block found.Clear(); this.FindMatchesFrom(other, ref found, out xCount, out yCount); if (xCount >= 2 || yCount >= 2) { found.Add(other); this.ClearMatches(found); } // Fill up the board by dropping blocks from the top int count = this.RefillBoard(); if (count > 0) { this.Animate(this.player); } else { turnCount++; if (turnCount > 1) { turnCount = 0; this.Animate(this.enemy); } } // Clear the flag to allow swaps again this.IsRunningBoardChange = false; }
void Start() { // Create a new board to store the blocks this.board = new Match3Block[this.Width, this.Height]; // Calculate the start position to center the board Vector2 offset = BlockPrefab.GetComponent <SpriteRenderer>().bounds.size * 0.9f; float startX = this.BoardPosition.position.x - offset.x * this.Width * 0.5f + offset.x * 0.5f; float startY = this.BoardPosition.position.y - offset.y * this.Height * 0.5f + offset.y * 0.5f; Sprite[] previousLeft = new Sprite[this.Height]; Sprite previousBelow = null; // Add each block making sure they don't immediately match for (int x = 0; x < this.Width; x++) { for (int y = 0; y < this.Height; y++) { // Create the block Vector3 position = new Vector3(startX + (offset.x * x), startY + (offset.y * y), 0); Match3Block block = Match3Block.Create(this.BlockPrefab, x, y, position); // Start out with all sprites available and remove matching ones List <Sprite> possibleBlocks = new List <Sprite>(); possibleBlocks.AddRange(this.BlockSprites); possibleBlocks.Remove(previousLeft[y]); possibleBlocks.Remove(previousBelow); // Set the new sprite Sprite picked = possibleBlocks[Random.Range(0, possibleBlocks.Count)]; block.SetSprite(picked); previousLeft[y] = picked; previousBelow = picked; // Store it in our board this.board[x, y] = block; } } // Add an enemy this.SpawnEnemy(); }
private void FindMatchesFrom(Match3Block startBlock, ref List <Match3Block> found, out int xCount, out int yCount) { xCount = 0; yCount = 0; // Ignore blocks with no sprite if (startBlock.IsEmpty) { return; } // Loop through left and right looking for matches, quit when we reach the edge or a non-matching block for (int x = startBlock.BoardX - 1; x >= 0; x--) { if (!CheckPositionMatch(startBlock, x, startBlock.BoardY, ref xCount, ref found)) { break; } } for (int x = startBlock.BoardX + 1; x < this.Width; x++) { if (!CheckPositionMatch(startBlock, x, startBlock.BoardY, ref xCount, ref found)) { break; } } // Loop through up and down looking for matches, quit when we reach the edge or a non-matching block for (int y = startBlock.BoardY - 1; y >= 0; y--) { if (!CheckPositionMatch(startBlock, startBlock.BoardX, y, ref yCount, ref found)) { break; } } for (int y = startBlock.BoardY + 1; y < this.Height; y++) { if (!CheckPositionMatch(startBlock, startBlock.BoardX, y, ref yCount, ref found)) { break; } } }
private void EndSwap() { Match3Block.isPerformingSwap = false; Match3Block other = this.otherSwappingBlock; this.otherSwappingBlock = null; // Swap the sprites Sprite temp = other.spriteRenderer.sprite; other.spriteRenderer.sprite = this.spriteRenderer.sprite; this.spriteRenderer.sprite = temp; this.transform.position = this.swapPosition1; other.transform.position = this.swapPosition2; // Look for 3's in a row DemoMatch3.Instance.StartFindingMatchesForSwap(this, other); }
public void MoveSpriteFrom(Match3Block source) { // Cut and paste the sprite this.spriteRenderer.sprite = source.spriteRenderer.sprite; source.spriteRenderer.sprite = null; }
public bool IsMatchTo(Match3Block other) { // Do the blocks have the same sprite? return(!this.IsEmpty && other.spriteRenderer.sprite == this.spriteRenderer.sprite); }