From 3ac0df99b25e4655e511fe0a2db5f5ee1f610686 Mon Sep 17 00:00:00 2001 From: alestiago Date: Wed, 6 Apr 2022 11:27:22 +0100 Subject: [PATCH] refactor: remove _loadSprites --- .../lib/src/components/boundaries.dart | 42 +++++----- .../lib/src/components/spaceship_ramp.dart | 78 ++++++++++--------- 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/packages/pinball_components/lib/src/components/boundaries.dart b/packages/pinball_components/lib/src/components/boundaries.dart index 79b3f909..b313fd63 100644 --- a/packages/pinball_components/lib/src/components/boundaries.dart +++ b/packages/pinball_components/lib/src/components/boundaries.dart @@ -63,23 +63,22 @@ class _BottomBoundary extends BodyComponent with InitialPosition { @override Future onLoad() async { await super.onLoad(); - await _loadSprite(); renderBody = false; + await add(_BottomBoundarySprite()); } +} - Future _loadSprite() async { +class _BottomBoundarySprite extends SpriteComponent with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); final sprite = await gameRef.loadSprite( Assets.images.boundary.bottom.keyName, ); - - await add( - SpriteComponent( - sprite: sprite, - size: sprite.originalSize / 10, - anchor: Anchor.center, - position: Vector2(-5.4, 57.4), - ), - ); + this.sprite = sprite; + size = sprite.originalSize / 10; + anchor = Anchor.center; + position = Vector2(-5.4, 57.4); } } @@ -135,22 +134,21 @@ class _OuterBoundary extends BodyComponent with InitialPosition { @override Future onLoad() async { await super.onLoad(); - await _loadSprite(); renderBody = false; + await add(_OuterBoundarySprite()); } +} - Future _loadSprite() async { +class _OuterBoundarySprite extends SpriteComponent with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); final sprite = await gameRef.loadSprite( Assets.images.boundary.outer.keyName, ); - - await add( - SpriteComponent( - sprite: sprite, - size: sprite.originalSize / 10, - anchor: Anchor.center, - position: Vector2(-0.2, -1.4), - ), - ); + this.sprite = sprite; + size = sprite.originalSize / 10; + anchor = Anchor.center; + position = Vector2(-0.2, -1.4); } } diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp.dart index 6fb6cda0..8ff2a3e3 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp.dart @@ -96,8 +96,6 @@ class _SpaceshipRampBackground extends BodyComponent @override Body createBody() { - renderBody = false; - final bodyDef = BodyDef() ..userData = this ..position = initialPosition; @@ -111,35 +109,40 @@ class _SpaceshipRampBackground extends BodyComponent @override Future onLoad() async { await super.onLoad(); - await _loadSprites(); - } - - Future _loadSprites() async { - final spriteRamp = await gameRef.loadSprite( - Assets.images.spaceship.ramp.main.keyName, - ); + renderBody = false; - final spriteRampComponent = SpriteComponent( - sprite: spriteRamp, - size: Vector2(38.1, 33.8), - anchor: Anchor.center, - position: Vector2(-12.2, -53.5), - ); + await add(_SpaceshipRampBackgroundRailingSprite()); + await add(_SpaceshipRampBackgroundRampSprite()); + } +} - final spriteRailingBg = await gameRef.loadSprite( +class _SpaceshipRampBackgroundRailingSprite extends SpriteComponent + with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); + final sprite = await gameRef.loadSprite( Assets.images.spaceship.ramp.railingBackground.keyName, ); - final spriteRailingBgComponent = SpriteComponent( - sprite: spriteRailingBg, - size: Vector2(38.3, 35.1), - anchor: Anchor.center, - position: spriteRampComponent.position + Vector2(0, -1), - ); + this.sprite = sprite; + size = Vector2(38.3, 35.1); + anchor = Anchor.center; + position = Vector2(-12.2, -54.5); + } +} - await addAll([ - spriteRailingBgComponent, - spriteRampComponent, - ]); +class _SpaceshipRampBackgroundRampSprite extends SpriteComponent + with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); + final sprite = await gameRef.loadSprite( + Assets.images.spaceship.ramp.main.keyName, + ); + this.sprite = sprite; + size = Vector2(38.3, 35.1); + anchor = Anchor.center; + position = Vector2(-12.2, -543.5); } } @@ -196,21 +199,22 @@ class _SpaceshipRampForegroundRailing extends BodyComponent @override Future onLoad() async { await super.onLoad(); - await _loadSprites(); + await add(_SpaceshipRampForegroundRalingSprite()); } +} - Future _loadSprites() async { - final spriteRailingFg = await gameRef.loadSprite( +class _SpaceshipRampForegroundRalingSprite extends SpriteComponent + with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); + final sprite = await gameRef.loadSprite( Assets.images.spaceship.ramp.railingForeground.keyName, ); - final spriteRailingFgComponent = SpriteComponent( - sprite: spriteRailingFg, - size: Vector2(26.1, 28.3), - anchor: Anchor.center, - position: Vector2(-12.2, -52.5), - ); - - await add(spriteRailingFgComponent); + this.sprite = sprite; + size = Vector2(26.1, 28.3); + anchor = Anchor.center; + position = Vector2(-12.2, -52.5); } }