Files
flame/examples/lib/stories/collision_detection/collidable_animation.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

124 lines
3.1 KiB
Dart

import 'dart:math';
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/geometry.dart';
import 'package:flame/input.dart';
import 'package:flame/palette.dart';
class CollidableAnimationExample extends FlameGame with HasCollidables {
static const description = '''
In this example you can see four animated birds which are flying straight
along the same route until they hit either another bird or the wall, which
makes them turn. The birds have PolygonHitboxes which are marked with the
green lines and dots.
''';
@override
Future<void> onLoad() async {
await super.onLoad();
add(ScreenCollidable());
// Top left component
add(
AnimatedComponent(Vector2.all(200), Vector2.all(100))..flipVertically(),
);
// Bottom right component
add(
AnimatedComponent(
Vector2(-100, -100),
size.clone()..sub(Vector2.all(200)),
),
);
// Bottom left component
add(
AnimatedComponent(
Vector2(100, -100),
Vector2(100, size.y - 100),
angle: pi / 4,
),
);
// Top right component
add(
AnimatedComponent(
Vector2(-300, 300),
Vector2(size.x - 100, 100),
angle: pi / 4,
)..flipVertically(),
);
}
}
class AnimatedComponent extends SpriteAnimationComponent
with HasHitboxes, Collidable, HasGameRef {
final Vector2 velocity;
final List<Collidable> activeCollisions = [];
AnimatedComponent(this.velocity, Vector2 position, {double angle = -pi / 4})
: super(
position: position,
size: Vector2(150, 100),
angle: angle,
anchor: Anchor.center,
);
late HitboxPolygon hitbox;
@override
Future<void> onLoad() async {
await super.onLoad();
animation = await gameRef.loadSpriteAnimation(
'bomb_ptero.png',
SpriteAnimationData.sequenced(
amount: 4,
stepTime: 0.2,
textureSize: Vector2.all(48),
),
);
hitbox = HitboxPolygon([
Vector2(0.0, -1.0),
Vector2(-1.0, -0.1),
Vector2(-0.2, 0.4),
Vector2(0.2, 0.4),
Vector2(1.0, -0.1),
]);
addHitbox(hitbox);
}
@override
void update(double dt) {
super.update(dt);
position += velocity * dt;
}
final Paint hitboxPaint = BasicPalette.green.paint()
..style = PaintingStyle.stroke;
final Paint dotPaint = BasicPalette.red.paint()..style = PaintingStyle.stroke;
@override
void render(Canvas canvas) {
super.render(canvas);
// This is just to clearly see the vertices in the hitboxes
hitbox.render(canvas, hitboxPaint);
hitbox
.localVertices()
.forEach((p) => canvas.drawCircle(p.toOffset(), 4, dotPaint));
}
@override
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (!activeCollisions.contains(other)) {
velocity.negate();
flipVertically();
activeCollisions.add(other);
}
}
@override
void onCollisionEnd(Collidable other) {
activeCollisions.remove(other);
}
}