Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow children of ComposedParticle to have varied lifespan #1879

Merged
merged 7 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/flame/lib/src/particles/composed_particle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import 'package:flame/src/particles/particle.dart';

/// A single [Particle] which manages multiple children
/// by proxying all lifecycle hooks.
///
/// [applyLifespanToChildren] if true, then [children] will have the same
/// lifespan as parent [ComposedParticle]
class ComposedParticle extends Particle {
final List<Particle> children;
final bool applyLifespanToChildren;

ComposedParticle({
required this.children,
super.lifespan,
this.applyLifespanToChildren = true,
});

@override
void setLifespan(double lifespan) {
super.setLifespan(lifespan);

for (final child in children) {
child.setLifespan(lifespan);
if (applyLifespanToChildren) {
for (final child in children) {
child.setLifespan(lifespan);
}
}
}

Expand All @@ -32,6 +39,8 @@ class ComposedParticle extends Particle {
void update(double dt) {
super.update(dt);

children.removeWhere((particle) => particle.shouldRemove);

for (final child in children) {
child.update(dt);
}
Expand Down
90 changes: 90 additions & 0 deletions packages/flame/test/particles/composed_particle_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:flame/particles.dart';
import 'package:flame/src/components/particle_system_component.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('ComposedParticle', () {
testWithFlameGame('particles with parent lifespan applied to children',
(game) async {
final childParticle1 = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 1,
);
final childParticle2 = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 3,
);

final particle = ComposedParticle(
children: [
childParticle1,
childParticle2,
],
lifespan: 2,
);

final component = ParticleSystemComponent(
particle: particle,
);

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

expect(particle.progress, 0.5);
expect(childParticle1.progress, 0.5);
expect(childParticle2.progress, 0.5);
expect(particle.children.length, 2);

game.update(1);

expect(particle.progress, 1);
expect(childParticle1.progress, 1);
expect(childParticle2.progress, 1);
expect(particle.children.length, 2);
});

testWithFlameGame('particles without parent lifespan applied to children',
(game) async {
final childParticle1 = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 1,
);
final childParticle2 = CircleParticle(
paint: Paint()..color = Colors.red,
lifespan: 4,
);

final particle = ComposedParticle(
children: [
childParticle1,
childParticle2,
],
lifespan: 2,
applyLifespanToChildren: false,
);

final component = ParticleSystemComponent(
particle: particle,
);

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

expect(particle.progress, 0.5);
expect(childParticle1.progress, 1);
expect(childParticle2.progress, 0.25);
expect(particle.children.length, 2);

game.update(1);

expect(particle.progress, 1);
expect(childParticle1.progress, 1);
expect(childParticle2.progress, 0.5);
expect(particle.children.length, 1);
});
});
}