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} /// {@macro ball}
Ball({ Ball({
required this.baseColor, required this.baseColor,
}) { }) : super(
children: [
_BallSpriteComponent()..tint(baseColor.withOpacity(0.5)),
],
) {
// TODO(ruimiguel): while developing Ball can be launched by clicking mouse, // 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 // 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. // 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 // 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. // bumper, it will need to explicit change layer to Layer.board then.
layer = Layer.board; layer = Layer.board;
renderBody = false;
} }
/// Render priority for the [Ball] while it's on the board. /// 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; double _boostTimer = 0;
static const _boostDuration = 2.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 @override
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = size.x / 2; final shape = CircleShape()..radius = size.x / 2;
@ -132,7 +123,10 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
((standardizedYPosition / boardHeight) * (1 - maxShrinkValue)); ((standardizedYPosition / boardHeight) * (1 - maxShrinkValue));
body.fixtures.first.shape.radius = (size.x / 2) * scaleFactor; 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() { void _setPositionalGravity() {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save