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

Effect controllers restructuring #1134

Merged
merged 59 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
9846325
Update effectController
st-pasha Nov 21, 2021
3d9ed0e
move effect controllers into the controllers/ directory
st-pasha Nov 21, 2021
ab20328
Add .forward property to EffectController
st-pasha Nov 21, 2021
b5a739e
SimpleEffectController supports reverse time
st-pasha Nov 21, 2021
ab4a59b
Fixing some compile errors
st-pasha Nov 21, 2021
3a00033
rename SimpleEffectController -> LinearEffectController
st-pasha Nov 21, 2021
634c18b
minor cleanup
st-pasha Nov 21, 2021
e1b8992
DurationEffectController and PauseEffectController
st-pasha Nov 22, 2021
540ef97
ReverseLinearEffectController
st-pasha Nov 22, 2021
9ebddbc
CurvedEffectController and its reverse
st-pasha Nov 22, 2021
515abf9
InfiniteEffectController
st-pasha Nov 22, 2021
662dd62
Added EffectController.recede()
st-pasha Nov 22, 2021
5669ccd
Add EffectController.update()
st-pasha Nov 22, 2021
9d6fe77
Add InfiniteEffectController'
st-pasha Nov 22, 2021
76a0b91
RepeatedEffectController
st-pasha Nov 22, 2021
05b591c
SequenceEffectController
st-pasha Nov 22, 2021
e54d396
DelayedEffectController
st-pasha Nov 22, 2021
c0dd3da
Merge branch 'main' into ps/effect-controllers
st-pasha Nov 22, 2021
346875c
Restore the [EffectController.started] property
st-pasha Nov 22, 2021
0550ddb
minor
st-pasha Nov 22, 2021
e67ae24
Rename reset() -> setToStart()
st-pasha Nov 23, 2021
65b27f9
time direction is now managed from the Effect class
st-pasha Nov 23, 2021
5ee69c9
StandardEffectController replaced with function standardController()
st-pasha Nov 23, 2021
0decab5
update some doc-comments
st-pasha Nov 23, 2021
e96c53e
flutter analyze
st-pasha Nov 23, 2021
6c8b867
flutter format
st-pasha Nov 23, 2021
7c7ec69
fix some tests
st-pasha Nov 23, 2021
14c736a
more test fixes
st-pasha Nov 23, 2021
bda35e2
fix remaining tests
st-pasha Nov 23, 2021
6c884ea
format
st-pasha Nov 23, 2021
1d0ef16
Merge branch 'main' into ps/effect-controllers
st-pasha Nov 23, 2021
60c85ae
rename local variable
st-pasha Nov 23, 2021
e926474
minor simplification
st-pasha Nov 23, 2021
59fa990
Expand docs in PauseEffectController
st-pasha Nov 23, 2021
8af256b
Merge branch 'main' into ps/effect-controllers
st-pasha Nov 24, 2021
cb6aeae
added tests
st-pasha Nov 24, 2021
cd83f53
Curved controller test
st-pasha Nov 26, 2021
376dd0e
Merge branch 'main' into ps/effect-controllers
st-pasha Nov 26, 2021
0619ffc
fix errors
st-pasha Nov 26, 2021
1c23de0
formatting
st-pasha Nov 26, 2021
6678b24
added more tests
st-pasha Nov 30, 2021
c5b3f8a
format
st-pasha Nov 30, 2021
9b95d4d
Merge branch 'main' into ps/effect-controllers
st-pasha Nov 30, 2021
d55b152
fix RepeatedEffectController
st-pasha Nov 30, 2021
892be64
more tests
st-pasha Dec 1, 2021
2fa2c68
format
st-pasha Dec 1, 2021
3aec0f9
changelog
st-pasha Dec 1, 2021
97d5a73
increase tolerance
st-pasha Dec 1, 2021
4401b52
Replaced standardController with factory EffectController constructor
st-pasha Dec 3, 2021
b7ddb2b
Added parameter EffectController({alternate=false})
st-pasha Dec 3, 2021
91097c0
Merge branch 'main' into ps/effect-controllers
st-pasha Dec 3, 2021
6052ef6
Added default for curve= parameter
st-pasha Dec 3, 2021
7cb7bd7
rename
st-pasha Dec 4, 2021
1c25728
rename tests
st-pasha Dec 4, 2021
8ef5fc2
Merge branch 'ttt' into ps/effect-controllers
st-pasha Dec 4, 2021
d8709f6
added more exports
st-pasha Dec 4, 2021
a6d1aff
rename tests
st-pasha Dec 4, 2021
663011b
rename src/effects2
st-pasha Dec 4, 2021
781c2fe
Merge branch 'main' into ps/effect-controllers
spydon Dec 4, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Rock extends SpriteComponent
add(
ScaleEffect.by(
Vector2.all(10),
StandardEffectController(duration: 0.3),
EffectController(duration: 0.3),
),
);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SimpleShapesExample extends FlameGame with HasTappables {
component.add(
MoveEffect.to(
size / 2,
StandardEffectController(
EffectController(
duration: 5,
reverseDuration: 5,
infinite: true,
Expand All @@ -75,7 +75,7 @@ class SimpleShapesExample extends FlameGame with HasTappables {
component.add(
RotateEffect.to(
3,
StandardEffectController(
EffectController(
duration: 1,
reverseDuration: 1,
infinite: true,
Expand Down
10 changes: 5 additions & 5 deletions examples/lib/stories/effects/move_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MoveEffectExample extends FlameGame {
)..add(
MoveEffect.to(
Vector2(380, 50),
StandardEffectController(
EffectController(
duration: 3,
reverseDuration: 3,
infinite: true,
Expand All @@ -58,7 +58,7 @@ class MoveEffectExample extends FlameGame {
..add(
MoveEffect.to(
Vector2(380, 150),
StandardEffectController(
EffectController(
duration: 3,
reverseDuration: 3,
infinite: true,
Expand All @@ -68,7 +68,7 @@ class MoveEffectExample extends FlameGame {
..add(
MoveEffect.by(
Vector2(0, -50),
StandardEffectController(
EffectController(
duration: 0.25,
reverseDuration: 0.25,
startDelay: 1,
Expand All @@ -92,7 +92,7 @@ class MoveEffectExample extends FlameGame {
..add(
MoveEffect.along(
path1,
StandardEffectController(
EffectController(
duration: 10,
startDelay: i * 0.2,
infinite: true,
Expand All @@ -110,7 +110,7 @@ class MoveEffectExample extends FlameGame {
..add(
MoveEffect.along(
path2,
StandardEffectController(
EffectController(
duration: 6,
startDelay: i * 0.3,
infinite: true,
Expand Down
7 changes: 3 additions & 4 deletions examples/lib/stories/effects/opacity_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';

import '../../commons/ember.dart';

class OpacityEffectExample extends FlameGame with TapDetector {
Expand Down Expand Up @@ -32,7 +31,7 @@ class OpacityEffectExample extends FlameGame with TapDetector {
size: Vector2.all(100),
)..add(
OpacityEffect.fadeOut(
StandardEffectController(
EffectController(
duration: 1.5,
reverseDuration: 1.5,
infinite: true,
Expand All @@ -46,9 +45,9 @@ class OpacityEffectExample extends FlameGame with TapDetector {
void onTap() {
final opacity = sprite.paint.color.opacity;
if (opacity >= 0.5) {
sprite.add(OpacityEffect.fadeOut(StandardEffectController(duration: 1)));
sprite.add(OpacityEffect.fadeOut(EffectController(duration: 1)));
} else {
sprite.add(OpacityEffect.fadeIn(StandardEffectController(duration: 1)));
sprite.add(OpacityEffect.fadeIn(EffectController(duration: 1)));
}
}
}
8 changes: 4 additions & 4 deletions examples/lib/stories/effects/rotate_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RotateEffectExample extends FlameGame {
compass.rim.add(
RotateEffect.by(
1.0,
StandardEffectController(
EffectController(
duration: 6,
reverseDuration: 3,
curve: Curves.ease,
Expand All @@ -37,7 +37,7 @@ class RotateEffectExample extends FlameGame {
..add(
RotateEffect.to(
Transform2D.tau,
StandardEffectController(
EffectController(
duration: 20,
infinite: true,
),
Expand All @@ -46,7 +46,7 @@ class RotateEffectExample extends FlameGame {
..add(
RotateEffect.by(
Transform2D.tau * 0.015,
StandardEffectController(
EffectController(
duration: 0.1,
reverseDuration: 0.1,
infinite: true,
Expand All @@ -56,7 +56,7 @@ class RotateEffectExample extends FlameGame {
..add(
RotateEffect.by(
Transform2D.tau * 0.021,
StandardEffectController(
EffectController(
duration: 0.13,
reverseDuration: 0.13,
infinite: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/stories/effects/scale_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ScaleEffectExample extends FlameGame with TapDetector {
square.add(
ScaleEffect.to(
Vector2.all(s),
StandardEffectController(
EffectController(
duration: 1.5,
curve: Curves.bounceInOut,
),
Expand Down
9 changes: 4 additions & 5 deletions examples/lib/stories/effects/size_effect_example.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';

class SizeEffectExample extends FlameGame with TapDetector {
static const String description = '''
Expand Down Expand Up @@ -42,10 +44,7 @@ class SizeEffectExample extends FlameGame with TapDetector {
square.add(
SizeEffect.to(
Vector2.all(s),
StandardEffectController(
duration: 1.5,
curve: Curves.bounceInOut,
),
EffectController(duration: 1.5, curve: Curves.bounceInOut),
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/lib/stories/input/joystick_advanced_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class JoystickAdvancedExample extends FlameGame
onPressed: () => player.add(
RotateEffect.by(
8 * rng.nextDouble(),
StandardEffectController(
EffectController(
duration: 1,
reverseDuration: 1,
curve: Curves.bounceOut,
Expand All @@ -131,7 +131,7 @@ class JoystickAdvancedExample extends FlameGame
onPressed: () => player.add(
ScaleEffect.by(
Vector2.all(1.5),
StandardEffectController(duration: 1.0, reverseDuration: 1.0),
EffectController(duration: 1.0, reverseDuration: 1.0),
),
),
);
Expand All @@ -152,7 +152,7 @@ class JoystickAdvancedExample extends FlameGame
size: Vector2(185, 50),
onPressed: () => player.add(
OpacityEffect.fadeOut(
StandardEffectController(duration: 0.5, reverseDuration: 0.5),
EffectController(duration: 0.5, reverseDuration: 0.5),
),
),
);
Expand Down
3 changes: 2 additions & 1 deletion packages/flame/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
## [Next]
- Add `ButtonComponent` backed by two `PositionComponent`s
- Add `SpriteButtonComponent` backed by two `Sprite`s
- Allow more flexible construction of `EffectController`s and make them able to run back in time
- Remove old effects system
- Export new effects system
- Introduce `updateTree` to follow the `renderTree` convention
- Fix `Parallax.load` with different loading times
- Fix render order of components and add tests
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
- `isHud` renamed to `respectCamera`

## [1.0.0-releasecandidate.18]
Expand All @@ -15,7 +17,6 @@
- Fixed position calculation in `HudMarginComponent` when using a viewport
- Add noClip option to `FixedResolutionViewport`
- Add a few missing helpers to SpriteAnimation
- Fix render order of components and add tests

## [1.0.0-releasecandidate.17]
- Added `StandardEffectController` class
Expand Down
14 changes: 11 additions & 3 deletions packages/flame/lib/effects.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
export 'src/effects/component_effect.dart';
export 'src/effects/controllers/curved_effect_controller.dart';
export 'src/effects/controllers/delayed_effect_controller.dart';
export 'src/effects/controllers/duration_effect_controller.dart';
export 'src/effects/controllers/effect_controller.dart';
export 'src/effects/controllers/infinite_effect_controller.dart';
export 'src/effects/controllers/linear_effect_controller.dart';
export 'src/effects/controllers/pause_effect_controller.dart';
export 'src/effects/controllers/repeated_effect_controller.dart';
export 'src/effects/controllers/reverse_curved_effect_controller.dart';
export 'src/effects/controllers/reverse_linear_effect_controller.dart';
export 'src/effects/controllers/sequence_effect_controller.dart';
export 'src/effects/effect.dart';
export 'src/effects/effect_controller.dart';
export 'src/effects/move_effect.dart';
export 'src/effects/opacity_effect.dart';
export 'src/effects/remove_effect.dart';
export 'src/effects/rotate_effect.dart';
export 'src/effects/scale_effect.dart';
export 'src/effects/simple_effect_controller.dart';
export 'src/effects/size_effect.dart';
export 'src/effects/standard_effect_controller.dart';
export 'src/effects/transform2d_effect.dart';
export 'src/extensions/vector2.dart';
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/component_effect.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/cupertino.dart';

import '../../components.dart';
import 'controllers/effect_controller.dart';
import 'effect.dart';
import 'effect_controller.dart';

/// Base class for effects that target a [Component] of type [T].
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/animation.dart';

import 'duration_effect_controller.dart';

/// A controller that grows non-linearly from 0 to 1 following the provided
/// [curve]. The [duration] cannot be 0.
class CurvedEffectController extends DurationEffectController {
CurvedEffectController(double duration, Curve curve)
: assert(duration > 0, 'Duration must be positive: $duration'),
_curve = curve,
super(duration);

Curve get curve => _curve;
final Curve _curve;

@override
double get progress => _curve.transform(timer / duration);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'effect_controller.dart';

/// An effect controller that waits for [delay] seconds before running the
/// child controller. While waiting, the progress will be reported at 0.
class DelayedEffectController extends EffectController {
DelayedEffectController(EffectController child, {required this.delay})
: assert(delay >= 0, 'Delay must be non-negative: $delay'),
_child = child,
_timer = 0,
super.empty();

final EffectController _child;
final double delay;
double _timer;

@override
bool get isInfinite => _child.isInfinite;

@override
bool get isRandom => _child.isRandom;

@override
bool get started => _timer == delay;

@override
bool get completed => started && _child.completed;

@override
double get progress => started ? _child.progress : 0;

@override
double? get duration {
final d = _child.duration;
return d == null ? null : d + delay;
}

@override
double advance(double dt) {
if (_timer == delay) {
return _child.advance(dt);
}
_timer += dt;
if (_timer > delay) {
final t = _child.advance(_timer - delay);
_timer = delay;
return t;
} else {
return 0;
}
}

@override
double recede(double dt) {
if (_timer == delay) {
_timer -= _child.recede(dt);
} else {
_timer -= dt;
}
if (_timer < 0) {
final leftoverTime = -_timer;
_timer = 0;
return leftoverTime;
}
return 0;
}

@override
void setToStart() {
_timer = 0;
}

@override
void setToEnd() {
_timer = delay;
_child.setToEnd();
}
}
Loading