diff --git a/CHANGELOG.md b/CHANGELOG.md index 4618d07e1..60361a623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add linting to all the examples - Run linting only on affected and changed examples - Add SequenceEffect + - Fixed but with travelTime in RotateEffect ## 0.21.0 - Adding AssetsCache.readBinaryFile diff --git a/doc/effects.md b/doc/effects.md index f69d9be39..765418f9f 100644 --- a/doc/effects.md +++ b/doc/effects.md @@ -68,7 +68,9 @@ square.addEffect(RotateEffect( ## SequenceEffect -This effect is a combination of other effects. You provide it with a list of your predefined effects, that are not added to a component. +This effect is a combination of other effects. You provide it with a list of your predefined effects. + +The effects in the list should only be passed to the SequenceEffect, never added to a PositionComponent with `addEffect`. Note that no effects added to the sequence can have their `isInfinite` property set to `true`, because then naturally the sequence will get stuck once it gets to that effect. diff --git a/doc/examples/effects/sequence_effect/README.md b/doc/examples/effects/sequence_effect/README.md index 4b404b376..dbb5fb788 100644 --- a/doc/examples/effects/sequence_effect/README.md +++ b/doc/examples/effects/sequence_effect/README.md @@ -1,3 +1,3 @@ -# Infinite effects +# Sequence effect -A Flame game showcasing how to create infinite alternating effects. +A Flame game showcasing how to put effects in a sequence. diff --git a/doc/examples/effects/sequence_effect/lib/main.dart b/doc/examples/effects/sequence_effect/lib/main.dart index 747f0f42c..7bc9bd23d 100644 --- a/doc/examples/effects/sequence_effect/lib/main.dart +++ b/doc/examples/effects/sequence_effect/lib/main.dart @@ -66,9 +66,10 @@ class MyGame extends BaseGame with TapDetector { ); final sequence = SequenceEffect( - effects: [move1, scale, move2, rotate], - isInfinite: true, - isAlternating: true); + effects: [move1, scale, move2, rotate], + isInfinite: true, + isAlternating: true, + ); greenSquare.addEffect(sequence); } } diff --git a/doc/examples/effects/sequence_effect/pubspec.yaml b/doc/examples/effects/sequence_effect/pubspec.yaml index 59cc1f3bb..6d5931ecf 100644 --- a/doc/examples/effects/sequence_effect/pubspec.yaml +++ b/doc/examples/effects/sequence_effect/pubspec.yaml @@ -1,5 +1,5 @@ -name: infinite_effects -description: Flame sample game showcasing infinite effects +name: sequence_effect +description: Flame sample game showcasing the sequence effect version: 1.0.0+1 diff --git a/lib/effects/effects.dart b/lib/effects/effects.dart index 4ddcf125e..8e1735370 100644 --- a/lib/effects/effects.dart +++ b/lib/effects/effects.dart @@ -20,6 +20,10 @@ abstract class PositionComponentEffect { double driftTime = 0.0; int curveDirection = 1; + /// If the effect is alternating the travel time is double the normal + /// travel time + double get totalTravelTime => travelTime * (isAlternating ? 2 : 1); + PositionComponentEffect(this.isInfinite, this.isAlternating); void update(double dt) { diff --git a/lib/effects/sequence_effect.dart b/lib/effects/sequence_effect.dart index 9456aa159..2585481a1 100644 --- a/lib/effects/sequence_effect.dart +++ b/lib/effects/sequence_effect.dart @@ -1,3 +1,4 @@ +import 'package:flame/components/component.dart'; import 'package:meta/meta.dart'; import './effects.dart'; @@ -14,11 +15,14 @@ class SequenceEffect extends PositionComponentEffect { @required this.effects, isInfinite = false, isAlternating = false, - }) : super(isInfinite, isAlternating); + }) : super(isInfinite, isAlternating) { + /// All effects need to be finite, otherwise the sequence will get stuck + assert(effects.every((effect) => !effect.isInfinite)); + } - @override - set component(_comp) { + void _prepare(PositionComponent _comp) { super.component = _comp; + _currentIndex = 0; final originalSize = _comp.toSize(); final originalPosition = _comp.toPosition(); Position currentSize = _comp.toSize(); @@ -35,15 +39,20 @@ class SequenceEffect extends PositionComponentEffect { } }); travelTime = effects.fold( - 0, - (time, effect) => - time + effect.travelTime * (effect.isAlternating ? 2 : 1)); + 0, + (time, effect) => time + effect.totalTravelTime, + ); component.setBySize(originalSize); component.setByPosition(originalPosition); currentEffect = effects.first; _currentWasAlternating = currentEffect.isAlternating; } + @override + set component(PositionComponent _comp) { + _prepare(_comp); + } + @override void update(double dt) { if (hasFinished()) { @@ -79,7 +88,6 @@ class SequenceEffect extends PositionComponentEffect { @override void reset() { super.reset(); - _currentIndex = 0; - component = component; + _prepare(component); } }