fix: `Plunger`'s pull not correctly working on some devices (#321)

* fix: plunger's pull not correctly working on some devices

* fix: plunger's pull not correctly working on some devices

* fix: plunger's pull not correctly working on some devices

Co-authored-by: Tom Arra <tarra3@gmail.com>
pull/320/head
Jochum van der Ploeg 3 years ago committed by GitHub
parent 8a8f86ba56
commit 9d98ff37fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -97,7 +97,7 @@ class Plunger extends BodyComponent with InitialPosition, Layered, ZIndex {
void update(double dt) { void update(double dt) {
// Ensure that we only pull or release when the time is greater than zero. // Ensure that we only pull or release when the time is greater than zero.
if (_pullingDownTime > 0) { if (_pullingDownTime > 0) {
_pullingDownTime -= dt; _pullingDownTime -= PinballForge2DGame.clampDt(dt);
if (_pullingDownTime <= 0) { if (_pullingDownTime <= 0) {
release(); release();
} else { } else {

@ -141,7 +141,11 @@ void main() {
expect(plunger.body.linearVelocity.y, isPositive); expect(plunger.body.linearVelocity.y, isPositive);
await tester.pump(const Duration(seconds: 2)); // Call game update at 120 FPS, so that the plunger will act as if it
// was pulled for 2 seconds.
for (var i = 0.0; i < 2; i += 1 / 120) {
game.update(1 / 20);
}
expect(plunger.body.linearVelocity.y, isZero); expect(plunger.body.linearVelocity.y, isZero);
}, },

@ -24,7 +24,7 @@ class PinballForge2DGame extends FlameGame implements Forge2DGame {
@override @override
void update(double dt) { void update(double dt) {
super.update(dt); super.update(dt);
world.stepDt(min(dt, 1 / 60)); world.stepDt(clampDt(dt));
} }
@override @override
@ -41,4 +41,14 @@ class PinballForge2DGame extends FlameGame implements Forge2DGame {
Vector2 worldToScreen(Vector2 position) { Vector2 worldToScreen(Vector2 position) {
throw UnimplementedError(); throw UnimplementedError();
} }
/// Clamp the [dt] in such a way that it would never exceed the minimal of
/// 1/60th of a second.
///
/// Note: this is a static method because composing this class as a generic
/// on `BodyComponent` and mixins for that class will crash the Dart analyzer
/// server.
static double clampDt(double dt) {
return min(dt, 1 / 60);
}
} }

@ -47,5 +47,17 @@ void main() {
); );
}, },
); );
group('clampDt', () {
test('returns dt', () {
const dt = 0.0001;
expect(PinballForge2DGame.clampDt(dt), equals(dt));
});
test('returns result of 1/60 as dt is to high', () {
const dt = 1.0;
expect(PinballForge2DGame.clampDt(dt), equals(1 / 60));
});
});
}); });
} }

Loading…
Cancel
Save