public static WPTile CreateTile(Texture2D emptyTexture, Texture2D filledTexture, float posX, float posY, bool collision, float tileSize, WPTile wpTilePrefab, bool filled = false) { WPTile wpTile = Instantiate(wpTilePrefab, new Vector3(posX, posY, 0), Quaternion.identity); wpTile.SetTextures(emptyTexture, filledTexture); wpTile.SetTileSize(tileSize); wpTile.SetTexture(filled); BoxCollider2D c = wpTile.GetComponent <BoxCollider2D>(); if (collision) { c.size = new Vector2(tileSize, tileSize); c.offset = new Vector2(tileSize / 2, tileSize / 2); } else { Destroy(c); } return(wpTile); }
public void CheckFilledTiles() { WPTile previousWpTile = _map[gridSize - 1, 0]; WPTile currentWpTile = _map[gridSize - 1, 1]; for (var y = 1; y < gridSize + 1; y++) { for (var x = 0; x < gridSize; x++) { var tile = _map[x, y]; if (tile != null) { tile.SetTexture(false); } } } while (currentWpTile != null && currentWpTile.IsFilled(previousWpTile)) { currentWpTile.SetTexture(true); Vector2 newCoordinates = currentWpTile.GetNewCoordinates(previousWpTile); int newX = (int)newCoordinates.x; int newY = (int)newCoordinates.y; previousWpTile = currentWpTile; currentWpTile = newX < gridSize && newX >= 0 ? _map[newX, newY] : null; if (currentWpTile == _endWpTile) { LevelComplete.Invoke(); currentWpTile = null; } } }
public void OnDragTile(Vector2 touch) { // allow movement only in available directions // and limit range of movement if (_selected == null || Time.timeScale == 0) { return; } _selected.SetTexture(false); // if tile is in starting position, detect if mouse movement // is predominantly horizontal or vertical and lock to that axis Vector2 gridPosition = _wpMapController.GetGridPosition(_selected.xGrid, _selected.yGrid); if (_selected.transform.position == new Vector3(gridPosition.x, gridPosition.y, 0)) { _newMousePos = GetMousePos(); if (_newMousePos != _startMousePos) { _moveHorizontal = Mathf.Abs(_newMousePos.x - _startMousePos.x) > Mathf.Abs(_newMousePos.y - _startMousePos.y); _moveVertical = !_moveHorizontal; } } Vector2 pos = new Vector2(_selected.xGrid, _selected.yGrid); // based on whether the user is trying to move the tile horizontally // or vertically, find out if there are any empty spaces in that // direction, and if so how many (to work out movement limits) if (_moveHorizontal) { _xMove = touch.x - _selected.touchOffset.x; _left = _selected.xGrid - _wpMapController.GetMoveLimit(pos, WPTile.Direction.Left); _right = _selected.xGrid + _wpMapController.GetMoveLimit(pos, WPTile.Direction.Right); _min = _wpMapController.GetGridPosition(_left, _selected.yGrid); _max = _wpMapController.GetGridPosition(_right, _selected.yGrid); if (_xMove > _max.x) { _xMove = _max.x; } if (_xMove < _min.x) { _xMove = _min.x; } _selected.transform.position = new Vector3(_xMove, _yMove, 0); } else if (_moveVertical) { _yMove = touch.y - _selected.touchOffset.y; _up = _selected.yGrid + _wpMapController.GetMoveLimit(pos, WPTile.Direction.Up); _down = _selected.yGrid - _wpMapController.GetMoveLimit(pos, WPTile.Direction.Down); _min = _wpMapController.GetGridPosition(_selected.xGrid, _down); _max = _wpMapController.GetGridPosition(_selected.xGrid, _up); if (_yMove < _max.y) { _yMove = _max.y; } if (_yMove > _min.y) { _yMove = _min.y; } _selected.transform.position = new Vector3(_xMove, _yMove, 0); } }