mirror of https://github.com/flutter/pinball.git
feat: add `BonusBallSpawningBehavior` (#378)
* feat: add BonusBallSpawningBehavior * refactor: remove on tick and remove behavior * fix: impulse test Co-authored-by: Tom Arra <tarra3@gmail.com>pull/386/head
parent
1a4bd2f85b
commit
54b0bc6e17
@ -0,0 +1,30 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball/select_character/select_character.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// {@template bonus_ball_spawning_behavior}
|
||||||
|
/// After a duration, spawns a bonus ball from the [DinoWalls] and boosts it
|
||||||
|
/// into the middle of the board.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BonusBallSpawningBehavior extends TimerComponent with HasGameRef {
|
||||||
|
/// {@macro bonus_ball_spawning_behavior}
|
||||||
|
BonusBallSpawningBehavior()
|
||||||
|
: super(
|
||||||
|
period: 5,
|
||||||
|
removeOnFinish: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onTick() {
|
||||||
|
final characterTheme = readBloc<CharacterThemeCubit, CharacterThemeState>()
|
||||||
|
.state
|
||||||
|
.characterTheme;
|
||||||
|
gameRef.descendants().whereType<ZCanvasComponent>().single.add(
|
||||||
|
Ball(assetPath: characterTheme.ball.keyName)
|
||||||
|
..add(BallImpulsingBehavior(impulse: Vector2(-40, 0)))
|
||||||
|
..initialPosition = Vector2(29.2, -24.5)
|
||||||
|
..zIndex = ZIndexes.ballOnBoard,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
/// {@template ball_impulsing_behavior}
|
||||||
|
/// Impulses the [Ball] in a given direction.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class BallImpulsingBehavior extends Component with ParentIsA<Ball> {
|
||||||
|
/// {@macro ball_impulsing_behavior}
|
||||||
|
BallImpulsingBehavior({
|
||||||
|
required Vector2 impulse,
|
||||||
|
}) : _impulse = impulse;
|
||||||
|
|
||||||
|
final Vector2 _impulse;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
parent.body.linearVelocity = _impulse;
|
||||||
|
shouldRemove = true;
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
export 'ball_gravitating_behavior.dart';
|
export 'ball_gravitating_behavior.dart';
|
||||||
|
export 'ball_impulsing_behavior.dart';
|
||||||
export 'ball_scaling_behavior.dart';
|
export 'ball_scaling_behavior.dart';
|
||||||
export 'ball_turbo_charging_behavior.dart';
|
export 'ball_turbo_charging_behavior.dart';
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/components.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_theme/pinball_theme.dart' as theme;
|
||||||
|
|
||||||
|
import '../../../../helpers/helpers.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group(
|
||||||
|
'BallImpulsingBehavior',
|
||||||
|
() {
|
||||||
|
final asset = theme.Assets.images.dash.ball.keyName;
|
||||||
|
final flameTester = FlameTester(() => TestGame([asset]));
|
||||||
|
|
||||||
|
test('can be instantiated', () {
|
||||||
|
expect(
|
||||||
|
BallImpulsingBehavior(impulse: Vector2.zero()),
|
||||||
|
isA<BallImpulsingBehavior>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'impulses the ball with the given velocity when loaded '
|
||||||
|
'and then removes itself',
|
||||||
|
(game) async {
|
||||||
|
final ball = Ball.test();
|
||||||
|
await game.ensureAdd(ball);
|
||||||
|
final impulse = Vector2.all(1);
|
||||||
|
final behavior = BallImpulsingBehavior(impulse: impulse);
|
||||||
|
await ball.ensureAdd(behavior);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
ball.body.linearVelocity.x,
|
||||||
|
equals(impulse.x),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
ball.body.linearVelocity.y,
|
||||||
|
equals(impulse.y),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
game.descendants().whereType<BallImpulsingBehavior>().isEmpty,
|
||||||
|
isTrue,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/forge2d_game.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pinball/game/behaviors/behaviors.dart';
|
||||||
|
import 'package:pinball/select_character/select_character.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_theme/pinball_theme.dart' as theme;
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
images.prefix = '';
|
||||||
|
await images.loadAll([
|
||||||
|
theme.Assets.images.dash.ball.keyName,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pump(BonusBallSpawningBehavior child) async {
|
||||||
|
await ensureAdd(
|
||||||
|
FlameBlocProvider<CharacterThemeCubit, CharacterThemeState>.value(
|
||||||
|
value: CharacterThemeCubit(),
|
||||||
|
children: [
|
||||||
|
ZCanvasComponent(
|
||||||
|
children: [child],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
group('FlutterForestBonusBehavior', () {
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'adds a ball with a BallImpulsingBehavior to the game onTick '
|
||||||
|
'resulting in a -40 x impulse',
|
||||||
|
(game) async {
|
||||||
|
await game.onLoad();
|
||||||
|
final behavior = BonusBallSpawningBehavior();
|
||||||
|
|
||||||
|
await game.pump(behavior);
|
||||||
|
|
||||||
|
game.update(behavior.timer.limit);
|
||||||
|
await game.ready();
|
||||||
|
|
||||||
|
final ball = game.descendants().whereType<Ball>().single;
|
||||||
|
|
||||||
|
expect(ball.body.linearVelocity.x, equals(-40));
|
||||||
|
expect(ball.body.linearVelocity.y, equals(0));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue