mirror of https://github.com/flutter/pinball.git
feat: game over with initials input (#187)
* adding backboard stories * progress on the input * feat: finishing contrls * adding missing tests * tests * removing todo * Apply suggestions from code review Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * feat: pr suggestions * feat: pr suggestions * fix: lint * feat: pr suggestions Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/178/head
parent
707e2e78b0
commit
43ca2beca7
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 'keyboard_input_controller.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 'extensions/extensions.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,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue