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) { if (state is LoadingState) {
_display.add(LoadingDisplay()); _display.add(LoadingDisplay());
} else if (state is InitialsFormState) { } else if (state is InitialsFormState) {
_display.add(
InfoDisplay(
onShared: () {
//_bloc.add(ScoreShared());
},
),
);
/*
_display.add( _display.add(
InitialsInputDisplay( InitialsInputDisplay(
score: state.score, score: state.score,
@ -76,9 +68,20 @@ class Backbox extends PositionComponent with ZIndex {
}, },
), ),
); );
*/
} else if (state is InitialsSuccessState) { } 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) { } else if (state is InitialsFailureState) {
_display.add(InitialsSubmissionFailureDisplay()); _display.add(InitialsSubmissionFailureDisplay());
} }

@ -18,6 +18,7 @@ class BackboxBloc extends Bloc<BackboxEvent, BackboxState> {
super(LoadingState()) { super(LoadingState()) {
on<PlayerInitialsRequested>(_onPlayerInitialsRequested); on<PlayerInitialsRequested>(_onPlayerInitialsRequested);
on<PlayerInitialsSubmitted>(_onPlayerInitialsSubmitted); on<PlayerInitialsSubmitted>(_onPlayerInitialsSubmitted);
on<ScoreShareRequested>(_onScoreShareRequested);
} }
final LeaderboardRepository _leaderboardRepository; final LeaderboardRepository _leaderboardRepository;
@ -47,10 +48,29 @@ class BackboxBloc extends Bloc<BackboxEvent, BackboxState> {
character: event.character.toType, character: event.character.toType,
), ),
); );
emit(InitialsSuccessState()); emit(
InitialsSuccessState(
initials: event.initials,
score: event.score,
character: event.character,
),
);
} catch (error, stackTrace) { } catch (error, stackTrace) {
addError(error, stackTrace); addError(error, stackTrace);
emit(InitialsFailureState()); 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 @override
List<Object?> get props => [score, initials, character]; 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]; List<Object?> get props => [score, character];
} }
/// {@template initials_form_state}
/// State when the leaderboard was successfully loaded. /// State when the leaderboard was successfully loaded.
/// {@endtemplate}
class InitialsSuccessState extends BackboxState { 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 @override
List<Object?> get props => []; List<Object?> get props => [score, initials, character];
} }
/// State when the initials submission failed. /// State when the initials submission failed.
@ -57,3 +75,27 @@ class InitialsFailureState extends BackboxState {
@override @override
List<Object?> get props => []; 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 'dart:async';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.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_flame/pinball_flame.dart';
import 'package:pinball_ui/pinball_ui.dart'; import 'package:pinball_ui/pinball_ui.dart';
/// Signature for the callback called when the used has /// Signature for the callback called when the used tries to share the score
/// shared score on the [InfoDisplay]. /// on the [InfoDisplay].
typedef ScoreOnShared = void Function(); 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( final _titleTextPaint = TextPaint(
style: const TextStyle( style: const TextStyle(
@ -35,7 +40,6 @@ final _linkTextPaint = TextPaint(
color: PinballColors.orange, color: PinballColors.orange,
fontFamily: PinballFonts.pixeloidSans, fontFamily: PinballFonts.pixeloidSans,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
decorationThickness: 1, decorationThickness: 1,
), ),
); );
@ -54,21 +58,17 @@ final _descriptionTextPaint = TextPaint(
class InfoDisplay extends Component with HasGameRef { class InfoDisplay extends Component with HasGameRef {
/// {@macro info_display} /// {@macro info_display}
InfoDisplay({ InfoDisplay({
ScoreOnShared? onShared, OnShareTap? onShare,
}) : _onShared = onShared, OnNavigateTap? onNavigate,
super( }) : super(
children: [ children: [
_InstructionsComponent(), _InstructionsComponent(
onShare: onShare,
onNavigate: onNavigate,
),
], ],
); );
final ScoreOnShared? _onShared;
bool _share() {
_onShared?.call();
return true;
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -77,19 +77,24 @@ class InfoDisplay extends Component with HasGameRef {
} }
class _InstructionsComponent extends PositionComponent with HasGameRef { class _InstructionsComponent extends PositionComponent with HasGameRef {
_InstructionsComponent() _InstructionsComponent({
: super( OnShareTap? onShare,
OnNavigateTap? onNavigate,
}) : super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(0, -25), position: Vector2(0, -25),
children: [ children: [
_TitleComponent(), _TitleComponent(),
_LinksComponent(), _LinksComponent(
onShare: onShare,
onNavigate: onNavigate,
),
_DescriptionComponent(), _DescriptionComponent(),
], ],
); );
} }
class _TitleComponent extends PositionComponent with HasGameRef<PinballGame> { class _TitleComponent extends PositionComponent with HasGameRef {
_TitleComponent() _TitleComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -102,8 +107,7 @@ class _TitleComponent extends PositionComponent with HasGameRef<PinballGame> {
); );
} }
class _ShareScoreTextComponent extends TextComponent class _ShareScoreTextComponent extends TextComponent with HasGameRef {
with HasGameRef<PinballGame> {
_ShareScoreTextComponent() _ShareScoreTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -118,8 +122,7 @@ class _ShareScoreTextComponent extends TextComponent
} }
} }
class _ChallengeFriendsTextComponent extends TextComponent class _ChallengeFriendsTextComponent extends TextComponent with HasGameRef {
with HasGameRef<PinballGame> {
_ChallengeFriendsTextComponent() _ChallengeFriendsTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -152,26 +155,42 @@ class _TitleBackgroundSpriteComponent extends SpriteComponent with HasGameRef {
} }
} }
class _LinksComponent extends PositionComponent with HasGameRef<PinballGame> { class _LinksComponent extends PositionComponent with HasGameRef {
_LinksComponent() _LinksComponent({
: super( OnShareTap? onShare,
OnNavigateTap? onNavigate,
}) : super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(0, 9.2), position: Vector2(0, 9.2),
children: [ children: [
_ShareLinkComponent(), ShareLinkComponent(onTap: onShare),
_GotoIOLinkComponent(), GotoIOLinkComponent(onTap: onNavigate),
], ],
); );
} }
class _ShareLinkComponent extends TextComponent with HasGameRef<PinballGame> { /// {@template share_link_component}
_ShareLinkComponent() /// Link button for navigate to sharing score screen.
: super( /// {@endtemplate}
class ShareLinkComponent extends TextComponent with HasGameRef, Tappable {
/// {@macro share_link_component}
ShareLinkComponent({
OnShareTap? onTap,
}) : _onTap = onTap,
super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(-7, 0), position: Vector2(-7, 0),
textRenderer: _linkTextPaint, textRenderer: _linkTextPaint,
); );
final OnShareTap? _onTap;
@override
bool onTapDown(TapDownInfo info) {
_onTap?.call();
return true;
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -179,14 +198,28 @@ class _ShareLinkComponent extends TextComponent with HasGameRef<PinballGame> {
} }
} }
class _GotoIOLinkComponent extends TextComponent with HasGameRef<PinballGame> { /// {@template goto_io_link_component}
_GotoIOLinkComponent() /// Link button for navigate to Google I/O site.
: super( /// {@endtemplate}
class GotoIOLinkComponent extends TextComponent with HasGameRef, Tappable {
/// {@macro goto_io_link_component}
GotoIOLinkComponent({
OnNavigateTap? onTap,
}) : _onTap = onTap,
super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(6, 0), position: Vector2(6, 0),
textRenderer: _linkTextPaint, textRenderer: _linkTextPaint,
); );
final OnNavigateTap? _onTap;
@override
bool onTapDown(TapDownInfo info) {
_onTap?.call();
return true;
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -194,8 +227,7 @@ class _GotoIOLinkComponent extends TextComponent with HasGameRef<PinballGame> {
} }
} }
class _DescriptionComponent extends PositionComponent class _DescriptionComponent extends PositionComponent with HasGameRef {
with HasGameRef<PinballGame> {
_DescriptionComponent() _DescriptionComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -207,8 +239,7 @@ class _DescriptionComponent extends PositionComponent
); );
} }
class _LearnMoreTextComponent extends TextComponent class _LearnMoreTextComponent extends TextComponent with HasGameRef {
with HasGameRef<PinballGame> {
_LearnMoreTextComponent() _LearnMoreTextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
@ -223,8 +254,7 @@ class _LearnMoreTextComponent extends TextComponent
} }
} }
class _LearnMore2TextComponent extends TextComponent class _LearnMore2TextComponent extends TextComponent with HasGameRef {
with HasGameRef<PinballGame> {
_LearnMore2TextComponent() _LearnMore2TextComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,

Loading…
Cancel
Save