mirror of https://github.com/flutter/pinball.git
parent
2ad0196e44
commit
a3e0e35b9c
@ -0,0 +1,56 @@
|
|||||||
|
import 'package:bloc/bloc.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:leaderboard_repository/leaderboard_repository.dart';
|
||||||
|
import 'package:pinball/leaderboard/models/leader_board_entry.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart';
|
||||||
|
|
||||||
|
part 'backbox_event.dart';
|
||||||
|
part 'backbox_state.dart';
|
||||||
|
|
||||||
|
/// {@template backbox_bloc}
|
||||||
|
/// Bloc which manages the Backbox display.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BackboxBloc extends Bloc<BackboxEvent, BackboxState> {
|
||||||
|
/// {@macro backbox_bloc}
|
||||||
|
BackboxBloc({
|
||||||
|
required LeaderboardRepository leaderboardRepository,
|
||||||
|
}) : _leaderboardRepository = leaderboardRepository,
|
||||||
|
super(LoadingState()) {
|
||||||
|
on<PlayerInitialsRequested>(_onPlayerInitialsRequested);
|
||||||
|
on<PlayerInitialsSubmited>(_onPlayerInitialsSubmited);
|
||||||
|
}
|
||||||
|
|
||||||
|
final LeaderboardRepository _leaderboardRepository;
|
||||||
|
|
||||||
|
void _onPlayerInitialsRequested(
|
||||||
|
PlayerInitialsRequested event,
|
||||||
|
Emitter<BackboxState> emit,
|
||||||
|
) {
|
||||||
|
emit(
|
||||||
|
InitialsFormState(
|
||||||
|
score: event.score,
|
||||||
|
character: event.character,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onPlayerInitialsSubmited(
|
||||||
|
PlayerInitialsSubmited event,
|
||||||
|
Emitter<BackboxState> emit,
|
||||||
|
) async {
|
||||||
|
try {
|
||||||
|
emit(LoadingState());
|
||||||
|
await _leaderboardRepository.addLeaderboardEntry(
|
||||||
|
LeaderboardEntryData(
|
||||||
|
playerInitials: event.initials,
|
||||||
|
score: event.score,
|
||||||
|
character: event.character.toType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
emit(InitialsSuccessState());
|
||||||
|
} catch (e, s) {
|
||||||
|
addError(e, s);
|
||||||
|
emit(InitialsFailureState());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
part of 'backbox_bloc.dart';
|
||||||
|
|
||||||
|
/// {@template backbox_event}
|
||||||
|
/// Base class for backbox events
|
||||||
|
/// {@endtemplate}
|
||||||
|
abstract class BackboxEvent extends Equatable {
|
||||||
|
/// {@macro backbox_event}
|
||||||
|
const BackboxEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template player_initials_requested}
|
||||||
|
/// Event that triggers the user initials display
|
||||||
|
/// {@endtemplate}
|
||||||
|
class PlayerInitialsRequested extends BackboxEvent {
|
||||||
|
/// {@template player_initials_requested}
|
||||||
|
const PlayerInitialsRequested({
|
||||||
|
required this.score,
|
||||||
|
required this.character,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Player's score
|
||||||
|
final int score;
|
||||||
|
/// Player's character
|
||||||
|
final CharacterTheme character;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [props, character];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template player_initials_submited}
|
||||||
|
/// Event that submits the user score and initials
|
||||||
|
/// {@endtemplate}
|
||||||
|
class PlayerInitialsSubmited extends BackboxEvent {
|
||||||
|
/// {@template player_initials_requested}
|
||||||
|
const PlayerInitialsSubmited({
|
||||||
|
required this.score,
|
||||||
|
required this.initials,
|
||||||
|
required this.character,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Player's score
|
||||||
|
final int score;
|
||||||
|
/// Player's initials
|
||||||
|
final String initials;
|
||||||
|
/// Player's character
|
||||||
|
final CharacterTheme character;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [props, initials, character];
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
part of 'backbox_bloc.dart';
|
||||||
|
|
||||||
|
/// {@template backbox_state}
|
||||||
|
/// The base state for all [BackboxState]
|
||||||
|
/// {@endtemplate backbox_state}
|
||||||
|
abstract class BackboxState extends Equatable {
|
||||||
|
/// {@macro backbox_state}
|
||||||
|
const BackboxState();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loading state for the backbox
|
||||||
|
class LoadingState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Failure state for the backbox
|
||||||
|
class FailureState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State when the leaderboard was successfully loaded
|
||||||
|
class LeaderboardSuccessState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State when the leaderboard was successfully loaded
|
||||||
|
class LeaderboardFailureState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template initials_form_state}
|
||||||
|
/// State when the user is inputting their initials
|
||||||
|
/// {@endtemplate}
|
||||||
|
class InitialsFormState extends BackboxState {
|
||||||
|
/// {@macro initials_form_state}
|
||||||
|
const InitialsFormState({
|
||||||
|
required this.score,
|
||||||
|
required this.character,
|
||||||
|
}) : super();
|
||||||
|
|
||||||
|
/// Player's score
|
||||||
|
final int score;
|
||||||
|
|
||||||
|
/// Player's character
|
||||||
|
final CharacterTheme character;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [score, character];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State when the leaderboard was successfully loaded
|
||||||
|
class InitialsSuccessState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State when the leaderboard was successfully loaded
|
||||||
|
class InitialsFailureState extends BackboxState {
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [];
|
||||||
|
}
|
@ -1 +1,4 @@
|
|||||||
export 'initials_input_display.dart';
|
export 'initials_input_display.dart';
|
||||||
|
export 'initials_submission_failure_display.dart';
|
||||||
|
export 'initials_submission_success_display.dart';
|
||||||
|
export 'loading_display.dart';
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
final _bodyTextPaint = TextPaint(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 3,
|
||||||
|
color: PinballColors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@template initials_submission_failure_display}
|
||||||
|
/// Display when the initials failed to submitted
|
||||||
|
/// {@endtemplate}
|
||||||
|
class InitialsSubmissionFailureDisplay extends TextComponent
|
||||||
|
with HasGameRef<PinballGame> {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
position = Vector2(0, -10);
|
||||||
|
anchor = Anchor.center;
|
||||||
|
text = 'Failure!';
|
||||||
|
textRenderer = _bodyTextPaint;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
final _bodyTextPaint = TextPaint(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 3,
|
||||||
|
color: PinballColors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@template initials_submission_success_display}
|
||||||
|
/// Display when the initials were successfully submitted
|
||||||
|
/// {@endtemplate}
|
||||||
|
class InitialsSubmissionSuccessDisplay extends TextComponent
|
||||||
|
with HasGameRef<PinballGame> {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
position = Vector2(0, -10);
|
||||||
|
anchor = Anchor.center;
|
||||||
|
text = 'Success!';
|
||||||
|
textRenderer = _bodyTextPaint;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
|
||||||
|
final _bodyTextPaint = TextPaint(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 3,
|
||||||
|
color: PinballColors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@template loading_display}
|
||||||
|
///
|
||||||
|
/// {@endtemplate}
|
||||||
|
class LoadingDisplay extends TextComponent with HasGameRef<PinballGame> {
|
||||||
|
late final String _label;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
|
||||||
|
position = Vector2(0, -10);
|
||||||
|
anchor = Anchor.center;
|
||||||
|
text = _label = gameRef.l10n.loading;
|
||||||
|
textRenderer = _bodyTextPaint;
|
||||||
|
|
||||||
|
await add(
|
||||||
|
TimerComponent(
|
||||||
|
period: 1,
|
||||||
|
repeat: true,
|
||||||
|
onTick: () {
|
||||||
|
final index = text.indexOf('.');
|
||||||
|
if (index != -1 && text.substring(index).length == 3) {
|
||||||
|
text = _label;
|
||||||
|
} else {
|
||||||
|
text = '$text.';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue