refactor: simplified spaceship_ramp

pull/212/head
alestiago 3 years ago
parent c6ec8197e6
commit 6c8a5a4334

@ -34,14 +34,17 @@ class SpaceshipRamp extends Blueprint {
_SpaceshipRampForegroundRailing(), _SpaceshipRampForegroundRailing(),
_SpaceshipRampBase()..initialPosition = Vector2(1.7, -20), _SpaceshipRampBase()..initialPosition = Vector2(1.7, -20),
_SpaceshipRampBackgroundRailingSpriteComponent(), _SpaceshipRampBackgroundRailingSpriteComponent(),
SpaceshipRampArrowSpriteComponent(), _SpaceshipRampArrowSpriteComponent(),
], ],
); );
/// Forwards the sprite to the next [SpaceshipRampArrowSpriteState]. /// Forwards the sprite to the next [SpaceshipRampArrowSpriteState].
/// ///
/// If the current state is the last one it cycles back to the initial state. /// If the current state is the last one it cycles back to the initial state.
void progress() => children.firstChild<SpaceshipRampArrowSpriteComponent>()?.progress(); void progress() => components
.whereType<_SpaceshipRampArrowSpriteComponent>()
.first
.progress();
} }
/// Indicates the state of the arrow on the [SpaceshipRamp]. /// Indicates the state of the arrow on the [SpaceshipRamp].
@ -204,13 +207,13 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
/// {@template spaceship_ramp_arrow_sprite_component} /// {@template spaceship_ramp_arrow_sprite_component}
/// An arrow inside [SpaceshipRamp]. /// An arrow inside [SpaceshipRamp].
/// ///
/// Lights up a each dash whenever a [Ball] gets into [SpaceshipRamp]. /// Lights progressively whenever a [Ball] gets into [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class SpaceshipRampArrowSpriteComponent class _SpaceshipRampArrowSpriteComponent
extends SpriteGroupComponent<SpaceshipRampArrowSpriteState> extends SpriteGroupComponent<SpaceshipRampArrowSpriteState>
with HasGameRef { with HasGameRef {
/// {@macro spaceship_ramp_arrow_sprite_component} /// {@macro spaceship_ramp_arrow_sprite_component}
SpaceshipRampArrowSpriteComponent() _SpaceshipRampArrowSpriteComponent()
: super( : super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(-3.9, -56.5), position: Vector2(-3.9, -56.5),
@ -238,6 +241,8 @@ class SpaceshipRampArrowSpriteComponent
class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent
with HasGameRef { with HasGameRef {
_SpaceshipRampBoardOpeningSpriteComponent() : super(anchor: Anchor.center);
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -248,7 +253,6 @@ class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent
); );
this.sprite = sprite; this.sprite = sprite;
size = sprite.originalSize / 10; size = sprite.originalSize / 10;
anchor = Anchor.center;
} }
} }
@ -264,8 +268,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent
} }
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[];
final innerLeftCurveShape = BezierCurveShape( final innerLeftCurveShape = BezierCurveShape(
controlPoints: [ controlPoints: [
Vector2(-24.5, -38), Vector2(-24.5, -38),
@ -273,10 +275,6 @@ class _SpaceshipRampForegroundRailing extends BodyComponent
Vector2(-13.8, -64.5), Vector2(-13.8, -64.5),
], ],
); );
final innerLeftCurveFixtureDef = FixtureDef(innerLeftCurveShape);
fixturesDef.add(innerLeftCurveFixtureDef);
final innerRightCurveShape = BezierCurveShape( final innerRightCurveShape = BezierCurveShape(
controlPoints: [ controlPoints: [
innerLeftCurveShape.vertices.last, innerLeftCurveShape.vertices.last,
@ -284,28 +282,22 @@ class _SpaceshipRampForegroundRailing extends BodyComponent
Vector2(0, -44.5), Vector2(0, -44.5),
], ],
); );
final innerRightCurveFixtureDef = FixtureDef(innerRightCurveShape);
fixturesDef.add(innerRightCurveFixtureDef);
final boardOpeningEdgeShape = EdgeShape() final boardOpeningEdgeShape = EdgeShape()
..set( ..set(
innerRightCurveShape.vertices.last, innerRightCurveShape.vertices.last,
Vector2(-0.85, -40.8), Vector2(-0.85, -40.8),
); );
final boardOpeningEdgeShapeFixtureDef = FixtureDef(boardOpeningEdgeShape);
fixturesDef.add(boardOpeningEdgeShapeFixtureDef);
return fixturesDef; return [
FixtureDef(innerLeftCurveShape),
FixtureDef(innerRightCurveShape),
FixtureDef(boardOpeningEdgeShape),
];
} }
@override @override
Body createBody() { Body createBody() {
final bodyDef = BodyDef( final bodyDef = BodyDef(position: initialPosition);
position: initialPosition,
userData: this,
);
final body = world.createBody(bodyDef); final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture); _createFixtureDefs().forEach(body.createFixture);
@ -354,10 +346,7 @@ class _SpaceshipRampBase extends BodyComponent with InitialPosition, Layered {
], ],
); );
final fixtureDef = FixtureDef(baseShape); final fixtureDef = FixtureDef(baseShape);
final bodyDef = BodyDef( final bodyDef = BodyDef(position: initialPosition);
position: initialPosition,
userData: this,
);
return world.createBody(bodyDef)..createFixture(fixtureDef); return world.createBody(bodyDef)..createFixture(fixtureDef);
} }

@ -1,7 +1,6 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
@ -46,10 +45,9 @@ void main() {
final spaceshipRamp = SpaceshipRamp(); final spaceshipRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipRamp); await game.addFromBlueprint(spaceshipRamp);
await game.ready(); await game.ready();
await tester.pump();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.inactive, SpaceshipRampArrowSpriteState.inactive,
); );
@ -70,11 +68,9 @@ void main() {
final spaceshipRamp = SpaceshipRamp(); final spaceshipRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipRamp); await game.addFromBlueprint(spaceshipRamp);
await game.ready(); await game.ready();
spaceshipRamp.progress();
await tester.pump();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.active1, SpaceshipRampArrowSpriteState.active1,
); );
@ -94,14 +90,13 @@ void main() {
await game.images.loadAll(assets); await game.images.loadAll(assets);
final spaceshipRamp = SpaceshipRamp(); final spaceshipRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipRamp); await game.addFromBlueprint(spaceshipRamp);
await game.ready();
spaceshipRamp spaceshipRamp
..progress() ..progress()
..progress(); ..progress();
await tester.pump(); await game.ready();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.active2, SpaceshipRampArrowSpriteState.active2,
); );
@ -126,10 +121,10 @@ void main() {
..progress() ..progress()
..progress() ..progress()
..progress(); ..progress();
await tester.pump(); await game.ready();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.active3, SpaceshipRampArrowSpriteState.active3,
); );
@ -149,16 +144,15 @@ void main() {
await game.images.loadAll(assets); await game.images.loadAll(assets);
final spaceshipRamp = SpaceshipRamp(); final spaceshipRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipRamp); await game.addFromBlueprint(spaceshipRamp);
await game.ready();
spaceshipRamp spaceshipRamp
..progress() ..progress()
..progress() ..progress()
..progress() ..progress()
..progress(); ..progress();
await tester.pump(); await game.ready();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.active4, SpaceshipRampArrowSpriteState.active4,
); );
@ -178,17 +172,16 @@ void main() {
await game.images.loadAll(assets); await game.images.loadAll(assets);
final spaceshipRamp = SpaceshipRamp(); final spaceshipRamp = SpaceshipRamp();
await game.addFromBlueprint(spaceshipRamp); await game.addFromBlueprint(spaceshipRamp);
await game.ready();
spaceshipRamp spaceshipRamp
..progress() ..progress()
..progress() ..progress()
..progress() ..progress()
..progress() ..progress()
..progress(); ..progress();
await tester.pump(); await game.ready();
expect( expect(
spaceshipRamp.spaceshipRampArrow.current, spaceshipRamp.firstChild<SpriteGroupComponent>()!.current,
SpaceshipRampArrowSpriteState.active5, SpaceshipRampArrowSpriteState.active5,
); );

Loading…
Cancel
Save