Skip to content

Commit

Permalink
docs: Improving Space Shooter tutorial by moving attributes to constr…
Browse files Browse the repository at this point in the history
…uctors (#2944)

This PR improves the Space Shooter Tutorial as a whole by moving the
setting of component's attributes from their `onLoad` method, to their
constructors.
  • Loading branch information
erickzanardo authored Dec 21, 2023
1 parent 1b1e489 commit 093a702
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 65 deletions.
8 changes: 6 additions & 2 deletions doc/tutorials/space_shooter/app/lib/step2/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ class SpaceShooterGame extends FlameGame with PanDetector {
}

class Player extends SpriteComponent with HasGameReference<SpaceShooterGame> {
Player()
: super(
size: Vector2(100, 150),
anchor: Anchor.center,
);

@override
Future<void> onLoad() async {
await super.onLoad();

sprite = await game.loadSprite('player-sprite.png');

position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;
}

Expand Down
9 changes: 6 additions & 3 deletions doc/tutorials/space_shooter/app/lib/step3/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class SpaceShooterGame extends FlameGame with PanDetector {

class Player extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Player()
: super(
size: Vector2(100, 150),
anchor: Anchor.center,
);

@override
Future<void> onLoad() async {
await super.onLoad();
Expand All @@ -52,9 +58,6 @@ class Player extends SpriteAnimationComponent
);

position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;
}

void move(Vector2 delta) {
Expand Down
18 changes: 10 additions & 8 deletions doc/tutorials/space_shooter/app/lib/step4/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class SpaceShooterGame extends FlameGame with PanDetector {

class Player extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Player()
: super(
size: Vector2(100, 150),
anchor: Anchor.center,
);

late final TimerComponent _bulletSpawner;

@override
Expand All @@ -64,9 +70,6 @@ class Player extends SpriteAnimationComponent
);

position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;

_bulletSpawner = TimerComponent(
period: .2,
Expand Down Expand Up @@ -104,7 +107,10 @@ class Bullet extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Bullet({
super.position,
}) : super(size: Vector2(25, 50));
}) : super(
size: Vector2(25, 50),
anchor: Anchor.center,
);

@override
Future<void> onLoad() async {
Expand All @@ -118,10 +124,6 @@ class Bullet extends SpriteAnimationComponent
textureSize: Vector2(8, 16),
),
);

width = 25;
height = 50;
anchor = Anchor.center;
}

@override
Expand Down
27 changes: 14 additions & 13 deletions doc/tutorials/space_shooter/app/lib/step5/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class SpaceShooterGame extends FlameGame with PanDetector {

class Player extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Player()
: super(
size: Vector2(100, 150),
anchor: Anchor.center,
);

late final TimerComponent _bulletSpawner;

@override
Expand All @@ -75,9 +81,6 @@ class Player extends SpriteAnimationComponent
);

position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;

_bulletSpawner = TimerComponent(
period: .2,
Expand Down Expand Up @@ -115,7 +118,10 @@ class Bullet extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Bullet({
super.position,
}) : super(size: Vector2(25, 50));
}) : super(
size: Vector2(25, 50),
anchor: Anchor.center,
);

@override
Future<void> onLoad() async {
Expand All @@ -129,10 +135,6 @@ class Bullet extends SpriteAnimationComponent
textureSize: Vector2(8, 16),
),
);

width = 25;
height = 50;
anchor = Anchor.center;
}

@override
Expand All @@ -151,7 +153,10 @@ class Enemy extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Enemy({
super.position,
}) : super(size: Vector2.all(enemySize));
}) : super(
size: Vector2.all(enemySize),
anchor: Anchor.center,
);

static const enemySize = 50.0;

Expand All @@ -167,10 +172,6 @@ class Enemy extends SpriteAnimationComponent
textureSize: Vector2.all(16),
),
);

width = 50;
height = 50;
anchor = Anchor.center;
}

@override
Expand Down
36 changes: 19 additions & 17 deletions doc/tutorials/space_shooter/app/lib/step6/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class SpaceShooterGame extends FlameGame

class Player extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Player()
: super(
size: Vector2(100, 150),
anchor: Anchor.center,
);

late final TimerComponent _bulletSpawner;

@override
Expand All @@ -77,9 +83,6 @@ class Player extends SpriteAnimationComponent
);

position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;

_bulletSpawner = TimerComponent(
period: .2,
Expand Down Expand Up @@ -117,7 +120,10 @@ class Bullet extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Bullet({
super.position,
}) : super(size: Vector2(25, 50));
}) : super(
size: Vector2(25, 50),
anchor: Anchor.center,
);

@override
Future<void> onLoad() async {
Expand All @@ -132,10 +138,6 @@ class Bullet extends SpriteAnimationComponent
),
);

width = 25;
height = 50;
anchor = Anchor.center;

add(RectangleHitbox());
}

Expand All @@ -155,7 +157,10 @@ class Enemy extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame>, CollisionCallbacks {
Enemy({
super.position,
}) : super(size: Vector2.all(enemySize));
}) : super(
size: Vector2.all(enemySize),
anchor: Anchor.center,
);

static const enemySize = 50.0;

Expand All @@ -172,10 +177,6 @@ class Enemy extends SpriteAnimationComponent
),
);

width = 50;
height = 50;
anchor = Anchor.center;

add(RectangleHitbox());
}

Expand Down Expand Up @@ -209,7 +210,11 @@ class Explosion extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Explosion({
super.position,
}) : super(size: Vector2.all(150));
}) : super(
size: Vector2.all(150),
anchor: Anchor.center,
removeOnFinish: true,
);

@override
Future<void> onLoad() async {
Expand All @@ -224,8 +229,5 @@ class Explosion extends SpriteAnimationComponent
loop: false,
),
);

anchor = Anchor.center;
removeOnFinish = true;
}
}
9 changes: 6 additions & 3 deletions doc/tutorials/space_shooter/step_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,19 @@ our game a little bit:

```dart
class Player extends SpriteComponent with HasGameRef<SpaceShooterGame> {
Player() : super(
size: Vector2(100, 150),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
await super.onLoad();
sprite = await gameRef.loadSprite('player-sprite.png');
position = gameRef.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;
}
void move(Vector2 delta) {
Expand Down
9 changes: 6 additions & 3 deletions doc/tutorials/space_shooter/step_3.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ simple, take a look at how the component will look like now:
```dart
class Player extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Player() : super(
size: Vector2(100, 150),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
await super.onLoad();
Expand All @@ -41,9 +47,6 @@ class Player extends SpriteAnimationComponent
);
position = game.size / 2;
width = 100;
height = 150;
anchor = Anchor.center;
}
// Other methods omitted
Expand Down
14 changes: 8 additions & 6 deletions doc/tutorials/space_shooter/step_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class Bullet extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Bullet({
super.position,
}) : super(size: Vector2(25, 50));
}) : super(
size: Vector2(25, 50),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
Expand All @@ -28,10 +31,6 @@ class Bullet extends SpriteAnimationComponent
textureSize: Vector2(8, 16),
),
);
width = 25;
height = 50;
anchor = Anchor.center;
}
}
```
Expand All @@ -48,7 +47,10 @@ class Bullet extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Bullet({
super.position,
}) : super(size: Vector2(25, 50));
}) : super(
size: Vector2(25, 50),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
Expand Down
10 changes: 5 additions & 5 deletions doc/tutorials/space_shooter/step_5.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class Enemy extends SpriteAnimationComponent
Enemy({
super.position,
}) : super(size: Vector2.all(enemySize));
}) : super(
size: Vector2.all(enemySize),
anchor: Anchor.center,
);
static const enemySize = 50.0;
Expand All @@ -27,10 +31,6 @@ class Enemy extends SpriteAnimationComponent
textureSize: Vector2.all(16),
),
);
width = 50;
height = 50;
anchor = Anchor.center;
}
@override
Expand Down
12 changes: 7 additions & 5 deletions doc/tutorials/space_shooter/step_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ class Explosion extends SpriteAnimationComponent
with HasGameReference<SpaceShooterGame> {
Explosion({
super.position,
}) : super(size: Vector2.all(150));
}) : super(
size: Vector2.all(150),
anchor: Anchor.center,
removeOnFinish: true,
);
@override
Future<void> onLoad() async {
Expand All @@ -99,16 +104,13 @@ class Explosion extends SpriteAnimationComponent
loop: false,
),
);
anchor = Anchor.center;
removeOnFinish = true;
}
}
```

There is not much new in it, the biggest difference compared to the other animation components is
that we are passing `loop: false` in the `SpriteAnimationData.sequenced` constructor and that we are
setting `removeOnFinish = true;`. We do that so that when the animation is finished, it will
setting `removeOnFinish: true;`. We do that so that when the animation is finished, it will
automatically be removed from the game!

And finally, we make a small change in the `onCollisionStart` method from the `Enemy` class
Expand Down

0 comments on commit 093a702

Please sign in to comment.