refactor: moved Sprites to children param (#189)

pull/202/head
Alejandro Santiago 3 years ago committed by GitHub
parent 62244f5a37
commit 0dbd8f1600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,13 +14,18 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
/// {@macro ball}
Ball({
required this.baseColor,
}) {
}) : super(
children: [
_BallSpriteComponent()..tint(baseColor.withOpacity(0.5)),
],
) {
// TODO(ruimiguel): while developing Ball can be launched by clicking mouse,
// and default layer is Layer.all. But on final game Ball will be always be
// be launched from Plunger and LauncherRamp will modify it to Layer.board.
// We need to see what happens if Ball appears from other place like nest
// bumper, it will need to explicit change layer to Layer.board then.
layer = Layer.board;
renderBody = false;
}
/// Render priority for the [Ball] while it's on the board.
@ -47,20 +52,6 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
double _boostTimer = 0;
static const _boostDuration = 2.0;
final _BallSpriteComponent _spriteComponent = _BallSpriteComponent();
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(
_spriteComponent..tint(baseColor.withOpacity(0.5)),
);
renderBody = false;
}
@override
Body createBody() {
final shape = CircleShape()..radius = size.x / 2;
@ -132,7 +123,10 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
((standardizedYPosition / boardHeight) * (1 - maxShrinkValue));
body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor;
_spriteComponent.scale = Vector2.all(scaleFactor);
// TODO(alestiago): Revisit and see if there's a better way to do this.
final spriteComponent = firstChild<_BallSpriteComponent>();
spriteComponent?.scale = Vector2.all(scaleFactor);
}
void _setPositionalGravity() {

@ -11,7 +11,12 @@ class Baseboard extends BodyComponent with InitialPosition {
/// {@macro baseboard}
Baseboard({
required BoardSide side,
}) : _side = side;
}) : _side = side,
super(
children: [_BaseboardSpriteComponent(side: side)],
) {
renderBody = false;
}
/// Whether the [Baseboard] is on the left or right side of the board.
final BoardSide _side;
@ -79,13 +84,6 @@ class Baseboard extends BodyComponent with InitialPosition {
return fixturesDef;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_BaseboardSpriteComponent(side: _side));
}
@override
Body createBody() {
const angle = 37.1 * (math.pi / 180);

@ -24,7 +24,13 @@ class Boundaries extends Forge2DBlueprint {
/// {@endtemplate bottom_boundary}
class _BottomBoundary extends BodyComponent with InitialPosition {
/// {@macro bottom_boundary}
_BottomBoundary() : super(priority: 1);
_BottomBoundary()
: super(
priority: 1,
children: [_BottomBoundarySpriteComponent()],
) {
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDefs = <FixtureDef>[];
@ -60,13 +66,6 @@ class _BottomBoundary extends BodyComponent with InitialPosition {
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_BottomBoundarySpriteComponent());
}
}
class _BottomBoundarySpriteComponent extends SpriteComponent with HasGameRef {
@ -89,7 +88,13 @@ class _BottomBoundarySpriteComponent extends SpriteComponent with HasGameRef {
/// {@endtemplate outer_boundary}
class _OuterBoundary extends BodyComponent with InitialPosition {
/// {@macro outer_boundary}
_OuterBoundary() : super(priority: Ball.launchRampPriority - 1);
_OuterBoundary()
: super(
priority: Ball.launchRampPriority - 1,
children: [_OuterBoundarySpriteComponent()],
) {
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDefs = <FixtureDef>[];
@ -131,13 +136,6 @@ class _OuterBoundary extends BodyComponent with InitialPosition {
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_OuterBoundarySpriteComponent());
}
}
class _OuterBoundarySpriteComponent extends SpriteComponent with HasGameRef {

@ -29,7 +29,13 @@ class DinoWalls extends Forge2DBlueprint {
/// {@endtemplate}
class _DinoTopWall extends BodyComponent with InitialPosition {
///{@macro dino_top_wall}
_DinoTopWall() : super(priority: 1);
_DinoTopWall()
: super(
priority: 1,
children: [_DinoTopWallSpriteComponent()],
) {
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
@ -98,14 +104,6 @@ class _DinoTopWall extends BodyComponent with InitialPosition {
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_DinoTopWallSpriteComponent());
}
}
class _DinoTopWallSpriteComponent extends SpriteComponent with HasGameRef {
@ -126,7 +124,12 @@ class _DinoTopWallSpriteComponent extends SpriteComponent with HasGameRef {
/// {@endtemplate}
class _DinoBottomWall extends BodyComponent with InitialPosition {
///{@macro dino_top_wall}
_DinoBottomWall();
_DinoBottomWall()
: super(
children: [_DinoBottomWallSpriteComponent()],
) {
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
@ -206,14 +209,6 @@ class _DinoBottomWall extends BodyComponent with InitialPosition {
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_DinoBottomWallSpriteComponent());
}
}
class _DinoBottomWallSpriteComponent extends SpriteComponent with HasGameRef {

@ -13,7 +13,11 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition {
/// {@macro flipper}
Flipper({
required this.side,
});
}) : super(
children: [_FlipperSpriteComponent(side: side)],
) {
renderBody = false;
}
/// The size of the [Flipper].
static final size = Vector2(13.5, 4.3);
@ -112,10 +116,8 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition {
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await _anchorToJoint();
await add(_FlipperSpriteComponent(side: side));
}
@override

@ -6,12 +6,12 @@ import 'package:pinball_components/pinball_components.dart';
/// A sign, found in the Flutter Forest.
/// {@endtemplate}
class FlutterSignPost extends BodyComponent with InitialPosition {
@override
Future<void> onLoad() async {
await super.onLoad();
/// {@macro flutter_sign_post}
FlutterSignPost()
: super(
children: [_FlutterSignPostSpriteComponent()],
) {
renderBody = false;
await add(_FlutterSignPostSpriteComponent());
}
@override

@ -16,7 +16,12 @@ class Kicker extends BodyComponent with InitialPosition {
/// {@macro kicker}
Kicker({
required BoardSide side,
}) : _side = side;
}) : _side = side,
super(
children: [_KickerSpriteComponent(side: side)],
) {
renderBody = false;
}
/// The size of the [Kicker] body.
static final Vector2 size = Vector2(4.4, 15);
@ -120,13 +125,6 @@ class Kicker extends BodyComponent with InitialPosition {
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_KickerSpriteComponent(side: _side));
}
}
class _KickerSpriteComponent extends SpriteComponent with HasGameRef {

@ -42,15 +42,17 @@ class Slingshot extends BodyComponent with InitialPosition {
required String spritePath,
}) : _length = length,
_angle = angle,
_spritePath = spritePath,
super(priority: 1);
super(
priority: 1,
children: [_SlinghsotSpriteComponent(spritePath, angle: angle)],
) {
renderBody = false;
}
final double _length;
final double _angle;
final String _spritePath;
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
const circleRadius = 1.55;
@ -104,24 +106,25 @@ class Slingshot extends BodyComponent with InitialPosition {
return body;
}
}
class _SlinghsotSpriteComponent extends SpriteComponent with HasGameRef {
_SlinghsotSpriteComponent(
String path, {
required double angle,
}) : _path = path,
super(
angle: -angle,
anchor: Anchor.center,
);
final String _path;
@override
Future<void> onLoad() async {
await super.onLoad();
await _loadSprite();
renderBody = false;
}
Future<void> _loadSprite() async {
final sprite = await gameRef.loadSprite(_spritePath);
await add(
SpriteComponent(
sprite: sprite,
size: sprite.originalSize / 10,
anchor: Anchor.center,
angle: -_angle,
),
);
final sprite = await gameRef.loadSprite(_path);
this.sprite = sprite;
size = sprite.originalSize / 10;
}
}

@ -92,16 +92,13 @@ class SpaceshipSaucer extends BodyComponent with InitialPosition, Layered {
/// {@endtemplate}
class AndroidHead extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_bridge}
AndroidHead() : super(priority: Ball.spaceshipPriority + 1) {
layer = Layer.spaceship;
}
@override
Future<void> onLoad() async {
await super.onLoad();
AndroidHead()
: super(
priority: Ball.spaceshipPriority + 1,
children: [_AndroidHeadSpriteAnimation()],
) {
renderBody = false;
await add(_AndroidHeadSpriteAnimation());
layer = Layer.spaceship;
}
@override

@ -170,8 +170,12 @@ class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent
class _SpaceshipRampForegroundRailing extends BodyComponent
with InitialPosition, Layered {
_SpaceshipRampForegroundRailing()
: super(priority: Ball.spaceshipRampPriority + 1) {
: super(
priority: Ball.spaceshipRampPriority + 1,
children: [_SpaceshipRampForegroundRailingSpriteComponent()],
) {
layer = Layer.spaceshipEntranceRamp;
renderBody = false;
}
List<FixtureDef> _createFixtureDefs() {
@ -222,14 +226,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_SpaceshipRampForegroundRailingSpriteComponent());
}
}
class _SpaceshipRampForegroundRailingSpriteComponent extends SpriteComponent

@ -18,6 +18,7 @@ void main() {
await game.addFromBlueprint(Boundaries());
game.camera.followVector2(Vector2.zero());
game.camera.zoom = 3.9;
await game.ready();
},
verify: (game, tester) async {
await expectLater(

@ -19,6 +19,7 @@ void main() {
await game.addFromBlueprint(DinoWalls());
game.camera.followVector2(Vector2.zero());
game.camera.zoom = 6.5;
await game.ready();
},
verify: (game, tester) async {
await expectLater(

@ -20,6 +20,7 @@ void main() {
setUp: (game, tester) async {
await game.addFromBlueprint(Slingshots());
game.camera.followVector2(Vector2.zero());
await game.ready();
},
verify: (game, tester) async {
await expectLater(

@ -17,6 +17,7 @@ void main() {
setUp: (game, tester) async {
await game.addFromBlueprint(SpaceshipRamp());
game.camera.followVector2(Vector2(-13, -50));
await game.ready();
},
verify: (game, tester) async {
await expectLater(

@ -47,6 +47,7 @@ void main() {
final position = Vector2(30, -30);
await game.addFromBlueprint(Spaceship(position: position));
game.camera.followVector2(position);
await game.ready();
},
verify: (game, tester) async {
await expectLater(

Loading…
Cancel
Save