refactor: controlled animatronic

pull/169/head
Allison Ryan 4 years ago
parent 9386069745
commit 7a378ab673

@ -37,7 +37,8 @@ class FlutterForest extends Component with Controls<_FlutterForestController> {
final smallRightNest = _ControlledSmallDashNestBumper.b( final smallRightNest = _ControlledSmallDashNestBumper.b(
id: 'small_nest_bumper_b', id: 'small_nest_bumper_b',
)..initialPosition = Vector2(23.3, 46.75); )..initialPosition = Vector2(23.3, 46.75);
final dashAnimatronic = _DashAnimatronic(); final dashAnimatronic = _ControlledDashAnimatronic()
..position = Vector2(20, -66);
await addAll([ await addAll([
signPost, signPost,
@ -75,7 +76,7 @@ class _FlutterForestController extends ComponentController<FlutterForest>
} }
void _startDashAnimatronic() { void _startDashAnimatronic() {
component.descendants().whereType<_DashAnimatronic>().single.playing = true; component.firstChild<_ControlledDashAnimatronic>()?.controller.start();
} }
Future<void> _addBonusBall() async { Future<void> _addBonusBall() async {
@ -87,45 +88,20 @@ class _FlutterForestController extends ComponentController<FlutterForest>
} }
} }
class _DashAnimatronic extends SpriteAnimationComponent with HasGameRef { class _ControlledDashAnimatronic extends DashAnimatronic
_DashAnimatronic() with Controls<_DashAnimatronicController> {
: super( _ControlledDashAnimatronic() {
size: Vector2(15, 15), controller = _DashAnimatronicController(this);
anchor: Anchor.center,
position: Vector2(20, -66),
playing: false,
);
late final SpriteAnimation _animation;
@override
Future<void>? onLoad() async {
await super.onLoad();
final spriteSheet = await gameRef.images.load(
Assets.images.dash.animatronic.keyName,
);
_animation = SpriteAnimation.fromFrameData(
spriteSheet,
SpriteAnimationData.sequenced(
amount: 96,
amountPerRow: 12,
stepTime: 1 / 24,
textureSize: Vector2(150, 150),
loop: false,
),
);
animation = _animation;
} }
@override
void update(double dt) {
super.update(dt);
if (_animation.isLastFrame) {
_animation.reset();
playing = false;
} }
class _DashAnimatronicController extends ComponentController<DashAnimatronic>
with HasGameRef<PinballGame> {
_DashAnimatronicController(DashAnimatronic dashAnimatronic)
: super(dashAnimatronic);
void start() {
component.playing = true;
} }
} }

@ -10,7 +10,7 @@ import 'package:pinball_components/pinball_components.dart';
/// {@endtemplate} /// {@endtemplate}
class Ball<T extends Forge2DGame> extends BodyComponent<T> class Ball<T extends Forge2DGame> extends BodyComponent<T>
with Layered, InitialPosition { with Layered, InitialPosition {
/// {@macro ball_body} /// {@macro ball}
Ball({ Ball({
required this.baseColor, required this.baseColor,
}) { }) {

@ -6,6 +6,7 @@ export 'board_side.dart';
export 'boundaries.dart'; export 'boundaries.dart';
export 'camera_zoom.dart'; export 'camera_zoom.dart';
export 'chrome_dino.dart'; export 'chrome_dino.dart';
export 'dash_animatronic.dart';
export 'dash_nest_bumper.dart'; export 'dash_nest_bumper.dart';
export 'dino_walls.dart'; export 'dino_walls.dart';
export 'fire_effect.dart'; export 'fire_effect.dart';

@ -0,0 +1,53 @@
import 'package:flame/components.dart';
import 'package:pinball_components/pinball_components.dart';
/// {@template dash_animatronic}
/// Animated Dash that sits on top of the [BigDashNestBumper].
/// {@endtemplate}
class DashAnimatronic extends SpriteAnimationComponent with HasGameRef {
/// {@macro dash_animatronic}
DashAnimatronic()
: super(
anchor: Anchor.center,
playing: false,
);
@override
Future<void>? onLoad() async {
await super.onLoad();
final spriteSheet = await gameRef.images.load(
Assets.images.dash.animatronic.keyName,
);
const amountPerRow = 12;
const amountPerColumn = 8;
final textureSize = Vector2(
spriteSheet.width / amountPerRow,
spriteSheet.height / amountPerColumn,
);
size = textureSize / 10;
animation = SpriteAnimation.fromFrameData(
spriteSheet,
SpriteAnimationData.sequenced(
amount: amountPerRow * amountPerColumn,
amountPerRow: amountPerRow,
stepTime: 1 / 24,
textureSize: textureSize,
loop: false,
),
);
}
@override
void update(double dt) {
super.update(dt);
if (animation != null) {
if (animation!.isLastFrame) {
animation!.reset();
playing = false;
}
}
}
}

@ -0,0 +1,38 @@
// ignore_for_file: cascade_invocations
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import '../../helpers/helpers.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(TestGame.new);
group('DashAnimatronic', () {
flameTester.test(
'loads correctly',
(game) async {
final dashAnimatronic = DashAnimatronic();
await game.ensureAdd(dashAnimatronic);
expect(game.contains(dashAnimatronic), isTrue);
},
);
flameTester.test(
'stops animating after animation completes',
(game) async {
final dashAnimatronic = DashAnimatronic();
await game.ensureAdd(dashAnimatronic);
dashAnimatronic.playing = true;
dashAnimatronic.animation?.setToLast();
game.update(1);
expect(dashAnimatronic.playing, isFalse);
},
);
});
}

@ -19,7 +19,6 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
await game.ready();
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
@ -31,7 +30,6 @@ void main() {
flameTester.test( flameTester.test(
'a FlutterSignPost', 'a FlutterSignPost',
(game) async { (game) async {
await game.ready();
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
@ -45,7 +43,6 @@ void main() {
flameTester.test( flameTester.test(
'a BigDashNestBumper', 'a BigDashNestBumper',
(game) async { (game) async {
await game.ready();
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
@ -59,7 +56,6 @@ void main() {
flameTester.test( flameTester.test(
'two SmallDashNestBumper', 'two SmallDashNestBumper',
(game) async { (game) async {
await game.ready();
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
@ -106,12 +102,11 @@ void main() {
'onNewState adds a new ball after a duration', 'onNewState adds a new ball after a duration',
(game) async { (game) async {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ready();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
final previousBalls = game.descendants().whereType<Ball>().length; final previousBalls = game.descendants().whereType<Ball>().length;
flutterForest.controller.onNewState(MockGameState()); flutterForest.controller.onNewState(MockGameState());
await game.ready();
await Future<void>.delayed(const Duration(milliseconds: 700)); await Future<void>.delayed(const Duration(milliseconds: 700));
await game.ready(); await game.ready();
@ -126,35 +121,13 @@ void main() {
'onNewState starts Dash animatronic', 'onNewState starts Dash animatronic',
(game) async { (game) async {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ready();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
flutterForest.controller.onNewState(MockGameState()); flutterForest.controller.onNewState(MockGameState());
await game.ready();
final dashAnimatronic = final dashAnimatronic =
game.descendants().whereType<SpriteAnimationComponent>().single; game.descendants().whereType<DashAnimatronic>().single;
expect(dashAnimatronic.playing, isTrue);
},
);
flameTester.test(
'Dash animatronic stops animating after animation completes',
(game) async {
final flutterForest = FlutterForest();
await game.ready();
await game.ensureAdd(flutterForest);
flutterForest.controller.onNewState(MockGameState());
await game.ready();
final dashAnimatronic =
game.descendants().whereType<SpriteAnimationComponent>().single;
expect(dashAnimatronic.playing, isTrue); expect(dashAnimatronic.playing, isTrue);
dashAnimatronic.animation?.setToLast();
game.update(1);
expect(dashAnimatronic.playing, isFalse);
}, },
); );

Loading…
Cancel
Save