// A co-routine which moves the tile into its destination. IEnumerator Moving(GridManager.XY xy) { GridManager.ReportTileMovement(); // Report to the GridManager that this tile is moving so it doesn't check for matches. Vector2 destination = new Vector2(xy.X, xy.Y); bool moving = true; while (moving) { transform.position = Vector2.MoveTowards(transform.position, destination, 5f * Time.deltaTime); if (Vector2.Distance(transform.position, destination) <= 0.1f) { transform.position = destination; moving = false; } yield return(null); } yield return(new WaitForSeconds(0.1f)); // wilol probably have a wait thingie when the tiles actually get matched for animation purposes. MyXY = xy; gameObject.name = xy.X + "/" + xy.Y; // Not necessary, just helps with the overview in the Hierarchy. GridManager.ReportTileStopped(); // Report to the GridManager that this tile is done moving. }
// Tries to select and move a tile if the player left-clicks and another tile has already been selected. void AttemptMove() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 50f, Tiles); if (hit) { if (Vector2.Distance(activeTile.transform.position, hit.collider.gameObject.transform.position) <= 1.25f) { TileControl activeControl = activeTile.GetComponent <TileControl>(); TileControl hitControl = hit.collider.gameObject.GetComponent <TileControl>(); GridManager.XY activeXY = activeControl.MyXY; GridManager.XY hitXY = hitControl.MyXY; activeControl.Move(hitXY); hitControl.Move(activeXY); gridManager.SwitchTiles(hitXY, activeXY); activeTile = null; } } }
public void Move(GridManager.XY xy) { StartCoroutine(Moving(xy)); }