refactor: moved scaling to `BallScalingBehavior`

pull/313/head
alestiago 3 years ago
parent f42927736e
commit 23ad66ed78

@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
@ -32,6 +31,16 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
layer = Layer.board;
}
/// Creates a [Ball] without any behaviors.
///
/// This can be used for testing [Ball]'s behaviors in isolation.
@visibleForTesting
Ball.test()
: baseColor = const Color(0xFFFFFFFF),
super(
children: [_BallSpriteComponent()],
);
/// The size of the [Ball].
static final Vector2 size = Vector2.all(4.13);

@ -16,6 +16,8 @@ class BallScalingBehavior extends Component with ParentIsA<Ball> {
parent.body.fixtures.first.shape.radius = (Ball.size.x / 2) * scaleFactor;
parent.firstChild<SpriteComponent>()!.scale = Vector2.all(scaleFactor);
parent.firstChild<SpriteComponent>()!.scale.setFrom(
Vector2.all(scaleFactor),
);
}
}

@ -6,8 +6,9 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/ball/behaviors/behaviors.dart';
import '../../helpers/helpers.dart';
import '../../../helpers/helpers.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
@ -25,6 +26,15 @@ void main() {
},
);
flameTester.test('add a BallScalingBehavior', (game) async {
final ball = Ball(baseColor: Colors.blue);
await game.ensureAdd(ball);
expect(
ball.descendants().whereType<BallScalingBehavior>().length,
equals(1),
);
});
group('body', () {
flameTester.test(
'is dynamic',
@ -116,19 +126,6 @@ void main() {
expect(ball.body.position, equals(ball.initialPosition));
});
});
// TODO(allisonryan0002): delete or retest this if/when solution is added
// to prevent forces on a ball while stopped.
// flameTester.test('by applying velocity', (game) async {
// final ball = Ball(baseColor: Colors.blue);
// await game.ensureAdd(ball);
// ball.stop();
// ball.body.linearVelocity.setValues(10, 10);
// game.update(1);
// expect(ball.body.position, equals(ball.initialPosition));
// });
});
group('resume', () {
Loading…
Cancel
Save