Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SpeedController advance() should execute after its effect's onStart() #2173

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ abstract class Effect extends Component {
/// similar to `EffectController.advance`.
@internal
double advance(double dt) {
final remainingDt = controller.advance(dt);
if (!_started && controller.started) {
_started = true;
onStart();
}
final remainingDt = controller.advance(dt);
if (_started) {
final progress = controller.progress;
apply(progress);
Expand Down
39 changes: 23 additions & 16 deletions packages/flame/test/effects/sequence_effect_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/game.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('SequenceEffect', () {
group('properties', () {
final game = FlameGame()..onGameResize(Vector2.all(1000));
assert(game.isMounted);

test('simple', () {
final effect = SequenceEffect([
MoveEffect.to(Vector2(10, 10), EffectController(duration: 3)),
Expand Down Expand Up @@ -89,8 +85,7 @@ void main() {
});

group('sequence progression', () {
test('simple sequence', () async {
final game = FlameGame()..onGameResize(Vector2.all(1000));
testWithFlameGame('simple sequence', (game) async {
final effect = SequenceEffect([
MoveEffect.by(Vector2(10, 0), EffectController(duration: 1)),
MoveEffect.by(Vector2(0, 10), EffectController(duration: 2)),
Expand All @@ -115,8 +110,7 @@ void main() {
}
});

test('large step', () async {
final game = FlameGame()..onGameResize(Vector2.all(1000));
testWithFlameGame('large step', (game) async {
final effect = SequenceEffect([
MoveEffect.by(Vector2(10, 0), EffectController(duration: 1)),
MoveEffect.by(Vector2(0, 10), EffectController(duration: 2)),
Expand All @@ -131,8 +125,7 @@ void main() {
expect(component.position, closeToVector(Vector2(30, 40)));
});

test('k-step sequence', () async {
final game = FlameGame()..onGameResize(Vector2.all(1000));
testWithFlameGame('k-step sequence', (game) async {
final effect = SequenceEffect(
[
MoveEffect.by(Vector2(10, 0), EffectController(duration: 1)),
Expand All @@ -157,8 +150,7 @@ void main() {
expect(component.position, closeToVector(Vector2(50, 50)));
});

test('alternating sequence', () async {
final game = FlameGame()..onGameResize(Vector2.all(1000));
testWithFlameGame('alternating sequence', (game) async {
final effect = SequenceEffect(
[
MoveEffect.by(Vector2(10, 0), EffectController(duration: 1)),
Expand Down Expand Up @@ -186,10 +178,9 @@ void main() {
expect(effect.controller.completed, true);
});

test('sequence of alternates', () async {
testWithFlameGame('sequence of alternates', (game) async {
EffectController controller() =>
EffectController(duration: 1, alternate: true);
final game = FlameGame()..onGameResize(Vector2.all(1000));
final effect = SequenceEffect(
[
MoveEffect.by(Vector2(1, 0), controller()),
Expand Down Expand Up @@ -222,7 +213,24 @@ void main() {
expect(effect.controller.completed, true);
});

test('sequence in sequence', () async {
testWithFlameGame('with SpeedEffectController', (game) async {
final effect = SequenceEffect([
MoveEffect.to(
Vector2(10, 0),
EffectController(speed: 10.0),
),
MoveEffect.by(
Vector2(10, 0),
EffectController(duration: 1.0),
),
]);
await game.ensureAdd(PositionComponent(children: [effect]));
game.update(0);
expect(effect.controller.duration, 2.0);
expect(effect.controller.completed, false);
});

testWithFlameGame('sequence in sequence', (game) async {
EffectController duration(double t) => EffectController(duration: t);
const dt = 0.01;
const x0 = 0.0, y0 = 0.0;
Expand All @@ -232,7 +240,6 @@ void main() {
const x4 = 10.0, y4 = 30.0;
const dx5 = 1.6, dy5 = 0.9;

final game = FlameGame()..onGameResize(Vector2.all(1000));
final effect = SequenceEffect(
[
MoveEffect.by(Vector2(x1 - x0, y1 - y0), duration(1)),
Expand Down