// slides each tile on the board in the passed direction void Slide(SlideController.Direction dir) { timeSinceSlide = 0f; if (!CanMove(dir)) { return; } foreach (Transform cell in GetCells()) // for each cell on the board { GridPosition pos = cell.GetComponentInParent <GridPosition>(); // get its grid position if (!pos.GetNext(dir)) // if this position is on the edge (has no next cell in the passed direction) { for (GridPosition nextMover = pos.GetOpposite(dir); nextMover != null; nextMover = nextMover.GetOpposite(dir)) // for each position, traveling in the opposite direction { DrugTile tileToMove = nextMover.gameObject.transform.GetComponentInChildren <DrugTile>(); if (tileToMove != null) { if (tileToMove.Slide(dir)) // returns true if tile matched { IncrementScore(1); } } } } } CreateCard(dir); // create a card at the opposite direction of the swipe if (CheckGameOver()) { Invoke("GameOver", 1); } }
// slides a tile in given SlideController.Direction // returns true if the tile slides into a match, false otherwise public bool Slide(SlideController.Direction dir) { GridPosition myPos = transform.parent.gameObject.GetComponent <GridPosition>(); GridPosition newPos = myPos.GetNext(dir); if (newPos) { RectTransform newCell = newPos.GetComponent <RectTransform>(); if (newCell.childCount < 1) // if there isn't a tile in the next cell { ChangeParent(newCell); } else if (newCell.GetComponentInChildren <DrugTile>() && CheckMatch(newCell.GetComponentInChildren <DrugTile>())) // if there is a tile and they match { Instantiate(matchParticles, newCell.position - new Vector3(0, 0, 20), Quaternion.identity); ChangeParent(newCell); foreach (Transform child in newCell) { controller.tilesOnScreen.Remove(child.GetComponent <DrugTile>()); //remove this tile from the list of active tiles child.GetComponent <DrugTile>().markedToDestroy = true; } StartCoroutine(Wait(newCell)); return(true); } } return(false); }
public GridPosition GetOpposite(SlideController.Direction dir) { switch (dir) { case SlideController.Direction.Up: return(down); case SlideController.Direction.Right: return(left); case SlideController.Direction.Left: return(right); case SlideController.Direction.Down: return(up); } return(null); // should not occur }
// returns true if the tile can slide in the given direction, false otherwise public bool CanSlide(SlideController.Direction dir) { GridPosition myPos = transform.parent.gameObject.GetComponent <GridPosition>(); GridPosition newPos = myPos.GetNext(dir); if (newPos) { RectTransform newCell = newPos.GetComponent <RectTransform>(); if (newCell.childCount < 1) // if there isn't a tile in the next cell { return(true); } else if (newCell.GetComponentInChildren <DrugTile>() && CheckMatch(newCell.GetComponentInChildren <DrugTile>())) // if there is a tile and they match { return(true); } } return(false); }
// returns true if a move in the given direction is valid, false otherwise bool CanMove(SlideController.Direction dir) { foreach (Transform cell in GetCells()) // for each cell on the board { GridPosition pos = cell.GetComponentInParent <GridPosition>(); // get its grid position if (pos && !pos.GetNext(dir)) // if this position is on the edge (has no next cell in the passed direction) { for (GridPosition nextMover = pos.GetOpposite(dir); nextMover != null; nextMover = nextMover.GetOpposite(dir)) // for each position, traveling in the opposite direction { DrugTile tileToMove = nextMover.gameObject.transform.GetComponentInChildren <DrugTile>(); if (tileToMove != null) { if (tileToMove.CanSlide(dir)) { return(true); } } } } } return(false); }