1 (use-modules (gacela gacela)
5 (set-game-properties! #:title "Gacela Asteroids")
7 (define max-x (/ (assoc-ref (get-game-properties) 'width) 2))
8 (define min-x (- max-x))
9 (define max-y (/ (assoc-ref (get-game-properties) 'height) 2))
10 (define min-y (- max-y))
15 (define-checking-mobs (asteroid-shots x y size) (shot (sx x) (sy y))
16 (if (< (distance-between-points (list sx sy) (list x y)) size) 1 0))
18 (define (asteroid-killed? x y size)
19 (> (apply + (asteroid-shots x y size)) 0))
22 (image (load-texture "Asteroid.png"))
23 (x 0) (y 0) (angle 0) (dir 0) (size 100))
24 (cond ((asteroid-killed? x y size)
27 (let ((r (degrees-to-radians (- dir))))
28 (set! x (+ x (sin r)))
29 (set! y (+ y (cos r))))
30 (set! angle (+ angle 1))
32 (cond ((or (> x max-x) (< x min-x))
33 (set! dir (* -1 dir))))
34 (cond ((or (> y max-y) (< y min-y))
35 (set! dir (- 180 dir))))))
45 (ship1 (load-texture "Ship1.png"))
46 (ship2 (load-texture "Ship2.png"))
49 (cond ((key? 'left) (set! angle (+ angle 5)))
50 ((key? 'right) (set! angle (- angle 5))))
52 (let ((r (degrees-to-radians (- angle))))
53 (set! x (+ x (* 4 (sin r))))
54 (set! y (+ y (* 4 (cos r)))))
55 (cond ((> x max-x) (set! x min-x))
56 ((< x min-x) (set! x max-x)))
57 (cond ((> y max-y) (set! y min-y))
58 ((< y min-y) (set! y max-y)))
62 (cond ((key-pressed? 'space)
63 (show-mob (make-shot #:x x #:y y #:angle angle))))
67 (draw-texture (if moving ship2 ship1)))
72 (define-checking-mobs (impacted-shots x y) (asteroid (ax x) (ay y) (size size))
73 (if (< (distance-between-points (list ax ay) (list x y)) size) 1 0))
75 (define (shot-killed? x y)
76 (> (apply + (impacted-shots x y)) 0))
78 (define-mob (shot (x 0) (y 0) (angle 0))
79 (cond ((shot-killed? x y)
82 (let ((r (degrees-to-radians (- angle))))
83 (set! x (+ x (* 10 (sin r))))
84 (set! y (+ y (* 10 (cos r))))
85 (cond ((or (> x max-x)
98 (define (init-asteroids n)
100 (let ((x (- (random (* max-x 2)) max-x))
101 (y (- (random (* max-y 2)) max-y)))
102 (cond ((< (distance-between-points (list x y) '(0 0)) 120)
105 (let ((angle (random 360)) (dir (- (random 360) 180)))
106 (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
107 (init-asteroids (- n 1))))))))
111 (show-mob (make-ship))
113 (let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
115 (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
118 ;; (define (new-game n)
119 ;; (set! asteroids (make-asteroids n))
120 ;; (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
125 ;; (define (killed-ship? s a)
126 ;; (cond ((null? a) #f)
128 ;; (or (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y))
129 ;; (list (assoc-ref (car a) 'x) (assoc-ref (car a) 'y)))
130 ;; (assoc-ref (car a) 'size))
131 ;; (killed-ship? s (cdr a))))))
134 ;; (let ((asteroids #f) (ship #f) (shots #f))
137 ;; (cond ((killed-ship? ship asteroids)
139 ;; (receive (s a) (kill-asteroids shots asteroids)
141 ;; (set! asteroids a))
142 ;; (set! asteroids (map move-asteroid asteroids))
143 ;; (set! ship (move-ship (alist-copy ship)))
144 ;; (let ((shot (ship-shot ship)))
146 ;; (set! shots (cons shot shots)))))
147 ;; (set! shots (move-shots shots))
148 ;; (for-each draw-asteroid asteroids)
149 ;; (for-each draw-shot shots)
150 ;; (draw-ship ship)))