mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 20:36:31 +08:00
feat: Allow sequence effect to be extended (#2864)
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.
This commit is contained in:
@ -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
|
||||
@ -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) {}
|
||||
|
||||
Reference in New Issue
Block a user