Files
flame/examples/lib/stories/effects/move_effect_example.dart
Lukas Klingsbo cd7a0bbb65 ShapeComponent and Hitbox to take transform of parents full ancestor tree into consideration (#1076)
* `ShapeComponent` changes size, position and angle of underlying Shape

* Added description to ShapeComponent

* Fix test

* Update packages/flame/lib/src/components/shape_component.dart

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Add absoluteScale and absoluteAngle to PositionComponent

* Refactor ShapeComponent

* Should be scaled by total scale, not scaled size

* Premature optimization for creation for objects in Polygon

* Use path for default Polygon constructor

* Do not sync component and hitbox shape

* Fix analyze issue

* Add example for flipping with collision detection

* Don't use absoluteScale

* Fix examples

* Fix examples

* Doesn't need super.render

* Fix Circle dartdoc

* Update changelog

* Update names of vertices caches in Polygon

* Update text docs

* Revert "Update text docs"

This reverts commit 73a68a465d76eb0eb50bb3753e57b2f4e3b5a7f4.

* Fix examples

* ShapeComponents docs

* Move example games to the top

* Fix dartdoc comment about polygon vertex relation

* Fix order of polygon vertices in dartdoc

* Fix anchor for PolygonComponent.fromPoints

* Add test with ancestors

* Update doc/components.md

Co-authored-by: Pasha Stetsenko <stpasha@google.com>

* Update doc/components.md

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Rename example classes

* Fix linting issues in examples

* Don't use px

* Use isTrue and isFalse

* Update doc/components.md

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Fixed comments on PR

Co-authored-by: Erick <erickzanardoo@gmail.com>
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
2021-11-13 16:00:24 +01:00

120 lines
3.3 KiB
Dart

import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/src/effects2/move_effect.dart'; // ignore: implementation_imports
import 'package:flame/src/effects2/standard_effect_controller.dart'; // ignore: implementation_imports
import 'package:flutter/material.dart';
import '../../commons/square_component.dart';
class MoveEffectExample extends FlameGame {
static const description = '''
Top square has `MoveEffect.to` effect that makes the component move along a
straight line back and forth. The effect uses a non-linear progression
curve, which makes the movement non-uniform.
The middle green square has a combination of two movement effects: a
`MoveEffect.to` and a `MoveEffect.by` which forces it to periodically jump.
At the bottom there are 60 more components which demonstrate movement along
an arbitrary path using `MoveEffect.along`.
''';
@override
void onMount() {
const tau = Transform2D.tau;
camera.viewport = FixedResolutionViewport(Vector2(400, 600));
final paint1 = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5.0
..color = const Color(0xFFFF6622);
final paint2 = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 5.0
..color = const Color(0xFF22FF66);
add(
SquareComponent(position: Vector2(20, 50), size: 20, paint: paint1)
..add(
MoveEffect.to(
Vector2(380, 50),
StandardEffectController(
duration: 3,
reverseDuration: 3,
infinite: true,
curve: Curves.easeOut,
),
),
),
);
add(
SquareComponent(position: Vector2(20, 150), size: 20, paint: paint2)
..add(
MoveEffect.to(
Vector2(380, 150),
StandardEffectController(
duration: 3,
reverseDuration: 3,
infinite: true,
),
),
)
..add(
MoveEffect.by(
Vector2(0, -50),
StandardEffectController(
duration: 0.25,
reverseDuration: 0.25,
startDelay: 1,
atMinDuration: 2,
curve: Curves.ease,
infinite: true,
),
),
),
);
final path1 = Path()..moveTo(200, 250);
for (var i = 1; i <= 5; i++) {
final x = 200 + 100 * sin(i * tau * 2 / 5);
final y = 350 - 100 * cos(i * tau * 2 / 5);
path1.lineTo(x, y);
}
for (var i = 0; i < 40; i++) {
add(
CircleComponent(radius: 5)
..add(
MoveEffect.along(
path1,
StandardEffectController(
duration: 10,
startDelay: i * 0.2,
infinite: true,
),
),
),
);
}
final path2 = Path()..addOval(const Rect.fromLTRB(80, 230, 320, 470));
for (var i = 0; i < 20; i++) {
add(
SquareComponent(size: 10)
..paint = (Paint()..color = const Color(0xFF00FFF7))
..add(
MoveEffect.along(
path2,
StandardEffectController(
duration: 6,
startDelay: i * 0.3,
infinite: true,
),
),
),
);
}
}
}