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;
/// When a [Ball] shot the [SpaceshipRamp] it achieve improvements for the
/// current game, like multipliers or score.
void shot() {
_hitsCounter++;
component._spaceshipRamp.progress();
// TODO(ruimiguel): Ramp shot
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) {
// 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 {
/// Sensor at the entrance of the opening.
door,
/// Sensor inside the [SpaceshipRamp].
inside,
}
@ -87,6 +94,7 @@ enum SpaceshipRampSensorType {
/// Small sensor body used to detect when a ball has entered the
/// [SpaceshipRamp].
/// {@endtemplate}
@visibleForTesting
class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered {
/// {@macro spaceship_ramp_sensor}
SpaceshipRampSensor({required this.type}) : super() {
@ -94,6 +102,7 @@ class SpaceshipRampSensor extends BodyComponent with InitialPosition, Layered {
renderBody = false;
}
/// Type for the sensor, to know if it's the one at the door or inside ramp.
final SpaceshipRampSensorType type;
@override

@ -34,7 +34,7 @@ class SpaceshipRamp extends Blueprint {
_SpaceshipRampForegroundRailing(),
_SpaceshipRampBase()..initialPosition = Vector2(1.7, -20),
_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.
void progress() => components
.whereType<_SpaceshipRampArrowSpriteComponent>()
.whereType<SpaceshipRampArrowSpriteComponent>()
.first
.progress();
}
@ -203,11 +203,12 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
///
/// Lights progressively whenever a [Ball] gets into [SpaceshipRamp].
/// {@endtemplate}
class _SpaceshipRampArrowSpriteComponent
@visibleForTesting
class SpaceshipRampArrowSpriteComponent
extends SpriteGroupComponent<SpaceshipRampArrowSpriteState>
with HasGameRef {
/// {@macro spaceship_ramp_arrow_sprite_component}
_SpaceshipRampArrowSpriteComponent()
SpaceshipRampArrowSpriteComponent()
: super(
anchor: Anchor.center,
position: Vector2(-3.9, -56.5),

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

Loading…
Cancel
Save