mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
* 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>
48 lines
1.1 KiB
Dart
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);
|
|
}
|
|
}
|
|
}
|
|
}
|