public void PlacePiece(BasePiece checkPiece) { if (!CanPlacePiece(checkPiece)) { return; } PieceHighlight[] highlightArray = checkPiece.CurrentHighlights; for (int i = 0; i < highlightArray.Length; i++) { // we aren't valid... we should stop now! if (highlightArray[i] == null) { return; } highlightArray[i].HideHighlight(); BoardGridLocation foundBoard = highlightArray[i].FoundGridPosition; foundBoard.InUse = true; //SFXManager.instance.PlayAudioClip(checkPiece.PlacementAudio); } }
public bool IsGridPositionValid() { Ray ray = new Ray(transform.position + Vector3.up, Vector3.down * 5); RaycastHit[] allHits = Physics.RaycastAll(ray); FoundGridPosition = null; for (int i = 0; i < allHits.Length; i++) { RaycastHit checkHit = allHits[i]; BoardGridLocation gridLocation = checkHit.transform.GetComponent <BoardGridLocation>(); if (gridLocation == null) { continue; } FoundGridPosition = gridLocation; return(!gridLocation.InUse); } return(false); }
public void GetNextPiece() { CurrentSelectedPiece = null; CurrentPieceRotation = (BasePiece.RotationDirection)Random.Range(0, (int)BasePiece.RotationDirection.Count); //BasePiece.RotationDirection.Normal; List <BoardGridLocation> availableLocations = new List <BoardGridLocation>(); int xLength = BoardGridLocations.GetLength(0); int yLength = BoardGridLocations.GetLength(1); for (int x = 0; x < xLength; x++) { for (int y = 0; y < yLength; y++) { BoardGridLocation checkLocation = BoardGridLocations[x, y]; if (checkLocation == null || checkLocation.InUse) { continue; } availableLocations.Add(checkLocation); } } int targetCategory = 0; int totalTiles = xLength * yLength; int availableTiles = availableLocations.Count; if (availableTiles > ((float)totalTiles * 0.8f)) { targetCategory = 2; } else if (availableTiles > (float)(totalTiles * 0.5f)) { targetCategory = 1; } else { targetCategory = 0; } List <int> checkPieces = GetPieceIndexesForCategory(targetCategory); int pieceIndex = 0; if (checkPieces != null && checkPieces.Count > 0) { pieceIndex = checkPieces[Random.Range(0, checkPieces.Count)]; } UpdatePiece(pieceIndex); if (availableLocations.Count > 0) { BoardGridLocation randomStartingPlace = availableLocations[Random.Range(0, availableLocations.Count)]; MoveCurrentPieceToBoardLocation(randomStartingPlace); } }
private void Update() { // Do nothing while we don't have a piece if (CurrentSelectedPiece == null) { return; } if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { BoardGridLocation hitGridLocation = hit.transform.GetComponent <BoardGridLocation>(); if (hitGridLocation == null) { return; } //Debug.Log("hit object at " + hoveredTransform.X + "," + hoveredTransform.Y); MoveCurrentPieceToBoardLocation(hitGridLocation); } } else if (Input.GetMouseButtonUp(0)) { previousTouchedBoardGrid = null; } if (Input.GetMouseButtonDown(1)) { ChangeRotation(1); } if (Input.GetKeyDown(KeyCode.Space)) { PlacePiece(CurrentSelectedPiece); } float mouseWheel = Input.GetAxis("Mouse ScrollWheel"); if (currentScrollWheelDuration <= 0f && Mathf.Abs(mouseWheel) > 0.01f) { // Use the scroll view to select the piece and since people will expect the scroll wheel to move it, don't change pieces via the mouse scrollWheel anymore // CyclePiece((int)Mathf.Sign(mouseWheel)); currentScrollWheelDuration = timeBeforeScrollWheel; } currentScrollWheelDuration -= Time.deltaTime; }
public void MoveCurrentPieceToBoardLocation(BoardGridLocation newLocation) { if (newLocation == null || newLocation == previousTouchedBoardGrid) { return; } if (newLocation != previousTouchedBoardGrid) { CurrentSelectedPiece.transform.localPosition = newLocation.transform.localPosition; CurrentSelectedPiece.UpdateHits(); SFXManager.instance.PlayAudioClip(SelectLocationAudioClip); } previousTouchedBoardGrid = newLocation; }
public void GenerateBoard(int gridX = 10, int gridY = 10) { BoardGridLocations = new BoardGridLocation[gridX, gridY]; for (int x = 0; x < BoardGridLocations.GetLength(0); x++) { for (int y = 0; y < BoardGridLocations.GetLength(1); y++) { BoardGridLocation thisLocation = BoardGridHolder.InstantiateChild <BoardGridLocation>(BoardGridLocationPrefab.gameObject, x + "_" + y + BoardGridLocationPrefab.name); float width = thisLocation.Width; thisLocation.transform.localPosition = new Vector3(width * x, 0f, width * y); thisLocation.X = x; thisLocation.Y = y; BoardGridLocations[x, y] = thisLocation; thisLocation.SetMaterial(x + y); } } }
public bool IsGameOver() { int numPlayers = PersistentData.instance.NumberOfPlayers; int numTilesRemaining = 0; for (int x = 0; x < BoardGridLocations.GetLength(0); x++) { for (int y = 0; y < BoardGridLocations.GetLength(1); y++) { BoardGridLocation checkLocation = BoardGridLocations[x, y]; if (checkLocation == null || checkLocation.InUse) { continue; } numTilesRemaining++; } } return(numTilesRemaining < numPlayers); }