mirror of https://github.com/flutter/pinball.git
commit
d1aa319002
After Width: | Height: | Size: 7.8 KiB |
@ -1,27 +1,39 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball/select_character/select_character.dart';
|
||||
import 'package:pinball_audio/pinball_audio.dart';
|
||||
|
||||
part 'assets_manager_state.dart';
|
||||
|
||||
/// {@template assets_manager_cubit}
|
||||
/// Cubit responsable for pre loading any game assets
|
||||
/// {@endtemplate}
|
||||
class AssetsManagerCubit extends Cubit<AssetsManagerState> {
|
||||
/// {@macro assets_manager_cubit}
|
||||
AssetsManagerCubit(List<Future> loadables)
|
||||
: super(
|
||||
AssetsManagerState.initial(
|
||||
loadables: loadables,
|
||||
),
|
||||
);
|
||||
AssetsManagerCubit(this._game, this._player)
|
||||
: super(const AssetsManagerState.initial());
|
||||
|
||||
final PinballGame _game;
|
||||
final PinballPlayer _player;
|
||||
|
||||
/// Loads the assets
|
||||
Future<void> load() async {
|
||||
/// Assigning loadables is a very expensive operation. With this purposeful
|
||||
/// delay here, which is a bit random in duration but enough to let the UI
|
||||
/// do its job without adding too much delay for the user, we are letting
|
||||
/// the UI paint first, and then we start loading the assets.
|
||||
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||
emit(
|
||||
state.copyWith(
|
||||
loadables: [
|
||||
_game.preFetchLeaderboard(),
|
||||
..._game.preLoadAssets(),
|
||||
..._player.load(),
|
||||
...BonusAnimation.loadAssets(),
|
||||
...SelectedCharacter.loadAssets(),
|
||||
],
|
||||
),
|
||||
);
|
||||
final all = state.loadables.map((loadable) async {
|
||||
await loadable;
|
||||
emit(state.copyWith(loaded: [...state.loaded, loadable]));
|
||||
}).toList();
|
||||
|
||||
await Future.wait(all);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,18 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
class AndroidSpaceshipEntranceBallContactBehavior
|
||||
extends ContactBehavior<AndroidSpaceshipEntrance> {
|
||||
extends ContactBehavior<AndroidSpaceshipEntrance>
|
||||
with FlameBlocReader<AndroidSpaceshipCubit, AndroidSpaceshipState> {
|
||||
@override
|
||||
void beginContact(Object other, Contact contact) {
|
||||
super.beginContact(other, contact);
|
||||
if (other is! Ball) return;
|
||||
|
||||
parent.parent.bloc.onBallEntered();
|
||||
bloc.onBallEntered();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
export 'dash_bumper_ball_contact_behavior.dart';
|
@ -0,0 +1,17 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
|
||||
part 'dash_bumper_state.dart';
|
||||
|
||||
class DashBumperCubit extends Cubit<DashBumperState> {
|
||||
DashBumperCubit() : super(DashBumperState.inactive);
|
||||
|
||||
/// Event added when the bumper contacts with a ball.
|
||||
void onBallContacted() {
|
||||
emit(DashBumperState.active);
|
||||
}
|
||||
|
||||
/// Event added when the bumper should return to its initial configuration.
|
||||
void onReset() {
|
||||
emit(DashBumperState.inactive);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
part of 'dash_bumper_cubit.dart';
|
||||
|
||||
/// Indicates the [DashBumperCubit]'s current state.
|
||||
enum DashBumperState {
|
||||
/// A lit up bumper.
|
||||
active,
|
||||
|
||||
/// A dimmed bumper.
|
||||
inactive,
|
||||
}
|
@ -1 +0,0 @@
|
||||
export 'dash_nest_bumper_contact_behavior.dart';
|
@ -1,17 +0,0 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
|
||||
part 'dash_nest_bumper_state.dart';
|
||||
|
||||
class DashNestBumperCubit extends Cubit<DashNestBumperState> {
|
||||
DashNestBumperCubit() : super(DashNestBumperState.inactive);
|
||||
|
||||
/// Event added when the bumper contacts with a ball.
|
||||
void onBallContacted() {
|
||||
emit(DashNestBumperState.active);
|
||||
}
|
||||
|
||||
/// Event added when the bumper should return to its initial configuration.
|
||||
void onReset() {
|
||||
emit(DashNestBumperState.inactive);
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
part of 'dash_nest_bumper_cubit.dart';
|
||||
|
||||
/// Indicates the [DashNestBumperCubit]'s current state.
|
||||
enum DashNestBumperState {
|
||||
/// A lit up bumper.
|
||||
active,
|
||||
|
||||
/// A dimmed bumper.
|
||||
inactive,
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'score_component_scaling_behavior.dart';
|
@ -0,0 +1,24 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// Scales a [ScoreComponent] according to its position on the board.
|
||||
class ScoreComponentScalingBehavior extends Component
|
||||
with ParentIsA<SpriteComponent> {
|
||||
@override
|
||||
void update(double dt) {
|
||||
super.update(dt);
|
||||
final boardHeight = BoardDimensions.bounds.height;
|
||||
const maxShrinkValue = 0.83;
|
||||
|
||||
final augmentedPosition = parent.position.y * 3;
|
||||
final standardizedYPosition = augmentedPosition + (boardHeight / 2);
|
||||
final scaleFactor = maxShrinkValue +
|
||||
((standardizedYPosition / boardHeight) * (1 - maxShrinkValue));
|
||||
|
||||
parent.scale.setValues(
|
||||
scaleFactor,
|
||||
scaleFactor,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/components.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 'package:pinball_components/src/components/bumping_behavior.dart';
|
||||
import 'package:pinball_components/src/components/dash_bumper/behaviors/behaviors.dart';
|
||||
|
||||
import '../../../helpers/helpers.dart';
|
||||
|
||||
class _MockDashBumperCubit extends Mock implements DashBumperCubit {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('DashBumper', () {
|
||||
final flameTester = FlameTester(
|
||||
() => TestGame(
|
||||
[
|
||||
Assets.images.dash.bumper.main.active.keyName,
|
||||
Assets.images.dash.bumper.main.inactive.keyName,
|
||||
Assets.images.dash.bumper.a.active.keyName,
|
||||
Assets.images.dash.bumper.a.inactive.keyName,
|
||||
Assets.images.dash.bumper.b.active.keyName,
|
||||
Assets.images.dash.bumper.b.inactive.keyName,
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
flameTester.test('"main" loads correctly', (game) async {
|
||||
final bumper = DashBumper.main();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"a" loads correctly', (game) async {
|
||||
final bumper = DashBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"b" loads correctly', (game) async {
|
||||
final bumper = DashBumper.b();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
// ignore: public_member_api_docs
|
||||
flameTester.test('closes bloc when removed', (game) async {
|
||||
final bloc = _MockDashBumperCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<DashBumperState>.empty(),
|
||||
initialState: DashBumperState.inactive,
|
||||
);
|
||||
when(bloc.close).thenAnswer((_) async {});
|
||||
final bumper = DashBumper.test(bloc: bloc);
|
||||
|
||||
await game.ensureAdd(bumper);
|
||||
game.remove(bumper);
|
||||
await game.ready();
|
||||
|
||||
verify(bloc.close).called(1);
|
||||
});
|
||||
|
||||
flameTester.test('adds a bumperBallContactBehavior', (game) async {
|
||||
final bumper = DashBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(
|
||||
bumper.children.whereType<DashBumperBallContactBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
|
||||
group("'main' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final bumper = DashBumper.main(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(bumper);
|
||||
expect(bumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final bumper = DashBumper.main();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(
|
||||
bumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("'a' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final bumper = DashBumper.a(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(bumper);
|
||||
expect(bumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final bumper = DashBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(
|
||||
bumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("'b' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final bumper = DashBumper.b(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(bumper);
|
||||
expect(bumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final bumper = DashBumper.b();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(
|
||||
bumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame/components.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 'package:pinball_components/src/components/bumping_behavior.dart';
|
||||
import 'package:pinball_components/src/components/dash_nest_bumper/behaviors/behaviors.dart';
|
||||
|
||||
import '../../../helpers/helpers.dart';
|
||||
|
||||
class _MockDashNestBumperCubit extends Mock implements DashNestBumperCubit {}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('DashNestBumper', () {
|
||||
final assets = [
|
||||
Assets.images.dash.bumper.main.active.keyName,
|
||||
Assets.images.dash.bumper.main.inactive.keyName,
|
||||
Assets.images.dash.bumper.a.active.keyName,
|
||||
Assets.images.dash.bumper.a.inactive.keyName,
|
||||
Assets.images.dash.bumper.b.active.keyName,
|
||||
Assets.images.dash.bumper.b.inactive.keyName,
|
||||
];
|
||||
final flameTester = FlameTester(() => TestGame(assets));
|
||||
|
||||
flameTester.test('"main" loads correctly', (game) async {
|
||||
final bumper = DashNestBumper.main();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"a" loads correctly', (game) async {
|
||||
final bumper = DashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"b" loads correctly', (game) async {
|
||||
final bumper = DashNestBumper.b();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('closes bloc when removed', (game) async {
|
||||
final bloc = _MockDashNestBumperCubit();
|
||||
whenListen(
|
||||
bloc,
|
||||
const Stream<DashNestBumperState>.empty(),
|
||||
initialState: DashNestBumperState.inactive,
|
||||
);
|
||||
when(bloc.close).thenAnswer((_) async {});
|
||||
final dashNestBumper = DashNestBumper.test(bloc: bloc);
|
||||
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
game.remove(dashNestBumper);
|
||||
await game.ready();
|
||||
|
||||
verify(bloc.close).called(1);
|
||||
});
|
||||
|
||||
flameTester.test('adds a DashNestBumperBallContactBehavior', (game) async {
|
||||
final dashNestBumper = DashNestBumper.a();
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(
|
||||
dashNestBumper.children
|
||||
.whereType<DashNestBumperBallContactBehavior>()
|
||||
.single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
|
||||
group("'main' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final dashNestBumper = DashNestBumper.main(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(dashNestBumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final dashNestBumper = DashNestBumper.main();
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(
|
||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("'a' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final dashNestBumper = DashNestBumper.a(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(dashNestBumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final dashNestBumper = DashNestBumper.a();
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(
|
||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group("'b' adds", () {
|
||||
flameTester.test('new children', (game) async {
|
||||
final component = Component();
|
||||
final dashNestBumper = DashNestBumper.b(
|
||||
children: [component],
|
||||
);
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(dashNestBumper.children, contains(component));
|
||||
});
|
||||
|
||||
flameTester.test('a BumpingBehavior', (game) async {
|
||||
final dashNestBumper = DashNestBumper.b();
|
||||
await game.ensureAdd(dashNestBumper);
|
||||
expect(
|
||||
dashNestBumper.children.whereType<BumpingBehavior>().single,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_components/src/components/score_component/behaviors/behaviors.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('ScoreComponentScalingBehavior', () {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(
|
||||
() => TestGame([
|
||||
Assets.images.score.fiveThousand.keyName,
|
||||
Assets.images.score.twentyThousand.keyName,
|
||||
Assets.images.score.twoHundredThousand.keyName,
|
||||
Assets.images.score.oneMillion.keyName,
|
||||
]),
|
||||
);
|
||||
|
||||
test('can be instantiated', () {
|
||||
expect(
|
||||
ScoreComponentScalingBehavior(),
|
||||
isA<ScoreComponentScalingBehavior>(),
|
||||
);
|
||||
});
|
||||
|
||||
flameTester.test('can be loaded', (game) async {
|
||||
final parent = ScoreComponent.test(
|
||||
points: Points.fiveThousand,
|
||||
position: Vector2.zero(),
|
||||
effectController: EffectController(duration: 1),
|
||||
);
|
||||
final behavior = ScoreComponentScalingBehavior();
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
expect(parent.children, contains(behavior));
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'scales the sprite',
|
||||
(game) async {
|
||||
final parent1 = ScoreComponent.test(
|
||||
points: Points.fiveThousand,
|
||||
position: Vector2(0, 10),
|
||||
effectController: EffectController(duration: 1),
|
||||
);
|
||||
final parent2 = ScoreComponent.test(
|
||||
points: Points.fiveThousand,
|
||||
position: Vector2(0, -10),
|
||||
effectController: EffectController(duration: 1),
|
||||
);
|
||||
await game.ensureAddAll([parent1, parent2]);
|
||||
|
||||
await parent1.ensureAdd(ScoreComponentScalingBehavior());
|
||||
await parent2.ensureAdd(ScoreComponentScalingBehavior());
|
||||
game.update(1);
|
||||
|
||||
expect(
|
||||
parent1.scale.x,
|
||||
greaterThan(parent2.scale.x),
|
||||
);
|
||||
expect(
|
||||
parent1.scale.y,
|
||||
greaterThan(parent2.scale.y),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/assets_manager/assets_manager.dart';
|
||||
|
||||
void main() {
|
||||
group('AssetsManagerCubit', () {
|
||||
final completer1 = Completer<void>();
|
||||
final completer2 = Completer<void>();
|
||||
|
||||
final future1 = completer1.future;
|
||||
final future2 = completer2.future;
|
||||
|
||||
blocTest<AssetsManagerCubit, AssetsManagerState>(
|
||||
'emits the loaded on the order that they load',
|
||||
build: () => AssetsManagerCubit([future1, future2]),
|
||||
act: (cubit) {
|
||||
cubit.load();
|
||||
completer2.complete();
|
||||
completer1.complete();
|
||||
},
|
||||
expect: () => [
|
||||
AssetsManagerState(
|
||||
loadables: [future1, future2],
|
||||
loaded: [future2],
|
||||
),
|
||||
AssetsManagerState(
|
||||
loadables: [future1, future2],
|
||||
loaded: [future2, future1],
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue