Make gameRef late (#742)

This commit is contained in:
Luan Nico
2021-04-03 19:47:37 -04:00
committed by GitHub
parent 971fc09add
commit bc3820c0fa
12 changed files with 41 additions and 16 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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',

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -11,7 +11,7 @@ class MyGame extends BaseGame {
class MyComponent extends PositionComponent with HasGameRef<MyGame> {
void foo() {
gameRef!.foo();
gameRef.foo();
}
}