diff --git a/examples/lib/stories/camera_and_viewport/camera_follow_and_world_bounds.dart b/examples/lib/stories/camera_and_viewport/camera_follow_and_world_bounds.dart index d327c9687..617508a87 100644 --- a/examples/lib/stories/camera_and_viewport/camera_follow_and_world_bounds.dart +++ b/examples/lib/stories/camera_and_viewport/camera_follow_and_world_bounds.dart @@ -9,7 +9,7 @@ import 'package:flutter/services.dart'; class CameraFollowAndWorldBoundsExample extends FlameGame with HasKeyboardHandlerComponents { - static const description = ''' + static const description = ''' This example demonstrates camera following the player, but also obeying the world bounds (which are set up to leave a small margin around the visible part of the ground). diff --git a/examples/lib/stories/collision_detection/raycast_max_distance_example.dart b/examples/lib/stories/collision_detection/raycast_max_distance_example.dart index f689a7764..936b5c2ad 100644 --- a/examples/lib/stories/collision_detection/raycast_max_distance_example.dart +++ b/examples/lib/stories/collision_detection/raycast_max_distance_example.dart @@ -85,7 +85,10 @@ This examples showcases how raycast APIs can be used to detect hits within certa camera.viewfinder.add( MoveEffect.by( Vector2(5, 5), - PerlinNoiseEffectController(duration: 0.2, frequency: 400), + NoiseEffectController( + duration: 0.2, + noise: PerlinNoise(frequency: 400), + ), ), ); } diff --git a/examples/lib/stories/effects/move_effect_example.dart b/examples/lib/stories/effects/move_effect_example.dart index 290064c94..0dfe91aa7 100644 --- a/examples/lib/stories/effects/move_effect_example.dart +++ b/examples/lib/stories/effects/move_effect_example.dart @@ -107,12 +107,18 @@ class _MoveEffectWorld extends World { [ MoveEffect.by( Vector2(5, 0), - PerlinNoiseEffectController(duration: 1, frequency: 20), + NoiseEffectController( + duration: 1, + noise: PerlinNoise(frequency: 20), + ), ), MoveEffect.by(Vector2.zero(), LinearEffectController(2)), MoveEffect.by( Vector2(0, 10), - PerlinNoiseEffectController(duration: 1, frequency: 10), + NoiseEffectController( + duration: 1, + noise: PerlinNoise(frequency: 10), + ), ), ], infinite: true, diff --git a/packages/flame_noise/lib/src/effects.dart b/packages/flame_noise/lib/src/effects.dart index c03da066c..616329d2c 100644 --- a/packages/flame_noise/lib/src/effects.dart +++ b/packages/flame_noise/lib/src/effects.dart @@ -1 +1,3 @@ -export 'effects/perlin_noise_effect_controller.dart'; +export 'package:fast_noise/fast_noise.dart'; + +export 'effects/noise_effect_controller.dart'; diff --git a/packages/flame_noise/lib/src/effects/perlin_noise_effect_controller.dart b/packages/flame_noise/lib/src/effects/noise_effect_controller.dart similarity index 62% rename from packages/flame_noise/lib/src/effects/perlin_noise_effect_controller.dart rename to packages/flame_noise/lib/src/effects/noise_effect_controller.dart index a2cfa43d4..7fefce9d2 100644 --- a/packages/flame_noise/lib/src/effects/perlin_noise_effect_controller.dart +++ b/packages/flame_noise/lib/src/effects/noise_effect_controller.dart @@ -2,7 +2,7 @@ import 'package:fast_noise/fast_noise.dart'; import 'package:flame/effects.dart'; import 'package:flutter/animation.dart' show Curve, Curves; -/// Effect controller that oscillates around 0 following a noise curve. +/// Effect controller that oscillates around following a noise curve. /// /// The [taperingCurve] describes how the effect fades out over time. The /// curve that you supply will be flipped along the X axis, so that the effect @@ -12,25 +12,21 @@ import 'package:flutter/animation.dart' show Curve, Curves; /// example, putting into a `MoveEffect.by` will create a shake motion, where /// the magnitude and the direction of shaking is controlled by the effect's /// `offset`. -class PerlinNoiseEffectController extends DurationEffectController { - PerlinNoiseEffectController({ - required double duration, - int octaves = 3, - double frequency = 0.05, - this.taperingCurve = Curves.easeInOutCubic, - int seed = 1337, - }) : assert(duration > 0, 'duration must be positive'), - assert(frequency > 0, 'frequency parameter must be positive'), - noise = PerlinNoise(seed: seed, octaves: octaves, frequency: frequency), - super(duration); - +class NoiseEffectController extends DurationEffectController { final Curve taperingCurve; - final PerlinNoise noise; + final Noise2 noise; + + NoiseEffectController({ + required double duration, + this.taperingCurve = Curves.easeInOutCubic, + Noise2? noise, + }) : noise = noise ?? PerlinNoise(), + super(duration); @override double get progress { final x = timer / duration; final amplitude = taperingCurve.transform(1 - x); - return noise.getPerlin2(x, 1) * amplitude; + return noise.getNoise2(x, 1) * amplitude; } } diff --git a/packages/flame_noise/pubspec.yaml b/packages/flame_noise/pubspec.yaml index 86818a26d..d6002aad8 100644 --- a/packages/flame_noise/pubspec.yaml +++ b/packages/flame_noise/pubspec.yaml @@ -12,7 +12,7 @@ environment: flutter: ">=3.13.0" dependencies: - fast_noise: ^1.0.1 + fast_noise: ^2.0.0 flame: ^1.14.0 flutter: sdk: flutter diff --git a/packages/flame_noise/test/perlin_noise_effect_controller_test.dart b/packages/flame_noise/test/noise_effect_controller_test.dart similarity index 73% rename from packages/flame_noise/test/perlin_noise_effect_controller_test.dart rename to packages/flame_noise/test/noise_effect_controller_test.dart index 73aa19b07..728f7ce6f 100644 --- a/packages/flame_noise/test/perlin_noise_effect_controller_test.dart +++ b/packages/flame_noise/test/noise_effect_controller_test.dart @@ -6,7 +6,10 @@ import 'package:test/test.dart'; void main() { group('PerlinNoiseEffectController', () { test('general properties', () { - final ec = PerlinNoiseEffectController(duration: 1, frequency: 12); + final ec = NoiseEffectController( + duration: 1, + noise: PerlinNoise(frequency: 12), + ); expect(ec.duration, 1.0); expect(ec.taperingCurve, Curves.easeInOutCubic); expect(ec.started, true); @@ -16,7 +19,10 @@ void main() { }); test('progression', () { - final ec = PerlinNoiseEffectController(duration: 1); + final ec = NoiseEffectController( + duration: 1, + noise: PerlinNoise(frequency: 0.05), + ); final observed = []; for (var t = 0.0; t < 1.0; t += 0.1) { observed.add(ec.progress); @@ -39,12 +45,8 @@ void main() { test('errors', () { expect( - () => PerlinNoiseEffectController(duration: 0, frequency: 1), - failsAssert('duration must be positive'), - ); - expect( - () => PerlinNoiseEffectController(duration: 1, frequency: 0), - failsAssert('frequency parameter must be positive'), + () => NoiseEffectController(duration: -1), + failsAssert('Duration cannot be negative: -1.0'), ); }); });