Event Based Functions

The following events are prepared for easy code customizations. These events should be first considered when wanting to implement something.

When performing a move, the following events sequence happens

OnPromotePiece -> OnPieceFinishMoving/OnPieceFinishAttacking -> OnCheckingKing -> OnChessGameFinished -> OnGameFinishedByDrawing/OnGameFinishedByEnding -> OnControllerActionEnded

virtual void OnPromotePiece(ControllerPromotionInfo promotionInfo, ChessPieceObject selectedTarget, ChessPieceObject capturedTarget, bool isMoveContinueGame)

Will only get called when the move is a promotion move.

promotionInfo - The information includes the move itself, what the piece is promoting into, if the promotion is an attack.

selectedTarget - Containing data of the selected piece GameObject, and the piece color, piece type.

capturedTarget - Containing data of the attacked piece GameObject, and the piece color, piece type. This will be null if the promotion move is not an attack.

isMoveContinueGame - If this game is a continue game. Since the way game continue from last game do actually call all method from EventCycle for each move recorded, this parameter is to prevent the function from calling anything that should not be called when game is loaded from continue game, this will be true during the game loading phase, it will always be false after the game is finished loaded from continuing a game.

virtual void OnPieceFinishMoving(ChessMove move, ChessPieceObject selectedTarget, bool isMoveContinueGame)

If the move does not involved a captured, this event is called. Otherwise, OnPieceFinishAttacking get called.

virtual void OnPieceFinishAttacking(ChessMove move, ChessPieceObject selectedTarget, ChessPieceObject capturedTarget, bool isMoveContinueGame)

For move that involved a captured.

virtual void OnCheckingKing()

Will be called when opponent king is checked for the move

virtual void OnChessGameFinished(ChessGameFinish finish)

Will be called when the game is finished after the move, by whatever chess game finishing rule.

finish - The information regarding the game finishing type and the winning parties, if any.

virtual void OnGameFinishedByDrawing()

This will only get called if the game is finished by drawing.

virtual void OnGameFinishedByEnding(ChessColor winningColor)

This will get called if the game is finished by having winning or losing parties.

winningColor - The color who won the game.

virtual bool OnControllerActionEnded()

The final phase of finishing everything from making a move.

ChessEventManager.InvokePreventControllerActionEndingEvent(bool prevent) can be use before OnControllerActionEnded() get executed from the event sequence, the boolean value returning from OnControllerActionEnded() base will be as same as the invoke caller function's prevent parameter value. If might be helpful for delaying code execution or to prevent some code inside this function from executing. To make things clearer, see example below.

protected override bool OnControllerActionEnded()
    bool hasControllerActionEnded = base.OnControllerActionEnded();
    if (hasControllerActionEnded)
        //Code only execute when prevent is true
        //Code only execute when prevent is false
    //Code will execute regardless

When the intention is to delay OnControllerActionEnded(), call ChessEventManager.InvokePreventControllerActionEndingEvent(true) to start the delay and call ChessEventManager.InvokePreventControllerActionEndingEvent(false) followed by OnControllerActionEnded() on anywhere after the delay ends.

Last updated