-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
codelab_rebuild.yaml
1281 lines (1180 loc) · 49.5 KB
/
codelab_rebuild.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Brick Breaker with Flame
steps:
- name: step_03
steps:
- name: Remove generated code
rmdir: step_03
- name: Create project
flutter: create brick_breaker --empty
- name: Strip DEVELOPMENT_TEAM
strip-lines-containing: DEVELOPMENT_TEAM =
path: brick_breaker/ios/Runner.xcodeproj/project.pbxproj
- name: Configure analysis_options.yaml
path: brick_breaker/analysis_options.yaml
replace-contents: |
include: ../../analysis_options.yaml
- name: Configure dependencies
path: brick_breaker/pubspec.yaml
replace-contents: |
name: brick_breaker
description: "Re-implementing Woz's Breakout"
publish_to: 'none'
version: 0.1.0
environment:
sdk: ^3.6.0-0
dependencies:
flame: ^1.18.0
flutter:
sdk: flutter
flutter_animate: ^4.5.0
google_fonts: ^6.2.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: true
- name: flutter pub upgrade
path: brick_breaker
flutter: pub upgrade --major-versions
- name: Remove README
rm: brick_breaker/README.md
- name: Add .vscode directory
mkdir: brick_breaker/.vscode
- name: Add .vscode/launch.json
path: brick_breaker/.vscode/launch.json
replace-contents: |
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "brick_breaker",
"request": "launch",
"type": "dart"
}
]
}
- name: Replace lib/main.dart
path: brick_breaker/lib/main.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
void main() {
final game = FlameGame();
runApp(GameWidget(game: game));
}
- name: Build iOS simulator bundle
platforms: [ macos ]
path: brick_breaker
flutter: build ios --simulator
- name: Build macOS app
platforms: [ macos ]
path: brick_breaker
flutter: build macos
- name: Build Linux app
platforms: [ linux ]
path: brick_breaker
flutter: build linux
- name: Build Windows app
platforms: [ windows ]
path: brick_breaker
flutter: build windows
- name: Copy step_03
copydir:
from: brick_breaker
to: step_03
- name: Flutter clean
path: step_03
flutter: clean
- name: step_04
steps:
- name: Remove generated code
rmdir: step_04
- name: Patch lib/main.dart
path: brick_breaker/lib/main.dart
patch-u: |
--- b/brick_breaker/step_04/lib/main.dart
+++ a/brick_breaker/step_04/lib/main.dart
@@ -5,7 +5,9 @@
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
+import 'src/brick_breaker.dart';
+
void main() {
- final game = FlameGame();
+ final game = BrickBreaker();
runApp(GameWidget(game: game));
}
- name: Mkdir lib/src
mkdir: brick_breaker/lib/src
- name: Add lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'components/components.dart';
import 'config.dart';
class BrickBreaker extends FlameGame {
BrickBreaker()
: super(
camera: CameraComponent.withFixedResolution(
width: gameWidth,
height: gameHeight,
),
);
double get width => size.x;
double get height => size.y;
@override
FutureOr<void> onLoad() async {
super.onLoad();
camera.viewfinder.anchor = Anchor.topLeft;
world.add(PlayArea());
}
}
- name: Add lib/src/config.dart
path: brick_breaker/lib/src/config.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const gameWidth = 820.0;
const gameHeight = 1600.0;
- name: Mkdir lib/src/components
mkdir: brick_breaker/lib/src/components
- name: Add lib/src/components/components.dart
path: brick_breaker/lib/src/components/components.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export 'play_area.dart';
- name: Add lib/src/components/play_area.dart
path: brick_breaker/lib/src/components/play_area.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import '../brick_breaker.dart';
class PlayArea extends RectangleComponent with HasGameReference<BrickBreaker> {
PlayArea()
: super(
paint: Paint()..color = const Color(0xfff2e8cf),
);
@override
FutureOr<void> onLoad() async {
super.onLoad();
size = Vector2(game.width, game.height);
}
}
- name: Copy step_04
copydir:
from: brick_breaker
to: step_04
- name: Flutter clean
path: step_04
flutter: clean
- name: step_05
steps:
- name: Remove generated code
rmdir: step_05
- name: Patch lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
patch-u: |
--- b/brick_breaker/step_05/lib/src/brick_breaker.dart
+++ a/brick_breaker/step_05/lib/src/brick_breaker.dart
@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
+import 'dart:math' as math;
import 'package:flame/components.dart';
import 'package:flame/game.dart';
@@ -19,6 +20,7 @@ class BrickBreaker extends FlameGame {
),
);
+ final rand = math.Random();
double get width => size.x;
double get height => size.y;
@@ -29,5 +31,14 @@ class BrickBreaker extends FlameGame {
camera.viewfinder.anchor = Anchor.topLeft;
world.add(PlayArea());
+
+ world.add(Ball(
+ radius: ballRadius,
+ position: size / 2,
+ velocity: Vector2((rand.nextDouble() - 0.5) * width, height * 0.2)
+ .normalized()
+ ..scale(height / 4)));
+
+ debugMode = true;
}
}
- name: Patch lib/src/config.dart
path: brick_breaker/lib/src/config.dart
patch-u: |
--- b/brick_breaker/step_05/lib/src/config.dart
+++ a/brick_breaker/step_05/lib/src/config.dart
@@ -4,3 +4,4 @@
const gameWidth = 820.0;
const gameHeight = 1600.0;
+const ballRadius = gameWidth * 0.02;
- name: Patch lib/src/components/components.dart
path: brick_breaker/lib/src/components/components.dart
patch-u: |
--- b/brick_breaker/step_05/lib/src/components/components.dart
+++ a/brick_breaker/step_05/lib/src/components/components.dart
@@ -2,4 +2,5 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+export 'ball.dart';
export 'play_area.dart';
- name: Add lib/src/components/ball.dart
path: brick_breaker/lib/src/components/ball.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
class Ball extends CircleComponent {
Ball({
required this.velocity,
required super.position,
required double radius,
}) : super(
radius: radius,
anchor: Anchor.center,
paint: Paint()
..color = const Color(0xff1e6091)
..style = PaintingStyle.fill);
final Vector2 velocity;
@override
void update(double dt) {
super.update(dt);
position += velocity * dt;
}
}
- name: Copy step_05
copydir:
from: brick_breaker
to: step_05
- name: Flutter clean
path: step_05
flutter: clean
- name: step_06
steps:
- name: Remove generated code
rmdir: step_06
- name: Patch lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
patch-u: |
--- b/brick_breaker/step_06/lib/src/brick_breaker.dart
+++ a/brick_breaker/step_06/lib/src/brick_breaker.dart
@@ -11,7 +11,7 @@ import 'package:flame/game.dart';
import 'components/components.dart';
import 'config.dart';
-class BrickBreaker extends FlameGame {
+class BrickBreaker extends FlameGame with HasCollisionDetection {
BrickBreaker()
: super(
camera: CameraComponent.withFixedResolution(
- name: Patch lib/src/components/ball.dart
path: brick_breaker/lib/src/components/ball.dart
patch-u: |
--- b/brick_breaker/step_06/lib/src/components/ball.dart
+++ a/brick_breaker/step_06/lib/src/components/ball.dart
@@ -2,10 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
-class Ball extends CircleComponent {
+import '../brick_breaker.dart';
+import 'play_area.dart';
+
+class Ball extends CircleComponent
+ with CollisionCallbacks, HasGameReference<BrickBreaker> {
Ball({
required this.velocity,
required super.position,
@@ -15,7 +20,8 @@ class Ball extends CircleComponent {
anchor: Anchor.center,
paint: Paint()
..color = const Color(0xff1e6091)
- ..style = PaintingStyle.fill);
+ ..style = PaintingStyle.fill,
+ children: [CircleHitbox()]);
final Vector2 velocity;
@@ -24,4 +30,23 @@ class Ball extends CircleComponent {
super.update(dt);
position += velocity * dt;
}
+
+ @override
+ void onCollisionStart(
+ Set<Vector2> intersectionPoints, PositionComponent other) {
+ super.onCollisionStart(intersectionPoints, other);
+ if (other is PlayArea) {
+ if (intersectionPoints.first.y <= 0) {
+ velocity.y = -velocity.y;
+ } else if (intersectionPoints.first.x <= 0) {
+ velocity.x = -velocity.x;
+ } else if (intersectionPoints.first.x >= game.width) {
+ velocity.x = -velocity.x;
+ } else if (intersectionPoints.first.y >= game.height) {
+ removeFromParent();
+ }
+ } else {
+ debugPrint('collision with $other');
+ }
+ }
}
- name: Patch lib/src/components/play_area.dart
path: brick_breaker/lib/src/components/play_area.dart
patch-u: |
--- b/brick_breaker/step_06/lib/src/components/play_area.dart
+++ a/brick_breaker/step_06/lib/src/components/play_area.dart
@@ -4,6 +4,7 @@
import 'dart:async';
+import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
@@ -13,6 +14,7 @@ class PlayArea extends RectangleComponent with HasGameReference<BrickBreaker> {
PlayArea()
: super(
paint: Paint()..color = const Color(0xfff2e8cf),
+ children: [RectangleHitbox()],
);
@override
- name: Copy step_06
copydir:
from: brick_breaker
to: step_06
- name: Flutter clean
path: step_06
flutter: clean
- name: step_07
steps:
- name: Remove generated code
rmdir: step_07
- name: Patch lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
patch-u: |
--- b/brick_breaker/step_07/lib/src/brick_breaker.dart
+++ a/brick_breaker/step_07/lib/src/brick_breaker.dart
@@ -6,12 +6,16 @@ import 'dart:async';
import 'dart:math' as math;
import 'package:flame/components.dart';
+import 'package:flame/events.dart';
import 'package:flame/game.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
import 'components/components.dart';
import 'config.dart';
-class BrickBreaker extends FlameGame with HasCollisionDetection {
+class BrickBreaker extends FlameGame
+ with HasCollisionDetection, KeyboardEvents {
BrickBreaker()
: super(
camera: CameraComponent.withFixedResolution(
@@ -39,6 +43,24 @@ class BrickBreaker extends FlameGame with HasCollisionDetection {
.normalized()
..scale(height / 4)));
+ world.add(Bat(
+ size: Vector2(batWidth, batHeight),
+ cornerRadius: const Radius.circular(ballRadius / 2),
+ position: Vector2(width / 2, height * 0.95)));
+
debugMode = true;
}
+
+ @override
+ KeyEventResult onKeyEvent(
+ KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
+ super.onKeyEvent(event, keysPressed);
+ switch (event.logicalKey) {
+ case LogicalKeyboardKey.arrowLeft:
+ world.children.query<Bat>().first.moveBy(-batStep);
+ case LogicalKeyboardKey.arrowRight:
+ world.children.query<Bat>().first.moveBy(batStep);
+ }
+ return KeyEventResult.handled;
+ }
}
- name: Patch lib/src/config.dart
path: brick_breaker/lib/src/config.dart
patch-u: |
--- b/brick_breaker/step_07/lib/src/config.dart
+++ a/brick_breaker/step_07/lib/src/config.dart
@@ -5,3 +5,6 @@
const gameWidth = 820.0;
const gameHeight = 1600.0;
const ballRadius = gameWidth * 0.02;
+const batWidth = gameWidth * 0.2;
+const batHeight = ballRadius * 2;
+const batStep = gameWidth * 0.05;
- name: Patch lib/src/components/ball.dart
path: brick_breaker/lib/src/components/ball.dart
patch-u: |
--- b/brick_breaker/step_07/lib/src/components/ball.dart
+++ a/brick_breaker/step_07/lib/src/components/ball.dart
@@ -4,9 +4,11 @@
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
+import 'package:flame/effects.dart';
import 'package:flutter/material.dart';
import '../brick_breaker.dart';
+import 'bat.dart';
import 'play_area.dart';
class Ball extends CircleComponent
@@ -43,8 +45,14 @@ class Ball extends CircleComponent
} else if (intersectionPoints.first.x >= game.width) {
velocity.x = -velocity.x;
} else if (intersectionPoints.first.y >= game.height) {
- removeFromParent();
+ add(RemoveEffect(
+ delay: 0.35,
+ ));
}
+ } else if (other is Bat) {
+ velocity.y = -velocity.y;
+ velocity.x = velocity.x +
+ (position.x - other.position.x) / other.size.x * game.width * 0.3;
} else {
debugPrint('collision with $other');
}
- name: Patch lib/src/components/components.dart
path: brick_breaker/lib/src/components/components.dart
patch-u: |
--- b/brick_breaker/step_07/lib/src/components/components.dart
+++ a/brick_breaker/step_07/lib/src/components/components.dart
@@ -3,4 +3,5 @@
// found in the LICENSE file.
export 'ball.dart';
+export 'bat.dart';
export 'play_area.dart';
- name: Add lib/src/components/bat.dart
path: brick_breaker/lib/src/components/bat.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';
import '../brick_breaker.dart';
class Bat extends PositionComponent
with DragCallbacks, HasGameReference<BrickBreaker> {
Bat({
required this.cornerRadius,
required super.position,
required super.size,
}) : super(
anchor: Anchor.center,
children: [RectangleHitbox()],
);
final Radius cornerRadius;
final _paint = Paint()
..color = const Color(0xff1e6091)
..style = PaintingStyle.fill;
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRRect(
RRect.fromRectAndRadius(
Offset.zero & size.toSize(),
cornerRadius,
),
_paint);
}
@override
void onDragUpdate(DragUpdateEvent event) {
super.onDragUpdate(event);
position.x = (position.x + event.localDelta.x).clamp(0, game.width);
}
void moveBy(double dx) {
add(MoveToEffect(
Vector2((position.x + dx).clamp(0, game.width), position.y),
EffectController(duration: 0.1),
));
}
}
- name: Copy step_07
copydir:
from: brick_breaker
to: step_07
- name: Flutter clean
path: step_07
flutter: clean
- name: step_08
steps:
- name: Remove generated code
rmdir: step_08
- name: Patch lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
patch-u: |
--- b/brick_breaker/step_08/lib/src/brick_breaker.dart
+++ a/brick_breaker/step_08/lib/src/brick_breaker.dart
@@ -37,6 +37,7 @@ class BrickBreaker extends FlameGame
world.add(PlayArea());
world.add(Ball(
+ difficultyModifier: difficultyModifier,
radius: ballRadius,
position: size / 2,
velocity: Vector2((rand.nextDouble() - 0.5) * width, height * 0.2)
@@ -48,6 +49,18 @@ class BrickBreaker extends FlameGame
cornerRadius: const Radius.circular(ballRadius / 2),
position: Vector2(width / 2, height * 0.95)));
+ await world.addAll([
+ for (var i = 0; i < brickColors.length; i++)
+ for (var j = 1; j <= 5; j++)
+ Brick(
+ position: Vector2(
+ (i + 0.5) * brickWidth + (i + 1) * brickGutter,
+ (j + 2.0) * brickHeight + j * brickGutter,
+ ),
+ color: brickColors[i],
+ ),
+ ]);
+
debugMode = true;
}
- name: Patch lib/src/config.dart
path: brick_breaker/lib/src/config.dart
patch-u: |
--- b/brick_breaker/step_08/lib/src/config.dart
+++ a/brick_breaker/step_08/lib/src/config.dart
@@ -2,9 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+import 'package:flutter/material.dart';
+
+const brickColors = [
+ Color(0xfff94144),
+ Color(0xfff3722c),
+ Color(0xfff8961e),
+ Color(0xfff9844a),
+ Color(0xfff9c74f),
+ Color(0xff90be6d),
+ Color(0xff43aa8b),
+ Color(0xff4d908e),
+ Color(0xff277da1),
+ Color(0xff577590),
+];
+
const gameWidth = 820.0;
const gameHeight = 1600.0;
const ballRadius = gameWidth * 0.02;
const batWidth = gameWidth * 0.2;
const batHeight = ballRadius * 2;
const batStep = gameWidth * 0.05;
+const brickGutter = gameWidth * 0.015;
+final brickWidth =
+ (gameWidth - (brickGutter * (brickColors.length + 1))) / brickColors.length;
+const brickHeight = gameHeight * 0.03;
+const difficultyModifier = 1.03;
- name: Patch lib/src/components/ball.dart
path: brick_breaker/lib/src/components/ball.dart
patch-u: |
--- b/brick_breaker/step_08/lib/src/components/ball.dart
+++ a/brick_breaker/step_08/lib/src/components/ball.dart
@@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
import '../brick_breaker.dart';
import 'bat.dart';
+import 'brick.dart';
import 'play_area.dart';
class Ball extends CircleComponent
@@ -17,6 +18,7 @@ class Ball extends CircleComponent
required this.velocity,
required super.position,
required double radius,
+ required this.difficultyModifier,
}) : super(
radius: radius,
anchor: Anchor.center,
@@ -26,6 +28,7 @@ class Ball extends CircleComponent
children: [CircleHitbox()]);
final Vector2 velocity;
+ final double difficultyModifier;
@override
void update(double dt) {
@@ -53,8 +56,17 @@ class Ball extends CircleComponent
velocity.y = -velocity.y;
velocity.x = velocity.x +
(position.x - other.position.x) / other.size.x * game.width * 0.3;
- } else {
- debugPrint('collision with $other');
+ } else if (other is Brick) {
+ if (position.y < other.position.y - other.size.y / 2) {
+ velocity.y = -velocity.y;
+ } else if (position.y > other.position.y + other.size.y / 2) {
+ velocity.y = -velocity.y;
+ } else if (position.x < other.position.x) {
+ velocity.x = -velocity.x;
+ } else if (position.x > other.position.x) {
+ velocity.x = -velocity.x;
+ }
+ velocity.setFrom(velocity * difficultyModifier);
}
}
}
- name: Patch lib/src/components/components.dart
path: brick_breaker/lib/src/components/components.dart
patch-u: |
--- b/brick_breaker/step_08/lib/src/components/components.dart
+++ a/brick_breaker/step_08/lib/src/components/components.dart
@@ -4,4 +4,5 @@
export 'ball.dart';
export 'bat.dart';
+export 'brick.dart';
export 'play_area.dart';
- name: Add lib/src/components/brick.dart
path: brick_breaker/lib/src/components/brick.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import '../brick_breaker.dart';
import '../config.dart';
import 'ball.dart';
import 'bat.dart';
class Brick extends RectangleComponent
with CollisionCallbacks, HasGameReference<BrickBreaker> {
Brick({required super.position, required Color color})
: super(
size: Vector2(brickWidth, brickHeight),
anchor: Anchor.center,
paint: Paint()
..color = color
..style = PaintingStyle.fill,
children: [RectangleHitbox()],
);
@override
void onCollisionStart(
Set<Vector2> intersectionPoints, PositionComponent other) {
super.onCollisionStart(intersectionPoints, other);
removeFromParent();
if (game.world.children.query<Brick>().length == 1) {
game.world.removeAll(game.world.children.query<Ball>());
game.world.removeAll(game.world.children.query<Bat>());
}
}
}
- name: Copy step_08
copydir:
from: brick_breaker
to: step_08
- name: Flutter clean
path: step_08
flutter: clean
- name: step_09
steps:
- name: Remove generated code
rmdir: step_09
- name: Patch lib/main.dart
path: brick_breaker/lib/main.dart
patch-u: |
--- b/brick_breaker/step_09/lib/main.dart
+++ a/brick_breaker/step_09/lib/main.dart
@@ -2,12 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-import 'package:flame/game.dart';
import 'package:flutter/material.dart';
-import 'src/brick_breaker.dart';
+import 'src/widgets/game_app.dart';
void main() {
- final game = BrickBreaker();
- runApp(GameWidget(game: game));
+ runApp(const GameApp());
}
- name: Patch lib/src/brick_breaker.dart
path: brick_breaker/lib/src/brick_breaker.dart
patch-u: |
--- b/brick_breaker/step_09/lib/src/brick_breaker.dart
+++ a/brick_breaker/step_09/lib/src/brick_breaker.dart
@@ -14,8 +14,10 @@ import 'package:flutter/services.dart';
import 'components/components.dart';
import 'config.dart';
+enum PlayState { welcome, playing, gameOver, won }
+
class BrickBreaker extends FlameGame
- with HasCollisionDetection, KeyboardEvents {
+ with HasCollisionDetection, KeyboardEvents, TapDetector {
BrickBreaker()
: super(
camera: CameraComponent.withFixedResolution(
@@ -28,6 +30,22 @@ class BrickBreaker extends FlameGame
double get width => size.x;
double get height => size.y;
+ late PlayState _playState;
+ PlayState get playState => _playState;
+ set playState(PlayState playState) {
+ _playState = playState;
+ switch (playState) {
+ case PlayState.welcome:
+ case PlayState.gameOver:
+ case PlayState.won:
+ overlays.add(playState.name);
+ case PlayState.playing:
+ overlays.remove(PlayState.welcome.name);
+ overlays.remove(PlayState.gameOver.name);
+ overlays.remove(PlayState.won.name);
+ }
+ }
+
@override
FutureOr<void> onLoad() async {
super.onLoad();
@@ -36,6 +54,18 @@ class BrickBreaker extends FlameGame
world.add(PlayArea());
+ playState = PlayState.welcome;
+ }
+
+ void startGame() {
+ if (playState == PlayState.playing) return;
+
+ world.removeAll(world.children.query<Ball>());
+ world.removeAll(world.children.query<Bat>());
+ world.removeAll(world.children.query<Brick>());
+
+ playState = PlayState.playing;
+
world.add(Ball(
difficultyModifier: difficultyModifier,
radius: ballRadius,
@@ -49,7 +79,7 @@ class BrickBreaker extends FlameGame
cornerRadius: const Radius.circular(ballRadius / 2),
position: Vector2(width / 2, height * 0.95)));
- await world.addAll([
+ world.addAll([
for (var i = 0; i < brickColors.length; i++)
for (var j = 1; j <= 5; j++)
Brick(
@@ -60,8 +90,12 @@ class BrickBreaker extends FlameGame
color: brickColors[i],
),
]);
+ }
- debugMode = true;
+ @override
+ void onTap() {
+ super.onTap();
+ startGame();
}
@override
@@ -73,7 +107,13 @@ class BrickBreaker extends FlameGame
world.children.query<Bat>().first.moveBy(-batStep);
case LogicalKeyboardKey.arrowRight:
world.children.query<Bat>().first.moveBy(batStep);
+ case LogicalKeyboardKey.space:
+ case LogicalKeyboardKey.enter:
+ startGame();
}
return KeyEventResult.handled;
}
+
+ @override
+ Color backgroundColor() => const Color(0xfff2e8cf);
}
- name: Patch lib/src/components/ball.dart
path: brick_breaker/lib/src/components/ball.dart
patch-u: |
--- b/brick_breaker/step_09/lib/src/components/ball.dart
+++ a/brick_breaker/step_09/lib/src/components/ball.dart
@@ -49,8 +49,10 @@ class Ball extends CircleComponent
velocity.x = -velocity.x;
} else if (intersectionPoints.first.y >= game.height) {
add(RemoveEffect(
- delay: 0.35,
- ));
+ delay: 0.35,
+ onComplete: () {
+ game.playState = PlayState.gameOver;
+ }));
}
} else if (other is Bat) {
velocity.y = -velocity.y;
- name: Patch lib/src/components/brick.dart
path: brick_breaker/lib/src/components/brick.dart
patch-u: |
--- b/brick_breaker/step_09/lib/src/components/brick.dart
+++ a/brick_breaker/step_09/lib/src/components/brick.dart
@@ -30,6 +30,7 @@ class Brick extends RectangleComponent
removeFromParent();
if (game.world.children.query<Brick>().length == 1) {
+ game.playState = PlayState.won;
game.world.removeAll(game.world.children.query<Ball>());
game.world.removeAll(game.world.children.query<Bat>());
}
- name: Mkdir lib/src/widgets
mkdir: brick_breaker/lib/src/widgets
- name: Add lib/src/widgets/game_app.dart
path: brick_breaker/lib/src/widgets/game_app.dart
replace-contents: |
// Copyright 2023 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import '../brick_breaker.dart';
import '../config.dart';
class GameApp extends StatelessWidget {
const GameApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
textTheme: GoogleFonts.pressStart2pTextTheme().apply(
bodyColor: const Color(0xff184e77),
displayColor: const Color(0xff184e77),
),
),
home: Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xffa9d6e5),
Color(0xfff2e8cf),
],
),
),
child: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Center(
child: FittedBox(
child: SizedBox(
width: gameWidth,
height: gameHeight,
child: GameWidget.controlled(
gameFactory: BrickBreaker.new,
overlayBuilderMap: {
PlayState.welcome.name: (context, game) => Center(
child: Text(
'TAP TO PLAY',
style:
Theme.of(context).textTheme.headlineLarge,
),
),
PlayState.gameOver.name: (context, game) => Center(
child: Text(
'G A M E O V E R',
style:
Theme.of(context).textTheme.headlineLarge,
),
),
PlayState.won.name: (context, game) => Center(
child: Text(
'Y O U W O N ! ! !',
style:
Theme.of(context).textTheme.headlineLarge,
),
),
},
),
),
),
),
),
),
),
),
);
}
}
- name: Patch macos/Runner/Release.entitlements
path: brick_breaker/macos/Runner/Release.entitlements
patch-u: |
--- step_09/macos/Runner/Release.entitlements.orig 2023-09-01 15:04:05.839873573 +1000
+++ step_09/macos/Runner/Release.entitlements 2023-11-07 17:01:53.611544371 +1100