Files
flame/examples/lib/stories/collision_detection/multiple_shapes_example.dart
Lukas Klingsbo b5bdf4ec17 feat!: The HasTappableComponents mixin is no longer needed (#2450)
This PR is the second in a series of refactors that aim to simplify event handling in Flame. The approach is as follows:

    Added the MultiTapDispatcher component, which contains the logic that used to be within the HasTappableComponents mixin. This component is internal; it mounts to a FlameGame directly, and ensures that it is a singleton.
    Whenever any TapCallbacks component is added to a game, it automatically adds the MultiTapDispatcher component (unless there is already one), which in turn registers a tap gesture detector with GestureDetectorBuilder and rebuilds the game widget.

The end result is that now in order to make a component tappable you only need to add the TapCallbacks mixin to that component, everything else will be handled by the framework.

Consequently, the HasTappableComponents mixin is now empty and marked as deprecated.
2023-04-02 16:52:57 +00:00

299 lines
7.8 KiB
Dart

import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
enum Shapes { circle, rectangle, polygon }
class MultipleShapesExample extends FlameGame with HasCollisionDetection {
static const description = '''
An example with many hitboxes that move around on the screen and during
collisions they change color depending on what it is that they have collided
with.
The snowman, the component built with three circles on top of each other,
works a little bit differently than the other components to show that you
can have multiple hitboxes within one component.
On this example, you can "throw" the components by dragging them quickly in
any direction.
''';
@override
Future<void> onLoad() async {
add(FpsTextComponent(position: Vector2(0, size.y - 24)));
final screenHitbox = ScreenHitbox();
final snowman = CollidableSnowman(
Vector2.all(150),
Vector2(120, 250),
Vector2(-100, 100),
screenHitbox,
);
MyCollidable lastToAdd = snowman;
add(screenHitbox);
add(snowman);
var totalAdded = 1;
while (totalAdded < 1000) {
lastToAdd = nextRandomCollidable(lastToAdd, screenHitbox);
final lastBottomRight = lastToAdd.toAbsoluteRect().bottomRight;
if (lastBottomRight.dx < size.x && lastBottomRight.dy < size.y) {
add(lastToAdd);
totalAdded++;
} else {
break;
}
}
}
final _rng = Random();
final _distance = Vector2(100, 0);
MyCollidable nextRandomCollidable(
MyCollidable lastCollidable,
ScreenHitbox screenHitbox,
) {
final collidableSize = Vector2.all(50) + Vector2.random(_rng) * 100;
final isXOverflow = lastCollidable.position.x +
lastCollidable.size.x / 2 +
_distance.x +
collidableSize.x >
size.x;
var position = _distance + Vector2(0, lastCollidable.position.y + 200);
if (!isXOverflow) {
position = (lastCollidable.position + _distance)
..x += collidableSize.x / 2;
}
final velocity = (Vector2.random(_rng) - Vector2.random(_rng)) * 400;
return randomCollidable(
position,
collidableSize,
velocity,
screenHitbox,
rng: _rng,
);
}
}
abstract class MyCollidable extends PositionComponent
with DragCallbacks, CollisionCallbacks, GestureHitboxes {
double rotationSpeed = 0.0;
final Vector2 velocity;
final delta = Vector2.zero();
double angleDelta = 0;
final Color _defaultColor = Colors.blue.withOpacity(0.8);
final Color _collisionColor = Colors.green.withOpacity(0.8);
late final Paint _dragIndicatorPaint;
final ScreenHitbox screenHitbox;
ShapeHitbox? hitbox;
bool isDragged = false;
MyCollidable(
Vector2 position,
Vector2 size,
this.velocity,
this.screenHitbox,
) : super(position: position, size: size, anchor: Anchor.center) {
_dragIndicatorPaint = BasicPalette.white.paint();
}
@override
void onMount() {
hitbox?.paint.color = _defaultColor;
super.onMount();
}
@override
void update(double dt) {
super.update(dt);
if (isDragged) {
return;
}
delta.setFrom(velocity * dt);
position.add(delta);
angleDelta = dt * rotationSpeed;
angle = (angle + angleDelta) % (2 * pi);
// Takes rotation into consideration (which topLeftPosition doesn't)
final topLeft = absoluteCenter - (scaledSize / 2);
if (topLeft.x + scaledSize.x < 0 ||
topLeft.y + scaledSize.y < 0 ||
topLeft.x > screenHitbox.scaledSize.x ||
topLeft.y > screenHitbox.scaledSize.y) {
final moduloSize = screenHitbox.scaledSize + scaledSize;
topLeftPosition = topLeftPosition % moduloSize;
}
}
@override
void render(Canvas canvas) {
if (isDragged) {
final localCenter = scaledSize.toOffset() / 2;
canvas.drawCircle(localCenter, 5, _dragIndicatorPaint);
}
}
@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
PositionComponent other,
) {
super.onCollisionStart(intersectionPoints, other);
hitbox?.paint.color = _collisionColor;
}
@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(other);
if (!isColliding) {
hitbox?.paint.color = _defaultColor;
}
}
@override
void onDragStart(DragStartEvent info) {
isDragged = true;
}
@override
void onDragEnd(DragEndEvent info) {
velocity.setFrom(info.velocity / 10);
isDragged = false;
}
}
class CollidablePolygon extends MyCollidable {
CollidablePolygon(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox,
) : super(position, size, velocity, screenHitbox) {
hitbox = PolygonHitbox.relative(
[
Vector2(-1.0, 0.0),
Vector2(-0.8, 0.6),
Vector2(0.0, 1.0),
Vector2(0.6, 0.9),
Vector2(1.0, 0.0),
Vector2(0.6, -0.8),
Vector2(0, -1.0),
Vector2(-0.8, -0.8),
],
parentSize: size,
)..renderShape = true;
add(hitbox!);
}
}
class CollidableRectangle extends MyCollidable {
CollidableRectangle(
super.position,
super.size,
super.velocity,
super.screenHitbox,
) {
hitbox = RectangleHitbox()..renderShape = true;
add(hitbox!);
}
}
class CollidableCircle extends MyCollidable {
CollidableCircle(
super.position,
super.size,
super.velocity,
super.screenHitbox,
) {
hitbox = CircleHitbox()..renderShape = true;
add(hitbox!);
}
}
class SnowmanPart extends CircleHitbox {
@override
final renderShape = true;
final startColor = Colors.white.withOpacity(0.8);
final Color hitColor;
SnowmanPart(double radius, Vector2 position, this.hitColor)
: super(radius: radius, position: position, anchor: Anchor.center) {
paint.color = startColor;
}
@override
void onCollisionStart(Set<Vector2> intersectionPoints, ShapeHitbox other) {
super.onCollisionStart(intersectionPoints, other);
if (other.hitboxParent is ScreenHitbox) {
paint.color = startColor;
} else {
paint.color = hitColor.withOpacity(0.8);
}
}
@override
void onCollisionEnd(ShapeHitbox other) {
super.onCollisionEnd(other);
if (!isColliding) {
paint.color = startColor;
}
}
}
class CollidableSnowman extends MyCollidable {
CollidableSnowman(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox,
) : super(position, size, velocity, screenHitbox) {
rotationSpeed = 0.3;
anchor = Anchor.topLeft;
final top = SnowmanPart(
size.x * 0.3,
Vector2(size.x / 2, size.y * 0.15),
Colors.red,
);
final middle = SnowmanPart(
size.x * 0.4,
Vector2(size.x / 2, size.y * 0.40),
Colors.yellow,
);
final bottom = SnowmanPart(
size.x / 2,
Vector2(size.x / 2, size.y - size.y / 4),
Colors.green,
);
add(bottom);
add(middle);
add(top);
}
}
MyCollidable randomCollidable(
Vector2 position,
Vector2 size,
Vector2 velocity,
ScreenHitbox screenHitbox, {
Random? rng,
}) {
final _rng = rng ?? Random();
final rotationSpeed = 0.5 - _rng.nextDouble();
final shapeType = Shapes.values[_rng.nextInt(Shapes.values.length)];
switch (shapeType) {
case Shapes.circle:
return CollidableCircle(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
case Shapes.rectangle:
return CollidableRectangle(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
case Shapes.polygon:
return CollidablePolygon(position, size, velocity, screenHitbox)
..rotationSpeed = rotationSpeed;
}
}