Working alternation of the effect sequence

This commit is contained in:
Lukas Klingsbo
2020-05-25 20:00:20 +02:00
parent 65b0193cb6
commit 932e974028
3 changed files with 4 additions and 25 deletions

View File

@ -20,8 +20,6 @@ void main() async {
class MyGame extends BaseGame with TapDetector { class MyGame extends BaseGame with TapDetector {
Square greenSquare; Square greenSquare;
Square redSquare;
Square orangeSquare;
MyGame() { MyGame() {
final green = Paint()..color = const Color(0xAA338833); final green = Paint()..color = const Color(0xAA338833);
@ -33,7 +31,6 @@ class MyGame extends BaseGame with TapDetector {
void onTapUp(TapUpDetails details) { void onTapUp(TapUpDetails details) {
final dx = details.localPosition.dx; final dx = details.localPosition.dx;
final dy = details.localPosition.dy; final dy = details.localPosition.dy;
greenSquare.clearEffects(); greenSquare.clearEffects();
final move1 = MoveEffect( final move1 = MoveEffect(
@ -62,7 +59,7 @@ class MyGame extends BaseGame with TapDetector {
final rotate = RotateEffect( final rotate = RotateEffect(
radians: (dx + dy) % pi, radians: (dx + dy) % pi,
speed: 2.0, // Radians per second speed: 2.0,
curve: Curves.decelerate, curve: Curves.decelerate,
isInfinite: false, isInfinite: false,
isAlternating: false, isAlternating: false,
@ -70,8 +67,6 @@ class MyGame extends BaseGame with TapDetector {
final sequence = SequenceEffect( final sequence = SequenceEffect(
effects: [move1, scale, move2, rotate], effects: [move1, scale, move2, rotate],
//effects: [rotate],
//effects: [scale],
isInfinite: true, isAlternating: true); isInfinite: true, isAlternating: true);
greenSquare.addEffect(sequence); greenSquare.addEffect(sequence);
} }

View File

@ -44,8 +44,6 @@ class MoveEffect extends PositionComponentEffect {
_yDirection = _direction(destination.y, component.y); _yDirection = _direction(destination.y, component.y);
final totalDistance = sqrt(pow(_xDistance, 2) + pow(_yDistance, 2)); final totalDistance = sqrt(pow(_xDistance, 2) + pow(_yDistance, 2));
print(totalDistance);
print(speed);
travelTime = totalDistance / speed; travelTime = totalDistance / speed;
} }

View File

@ -40,7 +40,6 @@ class SequenceEffect extends PositionComponentEffect {
}); });
travelTime = effects.fold(0, (time, effect) => time + effect.travelTime * travelTime = effects.fold(0, (time, effect) => time + effect.travelTime *
(effect.isAlternating ? 2 : 1)); (effect.isAlternating ? 2 : 1));
effects.forEach((element) {print("$element ${element.travelTime}");});
component.setBySize(originalSize); component.setBySize(originalSize);
component.setByPosition(originalPosition); component.setByPosition(originalPosition);
_currentEffect = effects.first; _currentEffect = effects.first;
@ -49,10 +48,10 @@ class SequenceEffect extends PositionComponentEffect {
@override @override
void update(double dt) { void update(double dt) {
super.update(dt);
if (hasFinished()) { if (hasFinished()) {
return; return;
} }
super.update(dt);
_currentEffect.update(dt + _driftModifier); _currentEffect.update(dt + _driftModifier);
_driftModifier = 0.0; _driftModifier = 0.0;
@ -61,20 +60,13 @@ class SequenceEffect extends PositionComponentEffect {
_currentEffect.isAlternating = _currentWasAlternating; _currentEffect.isAlternating = _currentWasAlternating;
_currentIndex++; _currentIndex++;
final iterationSize = isAlternating ? effects.length * 2 : effects.length; final iterationSize = isAlternating ? effects.length * 2 : effects.length;
if (isInfinite && if (_currentIndex != 0 && _currentIndex % iterationSize == 0) {
_currentIndex != 0 && isInfinite ? reset() : dispose();
_currentIndex % iterationSize == 0) {
reset();
return; return;
} }
final orderedEffects = final orderedEffects =
curveDirection.isNegative ? effects.reversed.toList() : effects; curveDirection.isNegative ? effects.reversed.toList() : effects;
_currentEffect = orderedEffects[_currentIndex % effects.length]; _currentEffect = orderedEffects[_currentIndex % effects.length];
print("Index: ${_currentIndex % effects.length}");
print(_currentIndex);
print(curveDirection);
print(_currentEffect);
print(orderedEffects);
_currentWasAlternating = _currentEffect.isAlternating; _currentWasAlternating = _currentEffect.isAlternating;
if (isAlternating && if (isAlternating &&
!_currentEffect.isAlternating && !_currentEffect.isAlternating &&
@ -92,10 +84,4 @@ class SequenceEffect extends PositionComponentEffect {
_currentIndex = 0; _currentIndex = 0;
component = component; component = component;
} }
@override
bool hasFinished() {
return super.hasFinished() &&
effects.every((effect) => effect.hasFinished());
}
} }