Skip to content

Commit bf1592a

Browse files
committed
add explanation about World initialization in KlondikeGame
1 parent 7f224a2 commit bf1592a

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

doc/tutorials/klondike/step5.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,23 +415,24 @@ re-created during each of the above actions.
415415

416416
### KlondikeWorld class
417417

418-
In Flame, a World is a type of Component that can contain other Components, such as Piles.
418+
In Flame, a ```World``` is a type of ```Component``` that can contain other ```Components```,
419+
such as Piles.
419420

420-
You can learn more about Worlds in game programming here:
421+
You can learn more about World in game programming here:
421422

422423
<https://media.worldbookonline.com/image/upload/v1467051964/asset/webquests/Electronic_Games_Advanced.pdf>
423424
<https://docs.flame-engine.org/latest/flame/game.html>
424-
We won't dive too deep into Worlds here, just understand their purpose for now.
425+
We won't dive too deep into World here, just understand their purpose for now.
425426

426427

427428
#### Creating KlondikeWorld
428429

429-
Let's create a World for our Klondike game, called KlondikeWorld.
430-
At the start of the game, we'll create a World. Each new game will be represented by a new World.
430+
Let's create a ```World``` for our Klondike game, called ```KlondikeWorld```.
431+
At the start of the game, we'll create a ```World```. Each new game will be represented by a new World.
431432
Worlds are also created when the player restarts the game.
432-
Each World is responsible for loading its own Components and dealing the cards accordingly.
433-
Therefore, the onLoad() method will be moved from the KlondikeGame class to KlondikeWorld.
434-
First, let's modify the KlondikeGame class:
433+
Each ```World``` is responsible for loading its own Components and dealing the cards accordingly.
434+
Therefore, the ```onLoad()``` method will be moved from the ```KlondikeGame``` class to ```KlondikeWorld```.
435+
First, let's modify the ```KlondikeGame``` class:
435436

436437
```dart
437438
// KlondikeWorld is our new World
@@ -449,6 +450,39 @@ class KlondikeGame extends FlameGame<KlondikeWorld> {
449450
450451
```
451452

453+
The code above shows that when ```FlameGame``` is initialized, a ```KlondikeWorld``` is
454+
also initialized.
455+
Previously, without the ```KlondikeWorld``` class, FlameGame would create a
456+
default ```World``` upon
457+
initialization. It's important to note that a Game can have multiple Worlds,
458+
but only one World is displayed at a time.
459+
460+
We removed the ```onLoad()``` method from the ```KlondikeGame``` class and now
461+
need to re-implement it in ```KlondikeWorld```.
462+
463+
First, create a file called ```klondike_world.dart``` in the lib folder and add
464+
the following ```KlondikeWorld``` class:
465+
466+
```dart
467+
class KlondikeWorld extends World with HasGameReference<KlondikeGame> {
468+
final cardGap = KlondikeGame.cardGap;
469+
final topGap = KlondikeGame.topGap;
470+
final cardSpaceWidth = KlondikeGame.cardSpaceWidth;
471+
final cardSpaceHeight = KlondikeGame.cardSpaceHeight;
472+
473+
final stock = StockPile(position: Vector2(0.0, 0.0));
474+
final waste = WastePile(position: Vector2(0.0, 0.0));
475+
final List<FoundationPile> foundations = [];
476+
final List<TableauPile> tableauPiles = [];
477+
final List<Card> cards = [];
478+
@override
479+
Future<void> onLoad() async {
480+
// ...
481+
482+
}
483+
}
484+
```
485+
452486

453487
#### what properties?
454488

0 commit comments

Comments
 (0)