mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 09:39:12 +08:00
Unify examples structure (#1118)
* Animations, CameraAndViewport, CollisionDetection and Components unified * Added descriptions to effects * Rename input games * Unify input stories * Add info to parallax section * Added descriptions to the rendering examples * Add descriptions to the sprites directory * Fix utils and rendering section * Add descriptions to the widgets section * Delete directory that rebase brought back * Unify game names * Added some styleguide docs for examples * Fix analyze issues * All files should have _example as suffix * Made the FollowComponentExample a bit easier to understand * Change priority of ember
This commit is contained in:
53
examples/lib/stories/input/mouse_movement_example.dart
Normal file
53
examples/lib/stories/input/mouse_movement_example.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class MouseMovementExample extends FlameGame with MouseMovementDetector {
|
||||
static const String description = '''
|
||||
In this example we show how you can use `MouseMovementDetector`.\n\n
|
||||
Move around the mouse on the canvas and the white square will follow it and
|
||||
turn into blue if it reaches the mouse, or the edge of the canvas.
|
||||
''';
|
||||
|
||||
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 info) {
|
||||
target = info.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user