summaryrefslogtreecommitdiff
path: root/game.scm
blob: edf43fc3090088ce18976aed35b4b96eba0ec2e2 (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
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
(define-module (game)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics font)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics viewport)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (srfi srfi-1)
  #:use-module (starling asset)
  #:use-module (starling gui)
  #:use-module (starling kernel)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:use-module (starling ring-buffer)
  #:use-module (starling scene)
  #:duplicates (merge-generics)
  #:export (launch-game))


;;;
;;; Constants
;;;

(define window-width 1280)
(define window-height 720)
(define game-width 640)
(define game-height 360)
(define player-display-name "")


;;;
;;; Assets
;;;

(define-asset dialog-box-texture (load-image "assets/images/dialog-box.png"))
(define-asset darkness (load-image "assets/images/darkness.png"))
(define-asset room-background (load-image "assets/images/room.png"))
(define-asset door-lock-texture (load-image "assets/images/door-lock.png"))
(define-asset terminal-texture (load-image "assets/images/terminal.png"))
(define-asset terminal-background (load-image "assets/images/terminal-background.png"))
(define-asset monogram-font (load-font "assets/fonts/monogram_extended.ttf" 12))
(define-asset monogram-font-big (load-font "assets/fonts/monogram_extended.ttf" 24))


;;;
;;; Text Box
;;;

(define-class <text-box> (<widget>)
  (text #:accessor text #:init-keyword #:text #:init-value "" #:watch? #t))

(define-method (on-change (text-box <text-box>) slot-name old new)
  (case slot-name
    ((text)
     (let ((l (& text-box text)))
       (when l
         (set! (text l) new))))
    (else
     (next-method))))

(define-method (apply-theme (text-box <text-box>))
  (next-method)
  (replace text-box
    (make <label>
      #:name 'text
      #:rank 1
      #:font (font text-box)
      #:text (text text-box)
      #:position (vec2 6.0
                       (- (height text-box)
                          (font-line-height
                           (asset-ref
                            (font text-box))))))))


;;;
;;; Device
;;;

(define %device-hover-tint (rgb #xff7777))

;; An object you can interact with by clicking.
(define-class <device> (<margin-container>)
  (texture #:accessor texture #:init-keyword #:texture
           #:init-value null-texture #:watch? #t))

(define-method (refresh-hover-state (device <device>))
  ;; A crude way of showing the user something is clickable.
  (set! (tint (& device sprite))
        (if (hover? device)
            %device-hover-tint
            white)))

(define-method (on-change (device <device>) slot-name old new)
  (case slot-name
    ((hover?)
     (refresh-hover-state device))
    ((texture)
     (let ((sprite (& device sprite)))
       (when sprite
         (set! (texture sprite) new))))
    (else
     (next-method))))

(define-method (apply-theme (device <device>))
  (next-method)
  (replace device
    (make <sprite>
      #:name 'sprite
      #:rank 1
      #:texture (texture device)))
  (refresh-hover-state device))


;;;
;;; Game
;;;

(define-theme gui-theme
  (<widget> (font monogram-font))
  (<button> (background dialog-box-texture)
            (press-background dialog-box-texture))
  (<text-box> (background dialog-box-texture)))

(define-class <game> (<gui-scene>)
  (state #:accessor state #:init-value #f)
  (friendship #:accessor friendship #:init-value 0)
  (dialog-container #:accessor dialog-container)
  (click-channel #:accessor click-channel #:init-thunk make-channel)
  (terminal-locked? #:accessor terminal-locked? #:init-value #t)
  (terminal-channel #:accessor terminal-channel #:init-thunk make-channel)
  (terminal-lines #:accessor terminal-lines #:init-form (make-ring-buffer 17)))

(define-method (reboot (game <game>))
  (set! (click-channel game) (make-channel))
  (set! (terminal-locked? game) #t)
  (set! (terminal-channel game) (make-channel))
  (set! (terminal-lines game) (make-ring-buffer 17))
  (next-method))

(define-method (detach-all (game <game>))
  (for-each detach (children game)))

(define (run-on-left-click node proc)
  (lambda (widget button)
    (if (eq? button 'left)
        (begin
          (run-script node (proc))
          #t)
        #f)))

(define-method (dialog (game <game>) name line)
  (let ((old-state (state game))
        (c (dialog-container game)))
    (set! (state game) 'dialog)
    (attach-to game c)
    (set! (text (& c name-margin name)) name)
    (set! (text (& c text-box)) line)
    (channel-get (click-channel game))
    (detach c)
    (set! (state game) old-state)))

(define-method (intro (game <game>))
  (set! (state game) 'intro)
  (attach-to game
             (make <sprite>
               #:name 'intro-darkness
               #:texture darkness))
  (dialog game player-display-name "> ...")
  (dialog game player-display-name "> Where am I?")
  (dialog game player-display-name "> What is this place?")
  (detach-all game)
  (explore game))

(define-method (pin-entry (game <game>))
  (let ((old-state (state game))
        (pin-channel (make-channel)))
    (define (margin widget)
      (make <margin-container>
        #:margin 1.0
        #:children (list widget)))
    (define (number-button n)
      (margin
       (make <button>
         #:width 32.0
         #:height 32.0
         #:text (number->string n)
         #:listeners
         `((click . ,(run-on-left-click game
                                        (lambda ()
                                          (input n))))))))
    (define (input n)
      (let ((pin (& game pin-pad display pin)))
        (when (< (string-length (text pin)) 6)
          (set! (text pin)
                (string-append (text pin)
                               (number->string n))))))
    (define (clear)
      (set! (text (& game pin-pad display pin)) ""))
    (define (submit)
      (channel-put! pin-channel (text (& game pin-pad display pin))))
    (set! (state game) 'pin-entry)
    (parameterize ((current-theme gui-theme))
      (attach-to game
                 ;; Hack to block clicks on devices in the room. Not working!
                 (make <margin-container>
                   #:name 'blocker
                   #:rank 98
                   #:width game-width
                   #:height game-height
                   #:listeners
                   `((click . ,(lambda (widget button)
                                 (pk 'hi)
                                 #t))))
                 (make <vertical-container>
                   #:name 'pin-pad
                   #:rank 99
                   #:position (vec2 (- (/ game-width 2.0)
                                       (/ (* 5.0 32.0) 2.0))
                                    (- (/ game-height 2.0)
                                       (/ (* 3.0 32.0) 2.0)))
                   #:children
                   (list (make <horizontal-container>
                           #:name 'display
                           #:children
                           (list (make <label>
                                   #:name 'pin
                                   #:font monogram-font-big
                                   #:text "")))
                         (make <horizontal-container>
                           #:name 'numbers-1
                           #:children
                           (list (number-button 0)
                                 (number-button 1)
                                 (number-button 2)
                                 (number-button 3)
                                 (number-button 4)))
                         (make <horizontal-container>
                           #:name 'numbers-2
                           #:children
                           (list (number-button 5)
                                 (number-button 6)
                                 (number-button 7)
                                 (number-button 8)
                                 (number-button 9)))
                         (make <horizontal-container>
                           #:name 'submission
                           #:children
                           (list (margin
                                  (make <button>
                                    #:name 'submit
                                    #:text "CLEAR"
                                    #:width 64.0
                                    #:height 32.0
                                    #:listeners
                                    `((click . ,(run-on-left-click game clear)))))
                                 (margin
                                  (make <button>
                                    #:name 'submit
                                    #:text "ENTER"
                                    #:width 64.0
                                    #:height 32.0
                                    #:listeners
                                    `((click . ,(run-on-left-click game submit)))))))))))
    (let ((pin (channel-get pin-channel)))
      (detach (& game blocker) (& game pin-pad))
      (set! (state game) old-state)
      pin)))

(define-method (terminal (game <game>))
  (let ((old-state (state game))
        (lines (terminal-lines game))
        (input "")
        (max-input-chars 48))
    (define (prompt)
      (if (terminal-locked? game)
          "password: "
          "$ "))
    (define (passwordify-maybe str)
      (if (terminal-locked? game)
          (make-string (string-length str) #\*)
          str))
    (define (refresh-output)
      (set! (text (& game terminal-group output))
            (string-concatenate
             (let loop ((i 0))
               (if (< i (ring-buffer-length lines))
                   (cons* (ring-buffer-ref lines i)
                          "\n"
                          (loop (+ i 1)))
                   (list (prompt)
                         (passwordify-maybe input)))))))
    (define (run-command command)
      (match command
        (("exit")
         (ring-buffer-put! lines "logout")
         #f)
        (("diagnostic" level)
         (let ((n (string->number level)))
           (if (and (integer? n) (positive? n))
               (begin
                 (ring-buffer-put! lines (string-append "running level " level " diagnostic..."))
                 (ring-buffer-put! lines "...")
                 (ring-buffer-put! lines "complete!")
                 (ring-buffer-put! lines "replenish dilithium crystals")
                 #t)
               (begin
                 (ring-buffer-put! lines "expected an integer")
                 #t))))
        ((name . _)
         (ring-buffer-put! lines "error: no such command")
         #t)))
    (set! (state game) 'terminal)
    (attach-to game
               (make <node-2d>
                 #:name 'terminal-group
                 #:rank 9
                 #:children
                 (list (make <sprite>
                         #:name 'background
                         #:texture terminal-background)
                       (make <label>
                         #:name 'output
                         #:font monogram-font
                         #:position
                         (vec2 162.0
                               (- 300.0 (font-line-height
                                         (asset-ref monogram-font))))))))
    (unless (terminal-locked? game)
      (ring-buffer-put! lines "login"))
    (refresh-output)
    (let loop ()
      (if (match (channel-get (terminal-channel game))
            ('backspace
             (set! input (substring input 0 (max (- (string-length input) 1) 0)))
             #t)
            ('return
             (ring-buffer-put! lines (string-append (prompt) (passwordify-maybe input)))
             (if (terminal-locked? game)
                 (begin
                   (if (string=? input "password")
                       (begin
                         (ring-buffer-put! lines "login successful")
                         (set! (terminal-locked? game) #f))
                       (ring-buffer-put! lines "incorrect password"))
                   (set! input ""))
                 (let ((continue? (run-command (remove string-null? (string-split input #\space)))))
                   (set! input "")
                   continue?)))
            (str
             (let ((new-input (string-append input str)))
               (set! input
                     (if (> (string-length new-input) 48)
                         (substring new-input 0 48)
                         new-input)))))
          (begin
            (refresh-output)
            (loop))
          (begin
            (detach (& game terminal-group))
            (set! (state game) old-state))))))

(define-method (explore (game <game>))
  (define (tint-all color)
    (set! (tint (& game room-background)) color)
    (set! (tint (& game door-lock sprite)) color)
    (set! (tint (& game terminal sprite)) color))
  (define (fade-in)
    (tween 60 black white tint-all
           #:interpolate color-lerp))
  (define (fade-out)
    (tween 60 white black tint-all
           #:interpolate color-lerp))
  (define (end-game)
    (run-script game
      (fade-out)
      (detach-all game)
      (if (> (friendship game) 3)
          (good-ending game)
          (bad-ending-1 game))))
  (define (open-door)
    (if (string=? (pin-entry game) "314159")
        (end-game)
        (pk 'invalid-access-code)))
  (define (open-terminal)
    (terminal game))
  (attach-to game
             (make <sprite>
              #:name 'room-background
              #:texture room-background)
             (make <device>
               #:name 'door-lock
               #:rank 1
               #:texture door-lock-texture
               #:position (vec2 586.0 196.0)
               #:listeners
               `((click . ,(run-on-left-click game open-door))))
             (make <device>
               #:name 'terminal
               #:rank 1
               #:texture terminal-texture
               #:position (vec2 109.0 40.0)
               #:listeners
               `((click . ,(run-on-left-click game open-terminal)))))
  (fade-in))

(define-method (good-ending (game <game>))
  (set! (state game) 'good-ending)
  (attach-to game
             (make <sprite>
               #:name 'darkness
               #:texture darkness)
             (make <label>
               #:name 'message
               #:font monogram-font
               #:text "this is the good ending. I am so proud of you. congratulations."
               #:align 'center
               #:vertical-align 'center
               #:position (vec2 (/ game-width 2.0) (/ game-height 2.0)))))

(define-method (bad-ending-1 (game <game>))
  (set! (state game) 'bad-ending-1)
  (attach-to game
             (make <sprite>
               #:name 'darkness
               #:texture darkness)
             (make <label>
               #:name 'message
               #:font monogram-font
               #:text "uh oh this is the bad ending. sorry, bucko."
               #:align 'center
               #:vertical-align 'center
               #:position (vec2 (/ game-width 2.0) (/ game-height 2.0)))))

(define-method (on-boot (game <game>))
  (set! (cameras game)
        (list (make <camera-2d>
                #:resolution (vec2 game-width game-height)
                #:viewport (make-viewport 0 0 window-width window-height
                                          #:clear-color black))))
  ;; Dialog nodes.
  (parameterize ((current-theme gui-theme))
    (set! (dialog-container game)
          (make <vertical-container>
            #:name 'dialog-container
            #:rank 999
            #:position (vec2 (/ (* game-width .25) 2.0) 0.0)
            #:children
            (list (make <margin-container>
                    #:name 'name-margin
                    #:margin 4.0
                    #:children
                    (list (make <label>
                            #:name 'name
                            #:font monogram-font)))
                  (make <text-box>
                    #:name 'text-box
                    #:width (round (* game-width .75))
                    #:height 60.0)))))
  (run-script game
    (intro game)))

(define-method (on-mouse-release (game <game>) button x y)
  (case (state game)
    ((dialog)
     (when (eq? button 'left)
       (channel-put! (click-channel game) #t)))
    ((good-ending bad-ending-1)
     (when (eq? button 'left)
       (detach-all game)
       (run-script game
         (intro game))))
    (else
     (next-method))))

(define-method (on-key-press (game <game>) key modifiers repeat?)
  (case (state game)
    ((terminal)
     (case key
       ((backspace return)
        (channel-put! (terminal-channel game) key))))))

(define-method (on-text-input (game <game>) text)
  (case (state game)
    ((terminal)
     (channel-put! (terminal-channel game) text))))

(define (launch-game)
  (boot-kernel (make <kernel>
                 #:window-config (make <window-config>
                                   #:title "Spring Lisp Game Jam 2021"
                                   #:width window-width
                                   #:height window-height))
               (lambda () (make <game>))))