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

pull/321/head
Jochum van der Ploeg 3 years ago
parent c753b65530
commit 98943cac95
No known key found for this signature in database
GPG Key ID: E961B7B51589CA09

@ -1,5 +1,3 @@
import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart';
@ -99,7 +97,7 @@ class Plunger extends BodyComponent with InitialPosition, Layered, ZIndex {
void update(double dt) {
// Ensure that we only pull or release when the time is greater than zero.
if (_pullingDownTime > 0) {
_pullingDownTime -= min(dt, 1 / 60);
_pullingDownTime -= PinballForge2DGame.clampDt(dt);
if (_pullingDownTime <= 0) {
release();
} else {

@ -24,7 +24,7 @@ class PinballForge2DGame extends FlameGame implements Forge2DGame {
@override
void update(double dt) {
super.update(dt);
world.stepDt(min(dt, 1 / 60));
world.stepDt(clampDt(dt));
}
@override
@ -41,4 +41,14 @@ class PinballForge2DGame extends FlameGame implements Forge2DGame {
Vector2 worldToScreen(Vector2 position) {
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