Files
Lukas Klingsbo 9c1e1c3098 chore: Remove deprecations for v1.9.0 (#2749)
Removes everything that was marked as deprecated and that should be
removed for v1.9.0.
2023-09-19 16:47:41 +02:00

79 lines
2.0 KiB
Dart

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
class CirclesExample extends FlameGame with HasCollisionDetection, TapDetector {
static const description = '''
This example will create a circle every time you tap on the screen. It will
have the initial velocity towards the center of the screen and if it touches
another circle both of them will change color.
''';
@override
Future<void> onLoad() async {
add(ScreenHitbox());
}
@override
void onTapDown(TapDownInfo info) {
add(MyCollidable(info.eventPosition.game));
}
}
class MyCollidable extends PositionComponent
with HasGameReference<CirclesExample>, CollisionCallbacks {
late Vector2 velocity;
final _collisionColor = Colors.amber;
final _defaultColor = Colors.cyan;
late ShapeHitbox hitbox;
MyCollidable(Vector2 position)
: super(
position: position,
size: Vector2.all(100),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
final defaultPaint = Paint()
..color = _defaultColor
..style = PaintingStyle.stroke;
hitbox = CircleHitbox()
..paint = defaultPaint
..renderShape = true;
add(hitbox);
final center = game.size / 2;
velocity = (center - position)..scaleTo(150);
}
@override
void update(double dt) {
super.update(dt);
position.add(velocity * dt);
}
@override
void onCollisionStart(
Set<Vector2> intersectionPoints,
PositionComponent other,
) {
super.onCollisionStart(intersectionPoints, other);
hitbox.paint.color = _collisionColor;
if (other is ScreenHitbox) {
removeFromParent();
return;
}
}
@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(other);
if (!isColliding) {
hitbox.paint.color = _defaultColor;
}
}
}