summaryrefslogtreecommitdiff
path: root/scripts/main.scm
blob: 9db17fc2f5f4090a655f4c9307760cd5971a12ea (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
(load "./scripts/structs.scm")
(load "./scripts/utils.scm")
(load "./scripts/prompt.scm")
(load "./scripts/gol.scm")

(define current-stage-number 0)
(define current-generation 0)
(define current-pressed-key #f)
(define starting-cells (list))
(define trigger-level-reload #t)
(define overlay-fade-time 0.0)
(define is-advancing #f)


(define stages (list (list '(1 . 0)
			   '(1 . 1)
			   '(0 . 0)
			   '(0 . 1))
		     (list '(0 . -1)
			   '(1 . 0)
			   '(0 . 1)
			   '(-1 . 0))
		     (list '(0 . 0)
			   '(0 . 1)
			   '(0 . 2))
		     (list '(0 . 0)
			   '(-1 . 0)
			   '(0 . -1))
		     (list '(1 . 0)
			   '(0 . 1))
		     (list '(2 . 0)
			   '(1 . 0)
			   '(1 . 1)
			   '(0 . 1)
			   '(0 . 2))
		     (list '(-1 . -1)
			   '(0 . -1)
			   '(1 . 0)
			   '(0 . 1)
			   '(-1 . 1)
			   '(-2 . 0))
		     (list '(-1 . -1)
			   '(0 . -1)
			   '(1 . -1)
			   '(-1 . 0)
			   '(1 . 0)
			   '(-1 . 1)
			   '(0 . 1)
			   '(1 . 1))
		     (list '(0 . -1)
			   '(0 . 0)
			   '(0 . 1)
			   '(1 . -1)
			   '(1 . 0)
			   '(1 . 1)
			   '(2 . 0))
		     ))

(define (update-generation)
  (clear-cells)
  (set-cells-array starting-cells)

  (let loop ((times 0))
    (if (< times current-generation)
	(begin (run-step)
	       (loop (+ times 1)))
	)
    )
  )

(define (is-solution? l1)
  (define trigger #t)
  (for-each (lambda (c)
	      (if (not (hash-table-ref current-cells c))
		  (begin (set! trigger #f)))
	      )
	    (stages current-stage-number))
  (and trigger (eq? (length (stages current-stage-number)) (length (hash-table-keys current-cells))))
  )

(define (update-game)
  
  (set! current-pressed-key (rl-get-key-pressed))
  
  (prompt-update)

  (if trigger-level-reload
      (begin (set! starting-cells (list))
	     (clear-cells)
	     (set-cells-array starting-cells)
	     (set! trigger-level-reload #f)))
  
  (cond ((and (not prompt-active) (not is-advancing))
	 (if (eq? current-pressed-key KEY_LEFT)
	     (cond ((> current-generation 0)
		    (set! current-generation (- current-generation 1))
		    (update-generation)))
	     )
	 
	 (if (eq? current-pressed-key KEY_RIGHT)
	     (cond ((< current-generation 1)
		    (set! current-generation (+ current-generation 1))
		    (update-generation)))
	     )
	 
	 ))

  (if (and (eq? current-generation 1) (not is-advancing))
      (if (is-solution? (stages current-stage-number))
	  (begin (set! is-advancing #t)
		 (set! overlay-fade-time 0.0))
	  )
      )

  (if (and (rl-is-mouse-button-pressed) (eq? current-generation 0))
      (let ((mouse-pos (rl-get-mouse-position)))
	(define tile-x (floor ( / (- (mouse-pos 0) 275) 50)))
	(define tile-y (floor ( / (- (mouse-pos 1) 200) 50)))
	(if (and (>= tile-x -1)
		 (<= tile-x 1)
		 (>= tile-y -1)
		 (<= tile-y 1))
	    (begin (toggle-cell `(,tile-x . ,tile-y))
		   (set! starting-cells (get-cells-list))))
	))

  (if is-advancing
      (set! overlay-fade-time (+ overlay-fade-time (rl-get-frame-time)))
      (set! overlay-fade-time (- overlay-fade-time (rl-get-frame-time)))
      )

  (if (> overlay-fade-time 1.0)
      (begin (set! is-advancing #f)
	     (clear-cells)
	     (set! starting-cells (list))
	     (set! current-generation 0)
	     (set! current-stage-number (+ current-stage-number 1))
	     )
      )
  )

(define (update-end)
  (if is-advancing
      (set! overlay-fade-time (+ overlay-fade-time (rl-get-frame-time)))
      (set! overlay-fade-time (- overlay-fade-time (rl-get-frame-time)))
      )
  )

(define (update)
  (if (>= current-stage-number (length stages))
      (update-end)
      (update-game)
      )
  )

(define (draw-game)
  (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 255))
  
  (prompt-draw)
  
  (rl-draw-text (string-append "Level " (number->string current-stage-number)) 75 15 20 (make-color 0 0 0 255))

  (draw-cells)

  (draw-grid)

  (draw-generation)
  
  (define lerped-alpha (* 255.0 overlay-fade-time) )
  (if (< lerped-alpha 0.0)
      (set! lerped-alpha 0.0))

  (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 (floor lerped-alpha)))
  )

(define (draw-end)
  (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 255))
  
  (rl-draw-text "The end! Thank you for playing :)"  38 200 30 (make-color 0 0 0 255))

  (rl-draw-text "Made with Raylib + S7 Scheme"  146 340 20 (make-color 0 0 0 255))
  
  (define lerped-alpha (* 255.0 overlay-fade-time) )
  (if (< lerped-alpha 0.0)
      (set! lerped-alpha 0.0))

  (rl-draw-rectangle 0 0 600 450 (make-color 255 255 255 (floor lerped-alpha)))
  )

(define (draw-generation)
  
  (let loop ((times 0))
    (if (< times 2)
	(begin
	  (rl-draw-rectangle (+ 75 (* times 40))
			     410
			     30
			     30
			     (make-color 190 190 190 255))
	  (if (eq? times current-generation)
	      (rl-draw-rectangle (+ 80 (* times 40))
				 415
				 20
				 20
				 (make-color 0 0 0 255)))
	  
	  (loop (+ times 1))
	  )))

  )

(define (draw)
  (if (>= current-stage-number (length stages))
      (draw-end)
      (draw-game)
      )
  )


(define (draw-grid)
  (define cell-width 50)
  (define cell-height 50)
  
  (let loop ((times 10))
    (if (> times 0)
	(begin (rl-draw-line (+ (* times cell-width) (* cell-width 0.5))
			     50
			     (+ (* times cell-width) (* cell-width 0.5))
			     400
			     (make-color 200 200 200 255))
	       (loop (- times 1)))
	)
    )

  (let loop ((times 8))
    (if (> times 0)
	(begin (rl-draw-line 75
			     (* times cell-height)
			     525
			     (* times cell-height)
			     (make-color 200 200 200 255))
	       (loop (- times 1)))
	)
    )

  (let loop ((times 4))
    (if (> times 0)
	(begin (rl-draw-line (+ (* times cell-width) 175)
			     150
			     (+ (* times cell-width) 175)
			     300
			     (make-color 0 0 0 255))
	       (loop (- times 1)))
	)
    )

  (let loop ((times 4))
    (if (> times 0)
	(begin (rl-draw-line 225
			     (+ (* times cell-height) 100)
			     375
			     (+ (* times cell-height) 100)
			     (make-color 0 0 0 255))
	       (loop (- times 1)))
	)
    )
  )

(define (draw-cells)
  (define cell-width 50)
  (define cell-height 50)
  (define cell-color (if (eq? current-generation 0)
			 (make-color 190 100 255 255)
			 (make-color 255 190 100 255)))

  
  (for-each (lambda (cell)
	      (rl-draw-rectangle
	       (+ (* (car cell) cell-width) (- 300 (* cell-width 0.5)) )
	       (+ (* (cdr cell) cell-height) (- 225 (* cell-height 0.5)) )
	       cell-width
	       cell-height
	       cell-color)
	      
	      ) (hash-table-keys current-cells))


  (for-each (lambda (cell)
	      (rl-draw-rectangle
	       (+ (* (car cell) 50) 290 )
	       (+ (* (cdr cell) 50) 215 )
	       20
	       20
	       (make-color 255 190 100 255))
	      
	      ) (stages current-stage-number))

  
  )