void FallPiece(Point p) { List <FallingPiece> available = new List <FallingPiece>(); for (int i = 0; i < fallen.Count; i++) { if (!fallen[i].falling) { available.Add(fallen[i]); } } FallingPiece set = null; if (available.Count > 0) { set = available[0]; } else { GameObject fell = GameObject.Instantiate(fallenPiece, fallenBoard); FallingPiece fPiece = fell.GetComponent <FallingPiece>(); set = fPiece; fallen.Add(fPiece); } int val = getValueAtPoint(p) - 1; if (set != null && val >= 0 && val < pieces.Length) { set.Initialize(pieces[val], getPositionFromPoint(p)); } }
void CreateFallingPiece() { FallingPiece piece = pieceGenerator.NextFallingPiece(); piece.OnStop += piece_OnStop; piece.StartFalling(); }
private void DrawFallingPiece(int pixelX, int pixelY, string pieceName, SpriteBatch spriteBatch) { FallingPiece p = _gameBoard.GetFallingPiece(pieceName); Rectangle rect = new Rectangle(pixelX, pixelY - p.VerticalOffset, GamePiece.PieceWidth, GamePiece.PieceHeight); spriteBatch.Draw(_playingPieces, rect, p.GetSoruceRect(), Color.White); }
void piece_OnStop(FallingPiece piece) { piece.OnStop -= piece_OnStop; if (!GameOver()) { CreateFallingPiece(); } }
public void UpdateFallingPieces() { Queue <string> keysToRemove = new Queue <string>(); foreach (string key in _fallingPieces.Keys) { FallingPiece p = _fallingPieces[key]; p.UpdatePiece(); if (p.VerticalOffset == 0) { keysToRemove.Enqueue(key); } } while (keysToRemove.Count > 0) { _fallingPieces.Remove(keysToRemove.Dequeue()); } }
public FallingPiece NextFallingPiece() { FallingPiece piece = new FallingPiece(NextPiece()); return(piece); }
/// <summary> /// Resets the playing field /// </summary> /// <param name="createBoard">An override whether to recreate the board [DEFAULT = FALSE]</param> public virtual void Reset(bool createBoard = false) { // If the board size changed, or we want to, we create a new board if (Settings.currentFieldSize != fieldSize || createBoard) { // First we calculate the new sizes and adjust stuff based on those calculations fieldSize = Settings.currentFieldSize; squareSize = CalculateSquareSize(); squareOffset = (int)(squareSize * 0.1f); FallingPiece.fallSpeed = squareSize * (fieldSize.Height / 1.5f); // Then we are ready to construct the board image ConstructBoardImage(); } // If the win-length needed to win changed, we need to check whether that win-length is possible in both axes if (Settings.currentWinLength != winLength) { winLength = Settings.currentWinLength; // Make sure the win-length is reachable both horizontally and vertically if (fieldSize.Height < winLength || fieldSize.Width < winLength) { winLength = Math.Min(fieldSize.Height, fieldSize.Width); ConnectFour.instance.ShowMessage($"You cannot have a win length of {Settings.currentWinLength} on this board! Set to {winLength}", Color.White); } } // Create the new field and reset all important values field = new Pieces[fieldSize.Width, fieldSize.Height]; fallingPiece = null; lockBoard = false; clearBoard = false; wonXpos = new int[winLength]; wonYpos = new int[winLength]; for (int i = 0; i < winLength; i++) { wonXpos[i] = wonYpos[i] = -1; } numPiecesOnBoard = 0; isGameOver = false; random = new Random(); currentPlayer = random.Next(0, 2) switch { 0 => Pieces.red, 1 => Pieces.yellow, _ => Pieces.red, }; }
private void AddFallingAnimation(FallingPiece piece, Storyboard storyboard) { double currentTop = (double) piece.GamePiece.GetUIElement().GetValue(Canvas.TopProperty); DoubleAnimation fadeAnimation = GetAnimation(0.5, 60*piece.newRow, currentTop); fadeAnimation.EasingFunction = new CircleEase(); storyboard.Children.Add(fadeAnimation); Storyboard.SetTarget(fadeAnimation, piece.GamePiece.GetUIElement()); Storyboard.SetTargetProperty(fadeAnimation, "(Canvas.Top)"); }
void CreateFallingPiece() { FallingPiece piece = new FallingPiece(pieceGenerator.NextFallingPiece()); piece.OnStop += piece_OnStop; }