Files
flame/examples/lib/stories/controls/mouse_movement.dart
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

48 lines
1.1 KiB
Dart

import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
class MouseMovementGame extends BaseGame with MouseMovementDetector {
static const speed = 200;
static final Paint _blue = BasicPalette.blue.paint();
static final Paint _white = BasicPalette.white.paint();
static final Vector2 objSize = Vector2.all(50);
Vector2 position = Vector2(0, 0);
Vector2? target;
bool onTarget = false;
@override
void onMouseMove(PointerHoverInfo event) {
target = event.eventPosition.game;
}
Rect _toRect() => position.toPositionedRect(objSize);
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(
_toRect(),
onTarget ? _blue : _white,
);
}
@override
void update(double dt) {
final target = this.target;
super.update(dt);
if (target != null) {
onTarget = _toRect().contains(target.toOffset());
if (!onTarget) {
final dir = (target - position).normalized();
position += dir * (speed * dt);
}
}
}
}