-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.scm
295 lines (240 loc) · 8.45 KB
/
game.scm
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
(add-to-load-path ".")
(use-modules (chickadee)
(srfi srfi-9)
(system repl coop-server)
(chickadee math rect)
(chickadee math vector)
(chickadee math easings)
(chickadee math bezier)
(chickadee graphics font)
(chickadee graphics texture)
(chickadee graphics sprite)
(chickadee graphics path)
(chickadee scripting)
(util)
(enemies)
(graphics)
(weapons)
(player))
(set! *random-state* (seed->random-state (current-time)))
(define repl (spawn-coop-repl-server))
(define agenda-dt 1)
(define game-over #f)
(define flying-upgrades '())
(define-record-type <upgrade>
(make-upgrade position type)
upgrade?
(position upgrade-position upgrade-position-set!)
(type upgrade-type))
(define keys (list (cons 'left #f)
(cons 'right #f)
(cons 'up #f)
(cons 'down #f)
(cons 'space #f)
(cons 'm #f)))
(define (key-press key modifiers repeat?)
(if (and (equal? key 'space) (not repeat?))
(fire (vec2+ FIREBALL-OFFSET player-position)))
(if (and (equal? key 'm) (not repeat?))
(fire-rocket))
(if (and (equal? key 'r))
(reset-game))
(assoc-set! keys key #t))
(define (key-release key modifiers)
(assoc-set! keys key #f))
(define (load)
(load-textures))
(define (reset-game)
(if game-over
(begin
(current-agenda (make-agenda))
(spawn-scripts)
(reset-player-position)
(set! enemy-ships '())
(set! enemy-fireballs '())
(set! agenda-dt 1)
(set! score 0)
(set! player-ship-upgrades '())
(set! game-over #f))))
(define (player-move-delta left? right? up? down?)
(let ((x (cond (left? (- MOVE-STEP))
(right? MOVE-STEP)
(else 0)))
(y (cond (up? MOVE-STEP)
(down? (- MOVE-STEP))
(else 0))))
(vec2 x y)))
(define (move-player!)
(let ((move-left? (assoc-ref keys 'left))
(move-right? (assoc-ref keys 'right))
(move-up? (assoc-ref keys 'up))
(move-down? (assoc-ref keys 'down)))
(set! player-position (vec2+ player-position (player-move-delta move-left?
move-right?
move-up?
move-down?)))))
(define (enemy-ship-rect ship)
(let ((enemy-position (enemy-ship-position ship)))
(rect (vec2-x enemy-position)
(vec2-y enemy-position)
16 16)))
(define (upgrade-rect upgrade)
(let ((position (upgrade-position upgrade)))
(rect (vec2-x position)
(vec2-y position)
32 32)))
(define (spawn-enemies)
(forever
(spawn-enemy)
(sleep (+ 1 (random 100)))))
(define (draw-score)
(let ((text (string-append "Score: " (number->string score)))
(position (vec2 (- SCREEN-WIDTH 120) (- SCREEN-HEIGHT 20))))
(draw-text text position
#:color hud-color
#:font game-font
#:scale (vec2 0.3 0.3))))
(define (draw-hud)
(draw-hud-rocket-progress)
(draw-score))
(define (draw-game-end)
(let ((text (string-append "Final score: \n" (number->string score)))
(position (vec2 (- (/ SCREEN-WIDTH 2) 130) (- (/ SCREEN-HEIGHT 2) 0))))
(draw-text text position
#:font game-font
#:color hud-color)))
(define (draw-debug)
(if (not (nil? rocket))
(draw-canvas
(make-canvas
(with-style ((stroke-color green)
(stroke-width 4.0)
(fill-color green))
(stroke
(circle (rocket-position rocket) ROCKET-BLAST-RADIUS))))))
(if (not (nil? enemy-ships))
(let ((enemy-path (enemy-bezier-path (car enemy-ships))))
(draw-canvas
(make-canvas
(with-style ((stroke-color green)
(stroke-width 4.0)
(fill-color green))
(stroke
(path
(move-to (bezier-curve-p0 enemy-path))
(bezier-to
(bezier-curve-p1 enemy-path)
(bezier-curve-p2 enemy-path)
(bezier-curve-p3 enemy-path)))))))))
(let ((text (cond ((assoc-ref keys 'left) "left")
((assoc-ref keys 'right) "right")
((assoc-ref keys 'space) "space")
(else "none"))))
(draw-text (number->string (agenda-time)) (vec2 220 220))
(draw-text text (vec2 260.0 240.0))
(draw-text (string-append "Rocket last shot time: "
(number->string rocket-last-shot-time)) (vec2 190.0 300.0))
(draw-text (string-append "Fireballs: "
(number->string (length fireballs))) (vec2 280.0 290.0))))
(define (object-collides-with-player? object-rect-fn)
(lambda (object)
(let* ((object-rect (object-rect-fn object))
(player-rect (rect (vec2-x player-position)
(vec2-y player-position)
32 24)))
(rect-intersects? object-rect player-rect))))
(define (player-collides?)
(let* ((enemy-collides?
(object-collides-with-player? enemy-ship-rect))
(ship-collisions
(filter enemy-collides? enemy-ships))
(fireball-collides?
(object-collides-with-player? enemy-fireball-rect))
(fireball-collisions
(filter fireball-collides? enemy-fireballs)))
(or
(not (nil? ship-collisions))
(not (nil? fireball-collisions)))))
(define (upgrade-collect-script)
(forever
(let* ((player-touches-upgrade? (object-collides-with-player? upgrade-rect))
(collected-upgrades (filter player-touches-upgrade? flying-upgrades)))
(if (not (nil? collected-upgrades))
(for-each
(lambda (upgrade)
(set! flying-upgrades (delete upgrade flying-upgrades))
(set! player-ship-upgrades (cons (upgrade-type upgrade) player-ship-upgrades))
(add-explosion player-position))
collected-upgrades))
(sleep 1))))
(define (move-upgrades)
(forever
(for-each
(lambda (upgrade)
(upgrade-position-set! upgrade (vec2+ (vec2 0 -1) (upgrade-position upgrade))))
flying-upgrades)
(sleep 1)))
(define (clear-upgrades-outside-view)
(let ((upgrade-inside-view?
(object-inside-view-fn (object-inside-view-fn
(lambda (upgrade-item)
(upgrade-position upgrade-item))))))
(forever
(set! flying-upgrades (filter upgrade-inside-view? flying-upgrades))
(sleep 1))))
(define (draw-upgrade)
(for-each
(lambda (upgrade)
(draw-sprite upgrade-texture (upgrade-position upgrade) #:tint upgrade-color))
flying-upgrades))
(define (spawn-scripts)
(spawn-script move-enemies)
(spawn-script spawn-enemy-fireballs)
(spawn-script clear-hit-enemies-script)
(spawn-script clear-enemies-outside-view)
(spawn-script upgrade-collect-script)
(spawn-script move-upgrades)
(spawn-script spawn-enemies)
(at 1
(script
(forever
(wait-until (player-collides?))
(add-explosion player-position)
(sleep 1)
(set! game-over #t)
(set! agenda-dt 0))))
(at 1000
(script
(let* ((x-pos (random SCREEN-WIDTH))
(new-upgrade (make-upgrade (vec2 x-pos (- SCREEN-HEIGHT 40)) 'multi-fireballs)))
(set! flying-upgrades (cons new-upgrade flying-upgrades))))))
(spawn-scripts)
(define (draw alpha)
(draw-sprite background-map (vec2 0 0) #:rect (rect 0 0 SCREEN-WIDTH SCREEN-HEIGHT))
(move-player!)
(draw-fireballs)
(move-fireballs)
(move-enemy-fireballs)
(draw-enemy-fireballs)
(draw-rocket)
(move-rocket)
(draw-upgrade)
(draw-hud)
;(draw-debug)
(update-agenda agenda-dt)
(if game-over (draw-game-end))
(draw-enemies)
(clear-old-explosions!)
(draw-explosions!)
(if (not game-over)
(draw-sprite player-ship-sprite player-position)))
(define (update dt)
(poll-coop-repl-server repl))
(run-game
#:load load
#:update update
#:window-width SCREEN-WIDTH
#:window-height SCREEN-HEIGHT
#:key-press key-press
#:key-release key-release
#:draw draw)