mirror of https://github.com/flutter/pinball.git
refactor: removed FireEffect (#402)
parent
93dd24f26a
commit
21bf4f478d
@ -1,78 +0,0 @@
|
|||||||
import 'dart:math' as math;
|
|
||||||
|
|
||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:flame/extensions.dart';
|
|
||||||
import 'package:flame/particles.dart';
|
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart' hide Particle;
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
const _particleRadius = 0.25;
|
|
||||||
|
|
||||||
/// {@template fire_effect}
|
|
||||||
/// A [BodyComponent] which creates a fire trail effect using the given
|
|
||||||
/// parameters
|
|
||||||
/// {@endtemplate}
|
|
||||||
class FireEffect extends ParticleSystemComponent {
|
|
||||||
/// {@macro fire_effect}
|
|
||||||
FireEffect({
|
|
||||||
required this.burstPower,
|
|
||||||
required this.direction,
|
|
||||||
Vector2? position,
|
|
||||||
}) : super(
|
|
||||||
position: position,
|
|
||||||
);
|
|
||||||
|
|
||||||
/// A [double] value that will define how "strong" the burst of particles
|
|
||||||
/// will be.
|
|
||||||
final double burstPower;
|
|
||||||
|
|
||||||
/// Which direction the burst will aim.
|
|
||||||
final Vector2 direction;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> onLoad() async {
|
|
||||||
await super.onLoad();
|
|
||||||
|
|
||||||
final children = [
|
|
||||||
...List.generate(4, (index) {
|
|
||||||
return CircleParticle(
|
|
||||||
radius: _particleRadius,
|
|
||||||
paint: Paint()..color = Colors.yellow.darken((index + 1) / 4),
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
...List.generate(4, (index) {
|
|
||||||
return CircleParticle(
|
|
||||||
radius: _particleRadius,
|
|
||||||
paint: Paint()..color = Colors.red.darken((index + 1) / 4),
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
...List.generate(4, (index) {
|
|
||||||
return CircleParticle(
|
|
||||||
radius: _particleRadius,
|
|
||||||
paint: Paint()..color = Colors.orange.darken((index + 1) / 4),
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
final random = math.Random();
|
|
||||||
final spreadTween = Tween<double>(begin: -0.2, end: 0.2);
|
|
||||||
|
|
||||||
particle = Particle.generate(
|
|
||||||
count: math.max((random.nextDouble() * (burstPower * 10)).toInt(), 1),
|
|
||||||
generator: (_) {
|
|
||||||
final spread = Vector2(
|
|
||||||
spreadTween.transform(random.nextDouble()),
|
|
||||||
spreadTween.transform(random.nextDouble()),
|
|
||||||
);
|
|
||||||
final finalDirection = Vector2(direction.x, direction.y) + spread;
|
|
||||||
final speed = finalDirection * (burstPower * 20);
|
|
||||||
|
|
||||||
return AcceleratedParticle(
|
|
||||||
lifespan: 5 / burstPower,
|
|
||||||
position: Vector2.zero(),
|
|
||||||
speed: speed,
|
|
||||||
child: children[random.nextInt(children.length)],
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
import 'package:sandbox/common/common.dart';
|
|
||||||
|
|
||||||
class FireEffectGame extends LineGame {
|
|
||||||
static const description = '''
|
|
||||||
Shows how the FireEffect renders.
|
|
||||||
|
|
||||||
- Drag a line to define the trail direction.
|
|
||||||
''';
|
|
||||||
|
|
||||||
@override
|
|
||||||
void onLine(Vector2 line) {
|
|
||||||
add(_EffectEmitter(line));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _EffectEmitter extends Component {
|
|
||||||
_EffectEmitter(this.line) {
|
|
||||||
_direction = line.normalized();
|
|
||||||
_force = line.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const _timerLimit = 2.0;
|
|
||||||
var _timer = _timerLimit;
|
|
||||||
|
|
||||||
final Vector2 line;
|
|
||||||
|
|
||||||
late Vector2 _direction;
|
|
||||||
late double _force;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void update(double dt) {
|
|
||||||
super.update(dt);
|
|
||||||
|
|
||||||
if (_timer > 0) {
|
|
||||||
add(
|
|
||||||
FireEffect(
|
|
||||||
burstPower: (_timer / _timerLimit) * _force,
|
|
||||||
direction: _direction,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
_timer -= dt;
|
|
||||||
} else {
|
|
||||||
removeFromParent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +1,11 @@
|
|||||||
import 'package:dashbook/dashbook.dart';
|
import 'package:dashbook/dashbook.dart';
|
||||||
import 'package:sandbox/common/common.dart';
|
import 'package:sandbox/common/common.dart';
|
||||||
import 'package:sandbox/stories/effects/camera_zoom_game.dart';
|
import 'package:sandbox/stories/effects/camera_zoom_game.dart';
|
||||||
import 'package:sandbox/stories/effects/fire_effect_game.dart';
|
|
||||||
|
|
||||||
void addEffectsStories(Dashbook dashbook) {
|
void addEffectsStories(Dashbook dashbook) {
|
||||||
dashbook.storiesOf('Effects')
|
dashbook.storiesOf('Effects').addGame(
|
||||||
..addGame(
|
title: 'CameraZoom',
|
||||||
title: 'Fire',
|
description: CameraZoomGame.description,
|
||||||
description: FireEffectGame.description,
|
gameBuilder: (_) => CameraZoomGame(),
|
||||||
gameBuilder: (_) => FireEffectGame(),
|
);
|
||||||
)
|
|
||||||
..addGame(
|
|
||||||
title: 'CameraZoom',
|
|
||||||
description: CameraZoomGame.description,
|
|
||||||
gameBuilder: (_) => CameraZoomGame(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
// ignore_for_file: cascade_invocations
|
|
||||||
|
|
||||||
import 'package:flame/components.dart';
|
|
||||||
import 'package:flame_test/flame_test.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
|
||||||
|
|
||||||
import '../../helpers/helpers.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
final flameTester = FlameTester(TestGame.new);
|
|
||||||
|
|
||||||
flameTester.test(
|
|
||||||
'loads correctly',
|
|
||||||
(game) async {
|
|
||||||
final fireEffect = FireEffect(
|
|
||||||
burstPower: 1,
|
|
||||||
direction: Vector2.zero(),
|
|
||||||
);
|
|
||||||
await game.ensureAdd(fireEffect);
|
|
||||||
|
|
||||||
expect(game.contains(fireEffect), isTrue);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
Loading…
Reference in new issue