public IEnumerator CheckMatchesAndDestroy(YWBlock firstBlock, YWBlock secondBlock) { if (!IsItMovable(firstBlock, secondBlock)) { state = GameState.UserInput; yield break; } state = GameState.Animating; yield return(StartCoroutine(Swap(firstBlock, secondBlock))); var firstMatches = blockManager.GetMatches(firstBlock); var secondMatches = blockManager.GetMatches(secondBlock); var totalMatches = firstMatches.Union(secondMatches).Distinct(); if (totalMatches.Count() < MINIMUM_BLOCK_COUNT) { yield return(StartCoroutine(Swap(firstBlock, secondBlock))); } while (totalMatches.Count() >= MINIMUM_BLOCK_COUNT) { foreach (var block in totalMatches) { block.mark.SetActive(true); } yield return(new WaitForSeconds(BLOCK_SHOW_REMOVE_DURATION)); foreach (var block in totalMatches) { blockManager.Remove(block.Column, block.Row); } score += totalMatches.Count() * SCORE_PER_BLOCK; if (OnChangedScore != null) { OnChangedScore(score); } var droppedBlocks = blockManager.GetDescendingBlocksAfterMatch(); var newBlocks = blockManager.GetNewBlocksAfterMatch(); //drop animation Drop(droppedBlocks); Drop(newBlocks); yield return(new WaitForSeconds(BLOCK_DROP_DURATION)); totalMatches = blockManager.GetMatches(droppedBlocks). Union(blockManager.GetMatches(newBlocks)).Distinct(); } state = GameState.UserInput; }
public IEnumerator Swap(YWBlock firstBlock, YWBlock secondBlock) { Vector3 tempPosition = firstBlock.transform.position; firstBlock.transform.DOMove(secondBlock.transform.position, BLOCK_SWAP_DURATION); secondBlock.transform.DOMove(tempPosition, BLOCK_SWAP_DURATION); blockManager.Blocks[firstBlock.Column, firstBlock.Row] = secondBlock; blockManager.Blocks[secondBlock.Column, secondBlock.Row] = firstBlock; firstBlock.Swap(secondBlock); yield return(new WaitForSeconds(BLOCK_SWAP_DURATION)); }
// Update is called once per frame void Update() { switch (state) { case GameState.UserInput: if (Input.GetMouseButtonDown(0)) { var blockWorldPos = gameCamera.ScreenToWorldPoint(Input.mousePosition); var hit = Physics2D.Raycast(blockWorldPos, Vector2.zero); if (hit.collider != null) { var block = hit.collider.gameObject.GetComponent <YWBlock> (); if (block != null) { selectedFirstBlock = block; state = GameState.SelectedFirstBlock; } } } break; case GameState.SelectedFirstBlock: if (Input.GetMouseButtonUp(0)) { var blockWorldPos = gameCamera.ScreenToWorldPoint(Input.mousePosition); var hit = Physics2D.Raycast(blockWorldPos, Vector2.zero); if (hit.collider != null) { var block = hit.collider.gameObject.GetComponent <YWBlock> (); if (block != null && selectedFirstBlock != null) { StartCoroutine(CheckMatchesAndDestroy(selectedFirstBlock, block)); selectedFirstBlock = null; } } else { state = GameState.UserInput; } } break; default: break; } }
protected YWBlock GenerateBlockWithColumnAndRow(int column, int row, YWBlock blockPrefab) { YWBlock block = Instantiate(blockPrefab) as YWBlock; block.transform.SetParent(this.transform, true); block.transform.localPosition = new Vector3 (originTransform.localPosition.x + column * BLOCK_SIZE, originTransform.localPosition.y + row * BLOCK_SIZE, 0.0f); block.transform.localScale = Vector3.one; block.Column = column; block.Row = row; block.Color = block.GetComponent <Image>().color; return(block); }
public void Swap(YWBlock block) { /* * Vector3 tempPos = this.transform.localPosition; * this.transform.localPosition = block.transform.localPosition; * block.transform.localPosition = tempPos; */ int tempColumn = this.Column; this.Column = block.Column; block.Column = tempColumn; int tempRow = this.Row; this.Row = block.Row; block.Row = tempRow; }
public bool IsItSame(YWBlock block) { return(this.Color.Equals(block.Color)); }
public bool IsItMovable(YWBlock firstBlock, YWBlock secondBlock) { return((Mathf.Abs(firstBlock.Row - secondBlock.Row) <= 1 && Mathf.Abs(firstBlock.Column - secondBlock.Column) <= 1) && !(Mathf.Abs(firstBlock.Row - secondBlock.Row) == 1 && Mathf.Abs(firstBlock.Column - secondBlock.Column) == 1) && !((firstBlock.Row == secondBlock.Row) && (firstBlock.Column == secondBlock.Column))); }
public IEnumerable <YWBlock> GetMatches(YWBlock block) { //* & & ! * * * * * //* * & ! & * * * * //* * * ! & & * * * List <YWBlock> horizontalMathces = new List <YWBlock> (); //From Origin To Left for (int column = block.Column; column >= 0; column--) { YWBlock newBlock = blocks [column, block.Row]; if (block.IsItSame(newBlock)) { horizontalMathces.Add(newBlock); } else { break; } } //From Origin To Right for (int column = block.Column + 1; column < COLUMN; column++) { YWBlock newBlock = blocks [column, block.Row]; if (block.IsItSame(newBlock)) { horizontalMathces.Add(newBlock); } else { break; } } if (horizontalMathces.Count < MINIMUM_BLOCK_COUNT) { horizontalMathces.Clear(); } List <YWBlock> verticalMathces = new List <YWBlock> (); //From Origin To Bottom for (int row = block.Row; row >= 0; row--) { YWBlock newBlock = blocks [block.Column, row]; if (block.IsItSame(newBlock)) { verticalMathces.Add(newBlock); } else { break; } } //From Origin To Top for (int row = block.Row + 1; row < ROW; row++) { YWBlock newBlock = blocks [block.Column, row]; if (block.IsItSame(newBlock)) { verticalMathces.Add(newBlock); } else { break; } } if (verticalMathces.Count < MINIMUM_BLOCK_COUNT) { verticalMathces.Clear(); } return(horizontalMathces.Union(verticalMathces).Distinct()); }