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
- Run linting only on affected and changed examples
- Add SequenceEffect
- Fixed but with travelTime in RotateEffect
## 0.21.0
- Adding AssetsCache.readBinaryFile

View File

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

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(
effects: [move1, scale, move2, rotate],
isInfinite: true,
isAlternating: true);
isAlternating: true,
);
greenSquare.addEffect(sequence);
}
}

View File

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

View File

@ -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) {

View File

@ -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();
@ -36,14 +40,19 @@ class SequenceEffect extends PositionComponentEffect {
});
travelTime = effects.fold(
0,
(time, effect) =>
time + effect.travelTime * (effect.isAlternating ? 2 : 1));
(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);
}
}