From 295468f03cde1c02ec9b27b52f387b45edfe54d2 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 27 Mar 2022 23:48:26 +0200 Subject: [PATCH] feat: Add `Transform2D.setFrom` and `Transform2D.clone` (#1495) --- .../input/joystick_advanced_example.dart | 3 +- .../lib/stories/input/joystick_player.dart | 30 ++++++++++++++----- packages/flame/lib/src/game/transform2d.dart | 13 ++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/examples/lib/stories/input/joystick_advanced_example.dart b/examples/lib/stories/input/joystick_advanced_example.dart index f57ace51c..48f48e64b 100644 --- a/examples/lib/stories/input/joystick_advanced_example.dart +++ b/examples/lib/stories/input/joystick_advanced_example.dart @@ -11,7 +11,7 @@ import 'package:flutter/material.dart'; import 'joystick_player.dart'; class JoystickAdvancedExample extends FlameGame - with HasDraggables, HasTappables { + with HasDraggables, HasTappables, HasCollisionDetection { static const String description = ''' In this example we showcase how to use the joystick by creating `SpriteComponent`s that serve as the joystick's knob and background. @@ -35,6 +35,7 @@ class JoystickAdvancedExample extends FlameGame columns: 6, rows: 1, ); + add(ScreenHitbox()); joystick = JoystickComponent( knob: SpriteComponent( sprite: sheet.getSpriteById(1), diff --git a/examples/lib/stories/input/joystick_player.dart b/examples/lib/stories/input/joystick_player.dart index becb7022e..d41f222d1 100644 --- a/examples/lib/stories/input/joystick_player.dart +++ b/examples/lib/stories/input/joystick_player.dart @@ -1,29 +1,45 @@ +import 'package:flame/collisions.dart'; import 'package:flame/components.dart'; +import 'package:flame/game.dart'; -class JoystickPlayer extends SpriteComponent with HasGameRef { +class JoystickPlayer extends SpriteComponent + with HasGameRef, CollisionCallbacks { /// Pixels/s double maxSpeed = 300.0; + late final Vector2 _lastSize = size.clone(); + late final Transform2D _lastTransform = transform.clone(); final JoystickComponent joystick; JoystickPlayer(this.joystick) - : super( - size: Vector2.all(100.0), - ) { - anchor = Anchor.center; - } + : super(size: Vector2.all(100.0), anchor: Anchor.center); @override Future onLoad() async { sprite = await gameRef.loadSprite('layers/player.png'); position = gameRef.size / 2; + add(RectangleHitbox()..debugMode = true); } @override void update(double dt) { - if (!joystick.delta.isZero()) { + if (!joystick.delta.isZero() && activeCollisions.isEmpty) { + _lastSize.setFrom(size); + _lastTransform.setFrom(transform); position.add(joystick.relativeDelta * maxSpeed * dt); angle = joystick.delta.screenAngle(); } } + + @override + void onCollisionStart(Set _, PositionComponent __) { + super.onCollisionStart(_, __); + transform.setFrom(_lastTransform); + size.setFrom(_lastSize); + } + + @override + void onCollisionEnd(PositionComponent __) { + super.onCollisionEnd(__); + } } diff --git a/packages/flame/lib/src/game/transform2d.dart b/packages/flame/lib/src/game/transform2d.dart index 7cd9abfa5..5ae78313f 100644 --- a/packages/flame/lib/src/game/transform2d.dart +++ b/packages/flame/lib/src/game/transform2d.dart @@ -1,6 +1,8 @@ import 'dart:math' as math; + import 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; + import 'notifying_vector2.dart'; /// This class describes a generic 2D transform, which is a combination of @@ -53,6 +55,17 @@ class Transform2D extends ChangeNotifier { ..scale = other.scale ..offset = other.offset; + /// Clone of this. + Transform2D clone() => Transform2D.copy(this); + + /// Set this to the values of the [other] [Transform2D]. + void setFrom(Transform2D other) { + angle = other.angle; + position = other.position; + scale = other.scale; + offset = other.offset; + } + /// Check whether this transform is equal to [other], up to the given /// [tolerance]. Setting tolerance to zero will check for exact equality. /// Transforms are considered equal if their rotation angles are the same