Do not reset state if not root effect

This commit is contained in:
Lukas Klingsbo
2020-11-07 13:17:07 +01:00
parent 39c2663798
commit 0d7a2a5c12
5 changed files with 23 additions and 32 deletions

View File

@ -85,6 +85,7 @@ abstract class ComponentEffect<T extends Component> {
bool isMax() => percentage == null ? false : percentage == 1.0;
bool isMin() => percentage == null ? false : percentage == 0.0;
bool isRootEffect() => component?.effects?.contains(this) ?? false;
void reset() {
_isDisposed = false;
@ -158,25 +159,19 @@ abstract class PositionComponentEffect
@override
void setComponentToOriginalState() {
if(isRootEffect()) {
component?.position?.setFrom(originalPosition);
component?.angle = originalAngle;
component?.size?.setFrom(endSize);
}
}
@override
void setComponentToEndState() {
// TODO: Remove ifs
if (originalPosition != endPosition) {
component.position.setFrom(endPosition);
print("Set position end state");
}
if (originalAngle != endAngle) {
component.angle = endAngle;
print("Set angle end state");
}
if (originalSize != endSize) {
print("Set size end state $originalSize $endSize ${component.size}");
component.size.setFrom(endSize);
if(isRootEffect()) {
component?.position?.setFrom(endPosition);
component?.angle = endAngle;
component?.size?.setFrom(endSize);
}
}
}

View File

@ -42,9 +42,11 @@ class SequenceEffect extends PositionComponentEffect {
endAngle = effect.endAngle;
endSize = effect.endSize;
});
// Add all the effects iteration time since they can alternate within the
// sequence effect
peakTime = effects.fold(
0,
(time, effect) => time + effect.peakTime,
(time, effect) => time + effect.iterationTime,
);
if (isAlternating) {
endPosition = originalPosition;

View File

@ -29,6 +29,7 @@ void main() {
final ScaleEffect scale = ScaleEffect(
size: argumentSize,
duration: randomDuration(),
isAlternating: true,
);
final RotateEffect rotate = RotateEffect(
angle: argumentAngle,

View File

@ -41,9 +41,6 @@ void effectTest(
game.update(stepDelta);
timeLeft -= stepDelta;
}
print("Current time: ${effect.currentTime}");
print("Current peak: ${effect.peakTime}");
print("Current perc: ${effect.percentage}");
if (!shouldComplete) {
const double floatRange = 0.01;
@ -70,16 +67,12 @@ void effectTest(
);
} else {
// To account for float number operations making effects not finish
const double epsilon = 0.000001;
const double epsilon = 0.001;
if (effect.percentage < epsilon) {
print("Last update min");
game.update(effect.currentTime);
} else if (1.0 - effect.percentage < epsilon) {
print("Last update max");
game.update(effect.peakTime - effect.currentTime);
}
print("Current perc after: ${effect.percentage}");
print("hasCompleted: ${effect.hasCompleted()}");
expect(
component.position,

View File

@ -23,7 +23,7 @@ void main() {
);
}
SequenceEffect effect(bool isInfinite, bool isAlternating) {
SequenceEffect effect({bool isInfinite = false, bool isAlternating = false}) {
final MoveEffect move = MoveEffect(path: path, duration: randomDuration());
final ScaleEffect scale = ScaleEffect(
size: argumentSize,
@ -44,7 +44,7 @@ void main() {
effectTest(
tester,
component(),
effect(false, false),
effect(),
expectedPosition: path.last,
expectedAngle: argumentAngle,
expectedSize: argumentSize,
@ -57,7 +57,7 @@ void main() {
effectTest(
tester,
component(),
effect(false, false),
effect(),
expectedPosition: path.last,
expectedAngle: argumentAngle,
expectedSize: argumentSize,
@ -73,7 +73,7 @@ void main() {
effectTest(
tester,
positionComponent,
effect(false, true),
effect(isAlternating: true),
expectedPosition: positionComponent.position.clone(),
expectedAngle: positionComponent.angle,
expectedSize: positionComponent.size.clone(),
@ -88,7 +88,7 @@ void main() {
effectTest(
tester,
positionComponent,
effect(true, true),
effect(isInfinite: true, isAlternating: true),
expectedPosition: positionComponent.position.clone(),
expectedAngle: positionComponent.angle,
expectedSize: positionComponent.size.clone(),
@ -103,7 +103,7 @@ void main() {
effectTest(
tester,
positionComponent,
effect(false, true),
effect(isAlternating: true),
expectedPosition: path.last,
expectedAngle: argumentAngle,
expectedSize: argumentSize,
@ -117,7 +117,7 @@ void main() {
effectTest(
tester,
positionComponent,
effect(true, false),
effect(isInfinite: true),
expectedPosition: path.last,
expectedAngle: argumentAngle,
expectedSize: argumentSize,