mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 17:06:50 +08:00 
			
		
		
		
	Always set end state
This commit is contained in:
		| @ -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); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -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,16 +109,19 @@ class MoveEffect extends SimplePositionComponentEffect { | |||||||
|   @override |   @override | ||||||
|   void update(double dt) { |   void update(double dt) { | ||||||
|     super.update(dt); |     super.update(dt); | ||||||
|     _currentSubPath ??= _percentagePath.first; |     if (!hasFinished()) { | ||||||
|     if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress || |       _currentSubPath ??= _percentagePath.first; | ||||||
|         curveDirection.isNegative && _currentSubPath.startAt > curveProgress) { |       if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress || | ||||||
|       _currentSubPath = |           curveDirection.isNegative && | ||||||
|           _percentagePath.firstWhere((v) => v.endAt >= curveProgress); |               _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); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -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); | ||||||
|     component.angle = _startAngle + _delta * curveProgress; |     if (!hasFinished()) { | ||||||
|  |       component.angle = _startAngle + _delta * curveProgress; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -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); | ||||||
|     component.size = _startSize + _delta * curveProgress; |     if (!hasFinished()) { | ||||||
|  |       component.size = _startSize + _delta * curveProgress; | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -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) { | ||||||
|   expect( |       return (expectedVector - vector).length < floatRange; | ||||||
|     callback.isCalled, |     } | ||||||
|     hasFinished, |  | ||||||
|     reason: 'Callback was not treated properly', |     final bool acceptablePosition = | ||||||
|   ); |         acceptableVector(component.position, expectedPosition); | ||||||
|   expect( |     final bool acceptableSize = acceptableVector(component.size, expectedSize); | ||||||
|     component.position, |     final bool acceptableAngle = | ||||||
|     expectedPosition, |         (expectedAngle - component.angle).abs() < floatRange; | ||||||
|     reason: "Position is not correct", |     assert( | ||||||
|   ); |       acceptablePosition, | ||||||
|   expect(component.angle, expectedAngle, reason: "Angle is not correct"); |       "Position is not correct (${component.position} vs $expectedPosition)", | ||||||
|   expect(component.size, expectedSize, reason: "Size is not correct"); |     ); | ||||||
|  |     assert( | ||||||
|  |       acceptableAngle, | ||||||
|  |       "Angle is not correct (${component.angle} vs $expectedAngle)", | ||||||
|  |     ); | ||||||
|  |     assert( | ||||||
|  |       acceptableSize, | ||||||
|  |       "Size is not correct (${component.size} vs $expectedSize)", | ||||||
|  |     ); | ||||||
|  |   } else { | ||||||
|  |     //game.update(0.1); | ||||||
|  |     expect( | ||||||
|  |       component.position, | ||||||
|  |       expectedPosition, | ||||||
|  |       reason: "Position is not exactly correct", | ||||||
|  |     ); | ||||||
|  |     expect( | ||||||
|  |       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 { | ||||||
|  | |||||||
| @ -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, | ||||||
|     ); |     ); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  | |||||||
| @ -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, | ||||||
|     ); |     ); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  | |||||||
| @ -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, | ||||||
|     ); |     ); | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Lukas Klingsbo
					Lukas Klingsbo