mirror of https://github.com/flutter/pinball.git
parent
b09a57541b
commit
e8cd40f14b
@ -1,64 +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 PositionComponent with HasGameRef {
|
|
||||||
/// {@macro backboard}
|
|
||||||
Backboard({
|
|
||||||
required Vector2 position,
|
|
||||||
this.startsAtWaiting = true,
|
|
||||||
}) : 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,
|
|
||||||
);
|
|
||||||
|
|
||||||
/// If true will start at the waiting mode
|
|
||||||
final bool startsAtWaiting;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
if (startsAtWaiting) {
|
|
||||||
await waitingMode();
|
|
||||||
} else {
|
|
||||||
await gameOverMode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the Backboard in waiting mode, where the scoreboard is shown.
|
|
||||||
Future<void> waitingMode() async {
|
|
||||||
children.removeWhere((element) => true);
|
|
||||||
final sprite = await gameRef.loadSprite(
|
|
||||||
Assets.images.backboard.backboardScores.keyName,
|
|
||||||
);
|
|
||||||
await add(
|
|
||||||
SpriteComponent(
|
|
||||||
sprite: sprite,
|
|
||||||
size: sprite.originalSize / 10,
|
|
||||||
anchor: Anchor.bottomCenter,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Puts the Backboard in game over mode, where the score input is shown.
|
|
||||||
Future<void> gameOverMode() async {
|
|
||||||
children.removeWhere((element) => true);
|
|
||||||
await add(BackboardGameOver());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// {@template backboard_game_over}
|
|
||||||
class BackboardGameOver extends SpriteComponent with HasGameRef {
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
final sprite = await gameRef.loadSprite(
|
|
||||||
Assets.images.backboard.backboardGameOver.keyName,
|
|
||||||
);
|
|
||||||
size = sprite.originalSize / 10;
|
|
||||||
anchor = Anchor.bottomCenter;
|
|
||||||
this.sprite = sprite;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,66 @@
|
|||||||
|
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(
|
||||||
|
// 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,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// [TextPaint] used on the [Backboard]
|
||||||
|
static final textPaint = TextPaint(
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 6,
|
||||||
|
color: Colors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@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,
|
||||||
|
}) {
|
||||||
|
return Backboard(position: position)
|
||||||
|
..gameOverMode(score: score);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the Backboard in waiting mode, where the scoreboard is shown.
|
||||||
|
Future<void> waitingMode() async {
|
||||||
|
children.removeWhere((element) => true);
|
||||||
|
await add(BackboardWaiting());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the Backboard in game over mode, where the score input is shown.
|
||||||
|
Future<void> gameOverMode({ required int score}) async {
|
||||||
|
children.removeWhere((element) => true);
|
||||||
|
await add(BackboardGameOver(score: score));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
|
||||||
|
/// {@template backboard_game_over}
|
||||||
|
/// [PositionComponent] that handles the user input on the
|
||||||
|
/// game over display view.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BackboardGameOver extends PositionComponent
|
||||||
|
with HasGameRef, KeyboardHandler {
|
||||||
|
/// {@macro backboard_game_over}
|
||||||
|
BackboardGameOver({
|
||||||
|
required int score,
|
||||||
|
}) : _score = score;
|
||||||
|
|
||||||
|
final int _score;
|
||||||
|
|
||||||
|
final _numberFormat = NumberFormat('#,###,###');
|
||||||
|
|
||||||
|
@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: _numberFormat.format(_score),
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
|
||||||
|
final isUp = event is RawKeyUpEvent;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flutter/material.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 = 26;
|
||||||
|
|
||||||
|
bool _hasFocus;
|
||||||
|
String _char = '';
|
||||||
|
|
||||||
|
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: _hasFocus ? '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));
|
||||||
|
}
|
||||||
|
|
||||||
|
void up() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void down() {
|
||||||
|
}
|
||||||
|
|
||||||
|
void nextPrompt() {
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue