mirror of https://github.com/flutter/pinball.git
commit
8da6f14178
@ -1,208 +0,0 @@
|
|||||||
// ignore_for_file: avoid_renaming_method_parameters
|
|
||||||
|
|
||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:flame/effects.dart';
|
|
||||||
import 'package:flame_bloc/flame_bloc.dart';
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
/// {@template bonus_word}
|
|
||||||
/// Loads all [BonusLetter]s to compose a [BonusWord].
|
|
||||||
/// {@endtemplate}
|
|
||||||
class BonusWord extends Component
|
|
||||||
with BlocComponent<GameBloc, GameState>, HasGameRef<PinballGame> {
|
|
||||||
/// {@macro bonus_word}
|
|
||||||
BonusWord({required Vector2 position}) : _position = position;
|
|
||||||
|
|
||||||
final Vector2 _position;
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool listenWhen(GameState? previousState, GameState newState) {
|
|
||||||
return (previousState?.bonusHistory.length ?? 0) <
|
|
||||||
newState.bonusHistory.length &&
|
|
||||||
newState.bonusHistory.last == GameBonus.word;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onNewState(GameState state) {
|
|
||||||
if (state.bonusHistory.last == GameBonus.word) {
|
|
||||||
gameRef.audio.googleBonus();
|
|
||||||
|
|
||||||
final letters = children.whereType<BonusLetter>().toList();
|
|
||||||
|
|
||||||
for (var i = 0; i < letters.length; i++) {
|
|
||||||
final letter = letters[i];
|
|
||||||
letter
|
|
||||||
..isEnabled = false
|
|
||||||
..add(
|
|
||||||
SequenceEffect(
|
|
||||||
[
|
|
||||||
ColorEffect(
|
|
||||||
i.isOdd
|
|
||||||
? BonusLetter._activeColor
|
|
||||||
: BonusLetter._disableColor,
|
|
||||||
const Offset(0, 1),
|
|
||||||
EffectController(duration: 0.25),
|
|
||||||
),
|
|
||||||
ColorEffect(
|
|
||||||
i.isOdd
|
|
||||||
? BonusLetter._disableColor
|
|
||||||
: BonusLetter._activeColor,
|
|
||||||
const Offset(0, 1),
|
|
||||||
EffectController(duration: 0.25),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
repeatCount: 4,
|
|
||||||
)..onFinishCallback = () {
|
|
||||||
letter
|
|
||||||
..isEnabled = true
|
|
||||||
..add(
|
|
||||||
ColorEffect(
|
|
||||||
BonusLetter._disableColor,
|
|
||||||
const Offset(0, 1),
|
|
||||||
EffectController(duration: 0.25),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
await super.onLoad();
|
|
||||||
|
|
||||||
final offsets = [
|
|
||||||
Vector2(-12.92, 1.82),
|
|
||||||
Vector2(-8.33, -0.65),
|
|
||||||
Vector2(-2.88, -1.75),
|
|
||||||
];
|
|
||||||
offsets.addAll(
|
|
||||||
offsets.reversed
|
|
||||||
.map(
|
|
||||||
(offset) => Vector2(-offset.x, offset.y),
|
|
||||||
)
|
|
||||||
.toList(),
|
|
||||||
);
|
|
||||||
assert(offsets.length == GameBloc.bonusWord.length, 'Invalid positions');
|
|
||||||
|
|
||||||
final letters = <BonusLetter>[];
|
|
||||||
for (var i = 0; i < GameBloc.bonusWord.length; i++) {
|
|
||||||
letters.add(
|
|
||||||
BonusLetter(
|
|
||||||
letter: GameBloc.bonusWord[i],
|
|
||||||
index: i,
|
|
||||||
)..initialPosition = _position + offsets[i],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await addAll(letters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// {@template bonus_letter}
|
|
||||||
/// [BodyType.static] sensor component, part of a word bonus,
|
|
||||||
/// which will activate its letter after contact with a [Ball].
|
|
||||||
/// {@endtemplate}
|
|
||||||
class BonusLetter extends BodyComponent<PinballGame>
|
|
||||||
with BlocComponent<GameBloc, GameState>, InitialPosition {
|
|
||||||
/// {@macro bonus_letter}
|
|
||||||
BonusLetter({
|
|
||||||
required String letter,
|
|
||||||
required int index,
|
|
||||||
}) : _letter = letter,
|
|
||||||
_index = index {
|
|
||||||
paint = Paint()..color = _disableColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The size of the [BonusLetter].
|
|
||||||
static final size = Vector2.all(3.7);
|
|
||||||
|
|
||||||
static const _activeColor = Colors.green;
|
|
||||||
static const _disableColor = Colors.red;
|
|
||||||
|
|
||||||
final String _letter;
|
|
||||||
final int _index;
|
|
||||||
|
|
||||||
/// Indicates if a [BonusLetter] can be activated on [Ball] contact.
|
|
||||||
///
|
|
||||||
/// It is disabled whilst animating and enabled again once the animation
|
|
||||||
/// completes. The animation is triggered when [GameBonus.word] is
|
|
||||||
/// awarded.
|
|
||||||
bool isEnabled = true;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
await super.onLoad();
|
|
||||||
|
|
||||||
await add(
|
|
||||||
TextComponent(
|
|
||||||
position: Vector2(-1, -1),
|
|
||||||
text: _letter,
|
|
||||||
textRenderer: TextPaint(
|
|
||||||
style: const TextStyle(fontSize: 2, color: Colors.white),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Body createBody() {
|
|
||||||
final shape = CircleShape()..radius = size.x / 2;
|
|
||||||
|
|
||||||
final fixtureDef = FixtureDef(shape)..isSensor = true;
|
|
||||||
|
|
||||||
final bodyDef = BodyDef()
|
|
||||||
..position = initialPosition
|
|
||||||
..userData = this
|
|
||||||
..type = BodyType.static;
|
|
||||||
|
|
||||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool listenWhen(GameState? previousState, GameState newState) {
|
|
||||||
final wasActive = previousState?.isLetterActivated(_index) ?? false;
|
|
||||||
final isActive = newState.isLetterActivated(_index);
|
|
||||||
|
|
||||||
return wasActive != isActive;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onNewState(GameState state) {
|
|
||||||
final isActive = state.isLetterActivated(_index);
|
|
||||||
|
|
||||||
add(
|
|
||||||
ColorEffect(
|
|
||||||
isActive ? _activeColor : _disableColor,
|
|
||||||
const Offset(0, 1),
|
|
||||||
EffectController(duration: 0.25),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Activates this [BonusLetter], if it's not already activated.
|
|
||||||
void activate() {
|
|
||||||
final isActive = state?.isLetterActivated(_index) ?? false;
|
|
||||||
if (!isActive) {
|
|
||||||
gameRef.read<GameBloc>().add(BonusLetterActivated(_index));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Triggers [BonusLetter.activate] method when a [BonusLetter] and a [Ball]
|
|
||||||
/// come in contact.
|
|
||||||
class BonusLetterBallContactCallback
|
|
||||||
extends ContactCallback<Ball, BonusLetter> {
|
|
||||||
@override
|
|
||||||
void begin(Ball ball, BonusLetter bonusLetter, Contact contact) {
|
|
||||||
if (bonusLetter.isEnabled) {
|
|
||||||
bonusLetter.activate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,83 @@
|
|||||||
|
// ignore_for_file: avoid_renaming_method_parameters
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:pinball/flame/flame.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template google_word}
|
||||||
|
/// Loads all [GoogleLetter]s to compose a [GoogleWord].
|
||||||
|
/// {@endtemplate}
|
||||||
|
class GoogleWord extends Component
|
||||||
|
with HasGameRef<PinballGame>, Controls<_GoogleWordController> {
|
||||||
|
/// {@macro google_word}
|
||||||
|
GoogleWord({
|
||||||
|
required Vector2 position,
|
||||||
|
}) : _position = position {
|
||||||
|
controller = _GoogleWordController(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Vector2 _position;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
gameRef.addContactCallback(_GoogleLetterBallContactCallback());
|
||||||
|
|
||||||
|
final offsets = [
|
||||||
|
Vector2(-12.92, 1.82),
|
||||||
|
Vector2(-8.33, -0.65),
|
||||||
|
Vector2(-2.88, -1.75),
|
||||||
|
Vector2(2.88, -1.75),
|
||||||
|
Vector2(8.33, -0.65),
|
||||||
|
Vector2(12.92, 1.82),
|
||||||
|
];
|
||||||
|
|
||||||
|
final letters = <GoogleLetter>[];
|
||||||
|
for (var index = 0; index < offsets.length; index++) {
|
||||||
|
letters.add(
|
||||||
|
GoogleLetter(index)..initialPosition = _position + offsets[index],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await addAll(letters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _GoogleWordController extends ComponentController<GoogleWord>
|
||||||
|
with HasGameRef<PinballGame> {
|
||||||
|
_GoogleWordController(GoogleWord googleWord) : super(googleWord);
|
||||||
|
|
||||||
|
final _activatedLetters = <GoogleLetter>{};
|
||||||
|
|
||||||
|
void activate(GoogleLetter googleLetter) {
|
||||||
|
if (!_activatedLetters.add(googleLetter)) return;
|
||||||
|
|
||||||
|
googleLetter.activate();
|
||||||
|
|
||||||
|
final activatedBonus = _activatedLetters.length == 6;
|
||||||
|
if (activatedBonus) {
|
||||||
|
gameRef.audio.googleBonus();
|
||||||
|
gameRef.read<GameBloc>().add(const BonusActivated(GameBonus.googleWord));
|
||||||
|
component.children.whereType<GoogleLetter>().forEach(
|
||||||
|
(letter) => letter.deactivate(),
|
||||||
|
);
|
||||||
|
_activatedLetters.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Activates a [GoogleLetter] when it contacts with a [Ball].
|
||||||
|
class _GoogleLetterBallContactCallback
|
||||||
|
extends ContactCallback<GoogleLetter, Ball> {
|
||||||
|
@override
|
||||||
|
void begin(GoogleLetter googleLetter, _, __) {
|
||||||
|
final parent = googleLetter.parent;
|
||||||
|
if (parent is GoogleWord) {
|
||||||
|
parent.controller.activate(googleLetter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 35 KiB |
@ -1,40 +0,0 @@
|
|||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
/// {@template backboard}
|
|
||||||
/// The [Backboard] of the pinball machine.
|
|
||||||
/// {@endtemplate}
|
|
||||||
class Backboard extends SpriteComponent with HasGameRef {
|
|
||||||
/// {@macro backboard}
|
|
||||||
Backboard({
|
|
||||||
required Vector2 position,
|
|
||||||
}) : super(
|
|
||||||
// TODO(erickzanardo): remove multiply after
|
|
||||||
// https://github.com/flame-engine/flame/pull/1506 is merged
|
|
||||||
position: position..clone().multiply(Vector2(1, -1)),
|
|
||||||
anchor: Anchor.bottomCenter,
|
|
||||||
);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
await waitingMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the Backboard in waiting mode, where the scoreboard is shown.
|
|
||||||
Future<void> waitingMode() async {
|
|
||||||
final sprite = await gameRef.loadSprite(
|
|
||||||
Assets.images.backboard.backboardScores.keyName,
|
|
||||||
);
|
|
||||||
size = sprite.originalSize / 10;
|
|
||||||
this.sprite = sprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the Backboard in game over mode, where the score input is shown.
|
|
||||||
Future<void> gameOverMode() async {
|
|
||||||
final sprite = await gameRef.loadSprite(
|
|
||||||
Assets.images.backboard.backboardGameOver.keyName,
|
|
||||||
);
|
|
||||||
size = sprite.originalSize / 10;
|
|
||||||
this.sprite = sprite;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,75 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
export 'backboard_game_over.dart';
|
||||||
|
export 'backboard_letter_prompt.dart';
|
||||||
|
export 'backboard_waiting.dart';
|
||||||
|
|
||||||
|
/// {@template backboard}
|
||||||
|
/// The [Backboard] of the pinball machine.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class Backboard extends PositionComponent with HasGameRef {
|
||||||
|
/// {@macro backboard}
|
||||||
|
Backboard({
|
||||||
|
required Vector2 position,
|
||||||
|
}) : super(
|
||||||
|
position: position,
|
||||||
|
anchor: Anchor.bottomCenter,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@macro backboard}
|
||||||
|
///
|
||||||
|
/// Returns a [Backboard] initialized in the waiting mode
|
||||||
|
factory Backboard.waiting({
|
||||||
|
required Vector2 position,
|
||||||
|
}) {
|
||||||
|
return Backboard(position: position)..waitingMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@macro backboard}
|
||||||
|
///
|
||||||
|
/// Returns a [Backboard] initialized in the game over mode
|
||||||
|
factory Backboard.gameOver({
|
||||||
|
required Vector2 position,
|
||||||
|
required int score,
|
||||||
|
required BackboardOnSubmit onSubmit,
|
||||||
|
}) {
|
||||||
|
return Backboard(position: position)
|
||||||
|
..gameOverMode(
|
||||||
|
score: score,
|
||||||
|
onSubmit: onSubmit,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [TextPaint] used on the [Backboard]
|
||||||
|
static final textPaint = TextPaint(
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 6,
|
||||||
|
color: Colors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Puts the Backboard in waiting mode, where the scoreboard is shown.
|
||||||
|
Future<void> waitingMode() async {
|
||||||
|
children.removeWhere((_) => true);
|
||||||
|
await add(BackboardWaiting());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the Backboard in game over mode, where the score input is shown.
|
||||||
|
Future<void> gameOverMode({
|
||||||
|
required int score,
|
||||||
|
BackboardOnSubmit? onSubmit,
|
||||||
|
}) async {
|
||||||
|
children.removeWhere((_) => true);
|
||||||
|
await add(
|
||||||
|
BackboardGameOver(
|
||||||
|
score: score,
|
||||||
|
onSubmit: onSubmit,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// Signature for the callback called when the used has
|
||||||
|
/// submettied their initials on the [BackboardGameOver]
|
||||||
|
typedef BackboardOnSubmit = void Function(String);
|
||||||
|
|
||||||
|
/// {@template backboard_game_over}
|
||||||
|
/// [PositionComponent] that handles the user input on the
|
||||||
|
/// game over display view.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BackboardGameOver extends PositionComponent with HasGameRef {
|
||||||
|
/// {@macro backboard_game_over}
|
||||||
|
BackboardGameOver({
|
||||||
|
required int score,
|
||||||
|
BackboardOnSubmit? onSubmit,
|
||||||
|
}) : _score = score,
|
||||||
|
_onSubmit = onSubmit;
|
||||||
|
|
||||||
|
final int _score;
|
||||||
|
final BackboardOnSubmit? _onSubmit;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
final backgroundSprite = await gameRef.loadSprite(
|
||||||
|
Assets.images.backboard.backboardGameOver.keyName,
|
||||||
|
);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
SpriteComponent(
|
||||||
|
sprite: backgroundSprite,
|
||||||
|
size: backgroundSprite.originalSize / 10,
|
||||||
|
anchor: Anchor.bottomCenter,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final displaySprite = await gameRef.loadSprite(
|
||||||
|
Assets.images.backboard.display.keyName,
|
||||||
|
);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
SpriteComponent(
|
||||||
|
sprite: displaySprite,
|
||||||
|
size: displaySprite.originalSize / 10,
|
||||||
|
anchor: Anchor.bottomCenter,
|
||||||
|
position: Vector2(0, -11.5),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
TextComponent(
|
||||||
|
text: _score.formatScore(),
|
||||||
|
position: Vector2(-22, -46.5),
|
||||||
|
anchor: Anchor.center,
|
||||||
|
textRenderer: Backboard.textPaint,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
for (var i = 0; i < 3; i++) {
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
BackboardLetterPrompt(
|
||||||
|
position: Vector2(
|
||||||
|
20 + (6 * i).toDouble(),
|
||||||
|
-46.5,
|
||||||
|
),
|
||||||
|
hasFocus: i == 0,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowLeft: () => _movePrompt(true),
|
||||||
|
LogicalKeyboardKey.arrowRight: () => _movePrompt(false),
|
||||||
|
LogicalKeyboardKey.enter: _submit,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current inputed initials
|
||||||
|
String get initials => children
|
||||||
|
.whereType<BackboardLetterPrompt>()
|
||||||
|
.map((prompt) => prompt.char)
|
||||||
|
.join();
|
||||||
|
|
||||||
|
bool _submit() {
|
||||||
|
_onSubmit?.call(initials);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _movePrompt(bool left) {
|
||||||
|
final prompts = children.whereType<BackboardLetterPrompt>().toList();
|
||||||
|
|
||||||
|
final current = prompts.firstWhere((prompt) => prompt.hasFocus)
|
||||||
|
..hasFocus = false;
|
||||||
|
var index = prompts.indexOf(current) + (left ? -1 : 1);
|
||||||
|
index = min(max(0, index), prompts.length - 1);
|
||||||
|
|
||||||
|
prompts[index].hasFocus = true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template backboard_letter_prompt}
|
||||||
|
/// A [PositionComponent] that renders a letter prompt used
|
||||||
|
/// on the [BackboardGameOver]
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BackboardLetterPrompt extends PositionComponent {
|
||||||
|
/// {@macro backboard_letter_prompt}
|
||||||
|
BackboardLetterPrompt({
|
||||||
|
required Vector2 position,
|
||||||
|
bool hasFocus = false,
|
||||||
|
}) : _hasFocus = hasFocus,
|
||||||
|
super(
|
||||||
|
position: position,
|
||||||
|
);
|
||||||
|
|
||||||
|
static const _alphabetCode = 65;
|
||||||
|
static const _alphabetLength = 25;
|
||||||
|
var _charIndex = 0;
|
||||||
|
|
||||||
|
bool _hasFocus;
|
||||||
|
|
||||||
|
late RectangleComponent _underscore;
|
||||||
|
late TextComponent _input;
|
||||||
|
late TimerComponent _underscoreBlinker;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
_underscore = RectangleComponent(
|
||||||
|
size: Vector2(
|
||||||
|
4,
|
||||||
|
1.2,
|
||||||
|
),
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 4),
|
||||||
|
);
|
||||||
|
|
||||||
|
unawaited(add(_underscore));
|
||||||
|
|
||||||
|
_input = TextComponent(
|
||||||
|
text: 'A',
|
||||||
|
textRenderer: Backboard.textPaint,
|
||||||
|
anchor: Anchor.center,
|
||||||
|
);
|
||||||
|
unawaited(add(_input));
|
||||||
|
|
||||||
|
_underscoreBlinker = TimerComponent(
|
||||||
|
period: 0.6,
|
||||||
|
repeat: true,
|
||||||
|
autoStart: _hasFocus,
|
||||||
|
onTick: () {
|
||||||
|
_underscore.paint.color = (_underscore.paint.color == Colors.white)
|
||||||
|
? Colors.transparent
|
||||||
|
: Colors.white;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
unawaited(add(_underscoreBlinker));
|
||||||
|
|
||||||
|
unawaited(
|
||||||
|
add(
|
||||||
|
KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowUp: () => _cycle(true),
|
||||||
|
LogicalKeyboardKey.arrowDown: () => _cycle(false),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current selected character
|
||||||
|
String get char => String.fromCharCode(_alphabetCode + _charIndex);
|
||||||
|
|
||||||
|
bool _cycle(bool up) {
|
||||||
|
if (_hasFocus) {
|
||||||
|
final newCharCode =
|
||||||
|
min(max(_charIndex + (up ? 1 : -1), 0), _alphabetLength);
|
||||||
|
_input.text = String.fromCharCode(_alphabetCode + newCharCode);
|
||||||
|
_charIndex = newCharCode;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns if this prompt has focus on it
|
||||||
|
bool get hasFocus => _hasFocus;
|
||||||
|
|
||||||
|
/// Updates this prompt focus
|
||||||
|
set hasFocus(bool hasFocus) {
|
||||||
|
if (hasFocus) {
|
||||||
|
_underscoreBlinker.timer.resume();
|
||||||
|
} else {
|
||||||
|
_underscoreBlinker.timer.pause();
|
||||||
|
}
|
||||||
|
_underscore.paint.color = Colors.white;
|
||||||
|
_hasFocus = hasFocus;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// [PositionComponent] that shows the leaderboard while the player
|
||||||
|
/// has not started the game yet.
|
||||||
|
class BackboardWaiting extends SpriteComponent with HasGameRef {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
final sprite = await gameRef.loadSprite(
|
||||||
|
Assets.images.backboard.backboardScores.keyName,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.sprite = sprite;
|
||||||
|
size = sprite.originalSize / 10;
|
||||||
|
anchor = Anchor.bottomCenter;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
export 'score.dart';
|
@ -0,0 +1,11 @@
|
|||||||
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
|
final _numberFormat = NumberFormat('#,###');
|
||||||
|
|
||||||
|
/// Adds score related extensions to int
|
||||||
|
extension ScoreX on int {
|
||||||
|
/// Formats this number as a score value
|
||||||
|
String formatScore() {
|
||||||
|
return _numberFormat.format(this);
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
export 'blueprint.dart';
|
export 'blueprint.dart';
|
||||||
|
export 'keyboard_input_controller.dart';
|
||||||
export 'priority.dart';
|
export 'priority.dart';
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
/// The signature for a key handle function
|
||||||
|
typedef KeyHandlerCallback = bool Function();
|
||||||
|
|
||||||
|
/// {@template keyboard_input_controller}
|
||||||
|
/// A [Component] that receives keyboard input and executes registered methods.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class KeyboardInputController extends Component with KeyboardHandler {
|
||||||
|
/// {@macro keyboard_input_controller}
|
||||||
|
KeyboardInputController({
|
||||||
|
Map<LogicalKeyboardKey, KeyHandlerCallback> keyUp = const {},
|
||||||
|
Map<LogicalKeyboardKey, KeyHandlerCallback> keyDown = const {},
|
||||||
|
}) : _keyUp = keyUp,
|
||||||
|
_keyDown = keyDown;
|
||||||
|
|
||||||
|
final Map<LogicalKeyboardKey, KeyHandlerCallback> _keyUp;
|
||||||
|
final Map<LogicalKeyboardKey, KeyHandlerCallback> _keyDown;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
|
||||||
|
final isUp = event is RawKeyUpEvent;
|
||||||
|
|
||||||
|
final handlers = isUp ? _keyUp : _keyDown;
|
||||||
|
final handler = handlers[event.logicalKey];
|
||||||
|
|
||||||
|
if (handler != null) {
|
||||||
|
return handler();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
export 'components/components.dart';
|
export 'components/components.dart';
|
||||||
|
export 'extensions/extensions.dart';
|
||||||
export 'flame/flame.dart';
|
export 'flame/flame.dart';
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class BackboardGameOverGame extends BasicKeyboardGame {
|
||||||
|
BackboardGameOverGame(this.score);
|
||||||
|
|
||||||
|
static const info = '''
|
||||||
|
Simple example showing the waiting mode of the backboard.
|
||||||
|
''';
|
||||||
|
|
||||||
|
final int score;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
camera
|
||||||
|
..followVector2(Vector2.zero())
|
||||||
|
..zoom = 5;
|
||||||
|
|
||||||
|
await add(
|
||||||
|
Backboard.gameOver(
|
||||||
|
position: Vector2(0, 20),
|
||||||
|
score: score,
|
||||||
|
onSubmit: (initials) {
|
||||||
|
add(
|
||||||
|
ScoreText(
|
||||||
|
text: 'User $initials made $score',
|
||||||
|
position: Vector2(0, 50),
|
||||||
|
color: Colors.pink,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
import 'package:dashbook/dashbook.dart';
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
import 'package:sandbox/stories/backboard/game_over.dart';
|
||||||
|
import 'package:sandbox/stories/backboard/waiting.dart';
|
||||||
|
|
||||||
|
void addBackboardStories(Dashbook dashbook) {
|
||||||
|
dashbook.storiesOf('Backboard')
|
||||||
|
..add(
|
||||||
|
'Waiting mode',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: BackboardWaitingGame(),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('backboard/waiting.dart'),
|
||||||
|
info: BackboardWaitingGame.info,
|
||||||
|
)
|
||||||
|
..add(
|
||||||
|
'Game over',
|
||||||
|
(context) => GameWidget(
|
||||||
|
game: BackboardGameOverGame(
|
||||||
|
context.numberProperty('score', 9000000000).toInt(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
codeLink: buildSourceLink('backboard/game_over.dart'),
|
||||||
|
info: BackboardGameOverGame.info,
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:sandbox/common/common.dart';
|
||||||
|
|
||||||
|
class BackboardWaitingGame extends BasicGame {
|
||||||
|
static const info = '''
|
||||||
|
Simple example showing the waiting mode of the backboard.
|
||||||
|
''';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
camera
|
||||||
|
..followVector2(Vector2.zero())
|
||||||
|
..zoom = 5;
|
||||||
|
|
||||||
|
final backboard = Backboard.waiting(position: Vector2(0, 20));
|
||||||
|
await add(backboard);
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 427 KiB |
@ -0,0 +1,78 @@
|
|||||||
|
// ignore_for_file: cascade_invocations, one_member_abstracts
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
abstract class _KeyCallStub {
|
||||||
|
bool onCall();
|
||||||
|
}
|
||||||
|
|
||||||
|
class KeyCallStub extends Mock implements _KeyCallStub {}
|
||||||
|
|
||||||
|
class MockRawKeyUpEvent extends Mock implements RawKeyUpEvent {
|
||||||
|
@override
|
||||||
|
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
|
||||||
|
return super.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RawKeyUpEvent _mockKeyUp(LogicalKeyboardKey key) {
|
||||||
|
final event = MockRawKeyUpEvent();
|
||||||
|
when(() => event.logicalKey).thenReturn(key);
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('KeyboardInputController', () {
|
||||||
|
test('calls registered handlers', () {
|
||||||
|
final stub = KeyCallStub();
|
||||||
|
when(stub.onCall).thenReturn(true);
|
||||||
|
|
||||||
|
final input = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowUp: stub.onCall,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
input.onKeyEvent(_mockKeyUp(LogicalKeyboardKey.arrowUp), {});
|
||||||
|
verify(stub.onCall).called(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'returns false the handler return value',
|
||||||
|
() {
|
||||||
|
final stub = KeyCallStub();
|
||||||
|
when(stub.onCall).thenReturn(false);
|
||||||
|
|
||||||
|
final input = KeyboardInputController(
|
||||||
|
keyUp: {
|
||||||
|
LogicalKeyboardKey.arrowUp: stub.onCall,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
input.onKeyEvent(_mockKeyUp(LogicalKeyboardKey.arrowUp), {}),
|
||||||
|
isFalse,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'returns true (allowing event to bubble) when no handler is registered',
|
||||||
|
() {
|
||||||
|
final stub = KeyCallStub();
|
||||||
|
when(stub.onCall).thenReturn(true);
|
||||||
|
|
||||||
|
final input = KeyboardInputController();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
input.onKeyEvent(_mockKeyUp(LogicalKeyboardKey.arrowUp), {}),
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -1,376 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'package:bloc_test/bloc_test.dart';
|
|
||||||
import 'package:flame/effects.dart';
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
|
||||||
import 'package:flame_test/flame_test.dart';
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:mocktail/mocktail.dart';
|
|
||||||
import 'package:pinball/game/game.dart';
|
|
||||||
import 'package:pinball_audio/pinball_audio.dart';
|
|
||||||
|
|
||||||
import '../../helpers/helpers.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
final flameTester = FlameTester(EmptyPinballGameTest.new);
|
|
||||||
|
|
||||||
group('BonusWord', () {
|
|
||||||
flameTester.test(
|
|
||||||
'loads the letters correctly',
|
|
||||||
(game) async {
|
|
||||||
final bonusWord = BonusWord(
|
|
||||||
position: Vector2.zero(),
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
|
|
||||||
final letters = bonusWord.descendants().whereType<BonusLetter>();
|
|
||||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
group('listenWhen', () {
|
|
||||||
final previousState = MockGameState();
|
|
||||||
final currentState = MockGameState();
|
|
||||||
|
|
||||||
test(
|
|
||||||
'returns true when there is a new word bonus awarded',
|
|
||||||
() {
|
|
||||||
when(() => previousState.bonusHistory).thenReturn([]);
|
|
||||||
when(() => currentState.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
BonusWord(position: Vector2.zero()).listenWhen(
|
|
||||||
previousState,
|
|
||||||
currentState,
|
|
||||||
),
|
|
||||||
isTrue,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test(
|
|
||||||
'returns false when there is no new word bonus awarded',
|
|
||||||
() {
|
|
||||||
when(() => previousState.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
when(() => currentState.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
BonusWord(position: Vector2.zero()).listenWhen(
|
|
||||||
previousState,
|
|
||||||
currentState,
|
|
||||||
),
|
|
||||||
isFalse,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('onNewState', () {
|
|
||||||
final state = MockGameState();
|
|
||||||
flameTester.test(
|
|
||||||
'adds sequence effect to the letters when the player receives a bonus',
|
|
||||||
(game) async {
|
|
||||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
|
|
||||||
final bonusWord = BonusWord(position: Vector2.zero());
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
bonusWord.onNewState(state);
|
|
||||||
game.update(0); // Run one frame so the effects are added
|
|
||||||
|
|
||||||
final letters = bonusWord.children.whereType<BonusLetter>();
|
|
||||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
|
||||||
|
|
||||||
for (final letter in letters) {
|
|
||||||
expect(
|
|
||||||
letter.children.whereType<SequenceEffect>().length,
|
|
||||||
equals(1),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'plays the google bonus sound',
|
|
||||||
(game) async {
|
|
||||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
|
|
||||||
final bonusWord = BonusWord(position: Vector2.zero());
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
bonusWord.onNewState(state);
|
|
||||||
|
|
||||||
verify(bonusWord.gameRef.audio.googleBonus).called(1);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'adds a color effect to reset the color when the sequence is finished',
|
|
||||||
(game) async {
|
|
||||||
when(() => state.bonusHistory).thenReturn([GameBonus.word]);
|
|
||||||
|
|
||||||
final bonusWord = BonusWord(position: Vector2.zero());
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
bonusWord.onNewState(state);
|
|
||||||
// Run the amount of time necessary for the animation to finish
|
|
||||||
game.update(3);
|
|
||||||
game.update(0); // Run one additional frame so the effects are added
|
|
||||||
|
|
||||||
final letters = bonusWord.children.whereType<BonusLetter>();
|
|
||||||
expect(letters.length, equals(GameBloc.bonusWord.length));
|
|
||||||
|
|
||||||
for (final letter in letters) {
|
|
||||||
expect(
|
|
||||||
letter.children.whereType<ColorEffect>().length,
|
|
||||||
equals(1),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
group('BonusLetter', () {
|
|
||||||
final flameTester = FlameTester(EmptyPinballGameTest.new);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'loads correctly',
|
|
||||||
(game) async {
|
|
||||||
final bonusLetter = BonusLetter(
|
|
||||||
letter: 'G',
|
|
||||||
index: 0,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusLetter);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
expect(game.contains(bonusLetter), isTrue);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
group('body', () {
|
|
||||||
flameTester.test(
|
|
||||||
'is static',
|
|
||||||
(game) async {
|
|
||||||
final bonusLetter = BonusLetter(
|
|
||||||
letter: 'G',
|
|
||||||
index: 0,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusLetter);
|
|
||||||
|
|
||||||
expect(bonusLetter.body.bodyType, equals(BodyType.static));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('fixture', () {
|
|
||||||
flameTester.test(
|
|
||||||
'exists',
|
|
||||||
(game) async {
|
|
||||||
final bonusLetter = BonusLetter(
|
|
||||||
letter: 'G',
|
|
||||||
index: 0,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusLetter);
|
|
||||||
|
|
||||||
expect(bonusLetter.body.fixtures[0], isA<Fixture>());
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'is sensor',
|
|
||||||
(game) async {
|
|
||||||
final bonusLetter = BonusLetter(
|
|
||||||
letter: 'G',
|
|
||||||
index: 0,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusLetter);
|
|
||||||
|
|
||||||
final fixture = bonusLetter.body.fixtures[0];
|
|
||||||
expect(fixture.isSensor, isTrue);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'shape is circular',
|
|
||||||
(game) async {
|
|
||||||
final bonusLetter = BonusLetter(
|
|
||||||
letter: 'G',
|
|
||||||
index: 0,
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusLetter);
|
|
||||||
|
|
||||||
final fixture = bonusLetter.body.fixtures[0];
|
|
||||||
expect(fixture.shape.shapeType, equals(ShapeType.circle));
|
|
||||||
expect(fixture.shape.radius, equals(1.85));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('bonus letter activation', () {
|
|
||||||
late GameBloc gameBloc;
|
|
||||||
late PinballAudio pinballAudio;
|
|
||||||
|
|
||||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
|
||||||
gameBuilder: EmptyPinballGameTest.new,
|
|
||||||
blocBuilder: () => gameBloc,
|
|
||||||
repositories: () => [
|
|
||||||
RepositoryProvider<PinballAudio>.value(value: pinballAudio),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
gameBloc = MockGameBloc();
|
|
||||||
whenListen(
|
|
||||||
gameBloc,
|
|
||||||
const Stream<GameState>.empty(),
|
|
||||||
initialState: const GameState.initial(),
|
|
||||||
);
|
|
||||||
|
|
||||||
pinballAudio = MockPinballAudio();
|
|
||||||
when(pinballAudio.googleBonus).thenAnswer((_) {});
|
|
||||||
});
|
|
||||||
|
|
||||||
flameBlocTester.testGameWidget(
|
|
||||||
'adds BonusLetterActivated to GameBloc when not activated',
|
|
||||||
setUp: (game, tester) async {
|
|
||||||
final bonusWord = BonusWord(
|
|
||||||
position: Vector2.zero(),
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
|
|
||||||
final bonusLetters =
|
|
||||||
game.descendants().whereType<BonusLetter>().toList();
|
|
||||||
for (var index = 0; index < bonusLetters.length; index++) {
|
|
||||||
final bonusLetter = bonusLetters[index];
|
|
||||||
bonusLetter.activate();
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
verify(() => gameBloc.add(BonusLetterActivated(index))).called(1);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameBlocTester.testGameWidget(
|
|
||||||
"doesn't add BonusLetterActivated to GameBloc when already activated",
|
|
||||||
setUp: (game, tester) async {
|
|
||||||
const state = GameState(
|
|
||||||
score: 0,
|
|
||||||
balls: 2,
|
|
||||||
activatedBonusLetters: [0],
|
|
||||||
activatedDashNests: {},
|
|
||||||
bonusHistory: [],
|
|
||||||
);
|
|
||||||
whenListen(
|
|
||||||
gameBloc,
|
|
||||||
Stream.value(state),
|
|
||||||
initialState: state,
|
|
||||||
);
|
|
||||||
|
|
||||||
final bonusLetter = BonusLetter(letter: '', index: 0);
|
|
||||||
await game.add(bonusLetter);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
bonusLetter.activate();
|
|
||||||
await game.ready();
|
|
||||||
},
|
|
||||||
verify: (game, tester) async {
|
|
||||||
verifyNever(() => gameBloc.add(const BonusLetterActivated(0)));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameBlocTester.testGameWidget(
|
|
||||||
'adds a ColorEffect',
|
|
||||||
setUp: (game, tester) async {
|
|
||||||
const state = GameState(
|
|
||||||
score: 0,
|
|
||||||
balls: 2,
|
|
||||||
activatedBonusLetters: [0],
|
|
||||||
activatedDashNests: {},
|
|
||||||
bonusHistory: [],
|
|
||||||
);
|
|
||||||
|
|
||||||
final bonusLetter = BonusLetter(letter: '', index: 0);
|
|
||||||
await game.add(bonusLetter);
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
bonusLetter.activate();
|
|
||||||
|
|
||||||
bonusLetter.onNewState(state);
|
|
||||||
await tester.pump();
|
|
||||||
},
|
|
||||||
verify: (game, tester) async {
|
|
||||||
// TODO(aleastiago): Look into making `testGameWidget` pass the
|
|
||||||
// subject.
|
|
||||||
final bonusLetter = game.descendants().whereType<BonusLetter>().last;
|
|
||||||
expect(
|
|
||||||
bonusLetter.children.whereType<ColorEffect>().length,
|
|
||||||
equals(1),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
flameBlocTester.testGameWidget(
|
|
||||||
'listens when there is a change on the letter status',
|
|
||||||
setUp: (game, tester) async {
|
|
||||||
final bonusWord = BonusWord(
|
|
||||||
position: Vector2.zero(),
|
|
||||||
);
|
|
||||||
await game.ensureAdd(bonusWord);
|
|
||||||
|
|
||||||
final bonusLetters =
|
|
||||||
game.descendants().whereType<BonusLetter>().toList();
|
|
||||||
for (var index = 0; index < bonusLetters.length; index++) {
|
|
||||||
final bonusLetter = bonusLetters[index];
|
|
||||||
bonusLetter.activate();
|
|
||||||
await game.ready();
|
|
||||||
|
|
||||||
final state = GameState(
|
|
||||||
score: 0,
|
|
||||||
balls: 2,
|
|
||||||
activatedBonusLetters: [index],
|
|
||||||
activatedDashNests: const {},
|
|
||||||
bonusHistory: const [],
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
bonusLetter.listenWhen(const GameState.initial(), state),
|
|
||||||
isTrue,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('BonusLetterBallContactCallback', () {
|
|
||||||
test('calls ball.activate', () {
|
|
||||||
final ball = MockBall();
|
|
||||||
final bonusLetter = MockBonusLetter();
|
|
||||||
final contactCallback = BonusLetterBallContactCallback();
|
|
||||||
|
|
||||||
when(() => bonusLetter.isEnabled).thenReturn(true);
|
|
||||||
|
|
||||||
contactCallback.begin(ball, bonusLetter, MockContact());
|
|
||||||
|
|
||||||
verify(bonusLetter.activate).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("doesn't call ball.activate when letter is disabled", () {
|
|
||||||
final ball = MockBall();
|
|
||||||
final bonusLetter = MockBonusLetter();
|
|
||||||
final contactCallback = BonusLetterBallContactCallback();
|
|
||||||
|
|
||||||
when(() => bonusLetter.isEnabled).thenReturn(false);
|
|
||||||
|
|
||||||
contactCallback.begin(ball, bonusLetter, MockContact());
|
|
||||||
|
|
||||||
verifyNever(bonusLetter.activate);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
@ -0,0 +1,73 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:bloc_test/bloc_test.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mockingjay/mockingjay.dart';
|
||||||
|
import 'package:pinball/game/game.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
import '../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('GoogleWord', () {
|
||||||
|
late GameBloc gameBloc;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
gameBloc = MockGameBloc();
|
||||||
|
whenListen(
|
||||||
|
gameBloc,
|
||||||
|
const Stream<GameState>.empty(),
|
||||||
|
initialState: const GameState.initial(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
final flameTester = FlameTester(EmptyPinballTestGame.new);
|
||||||
|
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||||
|
gameBuilder: EmptyPinballTestGame.new,
|
||||||
|
blocBuilder: () => gameBloc,
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'loads the letters correctly',
|
||||||
|
(game) async {
|
||||||
|
const word = 'Google';
|
||||||
|
final googleWord = GoogleWord(position: Vector2.zero());
|
||||||
|
await game.ensureAdd(googleWord);
|
||||||
|
|
||||||
|
final letters = googleWord.children.whereType<GoogleLetter>();
|
||||||
|
expect(letters.length, equals(word.length));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameBlocTester.testGameWidget(
|
||||||
|
'adds GameBonus.googleWord to the game when all letters are activated',
|
||||||
|
setUp: (game, _) async {
|
||||||
|
final ball = Ball(baseColor: const Color(0xFFFF0000));
|
||||||
|
final googleWord = GoogleWord(position: Vector2.zero());
|
||||||
|
await game.ensureAddAll([googleWord, ball]);
|
||||||
|
|
||||||
|
final letters = googleWord.children.whereType<GoogleLetter>();
|
||||||
|
expect(letters, isNotEmpty);
|
||||||
|
for (final letter in letters) {
|
||||||
|
beginContact(game, letter, ball);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
if (letter == letters.last) {
|
||||||
|
verify(
|
||||||
|
() => gameBloc.add(const BonusActivated(GameBonus.googleWord)),
|
||||||
|
).called(1);
|
||||||
|
} else {
|
||||||
|
verifyNever(
|
||||||
|
() => gameBloc.add(const BonusActivated(GameBonus.googleWord)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
import 'package:flame_bloc/flame_bloc.dart';
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
|
||||||
|
|
||||||
class TestGame extends Forge2DGame with FlameBloc {
|
|
||||||
TestGame() {
|
|
||||||
images.prefix = '';
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue