mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 01:18:38 +08:00
VectorUtil -> Vector2Operations
This commit is contained in:
@ -118,12 +118,12 @@ Let's you have your `build` method in one of your pages; pretty normal Flutter s
|
||||
}
|
||||
```
|
||||
|
||||
Note that it could be any component, however complex, inside your widgets tree. Note also that I have omitted the "magic" of the equation here. How is it that we create a component for an animation? Very basically (more details in the flame tutorial), Flame provides components, one of which is the `SpriteAnimationComponent` that receives a `SpriteAnimation` object describing the animation and does exactly what we want. All components live inside a `Game` instance, that can add custom logic relating to the game loop. For our case, we just want to create a simple, empty game and add a single `AnimationComponent` with a simple `Animation` inside. So Flame provides a helper to do that, the `Flame.util.animationAsWidget` method. It takes the size of the object as a Flame's `Position` instance (a generic class to represent a pair of doubles), and also takes in an `Animation` instance representing our frame list. To use that, let's import both `Flame` and the `Animation` class. However, since Flutter adds it's own animation classes, let's use an alias in order to not mess up the names. Therefore, add these imports to the top of the file:
|
||||
Note that it could be any component, however complex, inside your widgets tree. Note also that I have omitted the "magic" of the equation here. How do we create a component for an animation? Very basically (more details in the flame tutorial), Flame provides components, one of which is the `SpriteAnimationComponent` that receives a `SpriteAnimation` object describing the animation and does exactly what we want. All components live inside a `Game` instance, that can add custom logic relating to the game loop. For our case, we just want to create a simple, empty game and add a single `AnimationComponent` with a simple `Animation` inside. So Flame provides a helper to do that, the `Flame.util.animationAsWidget` method. It takes the size of the object as a Flame's `Position` instance (a generic class to represent a pair of doubles), and also takes in an `Animation` instance representing our frame list. To use that, let's import both `Flame` and the `Animation` class. However, since Flutter adds it's own animation classes, let's use an alias in order to not mess up the names. Therefore, add these imports to the top of the file:
|
||||
|
||||
```dart
|
||||
import 'package:flame/sprite_animation.dart'; // imports the SpriteAnimation class
|
||||
import 'package:flame/flame.dart'; // imports the Flame helper class
|
||||
import 'package:flame/vector.dart'; // imports the Vector2 class
|
||||
import 'package:flame/vector2.dart'; // imports the Vector2 class
|
||||
```
|
||||
|
||||
How do we do the magic then? Just add the following to your widget tree:
|
||||
|
||||
@ -2,7 +2,7 @@ import 'package:flame/gestures.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/sprite_animation.dart';
|
||||
import 'package:flame/components/sprite_animation_component.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/sprite_animation.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/components/sprite_animation_component.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ import 'package:flame/game.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/components/sprite_component.dart';
|
||||
import 'package:flame/components/mixins/resizable.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
|
||||
@ -3,7 +3,7 @@ import 'package:flame/effects/move_effect.dart';
|
||||
import 'package:flame/effects/scale_effect.dart';
|
||||
import 'package:flame/effects/rotate_effect.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@ -2,7 +2,7 @@ import 'package:flame/effects/move_effect.dart';
|
||||
import 'package:flame/effects/scale_effect.dart';
|
||||
import 'package:flame/effects/rotate_effect.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@ -3,7 +3,7 @@ import 'package:flame/effects/scale_effect.dart';
|
||||
import 'package:flame/effects/rotate_effect.dart';
|
||||
import 'package:flame/effects/sequence_effect.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/effects/effects.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
|
||||
import './square.dart';
|
||||
|
||||
@ -17,7 +17,7 @@ class MyGame extends BaseGame with TapDetector {
|
||||
@override
|
||||
void onTapUp(details) {
|
||||
square.addEffect(MoveEffect(
|
||||
destination: VectorUtil.fromOffset(details.localPosition),
|
||||
destination: Vector2Operations.fromOffset(details.localPosition),
|
||||
speed: 250.0,
|
||||
curve: Curves.bounceInOut,
|
||||
));
|
||||
|
||||
@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/anchor.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/effects/effects.dart';
|
||||
|
||||
import './square.dart';
|
||||
|
||||
@ -5,7 +5,7 @@ import 'package:flame/components/component.dart';
|
||||
import 'package:flame/components/joystick/joystick_component.dart';
|
||||
import 'package:flame/components/joystick/joystick_events.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
|
||||
class Player extends Component implements JoystickListener {
|
||||
final _whitePaint = BasicPalette.white.paint;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/components/parallax_component.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
|
||||
@ -21,7 +21,7 @@ import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/time.dart' as flame_time;
|
||||
import 'package:flame/particle.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flame/spritesheet.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
@ -108,7 +108,7 @@ class MyGame extends BaseGame {
|
||||
// lifecycle from the [BaseGame].
|
||||
TranslatedParticle(
|
||||
lifespan: 1,
|
||||
offset: VectorUtil.toOffset(cellCenter),
|
||||
offset: Vector2Operations.toOffset(cellCenter),
|
||||
child: particle,
|
||||
).asComponent(),
|
||||
);
|
||||
@ -405,8 +405,8 @@ class MyGame extends BaseGame {
|
||||
/// which is independent from the parent [Particle].
|
||||
Particle componentParticle() {
|
||||
return MovingParticle(
|
||||
from: VectorUtil.toOffset(-halfCellSize * .2),
|
||||
to: VectorUtil.toOffset(halfCellSize * .2),
|
||||
from: Vector2Operations.toOffset(-halfCellSize * .2),
|
||||
to: Vector2Operations.toOffset(halfCellSize * .2),
|
||||
curve: SineCurve(),
|
||||
child: ComponentParticle(component: trafficLight),
|
||||
);
|
||||
@ -473,8 +473,8 @@ class MyGame extends BaseGame {
|
||||
),
|
||||
);
|
||||
|
||||
final cellSizeOffset = VectorUtil.toOffset(cellSize);
|
||||
final halfCellSizeOffset = VectorUtil.toOffset(halfCellSize);
|
||||
final cellSizeOffset = Vector2Operations.toOffset(cellSize);
|
||||
final halfCellSizeOffset = Vector2Operations.toOffset(halfCellSize);
|
||||
|
||||
return ComposedParticle(children: <Particle>[
|
||||
rect
|
||||
|
||||
@ -2,7 +2,7 @@ import 'package:flame/sprite_animation.dart';
|
||||
import 'package:flame/components/sprite_animation_component.dart';
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flame/components/position_component.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ import 'package:flame/flame.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() async {
|
||||
|
||||
@ -60,7 +60,7 @@ import 'dart:ui';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/time.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
|
||||
class MyGame extends Game {
|
||||
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||
@ -95,7 +95,7 @@ import 'dart:ui';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/text_config.dart';
|
||||
import 'package:flame/time.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
|
||||
class MyGame extends Game {
|
||||
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||
|
||||
@ -6,11 +6,11 @@ import 'package:flame/components/joystick/joystick_component.dart';
|
||||
import 'package:flame/components/joystick/joystick_events.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../vector.dart';
|
||||
import '../../vector2.dart';
|
||||
|
||||
enum JoystickActionAlign { TOP_LEFT, BOTTOM_LEFT, TOP_RIGHT, BOTTOM_RIGHT }
|
||||
|
||||
@ -156,8 +156,8 @@ class JoystickAction {
|
||||
|
||||
// Distance between the center of joystick background & drag position
|
||||
final centerPosition =
|
||||
VectorUtil.fromOffset(_rectBackgroundDirection.center);
|
||||
final dragPosition = VectorUtil.fromOffset(_dragPosition);
|
||||
Vector2Operations.fromOffset(_rectBackgroundDirection.center);
|
||||
final dragPosition = Vector2Operations.fromOffset(_dragPosition);
|
||||
double dist = centerPosition.distanceTo(dragPosition);
|
||||
|
||||
// The maximum distance for the knob position to the edge of
|
||||
|
||||
@ -7,7 +7,7 @@ import 'package:flame/components/joystick/joystick_events.dart';
|
||||
import 'package:flame/components/mixins/has_game_ref.dart';
|
||||
import 'package:flame/game/base_game.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
|
||||
mixin JoystickListener {
|
||||
void joystickChangeDirectional(JoystickDirectionalEvent event);
|
||||
|
||||
@ -6,7 +6,7 @@ import 'package:flame/gestures.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../vector.dart';
|
||||
import '../../vector2.dart';
|
||||
|
||||
class JoystickDirectional {
|
||||
final double size;
|
||||
@ -117,8 +117,8 @@ class JoystickDirectional {
|
||||
final double degrees = _radAngle * 180 / pi;
|
||||
|
||||
// Distance between the center of joystick background & drag position
|
||||
final centerPosition = VectorUtil.fromOffset(_backgroundRect.center);
|
||||
final dragPosition = VectorUtil.fromOffset(_dragPosition);
|
||||
final centerPosition = Vector2Operations.fromOffset(_backgroundRect.center);
|
||||
final dragPosition = Vector2Operations.fromOffset(_dragPosition);
|
||||
double dist = centerPosition.distanceTo(dragPosition);
|
||||
|
||||
// The maximum distance for the knob position the edge of
|
||||
|
||||
@ -9,7 +9,7 @@ import 'package:flutter/services.dart';
|
||||
|
||||
import '../keyboard.dart';
|
||||
|
||||
import '../vector.dart';
|
||||
import '../vector2.dart';
|
||||
import 'widget_builder.dart';
|
||||
|
||||
/// Represents a generic game.
|
||||
|
||||
@ -5,7 +5,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/widgets.dart' hide WidgetBuilder;
|
||||
|
||||
import '../vector.dart';
|
||||
import '../vector2.dart';
|
||||
import 'game_loop.dart';
|
||||
import 'game.dart';
|
||||
|
||||
@ -24,7 +24,7 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver {
|
||||
@override
|
||||
void performResize() {
|
||||
super.performResize();
|
||||
game.resize(VectorUtil.fromSize(constraints.biggest));
|
||||
game.resize(Vector2Operations.fromSize(constraints.biggest));
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -3,7 +3,7 @@ import 'dart:ui';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../particle.dart';
|
||||
import '../vector.dart';
|
||||
import '../vector2.dart';
|
||||
|
||||
/// A [Particle] which renders given [Image] on a [Canvas]
|
||||
/// image is centered. If any other behavior is needed, consider
|
||||
|
||||
@ -6,7 +6,7 @@ import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import 'flame.dart';
|
||||
import 'palette.dart';
|
||||
import 'vector.dart';
|
||||
import 'vector2.dart';
|
||||
|
||||
class Sprite {
|
||||
Paint paint = BasicPalette.white.paint;
|
||||
@ -95,7 +95,7 @@ class Sprite {
|
||||
return;
|
||||
}
|
||||
size ??= this.size;
|
||||
renderRect(canvas, VectorUtil.rectFrom(p, size),
|
||||
renderRect(canvas, Vector2Operations.rectFrom(p, size),
|
||||
overridePaint: overridePaint);
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ import 'dart:ui';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'flame.dart';
|
||||
import 'vector.dart';
|
||||
import 'vector2.dart';
|
||||
|
||||
/// sprite atlas with an image and a set of rects and transforms
|
||||
class SpriteBatch {
|
||||
|
||||
@ -5,7 +5,7 @@ import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import 'anchor.dart';
|
||||
import 'memory_cache.dart';
|
||||
import 'vector.dart';
|
||||
import 'vector2.dart';
|
||||
|
||||
/// A Text Config contains all typographical information required to render texts; i.e., font size and color, family, etc.
|
||||
///
|
||||
@ -81,8 +81,8 @@ class TextConfig {
|
||||
{Anchor anchor = Anchor.topLeft}) {
|
||||
final material.TextPainter tp = toTextPainter(text);
|
||||
final Vector2 translatedPosition =
|
||||
anchor.translate(p, VectorUtil.fromSize(tp.size));
|
||||
tp.paint(canvas, VectorUtil.toOffset(translatedPosition));
|
||||
anchor.translate(p, Vector2Operations.fromSize(tp.size));
|
||||
tp.paint(canvas, Vector2Operations.toOffset(translatedPosition));
|
||||
}
|
||||
|
||||
/// Returns a [material.TextPainter] that allows for text rendering and size measuring.
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@ -105,13 +105,13 @@ class Util {
|
||||
final completer = Completer<Vector2>();
|
||||
window.onMetricsChanged = () {
|
||||
if (!window.physicalSize.isEmpty && !completer.isCompleted) {
|
||||
completer.complete(VectorUtil.fromSize(
|
||||
completer.complete(Vector2Operations.fromSize(
|
||||
window.physicalSize / window.devicePixelRatio));
|
||||
}
|
||||
};
|
||||
return completer.future;
|
||||
}
|
||||
return VectorUtil.fromSize(window.physicalSize / window.devicePixelRatio);
|
||||
return Vector2Operations.fromSize(window.physicalSize / window.devicePixelRatio);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import 'dart:math';
|
||||
import 'dart:ui';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
class VectorUtil {
|
||||
class Vector2Operations {
|
||||
/// Creates converting integers to double.
|
||||
///
|
||||
/// Internal representation is still using double, the conversion is made in the constructor only.
|
||||
@ -2,7 +2,7 @@ import 'dart:ui';
|
||||
|
||||
import 'package:flame/components/position_component.dart';
|
||||
import 'package:flame/components/sprite_component.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
@ -5,7 +5,7 @@ import 'package:flame/components/mixins/has_game_ref.dart';
|
||||
import 'package:flame/components/mixins/resizable.dart';
|
||||
import 'package:flame/components/mixins/tapable.dart';
|
||||
import 'package:flame/game/base_game.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import 'package:flame/game/base_game.dart';
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:flame/components/position_component.dart';
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import 'package:flame/vector.dart';
|
||||
import 'package:flame/vector2.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'dart:math' as math;
|
||||
|
||||
@ -25,7 +25,7 @@ void main() {
|
||||
|
||||
test('test rotate', () {
|
||||
final Vector2 p = Vector2(1.0, 0.0);
|
||||
VectorUtil.rotate(p, math.pi / 2);
|
||||
Vector2Operations.rotate(p, math.pi / 2);
|
||||
expectDouble(p.x, 0.0);
|
||||
expectDouble(p.y, 1.0);
|
||||
});
|
||||
@ -67,12 +67,12 @@ void main() {
|
||||
});
|
||||
|
||||
test('scaleTo', () {
|
||||
final Vector2 p = VectorUtil.rotated(Vector2(1.0, 0.0), math.pi / 4);
|
||||
VectorUtil.scaleTo(p, 2.0);
|
||||
final Vector2 p = Vector2Operations.rotated(Vector2(1.0, 0.0), math.pi / 4);
|
||||
Vector2Operations.scaleTo(p, 2.0);
|
||||
|
||||
expect(p.length, 2.0);
|
||||
|
||||
VectorUtil.rotate(p, -math.pi / 4);
|
||||
Vector2Operations.rotate(p, -math.pi / 4);
|
||||
expect(p.length, 2.0);
|
||||
expect(p.x, 2.0);
|
||||
expect(p.y, 0.0);
|
||||
|
||||
Reference in New Issue
Block a user