mchess-client/lib/chess_bloc/promotion_bloc.dart
2023-07-03 19:41:12 +02:00

69 lines
1.8 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/utils/chess_utils.dart';
class PromotionBloc extends Bloc<PromotionEvent, PromotionState> {
static final PromotionBloc _instance = PromotionBloc._internal();
static late ChessMove move;
PromotionBloc._internal() : super(PromotionState.init()) {
on<PawnMovedToPromotionField>(promotionMoveHandler);
on<PieceChosen>(pieceChosenHandler);
}
void promotionMoveHandler(
PawnMovedToPromotionField event,
Emitter<PromotionState> emit,
) {
move = event.move;
emit(PromotionState(
showPromotionDialog: true, colorMoved: event.colorMoved));
}
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();
}
factory PromotionBloc() {
return _instance;
}
}
abstract class PromotionEvent {}
class PawnMovedToPromotionField extends PromotionEvent {
final ChessMove move;
final ChessColor colorMoved;
PawnMovedToPromotionField({required this.move, required this.colorMoved});
}
class PieceChosen extends PromotionEvent {
final ChessPieceClass pieceClass;
final ChessColor color;
PieceChosen({required this.pieceClass, required this.color});
}
class PromotionState {
final bool showPromotionDialog;
final ChessColor colorMoved;
PromotionState({required this.showPromotionDialog, required this.colorMoved});
factory PromotionState.init() {
return PromotionState(
showPromotionDialog: false, colorMoved: ChessColor.white);
}
}