-;; (define (move-shots shots)
-;; (cond ((null? shots) '())
-;; (else
-;; (let* ((sh (car shots))
-;; (x (assoc-ref sh 'x)) (y (assoc-ref sh 'y))
-;; (angle (assoc-ref sh 'angle))
-;; (r (degrees-to-radians (- angle))))
-;; (set! x (+ x (* 10 (sin r))))
-;; (set! y (+ y (* 10 (cos r))))
-;; (cond ((and (<= x max-x)
-;; (>= x min-x)
-;; (<= y max-y)
-;; (>= y min-y))
-;; (cons `((x . ,x) (y . ,y) (angle . ,angle))
-;; (move-shots (cdr shots))))
-;; (else
-;; (move-shots (cdr shots))))))))
-
-;; (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) '())
-;; (else
-;; (cons `((x . ,(xy max-x 20)) (y . ,(xy max-y 20)) (angle . 0) (vx . 1) (vy . 1) (size . 95))
-;; (make-asteroids (- n 1))))))
+(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)))
+
+
+;;; Shots
+
+(define-checking-mobs (impacted-shots x y) (asteroid (ax x) (ay y) (size size))
+ (if (< (distance-between-points (list ax ay) (list x y)) size) 1 0))
+
+(define (shot-killed? x y)
+ (> (apply + (impacted-shots x y)) 0))
+
+(define-mob (shot (x 0) (y 0) (angle 0))
+ (cond ((shot-killed? x y)
+ (kill-me))
+ (else
+ (let ((r (degrees-to-radians (- angle))))
+ (set! x (+ x (* 10 (sin r))))
+ (set! y (+ y (* 10 (cos r))))
+ (cond ((or (> x max-x)
+ (< x min-x)
+ (> y max-y)
+ (< y min-y))
+ (kill-me))))))
+
+ (translate x y)
+ (rotate angle)
+ (draw-line 10))
+
+
+;;; Game
+
+(define (init-asteroids n)
+ (cond ((> n 0)
+ (let ((x (- (random (* max-x 2)) max-x))
+ (y (- (random (* max-y 2)) max-y)))
+ (cond ((< (distance-between-points (list x y) '(0 0)) 120)
+ (init-asteroids n))
+ (else
+ (let ((angle (random 360)) (dir (- (random 360) 180)))
+ (show-mob (make-asteroid #:x x #:y y #:angle angle #:dir dir)))
+ (init-asteroids (- n 1))))))))
+
+
+(init-asteroids 2)
+(show-mob (make-ship))
+
+(let ((font (load-font "../tetris/lazy.ttf" #:size 20)))
+ (game
+ (render-text (format #f "Mobs: ~a" (length (get-active-mobs))) font)))
+
+
+;; (define (new-game n)
+;; (set! asteroids (make-asteroids n))
+;; (set! ship '((x . 0) (y . 0) (angle . 0) (moving . #f)))
+;; (set! shots '()))
+
+;; (new-game 2)