public void GetInputs() { canPlace = true; mouseWorldPoint = cam.ScreenToWorldPoint(Input.mousePosition); mousePos = new Vector3Int(Mathf.FloorToInt(mouseWorldPoint.x), Mathf.FloorToInt(mouseWorldPoint.y), 0); tileIndex += Mathf.FloorToInt(Input.mouseScrollDelta.y); if (tileIndex < 0) { tileIndex = tileTypes.Length - 1; } else if (tileIndex >= tileTypes.Length) { tileIndex = 0; } if (Input.mouseScrollDelta.y != 0) { audioSource.PlayOneShot(rotatingTrack); } currentTileType = tileTypes[tileIndex]; if (InRange(mousePos)) { if ( HasTile(mousePos) || player.tracksHeld <= 0 || Physics2D.OverlapBox(mousePos + (currentTileType == TileType.Straight ? new Vector3(0, 1, 0) : new Vector3()) + new Vector3(0.5f, 0.5f, 0), new Vector2(0.9f, 0.9f), 0, placeCheckMask)) { canPlace = false; } if (Input.GetMouseButtonDown(0) && canPlace) { tileGrid[mousePos.x, mousePos.y] = currentTileType; UpdateTile(mousePos, true); player.tracksHeld--; RailStructure railStruct = new RailStructure(); railStruct.position = mousePos; railStruct.ResetStability(); rails.Add(railStruct); audioSource.PlayOneShot(placingTrack); } if (Input.GetMouseButtonDown(1) && HasTile(mousePos)) { RemoveTile(mousePos); } } else { canPlace = false; } drawHelper = true; }
void Start() { audioSource = GetComponent <AudioSource>(); tileGrid = new TileType[tileDimensions.x, tileDimensions.y]; List <Vector3Int> occupiedTiles = new List <Vector3Int>(); for (int i = 0; i < tileGrid.GetLength(0); i++) { for (int j = 0; j < tileGrid.GetLength(1); j++) { var tile = tilemap.GetTile(new Vector3Int(i, j, 0)); if (tile == tileSMiddle || tile == tileSLeft || tile == tileSRight || tile == tileSSingle) { tileGrid[i, j] = TileType.Straight; } else if (tile == tileRLeft) { tileGrid[i, j] = TileType.RampLeft; } else if (tile == tileRRight) { tileGrid[i, j] = TileType.RampRight; } else { tileGrid[i, j] = TileType.None; } if (tileGrid[i, j] != TileType.None) { occupiedTiles.Add(new Vector3Int(i, j, 0)); } } } // Updates each tile that is not empty foreach (Vector3Int tilePos in occupiedTiles) { UpdateTile(tilePos, true); RailStructure railStruct = new RailStructure(); railStruct.position = tilePos; railStruct.ResetStability(); rails.Add(railStruct); } }
void ChainStability(Vector3Int point, Vector3Int from) { RailStructure pointRail = RailStructureFind(point); if (pointRail != null) { pointRail.ResetStability(); } List <Vector3Int> connecting = GetConnecting(point); foreach (Vector3Int connection in connecting) { if (!connection.Equals(from) && !IsStable(connection)) { ChainStability(connection, point); } } }