-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.scm
291 lines (245 loc) · 10.9 KB
/
main.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
(define (pre-main-hook)
(move-camera (vertex 5 10 5))
;(turn-camera (vertex 45 225 0))
)
(define (game-looper delta)
;(print "Do something here!")
;(print delta)
(let* ((total-time (- (current-milliseconds) *start-time*))
(seconds (/ (inexact->exact (truncate total-time)) 1000))
(current-texture (inexact->exact (truncate (remainder seconds 2)))))
;(print seconds)
;(print current-texture)
;(print delta)
;(move-camera-forward (- 0.1))
(update-camera delta)
(adjust-for-camera)
;(camera-debug print)
;; (do ((x -20.0 (+ x 1))) ((> x 20))
;; (do ((z -2.0 (+ z 1))) ((> z 2))
;; (with-new-matrix
;; (textured-quad (u32vector-ref texture-handles 2)
;; (vertex x -0.5 z)
;; (vertex (+ x 1) -0.5 z)
;; (vertex (+ x 1) -0.5 (- z 1))
;; (vertex x -0.5 (- z 1))))))
(draw-floor)
(draw-trees)
(draw-custom-characters)
(with-new-matrix
(flat-triangle (color 0.25 0.5 0.75)
(vertex -1 1 -2)
(vertex 1 1 -2)
(vertex 1 0 -2)))))
(define tree-count 10)
(define tree-texture-handle 3)
(define tree-positions
(let* ((positions (make-vector tree-count)))
;; (do ((tree 0 (+ 1 tree))) ((equal? tree tree-count))
;; (vector-set! positions tree (vertex (/ (- (random 1000) 500) 100)
;; 0
;; (/ (- (random 1000) 500) 100))))
(vector-set! positions 0 (vertex -10 0 1))
(vector-set! positions 1 (vertex -10 0 2))
(vector-set! positions 2 (vertex -10 0 3))
(vector-set! positions 3 (vertex -10 0 4))
(vector-set! positions 4 (vertex -10 0 5))
(vector-set! positions 5 (vertex 10 0 6))
(vector-set! positions 6 (vertex 10 0 7))
(vector-set! positions 7 (vertex 10 0 8))
(vector-set! positions 8 (vertex 10 0 9))
(vector-set! positions 9 (vertex 10 0 10))
positions))
(define tree-heights
(let* ((heights (make-vector tree-count)))
(do ((tree 0 (+ 1 tree))) ((equal? tree tree-count))
(vector-set! heights tree (+ (/ (random 8) 2) 2)))
heights))
(define tree-rotations
(let* ((rotations (make-vector tree-count)))
(do ((tree 0 (+ 1 tree))) ((equal? tree tree-count))
;(vector-set! rotations tree (/ (- (random 1080) 720) 2)))
(vector-set! rotations tree 0))
rotations))
(define (draw-tree n)
(let ((position (vector-ref tree-positions n))
(rotation (vector-ref tree-rotations n))
(height (vector-ref tree-heights n)))
;(print (vertex->string position))
(with-new-matrix
(gl:Rotatef rotation 0 1 0)
(gl:Translatef (vertex-x position) (vertex-y position) (vertex-z position))
(textured-quad (u32vector-ref texture-handles tree-texture-handle)
(vertex 0 height 0)
(vertex 1 height 0)
(vertex 1 0 0)
(vertex 0 0 0)))))
(define (draw-trees)
(do ((counter 0 (+ counter 1))) ((equal? counter tree-count))
(draw-tree counter)))
;; ----- Characters -----
(define character-count 20)
(define character-texture-handle 0)
(define character-positions
(let* ((positions (make-vector character-count)))
(do ((character 0 (+ 1 character))) ((equal? character character-count))
;;(vector-set! positions character (vertex 0 -5 0)))
(vector-set! positions character (vertex (/ (- (random 1000) 500) 1000)
-0
(/ (- (random 1000) 500) 1000))))
positions))
(define character-velocities
(let* ((velocities (make-vector character-count)))
(do ((character 0 (+ 1 character))) ((equal? character character-count)))
velocities))
(define (update-character-positions)
(do ((character 0 (+ 1 character))) ((equal? character character-count))
(vector-set! character-positions character
(vertex-add (vector-ref character-positions character) (vector-ref character-velocities character)))
(vector-set! character-velocities character (vertex 0 0 0))))
(define character-rotations
(let* ((rotations (make-vector character-count)))
(do ((character 0 (+ 1 character))) ((equal? character character-count))
(vector-set! rotations character (/ (- (random 1080) 720) 2)))
rotations))
(define (characters-wander)
(do ((character 0 (+ 1 character))) ((equal? character character-count))
(vector-set! character-velocities character
(vertex (/ (- (random 1) 1) 10) 0 (/ (- (random 1) 1) 10)))))
(define (draw-custom-characters)
(draw-character (vertex -10.0 1 5.0) 90 1)
(draw-character (vertex -9.0 1 5.0) 90 1)
(draw-character (vertex -8.0 1 5.0) 90 1)
(draw-character (vertex -7.0 1 5.0) 90 1)
(draw-character (vertex -6.0 1 5.0) 90 1)
(draw-character (vertex -5.0 1 5.0) 90 1)
(draw-character (vertex -10.0 1 4.0) 90 1)
(draw-character (vertex -9.0 1 4.0) 90 1)
(draw-character (vertex -8.0 1 4.0) 90 1)
(draw-character (vertex -7.0 1 4.0) 90 1)
(draw-character (vertex -6.0 1 4.0) 90 1)
(draw-character (vertex -5.0 1 4.0) 90 1)
(draw-character (vertex -10.0 1 6.0) 90 1)
(draw-character (vertex -9.0 1 6.0) 90 1)
(draw-character (vertex -8.0 1 6.0) 90 1)
(draw-character (vertex -7.0 1 6.0) 90 1)
(draw-character (vertex -6.0 1 6.0) 90 1)
(draw-character (vertex -5.0 1 6.0) 90 1)
(draw-character (vertex -10.0 1 7.0) 90 1)
(draw-character (vertex -9.0 1 7.0) 90 1)
(draw-character (vertex -8.0 1 7.0) 90 1)
(draw-character (vertex -7.0 1 7.0) 90 1)
(draw-character (vertex -6.0 1 7.0) 90 1)
(draw-character (vertex -5.0 1 7.0) 90 1)
; v's
(draw-character (vertex -10.0 1 -1.0) 90 1)
(draw-character (vertex -9.0 1 -2.0) 90 1)
(draw-character (vertex -8.0 1 -3.0) 90 1)
(draw-character (vertex -7.0 1 -4.0) 90 1)
(draw-character (vertex -6.0 1 -3.0) 90 1)
(draw-character (vertex -5.0 1 -2.0) 90 1)
(draw-character (vertex -4.0 1 -1.0) 90 1)
(draw-character (vertex -7.0 1 -5.0) 90 1)
(draw-character (vertex -6.0 1 -4.0) 90 1)
(draw-character (vertex -5.0 1 -3.0) 90 1)
(draw-character (vertex -4.0 1 -2.0) 90 1)
(draw-character (vertex -3.0 1 -3.0) 90 1)
(draw-character (vertex -2.0 1 -4.0) 90 1)
(draw-character (vertex -1.0 1 -5.0) 90 1)
)
(define (draw-character position
rotation
scale)
(let ((texture-number (remainder (- (current-milliseconds) *start-time*) 2)))
(with-new-matrix
(gl:Rotatef rotation 0 1 0)
(gl:Translatef (vertex-x position) (vertex-y position) (vertex-z position))
(textured-quad (u32vector-ref texture-handles texture-number)
(vertex 0 1 0)
(vertex 1 1 0)
(vertex 1 0 0)
(vertex 0 0 0)))))
(define (draw-character-from-array n)
(characters-wander)
(update-character-positions)
(let ((position (vector-ref character-positions n))
(rotation (vector-ref character-rotations n)))
;(print (vertex->string position))
(with-new-matrix
;(gl:Rotatef rotation 0 1 0)
(gl:Translatef (vertex-x position) (vertex-y position) (vertex-z position))
(textured-quad (u32vector-ref texture-handles character-texture-handle)
(vertex 0 1 0)
(vertex 1 1 0)
(vertex 1 0 0)
(vertex 0 0 0)))))
(define (draw-characters)
(do ((counter 0 (+ counter 1))) ((equal? counter character-count))
(draw-character-from-array counter)))
;; floor
(define (draw-floor)
(let ((grass-texture (u32vector-ref texture-handles 5))
(water-texture (u32vector-ref texture-handles 6))
(grass-water-transition-texture (u32vector-ref texture-handles 7))
(grid-texture (u32vector-ref texture-handles 2))
(base -5))
; river
(do ((x 0 (+ x 1))) ((> x 2))
(do ((z 0 (+ z 1))) ((> z 10))
(bottomless-cube water-texture
water-texture
(vertex x 0 z)
1)))
; land
(do ((x 4 (+ x 1))) ((> x 10))
(do ((z 0 (+ z 1))) ((> z 10))
(bottomless-cube grass-texture
grass-texture
(vertex x 0 z)
1)))
(do ((z 0 (+ z 1))) ((> z 10))
(bottomless-cube grass-water-transition-texture
grid-texture
(vertex 3 0 z)
1))
(do ((z 0 (+ z 1))) ((> z 10))
(bottomless-cube grass-water-transition-texture
grid-texture
(vertex -1 0 z)
1
'flip-horizontal))
(do ((x -10 (+ x 1))) ((equal? x -1))
(do ((z 0 (+ z 1))) ((> z 10))
(bottomless-cube grass-texture
grass-texture
(vertex x 0 z)
1)))))
(define (handle-keyboard-state delta)
(let* ((delta (+ (- (current-milliseconds) *last-frame*) 1))
(turn-sensitivity (* 1.0 delta))
(move-sensitivity (* 0.01 delta))
(walk-length (* 5 delta)))
;(print delta)
(if (kb:key-pressed? glut:KEY_LEFT) (turn-camera (vertex 0 (- turn-sensitivity) 0)))
(if (kb:key-pressed? glut:KEY_RIGHT) (turn-camera (vertex 0 turn-sensitivity 0)))
(if (kb:key-pressed? glut:KEY_UP) (move-camera-forward walk-length))
(if (kb:key-pressed? glut:KEY_DOWN) (move-camera-forward (- walk-length)))
;; (if (kb:key-pressed? glut:KEY_LEFT) (iso-move-camera (vertex 1 0 0)))
;; (if (kb:key-pressed? glut:KEY_RIGHT) (iso-move-camera (vertex -1 0 0)))
;; (if (kb:key-pressed? glut:KEY_UP) (iso-move-camera (vertex 0 0 1)))
;; (if (kb:key-pressed? glut:KEY_DOWN) (iso-move-camera (vertex 0 0 -1)))
;; (if (kb:key-pressed? #\q) (iso-turn-camera (vertex 0 1 0)))
;; (if (kb:key-pressed? #\e) (iso-turn-camera (vertex 0 -1 0)))
(if (kb:key-pressed? #\a) (strafe-camera (vertex walk-length 0 0)))
(if (kb:key-pressed? #\d) (strafe-camera (vertex (- walk-length) 0 0)))
(if (kb:key-pressed? #\a) (strafe-camera (vertex walk-length 0 0)))
(if (kb:key-pressed? #\d) (strafe-camera (vertex (- walk-length) 0 0)))
(if (kb:key-pressed? #\w) (strafe-camera (vertex 0 walk-length 0)))
(if (kb:key-pressed? #\s) (strafe-camera (vertex 0 (- walk-length) 0)))
(if (kb:key-pressed? #\z) (move-camera (vertex 0 walk-length 0)))
(if (kb:key-pressed? #\x) (move-camera (vertex 0 (- walk-length) 0)))
(if (kb:key-pressed? #\r) (turn-camera (vertex 0 (- turn-sensitivity) 0)))
(if (kb:key-pressed? #\f) (turn-camera (vertex 0 turn-sensitivity 0)))
(if (kb:key-pressed? #\q) (orbit-camera (vertex 0 10 0)))
(if (kb:key-pressed? #\e) (orbit-camera (vertex 0 -10 0)))
(if (kb:key-pressed? #\space) (returnable-repl))))