mirror of https://github.com/flutter/pinball.git
feat: defined `ScoringBehavior` and `ScoringContactBehavior` (#329)
* refactor: renamed ScoringBehavior to ScoringContactBehavior * feat: defined ScoringBehavior * docs: improved documentationpull/333/head
parent
41aaaa4646
commit
8957b96ae3
@ -1,33 +1,77 @@
|
|||||||
// ignore_for_file: avoid_renaming_method_parameters
|
// ignore_for_file: avoid_renaming_method_parameters
|
||||||
|
|
||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame/effects.dart';
|
||||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
import 'package:pinball/game/game.dart';
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
import 'package:pinball_flame/pinball_flame.dart';
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
/// {@template scoring_behavior}
|
/// {@template scoring_behavior}
|
||||||
/// Adds points to the score when the [Ball] contacts the [parent].
|
/// Adds [_points] to the score and shows a text effect.
|
||||||
|
///
|
||||||
|
/// The behavior removes itself after the duration.
|
||||||
/// {@endtemplate}
|
/// {@endtemplate}
|
||||||
class ScoringBehavior extends ContactBehavior with HasGameRef<PinballGame> {
|
class ScoringBehavior extends Component with HasGameRef<PinballGame> {
|
||||||
/// {@macro scoring_behavior}
|
/// {@macto scoring_behavior}
|
||||||
ScoringBehavior({
|
ScoringBehavior({
|
||||||
required Points points,
|
required Points points,
|
||||||
}) : _points = points;
|
required Vector2 position,
|
||||||
|
double duration = 1,
|
||||||
|
}) : _points = points,
|
||||||
|
_position = position,
|
||||||
|
_effectController = EffectController(
|
||||||
|
duration: duration,
|
||||||
|
);
|
||||||
|
|
||||||
final Points _points;
|
final Points _points;
|
||||||
|
final Vector2 _position;
|
||||||
|
|
||||||
|
final EffectController _effectController;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void beginContact(Object other, Contact contact) {
|
void update(double dt) {
|
||||||
super.beginContact(other, contact);
|
super.update(dt);
|
||||||
if (other is! Ball) return;
|
if (_effectController.completed) {
|
||||||
|
removeFromParent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
gameRef.read<GameBloc>().add(Scored(points: _points.value));
|
gameRef.read<GameBloc>().add(Scored(points: _points.value));
|
||||||
gameRef.firstChild<ZCanvasComponent>()!.add(
|
await gameRef.firstChild<ZCanvasComponent>()!.add(
|
||||||
ScoreComponent(
|
ScoreComponent(
|
||||||
points: _points,
|
points: _points,
|
||||||
position: other.body.position,
|
position: _position,
|
||||||
|
effectController: _effectController,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// {@template scoring_contact_behavior}
|
||||||
|
/// Adds points to the score when the [Ball] contacts the [parent].
|
||||||
|
/// {@endtemplate}
|
||||||
|
class ScoringContactBehavior extends ContactBehavior
|
||||||
|
with HasGameRef<PinballGame> {
|
||||||
|
/// {@macro scoring_contact_behavior}
|
||||||
|
ScoringContactBehavior({
|
||||||
|
required Points points,
|
||||||
|
}) : _points = points;
|
||||||
|
|
||||||
|
final Points _points;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void beginContact(Object other, Contact contact) {
|
||||||
|
super.beginContact(other, contact);
|
||||||
|
if (other is! Ball) return;
|
||||||
|
|
||||||
|
parent.add(
|
||||||
|
ScoringBehavior(
|
||||||
|
points: _points,
|
||||||
|
position: other.body.position,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue