-
-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Scaling particle feature (#2830)
Added a ScalingParticle class that lerp between 1 and a given scale, a shorthand in the Particle class called scaling and updated the docs. Also exposed a getter for the lifespan property in the Particle class.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/src/components/mixins/single_child_particle.dart'; | ||
import 'package:flame/src/particles/curved_particle.dart'; | ||
import 'package:flame/src/particles/particle.dart'; | ||
|
||
/// A particle which scale its child over the lifespan | ||
/// between 1 and a provided scale. | ||
class ScalingParticle extends CurvedParticle with SingleChildParticle { | ||
@override | ||
Particle child; | ||
|
||
final double to; | ||
|
||
ScalingParticle({ | ||
required this.child, | ||
this.to = 0, | ||
super.lifespan, | ||
}); | ||
|
||
double get scale => lerpDouble(1, to, progress) ?? 0; | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
canvas.save(); | ||
canvas.scale(scale); | ||
super.render(canvas); | ||
canvas.restore(); | ||
} | ||
} |
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,36 @@ | ||
import 'package:flame/src/components/particle_system_component.dart'; | ||
import 'package:flame/src/particles/computed_particle.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('ScalingParticle', () { | ||
testWithFlameGame( | ||
'A particle which scale its child over the lifespan ' | ||
'between 1 and a provided scale', (game) async { | ||
final paint = Paint()..color = Colors.red; | ||
final rect = ComputedParticle( | ||
lifespan: 2, | ||
renderer: (canvas, _) => canvas.drawRect( | ||
Rect.fromCenter(center: Offset.zero, width: 10, height: 10), | ||
paint, | ||
), | ||
); | ||
|
||
final particle = rect.scaling(to: .5); | ||
|
||
final component = ParticleSystemComponent( | ||
particle: particle, | ||
); | ||
|
||
game.add(component); | ||
await game.ready(); | ||
game.update(1); | ||
|
||
expect(particle.scale, .75); | ||
expect(particle.child, isInstanceOf<ComputedParticle>()); | ||
expect(particle.child.progress, 0.5); | ||
}); | ||
}); | ||
} |