feat: game over info display flow at backbox

pull/359/head
RuiAlonso 3 years ago
parent 3a9cbacb57
commit b13ad6a06d

@ -53,14 +53,6 @@ class Backbox extends PositionComponent with ZIndex {
if (state is LoadingState) {
_display.add(LoadingDisplay());
} else if (state is InitialsFormState) {
_display.add(
InfoDisplay(
onShared: () {
//_bloc.add(ScoreShared());
},
),
);
/*
_display.add(
InitialsInputDisplay(
score: state.score,
@ -76,9 +68,20 @@ class Backbox extends PositionComponent with ZIndex {
},
),
);
*/
} else if (state is InitialsSuccessState) {
_display.add(InitialsSubmissionSuccessDisplay());
_display.add(
InfoDisplay(
onShare: () {
_bloc.add(
ScoreShareRequested(
score: state.score,
initials: state.initials,
character: state.character,
),
);
},
),
);
} else if (state is InitialsFailureState) {
_display.add(InitialsSubmissionFailureDisplay());
}

@ -18,6 +18,7 @@ class BackboxBloc extends Bloc<BackboxEvent, BackboxState> {
super(LoadingState()) {
on<PlayerInitialsRequested>(_onPlayerInitialsRequested);
on<PlayerInitialsSubmitted>(_onPlayerInitialsSubmitted);
on<ScoreShareRequested>(_onScoreShareRequested);
}
final LeaderboardRepository _leaderboardRepository;
@ -47,10 +48,29 @@ class BackboxBloc extends Bloc<BackboxEvent, BackboxState> {
character: event.character.toType,
),
);
emit(InitialsSuccessState());
emit(
InitialsSuccessState(
initials: event.initials,
score: event.score,
character: event.character,
),
);
} catch (error, stackTrace) {
addError(error, stackTrace);
emit(InitialsFailureState());
}
}
Future<void> _onScoreShareRequested(
ScoreShareRequested event,
Emitter<BackboxState> emit,
) async {
emit(
ShareState(
initials: event.initials,
score: event.score,
character: event.character,
),
);
}
}

@ -51,3 +51,27 @@ class PlayerInitialsSubmitted extends BackboxEvent {
@override
List<Object?> get props => [score, initials, character];
}
/// {@template score_share_requested}
/// Event that request the user to share score and initials.
/// {@endtemplate}
class ScoreShareRequested extends BackboxEvent {
/// {@macro score_share_requested}
const ScoreShareRequested({
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 => [score, initials, character];
}

@ -46,10 +46,28 @@ class InitialsFormState extends BackboxState {
List<Object?> get props => [score, character];
}
/// {@template initials_form_state}
/// State when the leaderboard was successfully loaded.
/// {@endtemplate}
class InitialsSuccessState extends BackboxState {
/// {@macro initials_success_state}
const InitialsSuccessState({
required this.score,
required this.initials,
required this.character,
}) : super();
/// Player's score.
final int score;
/// Player's initials.
final String initials;
/// Player's character.
final CharacterTheme character;
@override
List<Object?> get props => [];
List<Object?> get props => [score, initials, character];
}
/// State when the initials submission failed.
@ -57,3 +75,27 @@ class InitialsFailureState extends BackboxState {
@override
List<Object?> get props => [];
}
/// {@template initials_form_state}
/// State when the user is sharing the score.
/// {@endtemplate}
class ShareState extends BackboxState {
/// {@macro share_state}
const ShareState({
required this.score,
required this.initials,
required this.character,
}) : super();
/// Player's score.
final int score;
/// Player's initials.
final String initials;
/// Player's character.
final CharacterTheme character;
@override
List<Object?> get props => [score, initials, character];
}

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart';
@ -8,9 +9,13 @@ import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
import 'package:pinball_ui/pinball_ui.dart';
/// Signature for the callback called when the used has
/// shared score on the [InfoDisplay].
typedef ScoreOnShared = void Function();
/// Signature for the callback called when the used tries to share the score
/// on the [InfoDisplay].
typedef OnShareTap = void Function();
/// Signature for the callback called when the used tries to navigate to the
/// Google IO site on the [InfoDisplay].
typedef OnNavigateTap = void Function();
final _titleTextPaint = TextPaint(
style: const TextStyle(
@ -35,7 +40,6 @@ final _linkTextPaint = TextPaint(
color: PinballColors.orange,
fontFamily: PinballFonts.pixeloidSans,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationThickness: 1,
),
);
@ -54,21 +58,17 @@ final _descriptionTextPaint = TextPaint(
class InfoDisplay extends Component with HasGameRef {
/// {@macro info_display}
InfoDisplay({
ScoreOnShared? onShared,
}) : _onShared = onShared,
super(
OnShareTap? onShare,
OnNavigateTap? onNavigate,
}) : super(
children: [
_InstructionsComponent(),
_InstructionsComponent(
onShare: onShare,
onNavigate: onNavigate,
),
],
);
final ScoreOnShared? _onShared;
bool _share() {
_onShared?.call();
return true;
}
@override
Future<void> onLoad() async {
await super.onLoad();
@ -77,19 +77,24 @@ class InfoDisplay extends Component with HasGameRef {
}
class _InstructionsComponent extends PositionComponent with HasGameRef {
_InstructionsComponent()
: super(
_InstructionsComponent({
OnShareTap? onShare,
OnNavigateTap? onNavigate,
}) : super(
anchor: Anchor.center,
position: Vector2(0, -25),
children: [
_TitleComponent(),
_LinksComponent(),
_LinksComponent(
onShare: onShare,
onNavigate: onNavigate,
),
_DescriptionComponent(),
],
);
}
class _TitleComponent extends PositionComponent with HasGameRef<PinballGame> {
class _TitleComponent extends PositionComponent with HasGameRef {
_TitleComponent()
: super(
anchor: Anchor.center,
@ -102,8 +107,7 @@ class _TitleComponent extends PositionComponent with HasGameRef<PinballGame> {
);
}
class _ShareScoreTextComponent extends TextComponent
with HasGameRef<PinballGame> {
class _ShareScoreTextComponent extends TextComponent with HasGameRef {
_ShareScoreTextComponent()
: super(
anchor: Anchor.center,
@ -118,8 +122,7 @@ class _ShareScoreTextComponent extends TextComponent
}
}
class _ChallengeFriendsTextComponent extends TextComponent
with HasGameRef<PinballGame> {
class _ChallengeFriendsTextComponent extends TextComponent with HasGameRef {
_ChallengeFriendsTextComponent()
: super(
anchor: Anchor.center,
@ -152,26 +155,42 @@ class _TitleBackgroundSpriteComponent extends SpriteComponent with HasGameRef {
}
}
class _LinksComponent extends PositionComponent with HasGameRef<PinballGame> {
_LinksComponent()
: super(
class _LinksComponent extends PositionComponent with HasGameRef {
_LinksComponent({
OnShareTap? onShare,
OnNavigateTap? onNavigate,
}) : super(
anchor: Anchor.center,
position: Vector2(0, 9.2),
children: [
_ShareLinkComponent(),
_GotoIOLinkComponent(),
ShareLinkComponent(onTap: onShare),
GotoIOLinkComponent(onTap: onNavigate),
],
);
}
class _ShareLinkComponent extends TextComponent with HasGameRef<PinballGame> {
_ShareLinkComponent()
: super(
/// {@template share_link_component}
/// Link button for navigate to sharing score screen.
/// {@endtemplate}
class ShareLinkComponent extends TextComponent with HasGameRef, Tappable {
/// {@macro share_link_component}
ShareLinkComponent({
OnShareTap? onTap,
}) : _onTap = onTap,
super(
anchor: Anchor.center,
position: Vector2(-7, 0),
textRenderer: _linkTextPaint,
);
final OnShareTap? _onTap;
@override
bool onTapDown(TapDownInfo info) {
_onTap?.call();
return true;
}
@override
Future<void> onLoad() async {
await super.onLoad();
@ -179,14 +198,28 @@ class _ShareLinkComponent extends TextComponent with HasGameRef<PinballGame> {
}
}
class _GotoIOLinkComponent extends TextComponent with HasGameRef<PinballGame> {
_GotoIOLinkComponent()
: super(
/// {@template goto_io_link_component}
/// Link button for navigate to Google I/O site.
/// {@endtemplate}
class GotoIOLinkComponent extends TextComponent with HasGameRef, Tappable {
/// {@macro goto_io_link_component}
GotoIOLinkComponent({
OnNavigateTap? onTap,
}) : _onTap = onTap,
super(
anchor: Anchor.center,
position: Vector2(6, 0),
textRenderer: _linkTextPaint,
);
final OnNavigateTap? _onTap;
@override
bool onTapDown(TapDownInfo info) {
_onTap?.call();
return true;
}
@override
Future<void> onLoad() async {
await super.onLoad();
@ -194,8 +227,7 @@ class _GotoIOLinkComponent extends TextComponent with HasGameRef<PinballGame> {
}
}
class _DescriptionComponent extends PositionComponent
with HasGameRef<PinballGame> {
class _DescriptionComponent extends PositionComponent with HasGameRef {
_DescriptionComponent()
: super(
anchor: Anchor.center,
@ -207,8 +239,7 @@ class _DescriptionComponent extends PositionComponent
);
}
class _LearnMoreTextComponent extends TextComponent
with HasGameRef<PinballGame> {
class _LearnMoreTextComponent extends TextComponent with HasGameRef {
_LearnMoreTextComponent()
: super(
anchor: Anchor.center,
@ -223,8 +254,7 @@ class _LearnMoreTextComponent extends TextComponent
}
}
class _LearnMore2TextComponent extends TextComponent
with HasGameRef<PinballGame> {
class _LearnMore2TextComponent extends TextComponent with HasGameRef {
_LearnMore2TextComponent()
: super(
anchor: Anchor.center,

Loading…
Cancel
Save