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();
if (hasFinished()) {
onComplete?.call();
_setComponentToEndState();
}
}
}
@ -108,6 +109,8 @@ abstract class ComponentEffect<T extends Component> {
driftTime = 0;
}
}
void _setComponentToEndState();
}
abstract class PositionComponentEffect
@ -141,16 +144,23 @@ abstract class PositionComponentEffect
void initialize(PositionComponent component) {
super.initialize(component);
this.component = component;
originalPosition = component.position;
originalPosition = component.position.clone();
originalAngle = component.angle;
originalSize = component.size;
originalSize = component.size.clone();
/// 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
/// started in
endPosition = component.position;
endPosition = component.position.clone();
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 {
_movePath = path;
}
if (!isAlternating) {
endPosition = _movePath.last;
} else {
endPosition = _startPosition;
}
endPosition = isAlternating ? _startPosition : _movePath.last;
double pathLength = 0;
Vector2 lastPosition = _startPosition;
@ -113,16 +109,19 @@ class MoveEffect extends SimplePositionComponentEffect {
@override
void update(double dt) {
super.update(dt);
_currentSubPath ??= _percentagePath.first;
if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress ||
curveDirection.isNegative && _currentSubPath.startAt > curveProgress) {
_currentSubPath =
_percentagePath.firstWhere((v) => v.endAt >= curveProgress);
if (!hasFinished()) {
_currentSubPath ??= _percentagePath.first;
if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress ||
curveDirection.isNegative &&
_currentSubPath.startAt > curveProgress) {
_currentSubPath =
_percentagePath.firstWhere((v) => v.endAt >= curveProgress);
}
final double lastEndAt = _currentSubPath.startAt;
final double localPercentage =
(curveProgress - lastEndAt) / (_currentSubPath.endAt - lastEndAt);
component.position = _currentSubPath.previous +
((_currentSubPath.v - _currentSubPath.previous) * localPercentage);
}
final double lastEndAt = _currentSubPath.startAt;
final double localPercentage =
(curveProgress - lastEndAt) / (_currentSubPath.endAt - lastEndAt);
component.position = _currentSubPath.previous +
((_currentSubPath.v - _currentSubPath.previous) * localPercentage);
}
}

View File

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

View File

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