mirror of https://github.com/flutter/pinball.git
feat: implemented DashBumper assets (#119)
* chore: rebase * chore: rebase * chore: rebase * docs: removed extra paragraph * fix: removed checks * refactor: removed ephemeral state * refactor: uncommented code * feat: automatically sized Sprites * docs: included TODO comment * refactor: corrected typo * docs: updated doc comment * feat: adjusted tests * chore: rebase * feat: included mustCallSuper * feat: implemented FlutterForest controllers * feat: improved tests * fix: analyzer * refactor: removed unneccessary mock * feat: include DashNestBumper tests * docs: improved doc comment * refactor: fixed test name grammar * fix: fixed testpull/133/head
parent
48f831264e
commit
e5c3708952
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 8.6 KiB |
After Width: | Height: | Size: 8.4 KiB |
@ -0,0 +1,142 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
/// {@template dash_nest_bumper}
|
||||
/// Bumper with a nest appearance.
|
||||
/// {@endtemplate}
|
||||
abstract class DashNestBumper extends BodyComponent with InitialPosition {
|
||||
/// {@macro dash_nest_bumper}
|
||||
DashNestBumper._({
|
||||
required String activeAssetPath,
|
||||
required String inactiveAssetPath,
|
||||
required SpriteComponent spriteComponent,
|
||||
}) : _activeAssetPath = activeAssetPath,
|
||||
_inactiveAssetPath = inactiveAssetPath,
|
||||
_spriteComponent = spriteComponent;
|
||||
|
||||
final String _activeAssetPath;
|
||||
late final Sprite _activeSprite;
|
||||
final String _inactiveAssetPath;
|
||||
late final Sprite _inactiveSprite;
|
||||
final SpriteComponent _spriteComponent;
|
||||
|
||||
Future<void> _loadSprites() async {
|
||||
// TODO(alestiago): I think ideally we would like to do:
|
||||
// Sprite(path).load so we don't require to store the activeAssetPath and
|
||||
// the inactive assetPath.
|
||||
_inactiveSprite = await gameRef.loadSprite(_inactiveAssetPath);
|
||||
_activeSprite = await gameRef.loadSprite(_activeAssetPath);
|
||||
}
|
||||
|
||||
/// Activates the [DashNestBumper].
|
||||
void activate() {
|
||||
_spriteComponent
|
||||
..sprite = _activeSprite
|
||||
..size = _activeSprite.originalSize / 10;
|
||||
}
|
||||
|
||||
/// Deactivates the [DashNestBumper].
|
||||
void deactivate() {
|
||||
_spriteComponent
|
||||
..sprite = _inactiveSprite
|
||||
..size = _inactiveSprite.originalSize / 10;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await _loadSprites();
|
||||
|
||||
// TODO(erickzanardo): Look into using onNewState instead.
|
||||
// Currently doing: onNewState(gameRef.read<GameState>()) will throw an
|
||||
// `Exception: build context is not available yet`
|
||||
deactivate();
|
||||
await add(_spriteComponent);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
class BigDashNestBumper extends DashNestBumper {
|
||||
/// {@macro dash_nest_bumper}
|
||||
BigDashNestBumper()
|
||||
: super._(
|
||||
activeAssetPath: Assets.images.dashBumper.main.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.main.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = EllipseShape(
|
||||
center: Vector2.zero(),
|
||||
majorRadius: 4.85,
|
||||
minorRadius: 3.95,
|
||||
)..rotate(math.pi / 2);
|
||||
final fixtureDef = FixtureDef(shape);
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..position = initialPosition
|
||||
..userData = this;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
class SmallDashNestBumper extends DashNestBumper {
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper._({
|
||||
required String activeAssetPath,
|
||||
required String inactiveAssetPath,
|
||||
required SpriteComponent spriteComponent,
|
||||
}) : super._(
|
||||
activeAssetPath: activeAssetPath,
|
||||
inactiveAssetPath: inactiveAssetPath,
|
||||
spriteComponent: spriteComponent,
|
||||
);
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper.a()
|
||||
: this._(
|
||||
activeAssetPath: Assets.images.dashBumper.a.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.a.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(0.35, -1.2),
|
||||
),
|
||||
);
|
||||
|
||||
/// {@macro dash_nest_bumper}
|
||||
SmallDashNestBumper.b()
|
||||
: this._(
|
||||
activeAssetPath: Assets.images.dashBumper.b.active.keyName,
|
||||
inactiveAssetPath: Assets.images.dashBumper.b.inactive.keyName,
|
||||
spriteComponent: SpriteComponent(
|
||||
anchor: Anchor.center,
|
||||
position: Vector2(0.35, -1.2),
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
Body createBody() {
|
||||
final shape = EllipseShape(
|
||||
center: Vector2.zero(),
|
||||
majorRadius: 3,
|
||||
minorRadius: 2.25,
|
||||
)..rotate(math.pi / 2);
|
||||
final fixtureDef = FixtureDef(shape)
|
||||
..friction = 0
|
||||
..restitution = 4;
|
||||
|
||||
final bodyDef = BodyDef()
|
||||
..position = initialPosition
|
||||
..userData = this;
|
||||
|
||||
return world.createBody(bodyDef)..createFixture(fixtureDef);
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
// 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 '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(TestGame.new);
|
||||
|
||||
group('BigDashNestBumper', () {
|
||||
flameTester.test('loads correctly', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('activate returns normally', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.activate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('deactivate returns normally', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.deactivate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('changes sprite', (game) async {
|
||||
final bumper = BigDashNestBumper();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
|
||||
|
||||
final deactivatedSprite = spriteComponent.sprite;
|
||||
bumper.activate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
|
||||
final activatedSprite = spriteComponent.sprite;
|
||||
bumper.deactivate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(activatedSprite)),
|
||||
);
|
||||
|
||||
expect(
|
||||
activatedSprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('SmallDashNestBumper', () {
|
||||
flameTester.test('"a" loads correctly', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('"b" loads correctly', (game) async {
|
||||
final bumper = SmallDashNestBumper.b();
|
||||
await game.ensureAdd(bumper);
|
||||
expect(game.contains(bumper), isTrue);
|
||||
});
|
||||
|
||||
flameTester.test('activate returns normally', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.activate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('deactivate returns normally', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
expect(bumper.deactivate, returnsNormally);
|
||||
});
|
||||
|
||||
flameTester.test('changes sprite', (game) async {
|
||||
final bumper = SmallDashNestBumper.a();
|
||||
await game.ensureAdd(bumper);
|
||||
|
||||
final spriteComponent = bumper.firstChild<SpriteComponent>()!;
|
||||
|
||||
final deactivatedSprite = spriteComponent.sprite;
|
||||
bumper.activate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
|
||||
final activatedSprite = spriteComponent.sprite;
|
||||
bumper.deactivate();
|
||||
expect(
|
||||
spriteComponent.sprite,
|
||||
isNot(equals(activatedSprite)),
|
||||
);
|
||||
|
||||
expect(
|
||||
activatedSprite,
|
||||
isNot(equals(deactivatedSprite)),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue