Fix green flicker of squares on invalid move

This commit is contained in:
Marco 2023-08-25 12:28:58 +02:00
parent b9d574f2ab
commit f45ec47285
2 changed files with 13 additions and 11 deletions

View File

@ -22,8 +22,9 @@ class ChessBoard extends StatelessWidget {
final piece = boardState.position[ChessCoordinate(column, row)]; final piece = boardState.position[ChessCoordinate(column, row)];
bool squareWasPartOfLastMove = false; bool squareWasPartOfLastMove = false;
if (boardState.lastMove.to == ChessCoordinate(column, row) || if ((boardState.lastMove.to == ChessCoordinate(column, row) ||
boardState.lastMove.from == ChessCoordinate(column, row)) { boardState.lastMove.from == ChessCoordinate(column, row)) &&
boardState.positionAckd) {
squareWasPartOfLastMove = true; squareWasPartOfLastMove = true;
} }

View File

@ -43,6 +43,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
ChessColor.white, ChessColor.white,
ChessPositionManager.getInstance().currentPosition, ChessPositionManager.getInstance().currentPosition,
ChessMove.none(), ChessMove.none(),
false,
), ),
); );
} }
@ -56,6 +57,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
state.newTurnColor, state.newTurnColor,
state.position, state.position,
ChessMove.none(), ChessMove.none(),
false,
), ),
); );
} }
@ -76,6 +78,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
turnColor, turnColor,
event.position, event.position,
ChessMove(from: event.startSquare, to: event.endSquare), ChessMove(from: event.startSquare, to: event.endSquare),
true,
), ),
); );
} }
@ -101,12 +104,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
tempPosition[move.from] = const ChessPiece.none(); tempPosition[move.from] = const ChessPiece.none();
emit( emit(
ChessBoardState( ChessBoardState(state.bottomColor, turnColor, tempPosition, move, false),
state.bottomColor,
turnColor,
tempPosition,
move
),
); );
} }
@ -131,7 +129,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
InvalidMovePlayed event, Emitter<ChessBoardState> emit) { InvalidMovePlayed event, Emitter<ChessBoardState> emit) {
emit( emit(
ChessBoardState(state.bottomColor, turnColor, ChessBoardState(state.bottomColor, turnColor,
ChessPositionManager.getInstance().currentPosition, ChessMove.none()), ChessPositionManager.getInstance().currentPosition, ChessMove.none(), false),
); );
} }
} }
@ -141,17 +139,19 @@ class ChessBoardState {
final ChessColor newTurnColor; final ChessColor newTurnColor;
final ChessPosition position; final ChessPosition position;
final ChessMove lastMove; final ChessMove lastMove;
final bool positionAckd;
ChessBoardState._( ChessBoardState._(
this.bottomColor, this.newTurnColor, this.position, this.lastMove); this.bottomColor, this.newTurnColor, this.position, this.lastMove, this.positionAckd);
factory ChessBoardState( factory ChessBoardState(
ChessColor bottomColor, ChessColor bottomColor,
ChessColor turnColor, ChessColor turnColor,
ChessPosition position, ChessPosition position,
ChessMove lastMove, ChessMove lastMove,
bool positionAckd,
) { ) {
return ChessBoardState._(bottomColor, turnColor, position, lastMove); return ChessBoardState._(bottomColor, turnColor, position, lastMove, positionAckd);
} }
factory ChessBoardState.init() { factory ChessBoardState.init() {
@ -165,6 +165,7 @@ class ChessBoardState {
turnColor, turnColor,
ChessPositionManager.getInstance().currentPosition, ChessPositionManager.getInstance().currentPosition,
ChessMove.none(), ChessMove.none(),
false,
); );
} }