mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 08:56:01 +08:00 
			
		
		
		
	feat: Add Transform2D.setFrom and Transform2D.clone (#1495)
				
					
				
			This commit is contained in:
		| @ -11,7 +11,7 @@ import 'package:flutter/material.dart'; | |||||||
| import 'joystick_player.dart'; | import 'joystick_player.dart'; | ||||||
|  |  | ||||||
| class JoystickAdvancedExample extends FlameGame | class JoystickAdvancedExample extends FlameGame | ||||||
|     with HasDraggables, HasTappables { |     with HasDraggables, HasTappables, HasCollisionDetection { | ||||||
|   static const String description = ''' |   static const String description = ''' | ||||||
|     In this example we showcase how to use the joystick by creating  |     In this example we showcase how to use the joystick by creating  | ||||||
|     `SpriteComponent`s that serve as the joystick's knob and background. |     `SpriteComponent`s that serve as the joystick's knob and background. | ||||||
| @ -35,6 +35,7 @@ class JoystickAdvancedExample extends FlameGame | |||||||
|       columns: 6, |       columns: 6, | ||||||
|       rows: 1, |       rows: 1, | ||||||
|     ); |     ); | ||||||
|  |     add(ScreenHitbox()); | ||||||
|     joystick = JoystickComponent( |     joystick = JoystickComponent( | ||||||
|       knob: SpriteComponent( |       knob: SpriteComponent( | ||||||
|         sprite: sheet.getSpriteById(1), |         sprite: sheet.getSpriteById(1), | ||||||
|  | |||||||
| @ -1,29 +1,45 @@ | |||||||
|  | import 'package:flame/collisions.dart'; | ||||||
| import 'package:flame/components.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 |   /// Pixels/s | ||||||
|   double maxSpeed = 300.0; |   double maxSpeed = 300.0; | ||||||
|  |   late final Vector2 _lastSize = size.clone(); | ||||||
|  |   late final Transform2D _lastTransform = transform.clone(); | ||||||
|  |  | ||||||
|   final JoystickComponent joystick; |   final JoystickComponent joystick; | ||||||
|  |  | ||||||
|   JoystickPlayer(this.joystick) |   JoystickPlayer(this.joystick) | ||||||
|       : super( |       : super(size: Vector2.all(100.0), anchor: Anchor.center); | ||||||
|           size: Vector2.all(100.0), |  | ||||||
|         ) { |  | ||||||
|     anchor = Anchor.center; |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Future<void> onLoad() async { |   Future<void> onLoad() async { | ||||||
|     sprite = await gameRef.loadSprite('layers/player.png'); |     sprite = await gameRef.loadSprite('layers/player.png'); | ||||||
|     position = gameRef.size / 2; |     position = gameRef.size / 2; | ||||||
|  |     add(RectangleHitbox()..debugMode = true); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void update(double dt) { |   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); |       position.add(joystick.relativeDelta * maxSpeed * dt); | ||||||
|       angle = joystick.delta.screenAngle(); |       angle = joystick.delta.screenAngle(); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @override | ||||||
|  |   void onCollisionStart(Set<Vector2> _, PositionComponent __) { | ||||||
|  |     super.onCollisionStart(_, __); | ||||||
|  |     transform.setFrom(_lastTransform); | ||||||
|  |     size.setFrom(_lastSize); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @override | ||||||
|  |   void onCollisionEnd(PositionComponent __) { | ||||||
|  |     super.onCollisionEnd(__); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| import 'dart:math' as math; | import 'dart:math' as math; | ||||||
|  |  | ||||||
| import 'package:flutter/foundation.dart'; | import 'package:flutter/foundation.dart'; | ||||||
| import 'package:vector_math/vector_math_64.dart'; | import 'package:vector_math/vector_math_64.dart'; | ||||||
|  |  | ||||||
| import 'notifying_vector2.dart'; | import 'notifying_vector2.dart'; | ||||||
|  |  | ||||||
| /// This class describes a generic 2D transform, which is a combination of | /// This class describes a generic 2D transform, which is a combination of | ||||||
| @ -53,6 +55,17 @@ class Transform2D extends ChangeNotifier { | |||||||
|     ..scale = other.scale |     ..scale = other.scale | ||||||
|     ..offset = other.offset; |     ..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 |   /// Check whether this transform is equal to [other], up to the given | ||||||
|   /// [tolerance]. Setting tolerance to zero will check for exact equality. |   /// [tolerance]. Setting tolerance to zero will check for exact equality. | ||||||
|   /// Transforms are considered equal if their rotation angles are the same |   /// Transforms are considered equal if their rotation angles are the same | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Lukas Klingsbo
					Lukas Klingsbo