Standardize on dt in update methods (#679)

* Standardize on dt in update methods

* Update changelog entry

* Update CHANGELOG.md

Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>

Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>
This commit is contained in:
Lukas Klingsbo
2021-02-23 17:36:05 +01:00
committed by GitHub
parent 9735cf5e08
commit cdca4b6858
11 changed files with 27 additions and 26 deletions

View File

@@ -22,6 +22,7 @@
- Add utility methods to the Anchor class to make it more "enum like" - Add utility methods to the Anchor class to make it more "enum like"
- Enable user-defined anchors - Enable user-defined anchors
- Added `toImage` method for the `Sprite` class - Added `toImage` method for the `Sprite` class
- Uniform use of `dt` instead of `t` in all update methods
- Add more optional arguments for unified constructors of components - Add more optional arguments for unified constructors of components
## 1.0.0-rc6 ## 1.0.0-rc6

View File

@@ -31,5 +31,5 @@ class MyGame extends Game {
} }
@override @override
void update(double t) {} void update(double dt) {}
} }

View File

@@ -37,9 +37,9 @@ class Square extends PositionComponent with HasGameRef<MyGame> {
} }
@override @override
void update(double t) { void update(double dt) {
super.update(t); super.update(dt);
angle += SPEED * t; angle += SPEED * dt;
angle %= 2 * math.pi; angle %= 2 * math.pi;
} }

View File

@@ -110,8 +110,8 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef {
} }
@override @override
void update(double t) { void update(double dt) {
super.update(t); super.update(dt);
if (_dragging) { if (_dragging) {
final delta = _dragPosition - _backgroundRect.center.toVector2(); final delta = _dragPosition - _backgroundRect.center.toVector2();
final radAngle = atan2(delta.y, delta.x); final radAngle = atan2(delta.y, delta.x);

View File

@@ -41,10 +41,10 @@ mixin SingleChildParticle on Particle {
} }
@override @override
void update(double t) { void update(double dt) {
assert(child != null); assert(child != null);
super.update(t); super.update(dt);
child.update(t); child.update(dt);
} }
} }

View File

@@ -62,9 +62,9 @@ class ParallaxComponent extends PositionComponent {
} }
@override @override
void update(double t) { void update(double dt) {
super.update(t); super.update(dt);
parallax?.update(t); parallax?.update(dt);
} }
@mustCallSuper @mustCallSuper

View File

@@ -69,8 +69,8 @@ class SpriteAnimationComponent extends PositionComponent {
} }
@override @override
void update(double t) { void update(double dt) {
super.update(t); super.update(dt);
animation?.update(t); animation?.update(dt);
} }
} }

View File

@@ -136,7 +136,7 @@ class BaseGame extends Game with FPSCounter {
/// You can override it further to add more custom behavior. /// You can override it further to add more custom behavior.
@override @override
@mustCallSuper @mustCallSuper
void update(double t) { void update(double dt) {
_removeLater.addAll(components.where((c) => c.shouldRemove)); _removeLater.addAll(components.where((c) => c.shouldRemove));
_removeLater.forEach((c) { _removeLater.forEach((c) {
c.onRemove(); c.onRemove();
@@ -155,7 +155,7 @@ class BaseGame extends Game with FPSCounter {
addNow.forEach((component) => component.onMount()); addNow.forEach((component) => component.onMount());
} }
components.forEach((c) => c.update(t)); components.forEach((c) => c.update(dt));
} }
/// This implementation of resize passes the resize call along to every component in the list, enabling each one to make their decisions as how to handle the resize. /// This implementation of resize passes the resize call along to every component in the list, enabling each one to make their decisions as how to handle the resize.

View File

@@ -57,10 +57,10 @@ abstract class Game {
/// It cannot be changed at runtime, because the game widget does not get rebuild when this value changes. /// It cannot be changed at runtime, because the game widget does not get rebuild when this value changes.
Color backgroundColor() => const Color(0xFF000000); Color backgroundColor() => const Color(0xFF000000);
/// Implement this method to update the game state, given that a time [t] has passed. /// Implement this method to update the game state, given the time [dt] that has passed since the last update.
/// ///
/// Keep the updates as short as possible. [t] is in seconds, with microseconds precision. /// Keep the updates as short as possible. [dt] is in seconds, with microseconds precision.
void update(double t); void update(double dt);
/// Implement this method to render the current game state in the [canvas]. /// Implement this method to render the current game state in the [canvas].
void render(Canvas canvas); void render(Canvas canvas);

View File

@@ -266,10 +266,10 @@ class Parallax {
} }
} }
void update(double t) { void update(double dt) {
layers.forEach((layer) { layers.forEach((layer) {
layer.update( layer.update(
(baseVelocity.clone()..multiply(layer.velocityMultiplier)) * t, (baseVelocity.clone()..multiply(layer.velocityMultiplier)) * dt,
); );
}); });
} }

View File

@@ -38,10 +38,10 @@ class AcceleratedParticle extends CurvedParticle with SingleChildParticle {
} }
@override @override
void update(double t) { void update(double dt) {
speed += acceleration * t; speed += acceleration * dt;
position += speed * t - (acceleration * t * t) / 2; position += speed * dt - (acceleration * dt * dt) / 2;
super.update(t); super.update(dt);
} }
} }