// on input // check if i can move by asking tile manager private void Update() { if (btnDown && !myTile.GetIsEmpty()) { TileManager.Instance.Moved(myTile); btnDown = false; initialBtnDownPos = -Vector2.one; } }
private void SwapTilesAndDeepCpy(Vector2Int tappedTile, Vector2Int toCheckTile) { GameTileDisplay toCheckTileDisplay = tilesDictionary[toCheckTile].GetComponent <GameTileDisplay>(); GameTileDisplay movingTileDisplay = tilesDictionary[tappedTile].GetComponent <GameTileDisplay>(); GameTile copy = ScriptableObject.CreateInstance <GameTile>(); copy.CopyFrom(movingTileDisplay.myTile); copy.originalX = movingTileDisplay.myTile.originalX; copy.originalY = movingTileDisplay.myTile.originalY; copy.originalUVRect = movingTileDisplay.myTile.originalUVRect; copy.disabledColour = movingTileDisplay.myTile.disabledColour; copy.enabledColour = movingTileDisplay.myTile.enabledColour; copy.SetIsEmpty(movingTileDisplay.myTile.GetIsEmpty()); // set the moving tile to the empty tile stats movingTileDisplay.myTile.CopyFrom(toCheckTileDisplay.myTile); movingTileDisplay.SetIsEmpty(toCheckTileDisplay.myTile.GetIsEmpty()); // true if swapping with empty cell movingTileDisplay.SetImageRect(); // set the empty tile stats to the copied tile toCheckTileDisplay.myTile.CopyFrom(copy); toCheckTileDisplay.SetIsEmpty(copy.GetIsEmpty()); // false toCheckTileDisplay.SetImageRect(); }
// x1, y1 cell for the moving tile private void SwapTiles(int x1, int y1, int x2, int y2) { GameTileDisplay toCheckTile = tilesDictionary[new Vector2Int(x2, y2)].GetComponent <GameTileDisplay>(); GameTileDisplay movingTile = tilesDictionary[new Vector2Int(x1, y1)].GetComponent <GameTileDisplay>(); GameTile copy = ScriptableObject.CreateInstance <GameTile>(); copy.CopyFrom(movingTile.myTile); // save the moving tile // set the moving tile to the empty tile stats movingTile.myTile.CopyFrom(toCheckTile.myTile); movingTile.SetIsEmpty(toCheckTile.myTile.GetIsEmpty()); // true if swapping with empty cell // set the empty tile stats to the copied tile toCheckTile.myTile.CopyFrom(copy); toCheckTile.SetIsEmpty(copy.GetIsEmpty()); // false toCheckTile.SetImageRect(); Destroy(copy); }
public bool CheckMove(int startX, int startY, int dirX, int dirY) { int newX = startX + dirX; int newY = startY + dirY; bool canMove = false; if (newX < columns && newX >= 0 && newY < rows && newY >= 0) { GameTile toCheckTile = tilesDictionary[new Vector2Int(newX, newY)].GetComponent <GameTileDisplay>().myTile; // this is the empty tile if alpha is true canMove = toCheckTile.GetIsEmpty(); if (canMove) { // swap the two tiles SwapTiles(startX, startY, newX, newY); // check tiles bool won = true; for (int i = 0; i != tilesList.Count; ++i) { if (!tilesList[i].GetComponent <GameTileDisplay>().myTile.CheckCell()) { won = false; break; } } if (won) { Debug.Log("player won"); StartCoroutine(RestartGameCoroutine()); } } else { } // do nothing } return(canMove); }