Update according to comments

This commit is contained in:
Lukas Klingsbo
2020-05-26 17:45:14 +02:00
parent 0693dd8d7d
commit 5d3939b906
7 changed files with 32 additions and 16 deletions

View File

@ -6,6 +6,7 @@
- Add linting to all the examples - Add linting to all the examples
- Run linting only on affected and changed examples - Run linting only on affected and changed examples
- Add SequenceEffect - Add SequenceEffect
- Fixed but with travelTime in RotateEffect
## 0.21.0 ## 0.21.0
- Adding AssetsCache.readBinaryFile - Adding AssetsCache.readBinaryFile

View File

@ -68,7 +68,9 @@ square.addEffect(RotateEffect(
## SequenceEffect ## 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. 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.

View File

@ -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.

View File

@ -68,7 +68,8 @@ class MyGame extends BaseGame with TapDetector {
final sequence = SequenceEffect( final sequence = SequenceEffect(
effects: [move1, scale, move2, rotate], effects: [move1, scale, move2, rotate],
isInfinite: true, isInfinite: true,
isAlternating: true); isAlternating: true,
);
greenSquare.addEffect(sequence); greenSquare.addEffect(sequence);
} }
} }

View File

@ -1,5 +1,5 @@
name: infinite_effects name: sequence_effect
description: Flame sample game showcasing infinite effects description: Flame sample game showcasing the sequence effect
version: 1.0.0+1 version: 1.0.0+1

View File

@ -20,6 +20,10 @@ abstract class PositionComponentEffect {
double driftTime = 0.0; double driftTime = 0.0;
int curveDirection = 1; 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); PositionComponentEffect(this.isInfinite, this.isAlternating);
void update(double dt) { void update(double dt) {

View File

@ -1,3 +1,4 @@
import 'package:flame/components/component.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import './effects.dart'; import './effects.dart';
@ -14,11 +15,14 @@ class SequenceEffect extends PositionComponentEffect {
@required this.effects, @required this.effects,
isInfinite = false, isInfinite = false,
isAlternating = 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 void _prepare(PositionComponent _comp) {
set component(_comp) {
super.component = _comp; super.component = _comp;
_currentIndex = 0;
final originalSize = _comp.toSize(); final originalSize = _comp.toSize();
final originalPosition = _comp.toPosition(); final originalPosition = _comp.toPosition();
Position currentSize = _comp.toSize(); Position currentSize = _comp.toSize();
@ -36,14 +40,19 @@ class SequenceEffect extends PositionComponentEffect {
}); });
travelTime = effects.fold( travelTime = effects.fold(
0, 0,
(time, effect) => (time, effect) => time + effect.totalTravelTime,
time + effect.travelTime * (effect.isAlternating ? 2 : 1)); );
component.setBySize(originalSize); component.setBySize(originalSize);
component.setByPosition(originalPosition); component.setByPosition(originalPosition);
currentEffect = effects.first; currentEffect = effects.first;
_currentWasAlternating = currentEffect.isAlternating; _currentWasAlternating = currentEffect.isAlternating;
} }
@override
set component(PositionComponent _comp) {
_prepare(_comp);
}
@override @override
void update(double dt) { void update(double dt) {
if (hasFinished()) { if (hasFinished()) {
@ -79,7 +88,6 @@ class SequenceEffect extends PositionComponentEffect {
@override @override
void reset() { void reset() {
super.reset(); super.reset();
_currentIndex = 0; _prepare(component);
component = component;
} }
} }