Skip to content

Commit

Permalink
feat: Allow sequence effect to be extended (#2864)
Browse files Browse the repository at this point in the history
With previous implementation of the `SequenceEffect` didn't allowed
developers to extend it and create custom sequence effects. This PR
slightly refactors the class so it can now be extended.
  • Loading branch information
erickzanardo committed Nov 23, 2023
1 parent 082743d commit ee11aae
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions packages/flame/lib/src/effects/sequence_effect.dart
Expand Up @@ -3,6 +3,25 @@ import 'package:flame/src/effects/controllers/infinite_effect_controller.dart';
import 'package:flame/src/effects/controllers/repeated_effect_controller.dart';
import 'package:flame/src/effects/effect.dart';

EffectController _createController({
required List<Effect> effects,
required bool alternate,
required bool infinite,
required int repeatCount,
}) {
EffectController ec = _SequenceEffectEffectController(
effects,
alternate: alternate,
);
if (infinite) {
ec = InfiniteEffectController(ec);
} else if (repeatCount > 1) {
ec = RepeatedEffectController(ec, repeatCount);
}
effects.forEach((e) => e.removeOnFinish = false);
return ec;
}

/// Run multiple effects in a sequence, one after another.
///
/// The provided effects will be added as child components; however the custom
Expand All @@ -27,35 +46,28 @@ import 'package:flame/src/effects/effect.dart';
/// effect depends on the timings of individual effects, and cannot be
/// represented as a regular effect controller.
class SequenceEffect extends Effect {
factory SequenceEffect(
SequenceEffect(
List<Effect> effects, {
bool alternate = false,
bool infinite = false,
int repeatCount = 1,
void Function()? onComplete,
}) {
assert(effects.isNotEmpty, 'The list of effects cannot be empty');
assert(
!(infinite && repeatCount != 1),
'Parameters infinite and repeatCount cannot be specified simultaneously',
);
EffectController ec = _SequenceEffectEffectController(
effects,
alternate: alternate,
);
if (infinite) {
ec = InfiniteEffectController(ec);
} else if (repeatCount > 1) {
ec = RepeatedEffectController(ec, repeatCount);
}
effects.forEach((e) => e.removeOnFinish = false);
return SequenceEffect._(ec, onComplete: onComplete)..addAll(effects);
}

SequenceEffect._(
super.ec, {
super.onComplete,
});
}) : assert(effects.isNotEmpty, 'The list of effects cannot be empty'),
assert(
!(infinite && repeatCount != 1),
'Parameters infinite and repeatCount cannot be specified '
'simultaneously',
),
super(
_createController(
effects: effects,
alternate: alternate,
infinite: infinite,
repeatCount: repeatCount,
),
) {
addAll(effects);
}

@override
void apply(double progress) {}
Expand Down

0 comments on commit ee11aae

Please sign in to comment.