Skip to content

Commit

Permalink
feat: add applyLifespanToChildren to Particle generate (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmasarovic authored Sep 18, 2022
1 parent 27ab12f commit 884d519
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/flame/lib/src/particles/particle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ abstract class Particle {
int count = 10,
required ParticleGenerator generator,
double? lifespan,
bool applyLifespanToChildren = true,
}) {
return ComposedParticle(
lifespan: lifespan,
applyLifespanToChildren: applyLifespanToChildren,
children: List<Particle>.generate(count, generator),
);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/flame/test/particles/composed_particle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,45 @@ void main() {
expect(childParticle2.progress, 0.5);
expect(particle.children.length, 1);
});

testWithFlameGame(
'generate particles without parent lifespan applied to children',
(game) async {
const particlesCount = 15;
final component = ParticleSystemComponent(
particle: Particle.generate(
count: particlesCount,
generator: (i) {
return CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 5,
);
},
applyLifespanToChildren: false,
lifespan: 10,
),
);

game.add(component);
await game.ready();
game.update(1);

expect(component.particle!.progress, 0.1);
final children1 = (component.particle! as ComposedParticle).children;
expect(children1.length, particlesCount);
for (final child in children1) {
expect(child.progress, 0.2);
}

game.update(1);

expect(component.particle!.progress, 0.2);
final children2 = (component.particle! as ComposedParticle).children;
expect(children2.length, particlesCount);
for (final child in children2) {
expect(child.progress, 0.4);
}
},
);
});
}

0 comments on commit 884d519

Please sign in to comment.