-
-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(effects): Added SineEffectController (#1262)
An effect controller that represents a single period of the sine function. Use this to create natural-looking harmonic oscillations. Two perpendicular move effects governed by SineEffectControllers with different periods, will create a [Lissajous curve].
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/flame/lib/src/effects/controllers/sine_effect_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'dart:math' as math; | ||
import 'duration_effect_controller.dart'; | ||
import 'infinite_effect_controller.dart'; | ||
import 'repeated_effect_controller.dart'; | ||
|
||
/// This effect controller follows a sine wave. | ||
/// | ||
/// Use this controller to create effects that exhibit natural-looking harmonic | ||
/// motion. | ||
/// | ||
/// Combine with [RepeatedEffectController] or [InfiniteEffectController] in | ||
/// order to create longer waves. | ||
class SineEffectController extends DurationEffectController { | ||
SineEffectController({required double period}) | ||
: assert(period > 0, 'Period must be positive: $period'), | ||
super(period); | ||
|
||
@override | ||
double get progress { | ||
const tau = math.pi * 2; | ||
return math.sin(tau * timer / duration); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/flame/test/effects/controllers/sine_effect_controller_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flame/effects.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('SineEffectController', () { | ||
test('general properties', () { | ||
final ec = SineEffectController(period: 1); | ||
expect(ec.duration, 1); | ||
expect(ec.started, true); | ||
expect(ec.completed, false); | ||
expect(ec.progress, 0); | ||
expect(ec.isRandom, false); | ||
}); | ||
|
||
test('progression', () { | ||
final ec = SineEffectController(period: 3); | ||
final expectedProgress = | ||
List<double>.generate(101, (i) => sin(i * 0.01 * 2 * pi)); | ||
for (final p in expectedProgress) { | ||
expect(ec.progress, closeTo(p, 2e-14)); | ||
ec.advance(0.01 * 3); | ||
} | ||
expect(ec.completed, true); | ||
}); | ||
|
||
test('errors', () { | ||
expect( | ||
() => SineEffectController(period: 0), | ||
failsAssert('Period must be positive: 0.0'), | ||
); | ||
expect( | ||
() => SineEffectController(period: -1.1), | ||
failsAssert('Period must be positive: -1.1'), | ||
); | ||
}); | ||
}); | ||
} |