mchess-client/lib/chess_bloc/promotion_bloc.dart

69 lines
1.8 KiB
Dart
Raw Normal View History

2023-07-01 07:29:43 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-07-03 17:41:12 +00:00
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
2023-07-01 07:29:43 +00:00
import 'package:mchess/utils/chess_utils.dart';
2023-07-03 17:41:12 +00:00
class PromotionBloc extends Bloc<PromotionEvent, PromotionState> {
static final PromotionBloc _instance = PromotionBloc._internal();
static late ChessMove move;
2023-07-01 07:29:43 +00:00
2023-07-03 17:41:12 +00:00
PromotionBloc._internal() : super(PromotionState.init()) {
2023-07-01 07:29:43 +00:00
on<PawnMovedToPromotionField>(promotionMoveHandler);
2023-07-03 17:41:12 +00:00
on<PieceChosen>(pieceChosenHandler);
2023-07-01 07:29:43 +00:00
}
void promotionMoveHandler(
PawnMovedToPromotionField event,
2023-07-03 17:41:12 +00:00
Emitter<PromotionState> emit,
2023-07-01 07:29:43 +00:00
) {
2023-07-03 17:41:12 +00:00
move = event.move;
emit(PromotionState(
2023-07-01 07:29:43 +00:00
showPromotionDialog: true, colorMoved: event.colorMoved));
}
2023-07-03 17:41:12 +00:00
void pieceChosenHandler(
PieceChosen event,
Emitter<PromotionState> emit,
) {
ChessBloc.getInstance()
.add(OwnPromotionPlayed(pieceClass: event.pieceClass, move: move));
emit(PromotionState(showPromotionDialog: false, colorMoved: event.color));
}
factory PromotionBloc.getInstance() {
return PromotionBloc();
2023-07-01 07:29:43 +00:00
}
2023-07-03 17:41:12 +00:00
factory PromotionBloc() {
2023-07-01 07:29:43 +00:00
return _instance;
}
}
abstract class PromotionEvent {}
class PawnMovedToPromotionField extends PromotionEvent {
2023-07-03 17:41:12 +00:00
final ChessMove move;
2023-07-01 07:29:43 +00:00
final ChessColor colorMoved;
2023-07-03 17:41:12 +00:00
PawnMovedToPromotionField({required this.move, required this.colorMoved});
}
class PieceChosen extends PromotionEvent {
final ChessPieceClass pieceClass;
final ChessColor color;
PieceChosen({required this.pieceClass, required this.color});
2023-07-01 07:29:43 +00:00
}
2023-07-03 17:41:12 +00:00
class PromotionState {
2023-07-01 07:29:43 +00:00
final bool showPromotionDialog;
final ChessColor colorMoved;
2023-07-03 17:41:12 +00:00
PromotionState({required this.showPromotionDialog, required this.colorMoved});
2023-07-01 07:29:43 +00:00
2023-07-03 17:41:12 +00:00
factory PromotionState.init() {
return PromotionState(
2023-07-01 07:29:43 +00:00
showPromotionDialog: false, colorMoved: ChessColor.white);
}
}