mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
Update min Dart constraint to 3.8, which will enable us to use the fancier collection literals. This requires bumping the min Flutter version as well: <img width="1892" height="1122" alt="image" src="https://github.com/user-attachments/assets/7c7b07fc-4d96-4987-824d-9a7133ecfb85" />
88 lines
2.4 KiB
Dart
88 lines
2.4 KiB
Dart
import 'dart:ui';
|
|
|
|
import 'package:flame/components.dart';
|
|
import 'package:flame/flame.dart';
|
|
import 'package:flame/game.dart';
|
|
|
|
import 'components/card.dart';
|
|
import 'components/foundation_pile.dart';
|
|
import 'components/stock_pile.dart';
|
|
import 'components/tableau_pile.dart';
|
|
import 'components/waste_pile.dart';
|
|
|
|
class KlondikeGame extends FlameGame {
|
|
static const double cardGap = 175.0;
|
|
static const double cardWidth = 1000.0;
|
|
static const double cardHeight = 1400.0;
|
|
static const double cardRadius = 100.0;
|
|
static final Vector2 cardSize = Vector2(cardWidth, cardHeight);
|
|
static final cardRRect = RRect.fromRectAndRadius(
|
|
const Rect.fromLTWH(0, 0, cardWidth, cardHeight),
|
|
const Radius.circular(cardRadius),
|
|
);
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
await Flame.images.load('klondike-sprites.png');
|
|
|
|
final stock = StockPile(position: Vector2(cardGap, cardGap));
|
|
final waste = WastePile(
|
|
position: Vector2(cardWidth + 2 * cardGap, cardGap),
|
|
);
|
|
final foundations = List.generate(
|
|
4,
|
|
(i) => FoundationPile(
|
|
i,
|
|
position: Vector2((i + 3) * (cardWidth + cardGap) + cardGap, cardGap),
|
|
),
|
|
);
|
|
final piles = List.generate(
|
|
7,
|
|
(i) => TableauPile(
|
|
position: Vector2(
|
|
cardGap + i * (cardWidth + cardGap),
|
|
cardHeight + 2 * cardGap,
|
|
),
|
|
),
|
|
);
|
|
|
|
world.add(stock);
|
|
world.add(waste);
|
|
world.addAll(foundations);
|
|
world.addAll(piles);
|
|
|
|
camera.viewfinder.visibleGameSize = Vector2(
|
|
cardWidth * 7 + cardGap * 8,
|
|
4 * cardHeight + 3 * cardGap,
|
|
);
|
|
camera.viewfinder.position = Vector2(cardWidth * 3.5 + cardGap * 4, 0);
|
|
camera.viewfinder.anchor = Anchor.topCenter;
|
|
|
|
final cards = [
|
|
for (var rank = 1; rank <= 13; rank++)
|
|
for (var suit = 0; suit < 4; suit++) Card(rank, suit),
|
|
];
|
|
cards.shuffle();
|
|
world.addAll(cards);
|
|
|
|
var cardToDeal = cards.length - 1;
|
|
for (var i = 0; i < 7; i++) {
|
|
for (var j = i; j < 7; j++) {
|
|
piles[j].acquireCard(cards[cardToDeal--]);
|
|
}
|
|
piles[i].flipTopCard();
|
|
}
|
|
for (var n = 0; n <= cardToDeal; n++) {
|
|
stock.acquireCard(cards[n]);
|
|
}
|
|
}
|
|
}
|
|
|
|
Sprite klondikeSprite(double x, double y, double width, double height) {
|
|
return Sprite(
|
|
Flame.images.fromCache('klondike-sprites.png'),
|
|
srcPosition: Vector2(x, y),
|
|
srcSize: Vector2(width, height),
|
|
);
|
|
}
|