-(define draw-asteroid
- (let ((asteroid (load-texture "Asteroid.png")))
- (lambda (a)
- (to-origin)
- (translate (assoc-ref a 'x) (assoc-ref a 'y))
- (rotate (assoc-ref a 'angle))
- (draw-texture asteroid))))
-
-(define (move-asteroid a)
- (let* ((x (assoc-ref a 'x)) (y (assoc-ref a 'y))
- (angle (assoc-ref a 'angle))
- (vx (assoc-ref a 'vx)) (vy (assoc-ref a 'vy))
- (nx (+ x vx)) (ny (+ y vy)))
- (cond ((> nx max-x) (set! vx -1))
- ((< nx min-x) (set! vx 1)))
- (cond ((> ny max-y) (set! vy -1))
- ((< ny min-y) (set! vy 1)))
- (set! angle (+ angle 1))
- `((x . ,(+ x vx)) (y . ,(+ y vy)) (angle . ,angle) (vx . ,vx) (vy . ,vy))))
-
-(define draw-ship
- (let ((ship1 (load-texture "Ship1.png"))
- (ship2 (load-texture "Ship2.png")))
- (lambda (s)
- (to-origin)
- (translate (assoc-ref s 'x) (assoc-ref s 'y))
- (rotate (assoc-ref s 'angle))
- (let ((ship (if (assoc-ref s 'moving) ship2 ship1)))
- (draw-texture ship)))))
-
-(define (move-ship s)
- (let ((x (assoc-ref s 'x)) (y (assoc-ref s 'y))
- (angle (assoc-ref s 'angle))
- (moving (assoc-ref s 'moving)))
- (cond ((key? 'left) (set! angle (+ angle 5)))
- ((key? 'right) (set! angle (- angle 5))))
- (cond ((key? 'up)
- (let ((r (degrees-to-radians (- angle))))
- (set! x (+ x (* 4 (sin r))))
- (set! y (+ y (* 4 (cos r)))))
- (cond ((> x max-x) (set! x min-x))
- ((< x min-x) (set! x max-x)))
- (cond ((> y max-y) (set! y min-y))
- ((< y min-y) (set! y max-y)))
- (set! moving #t))
- (else
- (set! moving #f)))
- `((x . ,x) (y . ,y) (angle . ,angle) (moving . ,moving))))
-
-(define (ship-shot s)
- (cond ((key-released? 'space)
- #f)))
-
-(define (make-asteroids n)
- (define (xy n r)
- (let ((n2 (- (random (* n 2)) n)))
- (cond ((and (< n2 r) (>= n2 0)) r)
- ((and (> n2 (- r)) (< n2 0)) (- r))
- (else n2))))
-
- (cond ((= n 0) '())
+(define-macro (asteroid-killed?)
+ `(> (apply +
+ (map-mobs
+ (lambda (s) (if (< (distance-between-points (list (assoc-ref s 'x) (assoc-ref s 'y)) (list x y)) size) 1 0))
+ 'shot))
+ 0))
+
+(define-mob (asteroid
+ (image (load-texture "Asteroid.png"))
+ (x 0) (y 0) (angle 0) (dir 0) (size 100))
+ (cond ((asteroid-killed?)
+ (kill-me))
+ (else
+ (let ((r (degrees-to-radians (- dir))))
+ (set! x (+ x (sin r)))
+ (set! y (+ y (cos r))))
+ (set! angle (+ angle 1))
+
+ (cond ((or (> x max-x) (< x min-x))
+ (set! dir (* -1 dir))))
+ (cond ((or (> y max-y) (< y min-y))
+ (set! dir (- 180 dir))))))
+
+ (translate x y)
+ (rotate angle)
+ (draw-texture image))
+
+(define-mob (ship
+ (ship1 (load-texture "Ship1.png"))
+ (ship2 (load-texture "Ship2.png"))
+ (x 0) (y 0) (angle 0)
+ (moving #f))
+ (cond ((key? 'left) (set! angle (+ angle 5)))
+ ((key? 'right) (set! angle (- angle 5))))
+ (cond ((key? 'up)
+ (let ((r (degrees-to-radians (- angle))))
+ (set! x (+ x (* 4 (sin r))))
+ (set! y (+ y (* 4 (cos r)))))
+ (cond ((> x max-x) (set! x min-x))
+ ((< x min-x) (set! x max-x)))
+ (cond ((> y max-y) (set! y min-y))
+ ((< y min-y) (set! y max-y)))
+ (set! moving #t))
+ (else
+ (set! moving #f)))
+ (cond ((key-pressed? 'space)
+ (show-mob (make-shot #:x x #:y y #:angle angle))))
+
+ (translate x y)
+ (rotate angle)
+ (draw-texture (if moving ship2 ship1)))
+
+(define-macro (shot-killed?)
+ `(> (apply +
+ (map-mobs
+ (lambda (a) (if (< (distance-between-points (list (assoc-ref a 'x) (assoc-ref a 'y)) (list x y)) (assoc-ref a 'size)) 1 0))
+ 'asteroid))
+ 0))
+
+(define-mob (shot (x 0) (y 0) (angle 0))
+ (cond ((shot-killed?)
+ (kill-me))