blob: 12da7e16090b29506acd29f0b210a5c7b645f1f6 (
plain)
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
|
;;; Copyright 2023 David Thompson
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
(define-module (super-bloom game)
#:use-module (catbird)
#:use-module (catbird asset)
#:use-module (catbird camera)
#:use-module (catbird kernel)
#:use-module (catbird mode)
#:use-module (catbird node)
#:use-module (catbird node-2d)
#:use-module (catbird region)
#:use-module (catbird scene)
#:use-module (chickadee data quadtree)
#:use-module (chickadee graphics color)
#:use-module (chickadee graphics particles)
#:use-module (chickadee graphics texture)
#:use-module (chickadee math rect)
#:use-module (chickadee math vector)
#:use-module (chickadee scripting)
#:use-module (oop goops)
#:use-module (super-bloom actor)
#:use-module (super-bloom common)
#:use-module (super-bloom dirt-ball)
#:use-module (super-bloom flower)
#:use-module (super-bloom player)
#:use-module (super-bloom water)
#:export (<super-bloom-mode>
reset-game!))
(define-asset (background-texture (file (scope-datadir "images/background.png")))
(load-image file))
(define-asset (chickadee-texture (file (scope-datadir "images/chickadee.png")))
(load-image file))
(define-asset (water-particle-texture (file (scope-datadir "images/water-particle.png")))
(load-image file))
(define-asset (trail-particle-texture (file (scope-datadir "images/trail-particle.png")))
(load-image file))
(define-asset (explosion-particle-texture (file (scope-datadir "images/explosion-particle.png")))
(load-image file))
(define (make-game-quadtree)
(make-quadtree (make-rect 0.0 0.0 %game-width %game-height)))
(define-class <super-bloom-mode> (<major-mode>)
(show-instructions? #:accessor show-instructions? #:init-value #t
#:init-keyword #:show-instructions?)
(quadtree #:getter quadtree #:init-thunk make-game-quadtree))
(define-method (on-enter (mode <super-bloom-mode>))
(let ((scene (parent mode))
(water-particles
(make <particles>
#:name 'water-particles
#:rank 2
#:particles
(make-particles 1024
#:texture (artifact water-particle-texture)
#:start-color white
#:end-color white
#:speed-range (vec2 2.0 5.0)
#:acceleration-range (vec2 0.1 0.3)
#:lifetime 10)))
(trail-particles
(make <particles>
#:name 'trail-particles
#:rank 2
#:particles
(make-particles 1024
#:texture (artifact trail-particle-texture)
#:animation-columns 3
#:start-color white
#:end-color white
#:speed-range (vec2 0.2 0.5)
#:acceleration-range (vec2 0.1 0.2)
#:lifetime 12)))
(explosion-particles
(make <particles>
#:name 'explosion-particles
#:rank 2
#:particles
(make-particles 1024
#:texture (artifact explosion-particle-texture)
#:lifetime 20
#:end-color white))))
(attach-to scene
(make <sprite>
#:name 'background
#:rank 0
#:texture background-texture)
water-particles
trail-particles
explosion-particles
(make <flower>
#:name 'flower
#:rank 4
#:position (vec2 (/ %game-width:float 2.0)
(/ %game-height:float 2.0))
#:quadtree (quadtree mode)
#:growth-goal 20
#:growth-interval 3.0)
(make <player>
#:name 'player
#:rank 5
#:position (vec2 (/ %game-width:float 2.0)
(/ %game-height:float 4.0))
#:quadtree (quadtree mode)
#:water-particles water-particles
#:trail-particles trail-particles))
(run-script mode
(show-instructions mode)
(start-game mode))))
(define-method (spawn-water (mode <super-bloom-mode>) position)
(attach-to (parent mode)
(make <water>
#:rank 3
#:position position
#:quadtree (quadtree mode))))
(define-method (spawn-dirt-ball (mode <super-bloom-mode>) position)
(attach-to (parent mode)
(make <dirt-ball>
#:rank 3
#:position position
#:quadtree (quadtree mode)
#:flower (& (parent mode) flower)
#:speed 1.0
#:explosion-particles (& (parent mode) explosion-particles))))
(define-method (spawn-random-water (mode <super-bloom-mode>))
(spawn-water mode (vec2 (random:float %game-width:float)
(random:float %game-height:float))))
(define-method (spawn-random-dirt-ball (mode <super-bloom-mode>))
(spawn-dirt-ball mode (vec2 (if (= (random 2) 0)
-32.0
(+ %game-width:float 32.0))
(+ (/ %game-height 2.0)
(* (/ %game-height 2.0)
(- (random:float 2.0) 1.0))))))
(define-method (wave1 (mode <super-bloom-mode>))
(spawn-random-water mode)
(sleep 1.0)
(spawn-random-water mode)
(sleep 1.0)
(spawn-random-dirt-ball mode))
(define-method (wave2 (mode <super-bloom-mode>))
(spawn-random-water mode)
(spawn-random-water mode)
(sleep 1.0)
(spawn-random-water mode)
(spawn-random-water mode)
(sleep 1.0)
(spawn-random-dirt-ball mode)
(sleep 1.0)
(spawn-random-dirt-ball mode)
(sleep 1.0)
(spawn-random-dirt-ball mode))
(define-method (wave3 (mode <super-bloom-mode>))
(spawn-random-water mode)
(spawn-random-water mode)
(spawn-random-water mode)
(spawn-random-water mode)
(spawn-random-water mode)
(spawn-random-water mode)
(sleep 1.0)
(spawn-random-dirt-ball mode)
(sleep 1.0)
(spawn-random-dirt-ball mode)
(spawn-random-dirt-ball mode)
(sleep 1.0)
(spawn-random-dirt-ball mode))
(define hw (/ %game-width:float 2.0))
(define hh (/ %game-height:float 2.0))
(define-method (wave4 (mode <super-bloom-mode>))
(let ((offset 200.0))
(spawn-random-water mode)
(spawn-random-water mode)
(sleep 1.0)
(spawn-dirt-ball mode (vec2 hw (+ hh offset)))
(spawn-dirt-ball mode (vec2 hw (- hh offset)))
(spawn-dirt-ball mode (vec2 (+ hw offset) hh))
(spawn-dirt-ball mode (vec2 (- hw offset) hh))))
(define waves (vector wave1 wave2 wave3 wave4))
(define-method (do-random-wave (mode <super-bloom-mode>))
(let ((wave (vector-ref waves (random (vector-length waves)))))
(wave mode)))
(define-method (show-instructions (mode <super-bloom-mode>))
(when (show-instructions? mode)
(set! (show-instructions? mode) #f)
(let ((scene (parent mode))
(label (make <label>
#:name 'instructions
#:rank 99
#:position (vec2 0.0 140.0)
#:font monogram-font)))
(define (show-instruction str)
(set! (text label) str)
(center-horizontal-in-parent label))
(sleep 1.0)
(attach-to scene label)
(show-instruction "move with <arrows>")
(sleep 2.0)
(show-instruction "collect water by moving over droplets")
(sleep 2.0)
(show-instruction "spray stored water with <z> to water the plant")
(sleep 2.0)
(show-instruction "spray the dirt balls trying to kill the flower")
(sleep 2.0)
(show-instruction "good luck!")
(sleep 2.0)
(detach label))))
(define-method (start-game (mode <super-bloom-mode>))
(spawn-water mode (vec2 120.0 100.0))
(sleep 1.0)
(spawn-water mode (vec2 160.0 140.0))
(sleep 1.0)
(spawn-water mode (vec2 200.0 100.0))
(sleep 3.0)
(wave1 mode)
(sleep 5.0)
(wave2 mode)
(sleep 5.0)
(forever
(do-random-wave mode)
(sleep 5.0)))
(define-method (player (mode <super-bloom-mode>))
(& (parent mode) player))
(define-method (player-move-left (mode <super-bloom-mode>))
(set! (move-left? (player mode)) #t))
(define-method (player-move-left-stop (mode <super-bloom-mode>))
(set! (move-left? (player mode)) #f))
(define-method (player-move-right (mode <super-bloom-mode>))
(set! (move-right? (player mode)) #t))
(define-method (player-move-right-stop (mode <super-bloom-mode>))
(set! (move-right? (player mode)) #f))
(define-method (player-move-down (mode <super-bloom-mode>))
(set! (move-down? (player mode)) #t))
(define-method (player-move-down-stop (mode <super-bloom-mode>))
(set! (move-down? (player mode)) #f))
(define-method (player-move-up (mode <super-bloom-mode>))
(set! (move-up? (player mode)) #t))
(define-method (player-move-up-stop (mode <super-bloom-mode>))
(set! (move-up? (player mode)) #f))
(define-method (do-spray-water (mode <super-bloom-mode>))
(spray-water (player mode)))
(define-method (do-quit (mode <major-mode>))
(exit-catbird))
(define-method (update (mode <super-bloom-mode>) dt)
(with-agenda (agenda mode)
(update-agenda dt))
(let* ((scene (parent mode))
(flower (& scene flower)))
(if flower
(when (= (growth-progress flower) (growth-goal flower))
(stop-moving (& scene player))
(for-each-child (lambda (child)
(when (is-a? child <dirt-ball>)
(set! (dead? child) #t)))
scene)
(replace-major-mode scene (make <win-mode>)))
(begin
(stop-moving (& scene player))
(replace-major-mode scene (make <game-over-mode>))))))
(define-method (do-reset (mode <major-mode>))
(let ((scene (parent mode)))
(clear scene)
(replace-major-mode scene
(make <super-bloom-mode>
#:show-instructions? #f))))
(bind-input <super-bloom-mode> (key-press 'escape) do-quit)
(bind-input <super-bloom-mode> (key-press 'left) player-move-left)
(bind-input <super-bloom-mode> (key-release 'left) player-move-left-stop)
(bind-input <super-bloom-mode> (key-press 'right) player-move-right)
(bind-input <super-bloom-mode> (key-release 'right) player-move-right-stop)
(bind-input <super-bloom-mode> (key-press 'down) player-move-down)
(bind-input <super-bloom-mode> (key-release 'down) player-move-down-stop)
(bind-input <super-bloom-mode> (key-press 'up) player-move-up)
(bind-input <super-bloom-mode> (key-release 'up) player-move-up-stop)
(bind-input <super-bloom-mode> (key-press 'z) do-spray-water)
;;(bind-input <super-bloom-mode> (key-press 'r) do-reset)
(define-class <game-over-mode> (<major-mode>))
(define-method (on-enter (mode <game-over-mode>))
(let ((scene (parent mode)))
(attach-to scene
(make <label>
#:name 'game-over-label
#:rank 99
#:position (vec2 0.0 200.0)
#:font monogram-font
#:text "GAME OVER")
(make <label>
#:name 'instructions-label
#:rank 99
#:font monogram-font
#:text "Press <Z> to play again"))
(center-in-parent (& scene game-over-label))
(center-horizontal-in-parent (& scene instructions-label))
(place-below (& scene game-over-label)
(& scene instructions-label)
#:padding 16.0)))
(bind-input <game-over-mode> (key-press 'escape) do-quit)
(bind-input <game-over-mode> (key-press 'z) do-reset)
(define-class <win-mode> (<major-mode>))
(define-method (on-enter (mode <win-mode>))
(let ((scene (parent mode)))
(attach-to scene
(make <label>
#:name 'win-label
#:rank 99
#:position (vec2 0.0 200.0)
#:font monogram-font
#:text "YES! YOUR FLOWER HAS BLOOMED :)")
(make <label>
#:name 'instructions-label
#:rank 99
#:font monogram-font
#:text "Press <Z> to play again"))
(center-horizontal-in-parent (& scene win-label))
(center-horizontal-in-parent (& scene instructions-label))
(place-below (& scene win-label)
(& scene instructions-label)
#:padding 16.0)))
(bind-input <win-mode> (key-press 'escape) do-quit)
(bind-input <win-mode> (key-press 'z) do-reset)
(define (reset-game!)
(let ((game-scene (scene (find-region-by-name 'main))))
(clear game-scene)
(replace-major-mode game-scene (make <super-bloom-mode>))))
|