summaryrefslogtreecommitdiff
path: root/game.scm
blob: 7277322bff57ebf8135b863789c08aeeac1883b0 (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
(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 (starling asset)
  #:use-module (starling gui)
  #:use-module (starling kernel)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #: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 scanner-texture (load-image "assets/images/scanner.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
;;;

;; 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 (on-change (device <device>) slot-name old new)
  (case slot-name
    ((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
      #:texture (texture 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 #:getter click-channel #:init-thunk make-channel))

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

(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 . ,(lambda (widget button)
                       (if (eq? button 'left)
                           (begin
                             (input n)
                             #t)
                           #f)))))))
    (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.
                 (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 . ,(lambda (widget button)
                                                  (if (eq? button 'left)
                                                      (begin
                                                        (clear)
                                                        #t)
                                                      #f))))))
                                 (margin
                                  (make <button>
                                    #:name 'submit
                                    #:text "ENTER"
                                    #:width 64.0
                                    #:height 32.0
                                    #:listeners
                                    `((click . ,(lambda (widget button)
                                                  (if (eq? button 'left)
                                                      (begin
                                                        (submit)
                                                        #t)
                                                      #f))))))))))))
    (let ((pin (channel-get pin-channel)))
      (pk 'pin pin)
      (detach (& game blocker) (& game pin-pad))
      (set! (state game) old-state))))

(define-method (explore (game <game>))
  (define (tint-all color)
    (set! (tint (& game room-background)) color)
    (set! (tint (& game scanner 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))))
  (attach-to game
             (make <sprite>
              #:name 'room-background
              #:texture room-background)
             (make <device>
               #:name 'scanner
               #:rank 1
               #:texture scanner-texture
               #:position (vec2 586.0 196.0)
               #:listeners
               `((click . ,(lambda (widget button)
                             (if (eq? button 'left)
                                 (begin
                                   (run-script game
                                     (pin-entry game))
                                   #t)
                                 #f))))))
  (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
            #: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 game-width
                    #:height 50.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 (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>))))