public void SwapTiles(WPTile selected, int xSnap, int ySnap) { _map[xSnap, ySnap] = selected; _map[selected.xGrid, selected.yGrid] = null; selected.xGrid = xSnap; selected.yGrid = ySnap; }
private void WriteTile(JsonWriter writer, string device, WPTile tile) { if (tile == null) return; writer.WritePropertyName(device); if (tile.WPTileType == WPTileType.Flip) WriteFlipTile(writer, tile as FlipTile); if (tile.WPTileType == WPTileType.Cyclic) WriteCyclicTile(writer, tile as CyclicTile); if (tile.WPTileType == WPTileType.Iconic) WriteIconicTile(writer, tile as IconicTile); }
private WPTile[,] GenerateTileMap(int[,] map) { _gridPositions = new Vector2[gridSize, gridSize + 2]; var tileMap = new WPTile[gridSize, gridSize + 2]; float halfGridSize = gridSize / 2.0f; var startTile = WPTile.CreateTile(startTextureEmpty, startTexture, (gridSize - 1 - halfGridSize) * _tileSize, (0 - halfGridSize) * _tileSize * -1, false, _tileSize, wpTilePrefab, true); startTile.xGrid = gridSize - 1; startTile.yGrid = 0; startTile.end1 = WPTile.Direction.Down; startTile.end2 = WPTile.Direction.Down; tileMap[gridSize - 1, 0] = startTile; for (var y = 1; y < gridSize + 1; y++) { for (var x = 0; x < gridSize; x++) { var tileType = map[x, y - 1] - 1; var posX = (x - halfGridSize) * _tileSize; var posY = (y - halfGridSize) * _tileSize * -1; if (tileType != -1) { var tile = WPTile.CreateTile(tileTexturesEmpty[tileType], tileTexturesFull[tileType], posX, posY, true, _tileSize, wpTilePrefab); tile.xGrid = x; tile.yGrid = y; tile.end1 = WPTile.GetDirectionFromInt((int)tileBehaviours[tileType].x); tile.end2 = WPTile.GetDirectionFromInt((int)tileBehaviours[tileType].y); tile.TouchDelegate = _wpDragController.OnTouchTile; tile.DragDelegate = _wpDragController.OnDragTile; tile.ReleaseDelegate = _wpDragController.OnReleaseTile; tileMap[x, y] = tile; } _gridPositions[x, y] = new Vector2(posX, posY); } } var endTile = WPTile.CreateTile(endTexture, endTextureFilled, (0 - halfGridSize) * _tileSize, (gridSize + 1 - halfGridSize) * _tileSize * -1, false, _tileSize, wpTilePrefab); endTile.xGrid = 0; endTile.yGrid = gridSize + 1; _endWpTile = endTile; tileMap[0, gridSize + 1] = endTile; return(tileMap); }
public Vector2 GetNewCoordinates(WPTile previousWpTile) { int newX = xGrid; int newY = yGrid; switch (end1) { case Direction.Up: newY -= 1; break; case Direction.Right: newX += 1; break; case Direction.Down: newY += 1; break; case Direction.Left: newX -= 1; break; } if (newX == previousWpTile.xGrid && newY == previousWpTile.yGrid) { newX = xGrid; newY = yGrid; switch (end2) { case Direction.Up: newY -= 1; break; case Direction.Right: newX += 1; break; case Direction.Down: newY += 1; break; case Direction.Left: newX -= 1; break; } } return(new Vector2(newX, newY)); }
public void OnTouchTile(GameObject tile) { if (_selected != null || Time.timeScale == 0) { return; } _selected = tile.GetComponent <WPTile>(); _startMousePos = GetMousePos(); _moveHorizontal = _moveVertical = false; var position = _selected.transform.position; _yMove = position.y; _xMove = position.x; }
public void OnReleaseTile(GameObject tile) { if (Time.timeScale != 0 && tile == _selected.gameObject && _selected != null) { int xSnap = _selected.xGrid; int ySnap = _selected.yGrid; if (_moveHorizontal) { for (int i = _left; i <= _right; i++) { if (Mathf.Abs(_selected.transform.position.x - _wpMapController.GetGridPosition(i, _selected.yGrid).x) < _tileSize / 2) { xSnap = i; break; } } } if (_moveVertical) { for (int i = _down; i <= _up; i++) { if (Mathf.Abs(_selected.transform.position.y - _wpMapController.GetGridPosition(_selected.xGrid, i).y) < _tileSize / 2) { ySnap = i; break; } } } _xMove = _wpMapController.GetGridPosition(xSnap, _selected.yGrid).x; _yMove = _wpMapController.GetGridPosition(_selected.xGrid, ySnap).y; _selected.transform.position = new Vector3(_xMove, _yMove, 0); if (_selected.xGrid != xSnap || _selected.yGrid != ySnap) { _wpMapController.SwapTiles(_selected, xSnap, ySnap); } _selected = null; _wpMapController.CheckFilledTiles(); } }
private void WriteTile(JsonWriter writer, string device, WPTile tile) { if (tile == null) { return; } writer.WritePropertyName(device); if (tile.WPTileType == WPTileType.Flip) { WriteFlipTile(writer, tile as FlipTile); } if (tile.WPTileType == WPTileType.Cyclic) { WriteCyclicTile(writer, tile as CyclicTile); } if (tile.WPTileType == WPTileType.Iconic) { WriteIconicTile(writer, tile as IconicTile); } }
public bool IsFilled(WPTile previousWpTile) { if (previousWpTile.xGrid < xGrid && (previousWpTile.end1 == Direction.Right || previousWpTile.end2 == Direction.Right)) { return(end1 == Direction.Left || end2 == Direction.Left); } else if (previousWpTile.xGrid > xGrid && (previousWpTile.end1 == Direction.Left || previousWpTile.end2 == Direction.Left)) { return(end1 == Direction.Right || end2 == Direction.Right); } else if (previousWpTile.yGrid < yGrid && (previousWpTile.end1 == Direction.Down || previousWpTile.end2 == Direction.Down)) { return(end1 == Direction.Up || end2 == Direction.Up); } else if (previousWpTile.yGrid > yGrid && (previousWpTile.end1 == Direction.Up || previousWpTile.end2 == Direction.Up)) { return(end1 == Direction.Down || end2 == Direction.Down); } return(false); }
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 int GetMoveLimit(Vector2 position, WPTile.Direction direction) { Vector2 directionVector = WPTile.GetOutput(direction); int spaces = 0; bool movePossible = true; while (movePossible) { if (position.x + directionVector.x < 0) { movePossible = false; } else if (position.x + directionVector.x >= gridSize) { movePossible = false; } else if (position.y + directionVector.y < 1) { movePossible = false; } else if (position.y + directionVector.y >= gridSize + 1) { movePossible = false; } else if (_map[(int)(position.x + directionVector.x), (int)(position.y + directionVector.y)] != null) { movePossible = false; } else { position += directionVector; spaces++; } } return(spaces); }
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; } } }