Always set end state

This commit is contained in:
Lukas Klingsbo
2020-11-01 17:04:49 +01:00
parent 51d0c6e77d
commit 09adb4972c
8 changed files with 96 additions and 57 deletions

View File

@ -67,6 +67,7 @@ abstract class ComponentEffect<T extends Component> {
currentTime = currentTime.clamp(0.0, peakTime).toDouble(); currentTime = currentTime.clamp(0.0, peakTime).toDouble();
if (hasFinished()) { if (hasFinished()) {
onComplete?.call(); onComplete?.call();
_setComponentToEndState();
} }
} }
} }
@ -108,6 +109,8 @@ abstract class ComponentEffect<T extends Component> {
driftTime = 0; driftTime = 0;
} }
} }
void _setComponentToEndState();
} }
abstract class PositionComponentEffect abstract class PositionComponentEffect
@ -141,16 +144,23 @@ abstract class PositionComponentEffect
void initialize(PositionComponent component) { void initialize(PositionComponent component) {
super.initialize(component); super.initialize(component);
this.component = component; this.component = component;
originalPosition = component.position; originalPosition = component.position.clone();
originalAngle = component.angle; originalAngle = component.angle;
originalSize = component.size; originalSize = component.size.clone();
/// If these aren't modified by the extending effect it is assumed that the /// If these aren't modified by the extending effect it is assumed that the
/// effect didn't bring the component to another state than the one it /// effect didn't bring the component to another state than the one it
/// started in /// started in
endPosition = component.position; endPosition = component.position.clone();
endAngle = component.angle; endAngle = component.angle;
endSize = component.size; endSize = component.size.clone();
}
@override
void _setComponentToEndState() {
component.position.setFrom(endPosition);
component.angle = endAngle;
component.size.setFrom(endSize);
} }
} }

View File

@ -66,11 +66,7 @@ class MoveEffect extends SimplePositionComponentEffect {
} else { } else {
_movePath = path; _movePath = path;
} }
if (!isAlternating) { endPosition = isAlternating ? _startPosition : _movePath.last;
endPosition = _movePath.last;
} else {
endPosition = _startPosition;
}
double pathLength = 0; double pathLength = 0;
Vector2 lastPosition = _startPosition; Vector2 lastPosition = _startPosition;
@ -113,9 +109,11 @@ class MoveEffect extends SimplePositionComponentEffect {
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
if (!hasFinished()) {
_currentSubPath ??= _percentagePath.first; _currentSubPath ??= _percentagePath.first;
if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress || if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress ||
curveDirection.isNegative && _currentSubPath.startAt > curveProgress) { curveDirection.isNegative &&
_currentSubPath.startAt > curveProgress) {
_currentSubPath = _currentSubPath =
_percentagePath.firstWhere((v) => v.endAt >= curveProgress); _percentagePath.firstWhere((v) => v.endAt >= curveProgress);
} }
@ -126,3 +124,4 @@ class MoveEffect extends SimplePositionComponentEffect {
((_currentSubPath.v - _currentSubPath.previous) * localPercentage); ((_currentSubPath.v - _currentSubPath.previous) * localPercentage);
} }
} }
}

View File

@ -34,19 +34,21 @@ class RotateEffect extends SimplePositionComponentEffect {
@override @override
void initialize(_comp) { void initialize(_comp) {
super.initialize(_comp); super.initialize(_comp);
if (!isAlternating) {
endAngle = _comp.angle + angle;
}
_startAngle = component.angle; _startAngle = component.angle;
_delta = isRelative ? angle : angle - _startAngle; _delta = isRelative ? angle : angle - _startAngle;
if (!isAlternating) {
endAngle = _startAngle + _delta;
}
speed ??= _delta / duration; speed ??= _delta / duration;
duration ??= _delta / speed; duration ??= _delta / speed;
peakTime = duration; peakTime = isAlternating ? duration / 2 : duration;
} }
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
if (!hasFinished()) {
component.angle = _startAngle + _delta * curveProgress; component.angle = _startAngle + _delta * curveProgress;
} }
} }
}

View File

@ -35,19 +35,21 @@ class ScaleEffect extends SimplePositionComponentEffect {
@override @override
void initialize(_comp) { void initialize(_comp) {
super.initialize(_comp); super.initialize(_comp);
if (!isAlternating) {
endSize = size.clone();
}
_startSize = component.size; _startSize = component.size;
_delta = isRelative ? size : size - _startSize; _delta = isRelative ? size : size - _startSize;
if (!isAlternating) {
endSize = _startSize + _delta;
}
speed ??= _delta.length / duration; speed ??= _delta.length / duration;
duration ??= _delta.length / speed; duration ??= _delta.length / speed;
peakTime = duration; peakTime = isAlternating ? duration / 2 : duration;
} }
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
if (!hasFinished()) {
component.size = _startSize + _delta * curveProgress; component.size = _startSize + _delta * curveProgress;
} }
} }
}

View File

@ -18,7 +18,7 @@ void effectTest(
WidgetTester tester, WidgetTester tester,
PositionComponent component, PositionComponent component,
PositionComponentEffect effect, { PositionComponentEffect effect, {
bool hasFinished = true, bool shouldFinish = true,
double iterations = 1.0, double iterations = 1.0,
double expectedAngle = 0.0, double expectedAngle = 0.0,
Vector2 expectedPosition, Vector2 expectedPosition,
@ -41,25 +41,51 @@ void effectTest(
game.update(stepDelta); game.update(stepDelta);
timeLeft -= stepDelta; timeLeft -= stepDelta;
} }
expect( expect(effect.hasFinished(), shouldFinish, reason: "Effect shouldFinish");
effect.hasFinished(), expect(callback.isCalled, shouldFinish, reason: "Callback was treated wrong");
hasFinished, if (!shouldFinish) {
reason: "Effect.hasFinished() didn't have the expected value", const double floatRange = 0.001;
bool acceptableVector(Vector2 vector, Vector2 expectedVector) {
return (expectedVector - vector).length < floatRange;
}
final bool acceptablePosition =
acceptableVector(component.position, expectedPosition);
final bool acceptableSize = acceptableVector(component.size, expectedSize);
final bool acceptableAngle =
(expectedAngle - component.angle).abs() < floatRange;
assert(
acceptablePosition,
"Position is not correct (${component.position} vs $expectedPosition)",
); );
expect( assert(
callback.isCalled, acceptableAngle,
hasFinished, "Angle is not correct (${component.angle} vs $expectedAngle)",
reason: 'Callback was not treated properly',
); );
assert(
acceptableSize,
"Size is not correct (${component.size} vs $expectedSize)",
);
} else {
//game.update(0.1);
expect( expect(
component.position, component.position,
expectedPosition, expectedPosition,
reason: "Position is not correct", reason: "Position is not exactly correct",
); );
expect(component.angle, expectedAngle, reason: "Angle is not correct"); expect(
expect(component.size, expectedSize, reason: "Size is not correct"); component.angle,
expectedAngle,
reason: "Angle is not exactly correct",
);
expect(
component.size,
expectedSize,
reason: "Size is not exactly correct",
);
}
game.update(0.0); // Since effects are removed before they are updated game.update(0.0); // Since effects are removed before they are updated
expect(component.effects.isEmpty, hasFinished); expect(component.effects.isEmpty, shouldFinish);
} }
class Square extends PositionComponent { class Square extends PositionComponent {

View File

@ -68,7 +68,7 @@ void main() {
moveEffect, moveEffect,
expectedPosition: positionComponent.position.clone(), expectedPosition: positionComponent.position.clone(),
iterations: 1.0, iterations: 1.0,
hasFinished: false, shouldFinish: false,
); );
}, },
); );
@ -81,7 +81,7 @@ void main() {
positionComponent, positionComponent,
moveEffect, moveEffect,
expectedPosition: path.last, expectedPosition: path.last,
hasFinished: false, shouldFinish: false,
iterations: 0.5, iterations: 0.5,
); );
}); });
@ -95,7 +95,7 @@ void main() {
moveEffect, moveEffect,
expectedPosition: path.last, expectedPosition: path.last,
iterations: 3.0, iterations: 3.0,
hasFinished: false, shouldFinish: false,
); );
}); });
} }

View File

@ -73,7 +73,7 @@ void main() {
rotateEffect, rotateEffect,
expectedAngle: positionComponent.angle, expectedAngle: positionComponent.angle,
iterations: 1.0, iterations: 1.0,
hasFinished: false, shouldFinish: false,
); );
}, },
); );
@ -86,7 +86,7 @@ void main() {
positionComponent, positionComponent,
rotateEffect, rotateEffect,
expectedAngle: angleArgument, expectedAngle: angleArgument,
hasFinished: false, shouldFinish: false,
iterations: 0.5, iterations: 0.5,
); );
}); });
@ -100,7 +100,7 @@ void main() {
rotateEffect, rotateEffect,
expectedAngle: angleArgument, expectedAngle: angleArgument,
iterations: 3.0, iterations: 3.0,
hasFinished: false, shouldFinish: false,
); );
}); });
} }

View File

@ -68,7 +68,7 @@ void main() {
scaleEffect, scaleEffect,
expectedSize: positionComponent.size.clone(), expectedSize: positionComponent.size.clone(),
iterations: 1.0, iterations: 1.0,
hasFinished: false, shouldFinish: false,
); );
}, },
); );
@ -81,7 +81,7 @@ void main() {
positionComponent, positionComponent,
scaleEffect, scaleEffect,
expectedSize: argumentSize, expectedSize: argumentSize,
hasFinished: false, shouldFinish: false,
iterations: 0.5, iterations: 0.5,
); );
}); });
@ -95,7 +95,7 @@ void main() {
scaleEffect, scaleEffect,
expectedSize: argumentSize, expectedSize: argumentSize,
iterations: 3.0, iterations: 3.0,
hasFinished: false, shouldFinish: false,
); );
}); });
} }