mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 09:39:12 +08:00
fix: doc/example/particles/readme, attempt to embed webm preview fix: doc/example/particles better sample for chaining refactor: Particle, dropped duration support
49 lines
1.2 KiB
Dart
49 lines
1.2 KiB
Dart
import 'dart:math';
|
|
import 'dart:ui';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../components/mixins/single_child_particle.dart';
|
|
import '../particle.dart';
|
|
import 'curved_particle.dart';
|
|
|
|
/// A particle serves as a container for basic
|
|
/// acceleration physics.
|
|
/// [Offset] unit is logical px per second.
|
|
/// speed = Offset(0, 100) is 100 logical pixels per second, down
|
|
/// acceleration = Offset(-40, 0) will accelerate to left at rate of 40 px/s
|
|
class AcceleratedParticle extends CurvedParticle with SingleChildParticle {
|
|
@override
|
|
Particle child;
|
|
|
|
final Offset acceleration;
|
|
Offset speed;
|
|
Offset position;
|
|
|
|
AcceleratedParticle({
|
|
@required this.child,
|
|
this.acceleration = Offset.zero,
|
|
this.speed = Offset.zero,
|
|
this.position = Offset.zero,
|
|
double lifespan,
|
|
}) : super(
|
|
lifespan: lifespan,
|
|
);
|
|
|
|
@override
|
|
void render(Canvas canvas) {
|
|
canvas.save();
|
|
canvas.translate(position.dx, position.dy);
|
|
super.render(canvas);
|
|
canvas.restore();
|
|
}
|
|
|
|
@override
|
|
void update(double t) {
|
|
speed += acceleration * t;
|
|
position += speed * t - (acceleration * pow(t, 2)) / 2;
|
|
|
|
super.update(t);
|
|
}
|
|
}
|