onComplete callback for effects

This commit is contained in:
Lukas Klingsbo
2020-06-24 11:46:33 +02:00
parent 61bc9102f4
commit 9168ef3836
9 changed files with 23 additions and 6 deletions

View File

@ -3,6 +3,7 @@
## [next] ## [next]
- Adding BaseGame#markToRemove - Adding BaseGame#markToRemove
- Upgrade tiled and flutter_svg dependencies - Upgrade tiled and flutter_svg dependencies
- onComplete callback for effects
## 0.22.1 ## 0.22.1
- Fix Box2DComponent render priority - Fix Box2DComponent render priority

View File

@ -14,6 +14,8 @@ If `isInfinite` is false and `isAlternating` is true the effect will go from the
`isInfinite` and `isAlternating` are false by default and then the effect is just applied once, from the beginning of the curve until the end. `isInfinite` and `isAlternating` are false by default and then the effect is just applied once, from the beginning of the curve until the end.
When an effect is completed the callback `onComplete` will be called, it can be set as an optional argument to your effect.
## MoveEffect ## MoveEffect
Applied to `PositionComponent`s, this effect can be used to move the component to a new position, using an [animation curve](https://api.flutter.dev/flutter/animation/Curves-class.html). Applied to `PositionComponent`s, this effect can be used to move the component to a new position, using an [animation curve](https://api.flutter.dev/flutter/animation/Curves-class.html).

View File

@ -65,6 +65,7 @@ class MyGame extends BaseGame with TapDetector {
isInfinite: false, isInfinite: false,
isAlternating: true, isAlternating: true,
offset: 0.5, offset: 0.5,
onComplete: () => print("onComplete callback"),
); );
greenSquare.addEffect(combination); greenSquare.addEffect(combination);
} }

View File

@ -14,7 +14,8 @@ class CombinedEffect extends PositionComponentEffect {
this.offset = 0.0, this.offset = 0.0,
bool isInfinite = false, bool isInfinite = false,
bool isAlternating = false, bool isAlternating = false,
}) : super(isInfinite, isAlternating) { Function onComplete,
}) : super(isInfinite, isAlternating, onComplete: onComplete) {
final types = effects.map((e) => e.runtimeType); final types = effects.map((e) => e.runtimeType);
assert( assert(
types.toSet().length == types.length, types.toSet().length == types.length,

View File

@ -12,6 +12,7 @@ export './sequence_effect.dart';
abstract class PositionComponentEffect { abstract class PositionComponentEffect {
PositionComponent component; PositionComponent component;
Function() onComplete;
bool _isDisposed = false; bool _isDisposed = false;
bool get isDisposed => _isDisposed; bool get isDisposed => _isDisposed;
@ -37,7 +38,11 @@ abstract class PositionComponentEffect {
/// travel time /// travel time
double get totalTravelTime => travelTime * (isAlternating ? 2 : 1); double get totalTravelTime => travelTime * (isAlternating ? 2 : 1);
PositionComponentEffect(this._initialIsInfinite, this._initialIsAlternating) { PositionComponentEffect(
this._initialIsInfinite,
this._initialIsAlternating, {
this.onComplete,
}) {
isInfinite = _initialIsInfinite; isInfinite = _initialIsInfinite;
isAlternating = _initialIsAlternating; isAlternating = _initialIsAlternating;
} }
@ -53,6 +58,9 @@ abstract class PositionComponentEffect {
if (!hasFinished()) { if (!hasFinished()) {
currentTime += dt * curveDirection + driftTime * driftMultiplier; currentTime += dt * curveDirection + driftTime * driftMultiplier;
percentage = min(1.0, max(0.0, currentTime / travelTime)); percentage = min(1.0, max(0.0, currentTime / travelTime));
if (hasFinished()){
onComplete?.call();
}
} }
} }

View File

@ -28,7 +28,8 @@ class MoveEffect extends PositionComponentEffect {
this.curve, this.curve,
isInfinite = false, isInfinite = false,
isAlternating = false, isAlternating = false,
}) : super(isInfinite, isAlternating); Function onComplete
}) : super(isInfinite, isAlternating, onComplete: onComplete);
@override @override
void initialize(_comp) { void initialize(_comp) {

View File

@ -18,7 +18,8 @@ class RotateEffect extends PositionComponentEffect {
this.curve, this.curve,
isInfinite = false, isInfinite = false,
isAlternating = false, isAlternating = false,
}) : super(isInfinite, isAlternating); Function onComplete,
}) : super(isInfinite, isAlternating, onComplete: onComplete);
@override @override
void initialize(_comp) { void initialize(_comp) {

View File

@ -25,7 +25,8 @@ class ScaleEffect extends PositionComponentEffect {
this.curve, this.curve,
isInfinite = false, isInfinite = false,
isAlternating = false, isAlternating = false,
}) : super(isInfinite, isAlternating); Function onComplete,
}) : super(isInfinite, isAlternating, onComplete: onComplete);
@override @override
void initialize(_comp) { void initialize(_comp) {

View File

@ -14,7 +14,8 @@ class SequenceEffect extends PositionComponentEffect {
@required this.effects, @required this.effects,
isInfinite = false, isInfinite = false,
isAlternating = false, isAlternating = false,
}) : super(isInfinite, isAlternating) { Function onComplete,
}) : super(isInfinite, isAlternating, onComplete: onComplete) {
assert( assert(
effects.every((effect) => effect.component == null), effects.every((effect) => effect.component == null),
"No effects can be added to components from the start", "No effects can be added to components from the start",