refactor: moved Sprites to children param

pull/189/head
alestiago 3 years ago
parent 707e2e78b0
commit fa9d5b13d8

@ -52,12 +52,6 @@ class Ball<T extends Forge2DGame> extends BodyComponent<T>
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(
_spriteComponent..tint(baseColor.withOpacity(0.5)),
);
renderBody = false;
}
@override

@ -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);

@ -23,7 +23,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>[];
@ -59,13 +65,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 {
@ -88,7 +87,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>[];
@ -130,13 +135,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 {

@ -28,7 +28,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>[];
@ -96,14 +102,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 {
@ -124,7 +122,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>[];
@ -196,14 +199,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);
@ -110,10 +114,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);
@ -117,13 +122,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 {

@ -41,15 +41,15 @@ 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)],
);
final double _length;
final double _angle;
final String _spritePath;
List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
const circleRadius = 1.55;
@ -99,24 +99,22 @@ 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);
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;
}
}

@ -91,16 +91,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() {
@ -221,14 +225,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent
return body;
}
@override
Future<void> onLoad() async {
await super.onLoad();
renderBody = false;
await add(_SpaceshipRampForegroundRailingSpriteComponent());
}
}
class _SpaceshipRampForegroundRailingSpriteComponent extends SpriteComponent

Loading…
Cancel
Save