refactor: improved spaceship ramp logic

feat/spaceship-ramp-logic
RuiAlonso 3 years ago
parent a64f028b13
commit f61a1764ac

@ -54,15 +54,16 @@ class SpaceshipRampController
int _hitsCounter = 0; int _hitsCounter = 0;
/// When a [Ball] shot the [SpaceshipRamp] it achieve improvements for the
/// current game, like multipliers or score.
void shot() { void shot() {
_hitsCounter++; _hitsCounter++;
component._spaceshipRamp.progress(); component._spaceshipRamp.progress();
// TODO(ruimiguel): Ramp shot
gameRef.read<GameBloc>().add(const Scored(points: 5000)); gameRef.read<GameBloc>().add(const Scored(points: 5000));
// TODO(ruimiguel): increase score multiplier at GameBloc. // TODO(ruimiguel): increase here multiplier at GameBloc.
if (_hitsCounter % _oneMillionPointsTarget == 0) { if (_hitsCounter % _oneMillionPointsTarget == 0) {
// TODO(ruimiguel): One million by bonus?? // TODO(ruimiguel): One million by bonus??
@ -78,8 +79,14 @@ class SpaceshipRampController
} }
} }
/// Used to know when a [Ball] gets into the [SpaceshipRamp] against every ball
/// that crosses the opening.
@visibleForTesting
enum SpaceshipRampSensorType { enum SpaceshipRampSensorType {
/// Sensor at the entrance of the opening.
door, door,
/// Sensor inside the [SpaceshipRamp].
inside, inside,
} }
@ -87,6 +94,7 @@ enum SpaceshipRampSensorType {
/// Small sensor body used to detect when a ball has entered the /// Small sensor body used to detect when a ball has entered the
/// [SpaceshipRamp]. /// [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting
class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered { class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_ramp_sensor} /// {@macro spaceship_ramp_sensor}
SpaceshipRampSensor({required this.type}) : super() { SpaceshipRampSensor({required this.type}) : super() {
@ -94,6 +102,7 @@ class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered {
renderBody = false; renderBody = false;
} }
/// Type for the sensor, to know if it's the one at the door or inside ramp.
final SpaceshipRampSensorType type; final SpaceshipRampSensorType type;
@override @override

@ -34,7 +34,7 @@ class SpaceshipRamp extends Blueprint {
_SpaceshipRampForegroundRailing(), _SpaceshipRampForegroundRailing(),
_SpaceshipRampBase()..initialPosition = Vector2(1.7, -20), _SpaceshipRampBase()..initialPosition = Vector2(1.7, -20),
_SpaceshipRampBackgroundRailingSpriteComponent(), _SpaceshipRampBackgroundRailingSpriteComponent(),
_SpaceshipRampArrowSpriteComponent(), SpaceshipRampArrowSpriteComponent(),
], ],
); );
@ -42,7 +42,7 @@ class SpaceshipRamp extends Blueprint {
/// ///
/// 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() => components void progress() => components
.whereType<_SpaceshipRampArrowSpriteComponent>() .whereType<SpaceshipRampArrowSpriteComponent>()
.first .first
.progress(); .progress();
} }
@ -203,11 +203,12 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
/// ///
/// Lights progressively whenever a [Ball] gets into [SpaceshipRamp]. /// Lights progressively whenever a [Ball] gets into [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class _SpaceshipRampArrowSpriteComponent @visibleForTesting
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),

@ -1,6 +1,7 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/components.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -39,7 +40,7 @@ void main() {
group('loads', () { group('loads', () {
flameTester.test( flameTester.test(
'a SpaceshipRamp', 'four SpriteComponent (two rails, main and opening)',
(game) async { (game) async {
final controlledSpaceshipRamp = ControlledSpaceshipRamp(); final controlledSpaceshipRamp = ControlledSpaceshipRamp();
await game.ensureAdd(controlledSpaceshipRamp); await game.ensureAdd(controlledSpaceshipRamp);
@ -47,7 +48,23 @@ void main() {
expect( expect(
controlledSpaceshipRamp controlledSpaceshipRamp
.descendants() .descendants()
.whereType<SpaceshipRamp>() .whereType<SpriteComponent>()
.length,
equals(4),
);
},
);
flameTester.test(
'a SpaceshipRampArrowSpriteComponent',
(game) async {
final controlledSpaceshipRamp = ControlledSpaceshipRamp();
await game.ensureAdd(controlledSpaceshipRamp);
expect(
controlledSpaceshipRamp
.descendants()
.whereType<SpaceshipRampArrowSpriteComponent>()
.length, .length,
equals(1), equals(1),
); );

Loading…
Cancel
Save