mirror of https://github.com/flutter/pinball.git
commit
d76df61ae7
@ -0,0 +1,20 @@
|
||||
name: pinball_flame
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- "packages/pinball_flame/**"
|
||||
- ".github/workflows/pinball_flame.yaml"
|
||||
|
||||
pull_request:
|
||||
paths:
|
||||
- "packages/pinball_flame/**"
|
||||
- ".github/workflows/pinball_flame.yaml"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1
|
||||
with:
|
||||
working_directory: packages/pinball_flame
|
||||
coverage_excludes: "lib/gen/*.dart"
|
||||
test_optimization: false
|
@ -1 +0,0 @@
|
||||
export 'component_controller.dart';
|
@ -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/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
// ignore_for_file: avoid_renaming_method_parameters
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template layer_entrance_orientation}
|
||||
/// Determines if a layer entrance is oriented [up] or [down] on the board.
|
||||
/// {@endtemplate}
|
||||
enum LayerEntranceOrientation {
|
||||
/// Facing up on the Board.
|
||||
up,
|
||||
|
||||
/// Facing down on the Board.
|
||||
down,
|
||||
}
|
||||
|
||||
/// {@template layer_sensor}
|
||||
/// [BodyComponent] located at the entrance and exit of a [Layer].
|
||||
///
|
||||
/// [LayerSensorBallContactCallback] detects when a [Ball] passes
|
||||
/// through this sensor.
|
||||
///
|
||||
/// By default the base [layer] is set to [Layer.board] and the
|
||||
/// [outsidePriority] is set to the lowest possible [Layer].
|
||||
/// {@endtemplate}
|
||||
abstract class LayerSensor extends BodyComponent with InitialPosition, Layered {
|
||||
/// {@macro layer_sensor}
|
||||
LayerSensor({
|
||||
required Layer insideLayer,
|
||||
Layer? outsideLayer,
|
||||
required int insidePriority,
|
||||
int? outsidePriority,
|
||||
required this.orientation,
|
||||
}) : _insideLayer = insideLayer,
|
||||
_outsideLayer = outsideLayer ?? Layer.board,
|
||||
_insidePriority = insidePriority,
|
||||
_outsidePriority = outsidePriority ?? Ball.boardPriority {
|
||||
layer = Layer.opening;
|
||||
}
|
||||
final Layer _insideLayer;
|
||||
final Layer _outsideLayer;
|
||||
final int _insidePriority;
|
||||
final int _outsidePriority;
|
||||
|
||||
/// Mask bits value for collisions on [Layer].
|
||||
Layer get insideLayer => _insideLayer;
|
||||
|
||||
/// Mask bits value for collisions outside of [Layer].
|
||||
Layer get outsideLayer => _outsideLayer;
|
||||
|
||||
/// Render priority for the [Ball] on [Layer].
|
||||
int get insidePriority => _insidePriority;
|
||||
|
||||
/// Render priority for the [Ball] outside of [Layer].
|
||||
int get outsidePriority => _outsidePriority;
|
||||
|
||||
/// The [Shape] of the [LayerSensor].
|
||||
Shape get shape;
|
||||
|
||||
/// {@macro layer_entrance_orientation}
|
||||
// TODO(ruimiguel): Try to remove the need of [LayerEntranceOrientation] for
|
||||
// collision calculations.
|
||||
final LayerEntranceOrientation orientation;
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final fixtureDef = FixtureDef(
|
||||
shape,
|
||||
isSensor: true,
|
||||
);
|
||||
final bodyDef = BodyDef(
|
||||
position: initialPosition,
|
||||
userData: this,
|
||||
);
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template layer_sensor_ball_contact_callback}
|
||||
/// Detects when a [Ball] enters or exits a [Layer] through a [LayerSensor].
|
||||
///
|
||||
/// Modifies [Ball]'s [Layer] and render priority depending on whether the
|
||||
/// [Ball] is on or outside of a [Layer].
|
||||
/// {@endtemplate}
|
||||
class LayerSensorBallContactCallback<LayerEntrance extends LayerSensor>
|
||||
extends ContactCallback<Ball, LayerEntrance> {
|
||||
@override
|
||||
void begin(Ball ball, LayerEntrance layerEntrance, Contact _) {
|
||||
if (ball.layer != layerEntrance.insideLayer) {
|
||||
final isBallEnteringOpening =
|
||||
(layerEntrance.orientation == LayerEntranceOrientation.down &&
|
||||
ball.body.linearVelocity.y < 0) ||
|
||||
(layerEntrance.orientation == LayerEntranceOrientation.up &&
|
||||
ball.body.linearVelocity.y > 0);
|
||||
|
||||
if (isBallEnteringOpening) {
|
||||
ball
|
||||
..layer = layerEntrance.insideLayer
|
||||
..priority = layerEntrance.insidePriority
|
||||
..reorderChildren();
|
||||
}
|
||||
} else {
|
||||
ball
|
||||
..layer = layerEntrance.outsideLayer
|
||||
..priority = layerEntrance.outsidePriority
|
||||
..reorderChildren();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
// ignore_for_file: avoid_renaming_method_parameters
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template ramp_orientation}
|
||||
/// Determines if a ramp is facing [up] or [down] on the Board.
|
||||
/// {@endtemplate}
|
||||
enum RampOrientation {
|
||||
/// Facing up on the Board.
|
||||
up,
|
||||
|
||||
/// Facing down on the Board.
|
||||
down,
|
||||
}
|
||||
|
||||
/// {@template ramp_opening}
|
||||
/// [BodyComponent] located at the entrance and exit of a ramp.
|
||||
///
|
||||
/// [RampOpeningBallContactCallback] detects when a [Ball] passes
|
||||
/// through this opening.
|
||||
///
|
||||
/// By default the base [layer] is set to [Layer.board] and the
|
||||
/// [outsidePriority] is set to the lowest possible [Layer].
|
||||
/// {@endtemplate}
|
||||
// TODO(ruialonso): Consider renaming the class.
|
||||
abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
|
||||
/// {@macro ramp_opening}
|
||||
RampOpening({
|
||||
required Layer insideLayer,
|
||||
Layer? outsideLayer,
|
||||
required int insidePriority,
|
||||
int? outsidePriority,
|
||||
required this.orientation,
|
||||
}) : _insideLayer = insideLayer,
|
||||
_outsideLayer = outsideLayer ?? Layer.board,
|
||||
_insidePriority = insidePriority,
|
||||
_outsidePriority = outsidePriority ?? Ball.boardPriority {
|
||||
layer = Layer.opening;
|
||||
}
|
||||
final Layer _insideLayer;
|
||||
final Layer _outsideLayer;
|
||||
final int _insidePriority;
|
||||
final int _outsidePriority;
|
||||
|
||||
/// Mask of category bits for collision inside ramp.
|
||||
Layer get insideLayer => _insideLayer;
|
||||
|
||||
/// Mask of category bits for collision outside ramp.
|
||||
Layer get outsideLayer => _outsideLayer;
|
||||
|
||||
/// Priority for the [Ball] inside ramp.
|
||||
int get insidePriority => _insidePriority;
|
||||
|
||||
/// Priority for the [Ball] outside ramp.
|
||||
int get outsidePriority => _outsidePriority;
|
||||
|
||||
/// The [Shape] of the [RampOpening].
|
||||
Shape get shape;
|
||||
|
||||
/// {@macro ramp_orientation}
|
||||
// TODO(ruimiguel): Try to remove the need of [RampOrientation] for collision
|
||||
// calculations.
|
||||
final RampOrientation orientation;
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final fixtureDef = FixtureDef(shape)..isSensor = true;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..userData = this
|
||||
..position = initialPosition
|
||||
..type = BodyType.static;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template ramp_opening_ball_contact_callback}
|
||||
/// Detects when a [Ball] enters or exits a ramp through a [RampOpening].
|
||||
///
|
||||
/// Modifies [Ball]'s [Layer] accordingly depending on whether the [Ball] is
|
||||
/// outside or inside a ramp.
|
||||
/// {@endtemplate}
|
||||
class RampOpeningBallContactCallback<Opening extends RampOpening>
|
||||
extends ContactCallback<Ball, Opening> {
|
||||
/// [Ball]s currently inside the ramp.
|
||||
final _ballsInside = <Ball>{};
|
||||
|
||||
@override
|
||||
void begin(Ball ball, Opening opening, Contact _) {
|
||||
Layer layer;
|
||||
|
||||
if (!_ballsInside.contains(ball)) {
|
||||
layer = opening.insideLayer;
|
||||
_ballsInside.add(ball);
|
||||
ball
|
||||
..sendTo(opening.insidePriority)
|
||||
..layer = layer;
|
||||
} else {
|
||||
_ballsInside.remove(ball);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void end(Ball ball, Opening opening, Contact _) {
|
||||
if (!_ballsInside.contains(ball)) {
|
||||
ball.layer = opening.outsideLayer;
|
||||
} else {
|
||||
// TODO(ruimiguel): change this code. Check what happens with ball that
|
||||
// slightly touch Opening and goes out again. With InitialPosition change
|
||||
// now doesn't work position.y comparison
|
||||
final isBallOutsideOpening =
|
||||
(opening.orientation == RampOrientation.down &&
|
||||
ball.body.linearVelocity.y > 0) ||
|
||||
(opening.orientation == RampOrientation.up &&
|
||||
ball.body.linearVelocity.y < 0);
|
||||
|
||||
if (isBallOutsideOpening) {
|
||||
ball
|
||||
..sendTo(opening.outsidePriority)
|
||||
..layer = opening.outsideLayer;
|
||||
_ballsInside.remove(ball);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export 'blueprint.dart';
|
||||
export 'keyboard_input_controller.dart';
|
||||
export 'priority.dart';
|
@ -1,39 +0,0 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flame/components.dart';
|
||||
|
||||
/// Helper methods to change the [priority] of a [Component].
|
||||
extension ComponentPriorityX on Component {
|
||||
static const _lowestPriority = 0;
|
||||
|
||||
/// Changes the priority to a specific one.
|
||||
void sendTo(int destinationPriority) {
|
||||
if (priority != destinationPriority) {
|
||||
priority = math.max(destinationPriority, _lowestPriority);
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Changes the priority to the lowest possible.
|
||||
void sendToBack() {
|
||||
if (priority != _lowestPriority) {
|
||||
priority = _lowestPriority;
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Decreases the priority to be lower than another [Component].
|
||||
void showBehindOf(Component other) {
|
||||
if (priority >= other.priority) {
|
||||
priority = math.max(other.priority - 1, _lowestPriority);
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/// Increases the priority to be higher than another [Component].
|
||||
void showInFrontOf(Component other) {
|
||||
if (priority <= other.priority) {
|
||||
priority = other.priority + 1;
|
||||
reorderChildren();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
class TestLayerSensor extends LayerSensor {
|
||||
TestLayerSensor({
|
||||
required LayerEntranceOrientation orientation,
|
||||
required int insidePriority,
|
||||
required Layer insideLayer,
|
||||
}) : super(
|
||||
insideLayer: insideLayer,
|
||||
insidePriority: insidePriority,
|
||||
orientation: orientation,
|
||||
);
|
||||
|
||||
@override
|
||||
Shape get shape => PolygonShape()..setAsBoxXY(1, 1);
|
||||
}
|
||||
|
||||
class TestLayerSensorBallContactCallback
|
||||
extends LayerSensorBallContactCallback<TestLayerSensor> {
|
||||
TestLayerSensorBallContactCallback() : super();
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
const insidePriority = 1;
|
||||
|
||||
group('LayerSensor', () {
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final layerSensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
await game.ensureAdd(layerSensor);
|
||||
|
||||
expect(game.contains(layerSensor), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('body', () {
|
||||
flameTester.test(
|
||||
'is static',
|
||||
(game) async {
|
||||
final layerSensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
await game.ensureAdd(layerSensor);
|
||||
|
||||
expect(layerSensor.body.bodyType, equals(BodyType.static));
|
||||
},
|
||||
);
|
||||
|
||||
group('first fixture', () {
|
||||
const pathwayLayer = Layer.spaceshipEntranceRamp;
|
||||
const openingLayer = Layer.opening;
|
||||
|
||||
flameTester.test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final layerSensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(layerSensor);
|
||||
|
||||
expect(layerSensor.body.fixtures[0], isA<Fixture>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'shape is a polygon',
|
||||
(game) async {
|
||||
final layerSensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(layerSensor);
|
||||
|
||||
final fixture = layerSensor.body.fixtures[0];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.polygon));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'is sensor',
|
||||
(game) async {
|
||||
final layerSensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(layerSensor);
|
||||
|
||||
final fixture = layerSensor.body.fixtures[0];
|
||||
expect(fixture.isSensor, isTrue);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('LayerSensorBallContactCallback', () {
|
||||
late Ball ball;
|
||||
late Body body;
|
||||
|
||||
setUp(() {
|
||||
ball = MockBall();
|
||||
body = MockBody();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes ball layer and priority '
|
||||
'when a ball enters and exits a downward oriented LayerSensor',
|
||||
(game) async {
|
||||
final sensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: Layer.spaceshipEntranceRamp,
|
||||
)..initialPosition = Vector2(0, 10);
|
||||
final callback = TestLayerSensorBallContactCallback();
|
||||
|
||||
when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
|
||||
|
||||
callback.begin(ball, sensor, MockContact());
|
||||
verify(() => ball.layer = sensor.insideLayer).called(1);
|
||||
verify(() => ball.priority = sensor.insidePriority).called(1);
|
||||
verify(ball.reorderChildren).called(1);
|
||||
|
||||
when(() => ball.layer).thenReturn(sensor.insideLayer);
|
||||
|
||||
callback.begin(ball, sensor, MockContact());
|
||||
verify(() => ball.layer = Layer.board);
|
||||
verify(() => ball.priority = Ball.boardPriority).called(1);
|
||||
verify(ball.reorderChildren).called(1);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes ball layer and priority '
|
||||
'when a ball enters and exits an upward oriented LayerSensor',
|
||||
(game) async {
|
||||
final sensor = TestLayerSensor(
|
||||
orientation: LayerEntranceOrientation.up,
|
||||
insidePriority: insidePriority,
|
||||
insideLayer: Layer.spaceshipEntranceRamp,
|
||||
)..initialPosition = Vector2(0, 10);
|
||||
final callback = TestLayerSensorBallContactCallback();
|
||||
|
||||
when(() => body.linearVelocity).thenReturn(Vector2(0, 1));
|
||||
|
||||
callback.begin(ball, sensor, MockContact());
|
||||
verify(() => ball.layer = sensor.insideLayer).called(1);
|
||||
verify(() => ball.priority = sensor.insidePriority).called(1);
|
||||
verify(ball.reorderChildren).called(1);
|
||||
|
||||
when(() => ball.layer).thenReturn(sensor.insideLayer);
|
||||
|
||||
callback.begin(ball, sensor, MockContact());
|
||||
verify(() => ball.layer = Layer.board);
|
||||
verify(() => ball.priority = Ball.boardPriority).called(1);
|
||||
verify(ball.reorderChildren).called(1);
|
||||
});
|
||||
});
|
||||
}
|
@ -1,249 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
class TestRampOpening extends RampOpening {
|
||||
TestRampOpening({
|
||||
required RampOrientation orientation,
|
||||
required int insidePriority,
|
||||
required Layer pathwayLayer,
|
||||
}) : super(
|
||||
insideLayer: pathwayLayer,
|
||||
insidePriority: insidePriority,
|
||||
orientation: orientation,
|
||||
);
|
||||
|
||||
@override
|
||||
Shape get shape => PolygonShape()
|
||||
..set([
|
||||
Vector2(0, 0),
|
||||
Vector2(0, 1),
|
||||
Vector2(1, 1),
|
||||
Vector2(1, 0),
|
||||
]);
|
||||
}
|
||||
|
||||
class TestRampOpeningBallContactCallback
|
||||
extends RampOpeningBallContactCallback<TestRampOpening> {
|
||||
TestRampOpeningBallContactCallback() : super();
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
const insidePriority = 1;
|
||||
|
||||
group('RampOpening', () {
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final ramp = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
await game.ready();
|
||||
await game.ensureAdd(ramp);
|
||||
|
||||
expect(game.contains(ramp), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('body', () {
|
||||
flameTester.test(
|
||||
'is static',
|
||||
(game) async {
|
||||
final ramp = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
await game.ensureAdd(ramp);
|
||||
|
||||
expect(ramp.body.bodyType, equals(BodyType.static));
|
||||
},
|
||||
);
|
||||
|
||||
group('first fixture', () {
|
||||
const pathwayLayer = Layer.spaceshipEntranceRamp;
|
||||
const openingLayer = Layer.opening;
|
||||
|
||||
flameTester.test(
|
||||
'exists',
|
||||
(game) async {
|
||||
final ramp = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(ramp);
|
||||
|
||||
expect(ramp.body.fixtures[0], isA<Fixture>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'shape is a polygon',
|
||||
(game) async {
|
||||
final ramp = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(ramp);
|
||||
|
||||
final fixture = ramp.body.fixtures[0];
|
||||
expect(fixture.shape.shapeType, equals(ShapeType.polygon));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'is sensor',
|
||||
(game) async {
|
||||
final ramp = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: pathwayLayer,
|
||||
)..layer = openingLayer;
|
||||
await game.ensureAdd(ramp);
|
||||
|
||||
final fixture = ramp.body.fixtures[0];
|
||||
expect(fixture.isSensor, isTrue);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('RampOpeningBallContactCallback', () {
|
||||
flameTester.test(
|
||||
'changes ball layer '
|
||||
'when a ball enters upwards into a downward ramp opening',
|
||||
(game) async {
|
||||
final ball = MockBall();
|
||||
final body = MockBody();
|
||||
final area = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
final callback = TestRampOpeningBallContactCallback();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => body.position).thenReturn(Vector2.zero());
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verify(() => ball.layer = area.insideLayer).called(1);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes ball layer '
|
||||
'when a ball enters downwards into a upward ramp opening',
|
||||
(game) async {
|
||||
final ball = MockBall();
|
||||
final body = MockBody();
|
||||
final area = TestRampOpening(
|
||||
orientation: RampOrientation.up,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
);
|
||||
final callback = TestRampOpeningBallContactCallback();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => body.position).thenReturn(Vector2.zero());
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verify(() => ball.layer = area.insideLayer).called(1);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes ball layer '
|
||||
'when a ball exits from a downward oriented ramp', (game) async {
|
||||
final ball = MockBall();
|
||||
final body = MockBody();
|
||||
final area = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
)..initialPosition = Vector2(0, 10);
|
||||
final callback = TestRampOpeningBallContactCallback();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => body.position).thenReturn(Vector2.zero());
|
||||
when(() => body.linearVelocity).thenReturn(Vector2(0, 1));
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verify(() => ball.layer = area.insideLayer).called(1);
|
||||
|
||||
callback.end(ball, area, MockContact());
|
||||
verify(() => ball.layer = Layer.board);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'changes ball layer '
|
||||
'when a ball exits from a upward oriented ramp', (game) async {
|
||||
final ball = MockBall();
|
||||
final body = MockBody();
|
||||
final area = TestRampOpening(
|
||||
orientation: RampOrientation.up,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
)..initialPosition = Vector2(0, 10);
|
||||
final callback = TestRampOpeningBallContactCallback();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => body.position).thenReturn(Vector2.zero());
|
||||
when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verify(() => ball.layer = area.insideLayer).called(1);
|
||||
|
||||
callback.end(ball, area, MockContact());
|
||||
verify(() => ball.layer = Layer.board);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'change ball layer from pathwayLayer to Layer.board '
|
||||
'when a ball enters and exits from ramp', (game) async {
|
||||
final ball = MockBall();
|
||||
final body = MockBody();
|
||||
final area = TestRampOpening(
|
||||
orientation: RampOrientation.down,
|
||||
insidePriority: insidePriority,
|
||||
pathwayLayer: Layer.spaceshipEntranceRamp,
|
||||
)..initialPosition = Vector2(0, 10);
|
||||
final callback = TestRampOpeningBallContactCallback();
|
||||
|
||||
when(() => ball.body).thenReturn(body);
|
||||
when(() => ball.priority).thenReturn(1);
|
||||
when(() => body.position).thenReturn(Vector2.zero());
|
||||
when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
|
||||
when(() => ball.layer).thenReturn(Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verify(() => ball.layer = area.insideLayer).called(1);
|
||||
|
||||
callback.end(ball, area, MockContact());
|
||||
verifyNever(() => ball.layer = Layer.board);
|
||||
|
||||
callback.begin(ball, area, MockContact());
|
||||
verifyNever(() => ball.layer = area.insideLayer);
|
||||
|
||||
callback.end(ball, area, MockContact());
|
||||
verify(() => ball.layer = Layer.board);
|
||||
});
|
||||
});
|
||||
}
|
@ -1,221 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball_components/src/flame/priority.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
class TestBodyComponent extends BodyComponent {
|
||||
@override
|
||||
Body createBody() {
|
||||
final fixtureDef = FixtureDef(CircleShape());
|
||||
return world.createBody(BodyDef())..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
group('ComponentPriorityX', () {
|
||||
group('sendTo', () {
|
||||
flameTester.test(
|
||||
'changes the priority correctly to other level',
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = TestBodyComponent()..priority = 4;
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
expect(component.priority, equals(newPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the new priority is different',
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(4);
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is the same",
|
||||
(game) async {
|
||||
const newPriority = 5;
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(newPriority);
|
||||
|
||||
component.sendTo(newPriority);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('sendToBack', () {
|
||||
flameTester.test(
|
||||
'changes the priority correctly to board level',
|
||||
(game) async {
|
||||
final component = TestBodyComponent()..priority = 4;
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
expect(component.priority, equals(0));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is greater than lowest level',
|
||||
(game) async {
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(4);
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is the lowest level",
|
||||
(game) async {
|
||||
final component = MockComponent();
|
||||
when(() => component.priority).thenReturn(0);
|
||||
|
||||
component.sendToBack();
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('showBehindOf', () {
|
||||
flameTester.test(
|
||||
'changes the priority if it is greater than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority - 1;
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(otherComponent.priority - 1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't change the priority if it is lower than other component",
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority + 1;
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(startPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is greater than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority - 1);
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is lower than other "
|
||||
'component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority + 1);
|
||||
|
||||
component.showBehindOf(otherComponent);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('showInFrontOf', () {
|
||||
flameTester.test(
|
||||
'changes the priority if it is lower than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority + 1;
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(otherComponent.priority + 1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't change the priority if it is greater than other component",
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = TestBodyComponent()..priority = startPriority;
|
||||
final otherComponent = TestBodyComponent()
|
||||
..priority = startPriority - 1;
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
expect(component.priority, equals(startPriority));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'calls reorderChildren if the priority is lower than other component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority + 1);
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
verify(component.reorderChildren).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
"doesn't call reorderChildren if the priority is greater than other "
|
||||
'component',
|
||||
(game) async {
|
||||
const startPriority = 2;
|
||||
final component = MockComponent();
|
||||
final otherComponent = MockComponent();
|
||||
when(() => component.priority).thenReturn(startPriority);
|
||||
when(() => otherComponent.priority).thenReturn(startPriority - 1);
|
||||
|
||||
component.showInFrontOf(otherComponent);
|
||||
|
||||
verifyNever(component.reorderChildren);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# VSCode related
|
||||
.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
**/doc/api/
|
||||
**/ios/Flutter/.last_build_id
|
||||
.dart_tool/
|
||||
.flutter-plugins
|
||||
.flutter-plugins-dependencies
|
||||
.packages
|
||||
.pub-cache/
|
||||
.pub/
|
||||
/build/
|
||||
|
||||
# Web related
|
||||
lib/generated_plugin_registrant.dart
|
||||
|
||||
# Symbolication related
|
||||
app.*.symbols
|
||||
|
||||
# Obfuscation related
|
||||
app.*.map.json
|
@ -0,0 +1,11 @@
|
||||
# pinball_flame
|
||||
|
||||
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
|
||||
[![License: MIT][license_badge]][license_link]
|
||||
|
||||
Set of out-of-the-way solutions for common Pinball game problems.
|
||||
|
||||
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
|
||||
[license_link]: https://opensource.org/licenses/MIT
|
||||
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
|
||||
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
|
@ -0,0 +1 @@
|
||||
include: package:very_good_analysis/analysis_options.2.4.0.yaml
|
@ -0,0 +1,5 @@
|
||||
library pinball_flame;
|
||||
|
||||
export 'src/blueprint.dart';
|
||||
export 'src/component_controller.dart';
|
||||
export 'src/keyboard_input_controller.dart';
|
@ -1,12 +1,9 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// {@template component_controller}
|
||||
/// A [ComponentController] is a [Component] in charge of handling the logic
|
||||
/// associated with another [Component].
|
||||
///
|
||||
/// [ComponentController]s usually implement [BlocComponent].
|
||||
/// {@endtemplate}
|
||||
abstract class ComponentController<T extends Component> extends Component {
|
||||
/// {@macro component_controller}
|
@ -0,0 +1,20 @@
|
||||
name: pinball_flame
|
||||
description: Set of out-of-the-way solutions for common Pinball game problems.
|
||||
version: 1.0.0+1
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ">=2.16.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
flame: ^1.1.1
|
||||
flame_forge2d: ^0.11.0
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
dev_dependencies:
|
||||
flame_test: ^1.3.0
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
mocktail: ^0.3.0
|
||||
very_good_analysis: ^2.4.0
|
@ -0,0 +1 @@
|
||||
export 'mocks.dart';
|
@ -0,0 +1,10 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockForge2DGame extends Mock implements Forge2DGame {}
|
||||
|
||||
class MockContactCallback extends Mock
|
||||
implements ContactCallback<dynamic, dynamic> {}
|
||||
|
||||
class MockComponent extends Mock implements Component {}
|
@ -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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue