Files
Erick 6b2f8032fe Making Flame events aware of camera (#755)
* some initial refactoring

* Fixing examples and joystick component

* Fixing examples

* Addressing comments from PR

* Fixing tutorials

* Updating docs

* Fixing tests

* PR followup

* A big follow up

* linting

* doc nit

* Changelog and fix tutorial

* Addressing comments

* Fixing BaseGame project and scale offset methods

* Formatting

* doc suggestions

* Update packages/flame/lib/src/gestures/events.dart

Co-authored-by: Luan Nico <luanpotter27@gmail.com>

* Hopefully, the last follow up

* Linting and adding dart-code-metrics again

* fixing tutorial

Co-authored-by: Luan Nico <luanpotter27@gmail.com>
2021-04-15 15:28:46 -03:00

72 lines
1.6 KiB
Dart

import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
class MyCollidable extends PositionComponent
with HasGameRef<Circles>, Hitbox, 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,
) {
addShape(HitboxCircle());
}
@override
Future<void> onLoad() async {
final center = gameRef.size / 2;
velocity = (center - position)..scaleTo(150);
}
@override
void update(double dt) {
super.update(dt);
if (_isWallHit) {
remove();
return;
}
debugColor = _isCollision ? _collisionColor : _defaultColor;
position.add(velocity * dt);
_isCollision = false;
}
@override
void render(Canvas canvas) {
super.render(canvas);
renderShapes(canvas);
}
@override
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
return;
}
_isCollision = true;
}
}
class Circles extends BaseGame with HasCollidables, TapDetector {
@override
Future<void> onLoad() async {
add(ScreenCollidable());
}
@override
void onTapDown(TapDownInfo event) {
add(MyCollidable(event.eventPosition.game));
}
}