mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +08:00 
			
		
		
		
	Make gameRef late (#742)
This commit is contained in:
		| @ -1,7 +1,7 @@ | |||||||
| import 'package:flame/components.dart'; | import 'package:flame/components.dart'; | ||||||
| import 'package:flame/game.dart'; | import 'package:flame/game.dart'; | ||||||
|  |  | ||||||
| class Square extends PositionComponent with HasGameRef<Composability> { | class Square extends PositionComponent { | ||||||
|   Square(Vector2 position, Vector2 size, {double angle = 0}) { |   Square(Vector2 position, Vector2 size, {double angle = 0}) { | ||||||
|     this.position.setFrom(position); |     this.position.setFrom(position); | ||||||
|     this.size.setFrom(size); |     this.size.setFrom(size); | ||||||
|  | |||||||
| @ -20,14 +20,14 @@ class LogoCompomnent extends SpriteComponent with HasGameRef<DebugGame> { | |||||||
|     final rect = toRect(); |     final rect = toRect(); | ||||||
|  |  | ||||||
|     if ((x <= 0 && xDirection == -1) || |     if ((x <= 0 && xDirection == -1) || | ||||||
|         (rect.right >= gameRef!.size.x && xDirection == 1)) { |         (rect.right >= gameRef.size.x && xDirection == 1)) { | ||||||
|       xDirection = xDirection * -1; |       xDirection = xDirection * -1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     y += yDirection * speed * dt; |     y += yDirection * speed * dt; | ||||||
|  |  | ||||||
|     if ((y <= 0 && yDirection == -1) || |     if ((y <= 0 && yDirection == -1) || | ||||||
|         (rect.bottom >= gameRef!.size.y && yDirection == 1)) { |         (rect.bottom >= gameRef.size.y && yDirection == 1)) { | ||||||
|       yDirection = yDirection * -1; |       yDirection = yDirection * -1; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -39,7 +39,7 @@ class DraggableSquare extends PositionComponent | |||||||
|       return false; |       return false; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     final localCoords = gameRef!.convertGlobalToLocalCoordinate( |     final localCoords = gameRef.convertGlobalToLocalCoordinate( | ||||||
|       details.globalPosition.toVector2(), |       details.globalPosition.toVector2(), | ||||||
|     ); |     ); | ||||||
|     position.setFrom(localCoords - dragDeltaPosition); |     position.setFrom(localCoords - dragDeltaPosition); | ||||||
|  | |||||||
| @ -13,7 +13,7 @@ class MyParallaxComponent extends ParallaxComponent | |||||||
|     with HasGameRef<ComponentParallaxGame> { |     with HasGameRef<ComponentParallaxGame> { | ||||||
|   @override |   @override | ||||||
|   Future<void> onLoad() async { |   Future<void> onLoad() async { | ||||||
|     parallax = await gameRef!.loadParallax( |     parallax = await gameRef.loadParallax( | ||||||
|       [ |       [ | ||||||
|         'parallax/bg.png', |         'parallax/bg.png', | ||||||
|         'parallax/mountain-far.png', |         'parallax/mountain-far.png', | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent | |||||||
|     with HasGameRef<SpritebatchAutoLoadGame> { |     with HasGameRef<SpritebatchAutoLoadGame> { | ||||||
|   @override |   @override | ||||||
|   Future<void> onLoad() async { |   Future<void> onLoad() async { | ||||||
|     final spriteBatch = await gameRef!.loadSpriteBatch('boom.png'); |     final spriteBatch = await gameRef.loadSpriteBatch('boom.png'); | ||||||
|     this.spriteBatch = spriteBatch; |     this.spriteBatch = spriteBatch; | ||||||
|  |  | ||||||
|     spriteBatch.add( |     spriteBatch.add( | ||||||
| @ -26,7 +26,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent | |||||||
|       color: Colors.redAccent, |       color: Colors.redAccent, | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|     final size = gameRef!.size; |     final size = gameRef.size; | ||||||
|     const num = 100; |     const num = 100; | ||||||
|     final r = Random(); |     final r = Random(); | ||||||
|     for (var i = 0; i < num; ++i) { |     for (var i = 0; i < num; ++i) { | ||||||
|  | |||||||
| @ -24,7 +24,7 @@ class MovableSquare extends SquareComponent | |||||||
|     timer = Timer(3.0) |     timer = Timer(3.0) | ||||||
|       ..stop() |       ..stop() | ||||||
|       ..callback = () { |       ..callback = () { | ||||||
|         gameRef!.camera.setRelativeOffset(Anchor.center.toVector2()); |         gameRef.camera.setRelativeOffset(Anchor.center.toVector2()); | ||||||
|       }; |       }; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @ -50,7 +50,7 @@ class MovableSquare extends SquareComponent | |||||||
|   @override |   @override | ||||||
|   void onCollision(Set<Vector2> points, Collidable other) { |   void onCollision(Set<Vector2> points, Collidable other) { | ||||||
|     if (other is Rock) { |     if (other is Rock) { | ||||||
|       gameRef!.camera.setRelativeOffset(Anchor.topCenter.toVector2()); |       gameRef.camera.setRelativeOffset(Anchor.topCenter.toVector2()); | ||||||
|       timer.start(); |       timer.start(); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -16,6 +16,7 @@ | |||||||
|  - Revamp all the docs to be up to date with v1.0.0 |  - Revamp all the docs to be up to date with v1.0.0 | ||||||
|  - Make Assets and Images caches have a configurable prefix |  - Make Assets and Images caches have a configurable prefix | ||||||
|  - Add `followVector2` method to the `Camera` |  - Add `followVector2` method to the `Camera` | ||||||
|  |  - Make `gameRef` late | ||||||
|  |  | ||||||
| ## 1.0.0-rc8 | ## 1.0.0-rc8 | ||||||
|  - Migrate to null safety |  - Migrate to null safety | ||||||
|  | |||||||
| @ -143,7 +143,10 @@ abstract class BaseComponent extends Component { | |||||||
|   /// For children that don't need preparation from the game instance can |   /// For children that don't need preparation from the game instance can | ||||||
|   /// disregard both the options given above. |   /// disregard both the options given above. | ||||||
|   Future<void> addChild(Component child, {Game? gameRef}) async { |   Future<void> addChild(Component child, {Game? gameRef}) async { | ||||||
|     gameRef ??= (this as HasGameRef).gameRef; |     if (this is HasGameRef) { | ||||||
|  |       final c = this as HasGameRef; | ||||||
|  |       gameRef ??= c.hasGameRef ? c.gameRef : null; | ||||||
|  |     } | ||||||
|     if (gameRef is BaseGame) { |     if (gameRef is BaseGame) { | ||||||
|       gameRef.prepare(child); |       gameRef.prepare(child); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -67,7 +67,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Future<void> onLoad() async { |   Future<void> onLoad() async { | ||||||
|     initialize(gameRef!.size); |     initialize(gameRef.size); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
| @ -188,7 +188,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { | |||||||
|   @override |   @override | ||||||
|   bool onDragUpdate(int pointerId, DragUpdateDetails details) { |   bool onDragUpdate(int pointerId, DragUpdateDetails details) { | ||||||
|     if (_dragging) { |     if (_dragging) { | ||||||
|       _dragPosition = gameRef!.convertGlobalToLocalCoordinate( |       _dragPosition = gameRef.convertGlobalToLocalCoordinate( | ||||||
|         details.globalPosition.toVector2(), |         details.globalPosition.toVector2(), | ||||||
|       ); |       ); | ||||||
|       return true; |       return true; | ||||||
|  | |||||||
| @ -50,7 +50,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { | |||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   Future<void> onLoad() async { |   Future<void> onLoad() async { | ||||||
|     initialize(gameRef!.size); |     initialize(gameRef.size); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
| @ -163,7 +163,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { | |||||||
|   @override |   @override | ||||||
|   bool onDragUpdate(int pointerId, DragUpdateDetails details) { |   bool onDragUpdate(int pointerId, DragUpdateDetails details) { | ||||||
|     if (_dragging) { |     if (_dragging) { | ||||||
|       _dragPosition = gameRef!.convertGlobalToLocalCoordinate( |       _dragPosition = gameRef.convertGlobalToLocalCoordinate( | ||||||
|         details.globalPosition.toVector2(), |         details.globalPosition.toVector2(), | ||||||
|       ); |       ); | ||||||
|       return false; |       return false; | ||||||
|  | |||||||
| @ -1,5 +1,26 @@ | |||||||
|  | import '../../../components.dart'; | ||||||
| import '../../game/game.dart'; | import '../../game/game.dart'; | ||||||
|  |  | ||||||
| mixin HasGameRef<T extends Game> { | mixin HasGameRef<T extends Game> { | ||||||
|   T? gameRef; |   T? _gameRef; | ||||||
|  |  | ||||||
|  |   T get gameRef { | ||||||
|  |     final ref = _gameRef; | ||||||
|  |     if (ref == null) { | ||||||
|  |       throw 'Accessing gameRef before the component was added to the game!'; | ||||||
|  |     } | ||||||
|  |     return ref; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   bool get hasGameRef => _gameRef != null; | ||||||
|  |  | ||||||
|  |   set gameRef(T gameRef) { | ||||||
|  |     _gameRef = gameRef; | ||||||
|  |     if (this is BaseComponent) { | ||||||
|  |       (this as BaseComponent) | ||||||
|  |           .children | ||||||
|  |           .whereType<HasGameRef<T>>() | ||||||
|  |           .forEach((e) => e.gameRef = gameRef); | ||||||
|  |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -11,7 +11,7 @@ class MyGame extends BaseGame { | |||||||
|  |  | ||||||
| class MyComponent extends PositionComponent with HasGameRef<MyGame> { | class MyComponent extends PositionComponent with HasGameRef<MyGame> { | ||||||
|   void foo() { |   void foo() { | ||||||
|     gameRef!.foo(); |     gameRef.foo(); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Luan Nico
					Luan Nico