Files
Lukas Klingsbo 5c47d7f6d7 chore: analyze issues introduced from new dart version (#1196)
* Added Component.childrenFactory

* fix some of the lint warnings

* more lint warnings

* remove changelog entry

* more analyzer warnings

* one more warning

* one more warning

* remove more unused imports

* fix more warnings

* another warning

* one more warning

* a lot more warnings

* some more warnings

* fix warnings in flame_svg

* fix warnings in flame_bloc

* Remove OrderedSet override feature

* Remove testRandom change

* Remove unnecessary type checks

* Re-remove deprecated argument in random_test

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
2021-12-09 15:40:43 +01:00

75 lines
1.9 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
class CirclesExample extends FlameGame with HasCollidables, 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 {
super.onLoad();
add(ScreenCollidable());
}
@override
void onTapDown(TapDownInfo info) {
add(MyCollidable(info.eventPosition.game));
}
}
class MyCollidable extends PositionComponent
with HasGameRef<CirclesExample>, HasHitboxes, Collidable {
late Vector2 velocity;
final _collisionColor = Colors.amber;
final _defaultColor = Colors.cyan;
bool _isWallHit = false;
bool _isCollision = false;
MyCollidable(Vector2 position)
: super(
position: position,
size: Vector2.all(100),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
await super.onLoad();
addHitbox(HitboxCircle());
final center = gameRef.size / 2;
velocity = (center - position)..scaleTo(150);
}
@override
void update(double dt) {
if (_isWallHit) {
removeFromParent();
return;
}
debugColor = _isCollision ? _collisionColor : _defaultColor;
position.add(velocity * dt);
_isCollision = false;
}
@override
void render(Canvas canvas) {
renderHitboxes(canvas);
}
@override
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
return;
}
_isCollision = true;
}
}