feat: used a Timer in ChromeDino animation

pull/76/head
alestiago 4 years ago
parent a90b61ef80
commit 516c9b3662

@ -1,7 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart' hide Timer;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -30,7 +30,7 @@ class ChromeDino extends BodyComponent with InitialPosition {
anchor: anchor, anchor: anchor,
); );
final joint = _ChromeDinoJoint(jointDef)..create(world); final joint = _ChromeDinoJoint(jointDef)..create(world);
joint.swivel(joint, removed); unawaited(removed.future.whenComplete(joint.dispose));
} }
// TODO(alestiago): Remove once the following is added to Flame. // TODO(alestiago): Remove once the following is added to Flame.
@ -145,7 +145,14 @@ class _ChromeDinoAnchorRevoluteJointDef extends RevoluteJointDef {
} }
class _ChromeDinoJoint extends RevoluteJoint { class _ChromeDinoJoint extends RevoluteJoint {
_ChromeDinoJoint(_ChromeDinoAnchorRevoluteJointDef def) : super(def); _ChromeDinoJoint(_ChromeDinoAnchorRevoluteJointDef def) : super(def) {
_timer = Timer.periodic(
const Duration(milliseconds: 100),
(_) => _swivel(),
);
}
late final Timer _timer;
// TODO(alestiago): Remove once Forge2D supports custom joints. // TODO(alestiago): Remove once Forge2D supports custom joints.
void create(World world) { void create(World world) {
@ -155,14 +162,11 @@ class _ChromeDinoJoint extends RevoluteJoint {
} }
/// Sweeps the [ChromeDino] up and down repeatedly. /// Sweeps the [ChromeDino] up and down repeatedly.
void swivel(RevoluteJoint joint, Completer completer) { void _swivel() {
Future.doWhile(() async { setMotorSpeed(-motorSpeed);
if (completer.isCompleted) return false; }
// TODO(alestiago): Tune this values. void dispose() {
await Future<void>.delayed(const Duration(milliseconds: 1000)); _timer.cancel();
joint.setMotorSpeed(-joint.motorSpeed);
return true;
});
} }
} }

Loading…
Cancel
Save