Files
flame/examples/lib/stories/rendering/particles_interactive_example.dart
Lukas Klingsbo 2d45d2be39 chore: Remove 1.8.0 deprecations (#2538)
Removes all the deprecated methods before 1.8.0 release.
2023-05-22 19:01:55 +02:00

66 lines
1.8 KiB
Dart

import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/particles.dart';
import 'package:flutter/material.dart';
class ParticlesInteractiveExample extends FlameGame with PanDetector {
static const description = 'An example which shows how '
'ParticleSystemComponent can be added in runtime '
'following an event, in this example, the mouse '
'dragging';
final random = Random();
final Tween<double> noise = Tween(begin: -1, end: 1);
final ColorTween colorTween;
final double zoom;
final world = World();
late final CameraComponent cameraComponent;
ParticlesInteractiveExample({
required Color from,
required Color to,
required this.zoom,
}) : colorTween = ColorTween(begin: from, end: to);
@override
Future<void> onLoad() async {
cameraComponent = CameraComponent.withFixedResolution(
world: world,
width: 400,
height: 600,
);
addAll([cameraComponent, world]);
cameraComponent.viewfinder.zoom = zoom;
}
@override
void onPanUpdate(DragUpdateInfo info) {
add(
ParticleSystemComponent(
position: info.eventPosition.game,
particle: Particle.generate(
count: 40,
generator: (i) {
return AcceleratedParticle(
lifespan: 2,
speed: Vector2(
noise.transform(random.nextDouble()),
noise.transform(random.nextDouble()),
) *
i.toDouble(),
child: CircleParticle(
radius: 2,
paint: Paint()
..color = colorTween.transform(random.nextDouble())!,
),
);
},
),
),
);
}
}