From f1e4330fac35aa2bac4050567261a72646a34496 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 7 Apr 2022 11:02:42 +0100 Subject: [PATCH] refactor: defined DinoWalls sprites --- .../lib/src/components/dino_walls.dart | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/pinball_components/lib/src/components/dino_walls.dart b/packages/pinball_components/lib/src/components/dino_walls.dart index daf83850..1b2b42b1 100644 --- a/packages/pinball_components/lib/src/components/dino_walls.dart +++ b/packages/pinball_components/lib/src/components/dino_walls.dart @@ -81,13 +81,10 @@ class _DinoTopWall extends BodyComponent with InitialPosition { @override Body createBody() { - renderBody = false; - final bodyDef = BodyDef() ..userData = this ..position = initialPosition ..type = BodyType.static; - final body = world.createBody(bodyDef); _createFixtureDefs().forEach( (fixture) => body.createFixture( @@ -103,21 +100,22 @@ class _DinoTopWall extends BodyComponent with InitialPosition { @override Future onLoad() async { await super.onLoad(); - await _loadSprite(); + renderBody = false; + + await add(_DinoTopWallSprite()); } +} - Future _loadSprite() async { +class _DinoTopWallSprite extends SpriteComponent with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); final sprite = await gameRef.loadSprite( Assets.images.dino.dinoLandTop.keyName, ); - final spriteComponent = SpriteComponent( - sprite: sprite, - size: Vector2(10.6, 27.7), - anchor: Anchor.center, - position: Vector2(27, -28.2), - ); - - await add(spriteComponent); + this.sprite = sprite; + size = sprite.originalSize / 10; + position = Vector2(27, -28.2); } } @@ -182,8 +180,6 @@ class _DinoBottomWall extends BodyComponent with InitialPosition { @override Body createBody() { - renderBody = false; - final bodyDef = BodyDef() ..userData = this ..position = initialPosition @@ -204,19 +200,21 @@ class _DinoBottomWall extends BodyComponent with InitialPosition { @override Future onLoad() async { await super.onLoad(); - await _loadSprite(); + renderBody = false; + + await add(_DinoBottomWallSprite()); } +} - Future _loadSprite() async { +class _DinoBottomWallSprite extends SpriteComponent with HasGameRef { + @override + Future onLoad() async { + await super.onLoad(); final sprite = await gameRef.loadSprite( Assets.images.dino.dinoLandBottom.keyName, ); - final spriteComponent = SpriteComponent( - sprite: sprite, - size: Vector2(15.6, 54.8), - anchor: Anchor.center, - )..position = Vector2(31.7, 18); - - await add(spriteComponent); + this.sprite = sprite; + size = sprite.originalSize / 10; + position = Vector2(31.7, 18); } }