public void Move(int x, int y) { if (OnCheck != null) { var headpos = snakePeaces[0].Position; Vector2 pos = headpos + new Vector2(x, y); IObject obj; if ((obj = OnCheck.Invoke(this, pos)).GetType() != typeof(SceneObject)) { if (obj is SnakePeace peace) { Cut(peace); } else if (obj is Food food) { Eat(food); } } else { for (int i = 0; i < snakePeaces.Count; i++) { var peace = snakePeaces[i]; Vector2 tmppos = peace.Position; peace.Position = pos; pos = tmppos; } } } }
/// <summary> /// /// permet de sauvegarder la nouvelle position de checkpoint en fonction de si le checkpoint /// checké est un petit ou un gros checkpoint /// /// </summary> /// <param name="pos"></param> /// <param name="isSmall"></param> static private void Checkpoint_OnCheck(Vector3 pos, bool isSmall) { if (!isSmall) { SoundManager.Instance.Play(SoundManager.Instance.Sounds.Checkpoint); } if (isSmall && pos != _smallCheckPointPosition) { _smallCheckPointPosition = pos; OnCheck?.Invoke(isSmall); } else if (!isSmall && pos != _mainCheckPointPosition) { _mainCheckPointPosition = pos; _smallCheckPointPosition = pos; OnCheck?.Invoke(isSmall); } }
protected virtual void SwitchState(CheckableState newState) { if (newState == State) { return; } State = newState; switch (State) { case CheckableState.Unchecked: OnUncheck?.Invoke(this); return; case CheckableState.Checked: OnCheck?.Invoke(this); return; } throw new Exception($"Unrecognized State {State}"); }
}//SetMove internal void Check(Sign sign) { OnCheck?.Invoke((State)Evaluate(sign)); }//Check
private void Chessboard_MouseUp(object sender, MouseEventArgs e) { Cursor = Cursors.Default; if (DragDropOperation.DraggedPiece != null && DragDropOperation.DraggedPiece.Kind != ChessPieceKind.None) { if (e.X > DigitAreaWidth && e.X < this.Width && e.Y < Height - LetterAreaHeight && e.Y > 0) { // Drops a piece on the board var destinationSquare = (BoardDirection == BoardDirection.BlackOnTop ? new ChessSquare((ChessFile)((e.X - DigitAreaWidth) / SquareWidth), (ChessRank)(7 - (e.Y / SquareHeight))) : new ChessSquare((ChessFile)(7 - (e.X - DigitAreaWidth) / SquareWidth), (ChessRank)(e.Y / SquareHeight))); var moveValidationResult = ChessEngine.GetMoveValidity(DragDropOperation.Origin, destinationSquare); if (moveValidationResult.IsValid) { var promotionCancelled = false; if (moveValidationResult.MoveKind.HasFlag(ChessMoveType.Promotion)) { var pieceChooser = new FrmPromotion(ChessEngine.Turn); promotionCancelled = pieceChooser.ShowDialog() != DialogResult.OK; moveValidationResult.PromotedTo = pieceChooser.ChoosePiece; } if (!promotionCancelled) { moveValidationResult.ToSAN = ChessEngine.MoveToSAN(moveValidationResult); // Update the SAN after promotion MovePiece(moveValidationResult); OnPieceMoved?.Invoke(this, moveValidationResult); if (ChessEngine.IsCheckmate) { OnCheckmate?.Invoke(this, new EventArgs()); } else if (ChessEngine.IsCheck) { OnCheck?.Invoke(this, new EventArgs()); } else if (ChessEngine.IsDraw) { OnDraw?.Invoke(this, new EventArgs()); } } else { Invalidate(); } } else { Invalidate(); } } else { // Drops a piece outside of the board ChessEngine.RemovePieceAt(DragDropOperation.Origin); Invalidate(); OnPieceRemoved?.Invoke(this, DragDropOperation.Origin, DragDropOperation.DraggedPiece, e.Location); } } DragDropOperation = new DragOperation(null, null); OnSquareUnselected?.Invoke(this, new EventArgs()); }