5 (use-modules (gacela gacela)
9 (set-game-properties! #:title "Gacela Asteroids")
11 (define max-x (/ (assoc-ref (get-game-properties) 'width) 2))
12 (define min-x (- max-x))
13 (define max-y (/ (assoc-ref (get-game-properties) 'height) 2))
14 (define min-y (- max-y))
19 (define-checking-mobs (asteroid-shots x y size) (shot (sx x) (sy y))
20 (if (< (distance-between-points (list sx sy) (list x y)) size) 1 0))
22 (define (asteroid-killed? x y size)
23 (> (apply + (asteroid-shots x y size)) 0))
26 (image (load-texture "Asteroid.png"))
27 (x 0) (y 0) (angle 0) (dir 0) (size 100))
28 (cond ((asteroid-killed? x y size)
31 (let ((r (degrees-to-radians (- dir))))
32 (set! x (+ x (sin r)))
33 (set! y (+ y (cos r))))
34 (set! angle (+ angle 1))
36 (cond ((or (> x max-x) (< x min-x))
37 (set! dir (* -1 dir))))
38 (cond ((or (> y max-y) (< y min-y))
39 (set! dir (- 180 dir))))))
49 (ship1 (load-texture "Ship1.png"))
50 (ship2 (load-texture "Ship2.png"))
53 (cond ((key? 'left) (set! angle (+ angle 5)))
54 ((key? 'right) (set! angle (- angle 5))))
56 (let ((r (degrees-to-radians (- angle))))
57 (set! x (+ x (* 4 (sin r))))
58 (set! y (+ y (* 4 (cos r)))))
59 (cond ((> x max-x) (set! x min-x))
60 ((< x min-x) (set! x max-x)))
61 (cond ((> y max-y) (set! y min-y))
62 ((< y min-y) (set! y max-y)))
66 (cond ((key-pressed? 'space)
67 (show-mob (make-shot #:x x #:y y #:angle angle))))
71 (draw-texture (if moving ship2 ship1)))
76 (define-checking-mobs (impacted-shots x y) (asteroid (ax x) (ay y) (size size))
77 (if (< (distance-between-points (list ax ay) (list x y)) size) 1 0))
79 (define (shot-killed? x y)
80 (> (apply + (impacted-shots x y)) 0))
82 (define-mob (shot (x 0) (y 0) (angle 0))
83 (cond ((shot-killed? x y)
86 (let ((r (degrees-to-radians (- angle))))
87 (set! x (+ x (* 10 (sin r))))
88 (set! y (+ y (* 10 (cos r))))
89 (cond ((or (> x max-x)
102 (define (init-asteroids n)
104 (let ((x (- (random (* max-x 2)) max-x))
105 (y (- (random (* max-y 2)) max-y)))
106 (cond ((< (distance-between-points (list x y) '(0 0)) 120)
109 (let ((angle (random 360)) (dir (- (random 360) 180)))
110 (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
111 (init-asteroids (- n 1))))))))
115 (show-mob (make-ship))
117 (let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
119 (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
122 ;; (define (new-game n)
123 ;; (set! asteroids (make-asteroids n))
124 ;; (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
129 ;; (define (killed-ship? s a)
130 ;; (cond ((null? a) #f)
132 ;; (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
133 ;; (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
134 ;; (assoc-ref (car a) 'size))
135 ;; (killed-ship? s (cdr a))))))
138 ;; (let ((asteroids #f) (ship #f) (shots #f))
141 ;; (cond ((killed-ship? ship asteroids)
143 ;; (receive (s a) (kill-asteroids shots asteroids)
145 ;; (set! asteroids a))
146 ;; (set! asteroids (map move-asteroid asteroids))
147 ;; (set! ship (move-ship (alist-copy ship)))
148 ;; (let ((shot (ship-shot ship)))
150 ;; (set! shots (cons shot shots)))))
151 ;; (set! shots (move-shots shots))
152 ;; (for-each draw-asteroid asteroids)
153 ;; (for-each draw-shot shots)
154 ;; (draw-ship ship)))