mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 17:06:50 +08:00 
			
		
		
		
	Make gameRef late (#742)
This commit is contained in:
		| @ -1,7 +1,7 @@ | ||||
| import 'package:flame/components.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}) { | ||||
|     this.position.setFrom(position); | ||||
|     this.size.setFrom(size); | ||||
|  | ||||
| @ -20,14 +20,14 @@ class LogoCompomnent extends SpriteComponent with HasGameRef<DebugGame> { | ||||
|     final rect = toRect(); | ||||
|  | ||||
|     if ((x <= 0 && xDirection == -1) || | ||||
|         (rect.right >= gameRef!.size.x && xDirection == 1)) { | ||||
|         (rect.right >= gameRef.size.x && xDirection == 1)) { | ||||
|       xDirection = xDirection * -1; | ||||
|     } | ||||
|  | ||||
|     y += yDirection * speed * dt; | ||||
|  | ||||
|     if ((y <= 0 && yDirection == -1) || | ||||
|         (rect.bottom >= gameRef!.size.y && yDirection == 1)) { | ||||
|         (rect.bottom >= gameRef.size.y && yDirection == 1)) { | ||||
|       yDirection = yDirection * -1; | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @ -39,7 +39,7 @@ class DraggableSquare extends PositionComponent | ||||
|       return false; | ||||
|     } | ||||
|  | ||||
|     final localCoords = gameRef!.convertGlobalToLocalCoordinate( | ||||
|     final localCoords = gameRef.convertGlobalToLocalCoordinate( | ||||
|       details.globalPosition.toVector2(), | ||||
|     ); | ||||
|     position.setFrom(localCoords - dragDeltaPosition); | ||||
|  | ||||
| @ -13,7 +13,7 @@ class MyParallaxComponent extends ParallaxComponent | ||||
|     with HasGameRef<ComponentParallaxGame> { | ||||
|   @override | ||||
|   Future<void> onLoad() async { | ||||
|     parallax = await gameRef!.loadParallax( | ||||
|     parallax = await gameRef.loadParallax( | ||||
|       [ | ||||
|         'parallax/bg.png', | ||||
|         'parallax/mountain-far.png', | ||||
|  | ||||
| @ -9,7 +9,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent | ||||
|     with HasGameRef<SpritebatchAutoLoadGame> { | ||||
|   @override | ||||
|   Future<void> onLoad() async { | ||||
|     final spriteBatch = await gameRef!.loadSpriteBatch('boom.png'); | ||||
|     final spriteBatch = await gameRef.loadSpriteBatch('boom.png'); | ||||
|     this.spriteBatch = spriteBatch; | ||||
|  | ||||
|     spriteBatch.add( | ||||
| @ -26,7 +26,7 @@ class MySpriteBatchComponent extends SpriteBatchComponent | ||||
|       color: Colors.redAccent, | ||||
|     ); | ||||
|  | ||||
|     final size = gameRef!.size; | ||||
|     final size = gameRef.size; | ||||
|     const num = 100; | ||||
|     final r = Random(); | ||||
|     for (var i = 0; i < num; ++i) { | ||||
|  | ||||
| @ -24,7 +24,7 @@ class MovableSquare extends SquareComponent | ||||
|     timer = Timer(3.0) | ||||
|       ..stop() | ||||
|       ..callback = () { | ||||
|         gameRef!.camera.setRelativeOffset(Anchor.center.toVector2()); | ||||
|         gameRef.camera.setRelativeOffset(Anchor.center.toVector2()); | ||||
|       }; | ||||
|   } | ||||
|  | ||||
| @ -50,7 +50,7 @@ class MovableSquare extends SquareComponent | ||||
|   @override | ||||
|   void onCollision(Set<Vector2> points, Collidable other) { | ||||
|     if (other is Rock) { | ||||
|       gameRef!.camera.setRelativeOffset(Anchor.topCenter.toVector2()); | ||||
|       gameRef.camera.setRelativeOffset(Anchor.topCenter.toVector2()); | ||||
|       timer.start(); | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @ -16,6 +16,7 @@ | ||||
|  - Revamp all the docs to be up to date with v1.0.0 | ||||
|  - Make Assets and Images caches have a configurable prefix | ||||
|  - Add `followVector2` method to the `Camera` | ||||
|  - Make `gameRef` late | ||||
|  | ||||
| ## 1.0.0-rc8 | ||||
|  - 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 | ||||
|   /// disregard both the options given above. | ||||
|   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) { | ||||
|       gameRef.prepare(child); | ||||
|     } | ||||
|  | ||||
| @ -67,7 +67,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { | ||||
|  | ||||
|   @override | ||||
|   Future<void> onLoad() async { | ||||
|     initialize(gameRef!.size); | ||||
|     initialize(gameRef.size); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
| @ -188,7 +188,7 @@ class JoystickAction extends BaseComponent with Draggable, HasGameRef { | ||||
|   @override | ||||
|   bool onDragUpdate(int pointerId, DragUpdateDetails details) { | ||||
|     if (_dragging) { | ||||
|       _dragPosition = gameRef!.convertGlobalToLocalCoordinate( | ||||
|       _dragPosition = gameRef.convertGlobalToLocalCoordinate( | ||||
|         details.globalPosition.toVector2(), | ||||
|       ); | ||||
|       return true; | ||||
|  | ||||
| @ -50,7 +50,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { | ||||
|  | ||||
|   @override | ||||
|   Future<void> onLoad() async { | ||||
|     initialize(gameRef!.size); | ||||
|     initialize(gameRef.size); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
| @ -163,7 +163,7 @@ class JoystickDirectional extends BaseComponent with Draggable, HasGameRef { | ||||
|   @override | ||||
|   bool onDragUpdate(int pointerId, DragUpdateDetails details) { | ||||
|     if (_dragging) { | ||||
|       _dragPosition = gameRef!.convertGlobalToLocalCoordinate( | ||||
|       _dragPosition = gameRef.convertGlobalToLocalCoordinate( | ||||
|         details.globalPosition.toVector2(), | ||||
|       ); | ||||
|       return false; | ||||
|  | ||||
| @ -1,5 +1,26 @@ | ||||
| import '../../../components.dart'; | ||||
| import '../../game/game.dart'; | ||||
|  | ||||
| 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> { | ||||
|   void foo() { | ||||
|     gameRef!.foo(); | ||||
|     gameRef.foo(); | ||||
|   } | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user