Commits

Christian Kellermann committed e13df05

Add upgrades that increase shields

Comments (0)

Files changed (1)

 
 ;; Our world holds information to enforce game rules.
 ;; Also bullets asteroids are lists holding these things, ship holds our ship
-(define-record world level bullets-left lives score shield paused? bullets asteroids ship)
+(define-record world level bullets-left lives score shield paused? bullets asteroids upgrades upgrade-timer ship)
 
 ;; Trigonometry helper functions
 
          (velocity (map - (radial-point-from pos speed angle) pos)))
     (make-thing 'asteroid pos velocity 0 0 radius es)))
 
+(define (make-upgrade pos)
+  (let ((es (list
+             (radial-point-from pos 15 0)
+             (radial-point-from pos 15 45)
+             (radial-point-from pos 15 90)
+             (radial-point-from pos 15 135)
+             (radial-point-from pos 15 180)
+             (radial-point-from pos 15 215)
+             (radial-point-from pos 15 270)
+             (radial-point-from pos 15 315))))
+    (make-thing 'upgrade pos '(0 0) 0 0 10 es)))
+
 ;; Move one thing around
 (define (move-thing dt t)
   (let* ((pos (thing-pos t))
     (text (car center) 20
           "- PAUSED -"
           align: #:center))
+
+  (text (- *width* 10) 20
+        (sprintf "~a upgrades ~a dt, ~a FPS" (world-upgrade-timer world) dt *fps*)
+        align: #:right)
+#;
   (unless (<= (world-shield world) 0)
-    (text (- *width* 10) 20
-          (sprintf "Shielded for ~a sec "
-                   (truncate (world-shield world)))
-          align: #:right))
+          (text (- *width* 10) 20
+                (sprintf "Shielded for ~a sec "
+                         (truncate (world-shield world)))
+                align: #:right))
   (text 10 (- *height* 10)
         (sprintf "Ships: ~a" (world-lives world)))
   (text (car center) (- *height* 10)
              level
              (lambda _ (make-asteroid (random-position) 50 15 10))))
         (s (or ship (make-ship))))
-    (make-world level (+ (* 2 level) 5) lives score 20 #f '() as s)))
+    (make-world level (+ (* 2 level) 5) lives score 20 #f '() as '() 0 s)))
 
 ;; what to do when we are hit
 (define (ship-wrecked)
                    (second pos)))
          (a2 (list (- (first pos) r)
                    (second pos)))
-         (new-r (- r 10)))
+         (new-r (- r 10))
+         (upgrade? (> (random 10) 7)))
     (unless *muted* (play-sample *explosion-sound* channel: 3 duration: 3000))
-    (if (> new-r 20)
-        (world-asteroids-set!
-         *w*
-         (cons (make-asteroid a1 new-r 20 10)
-               (cons (make-asteroid a1 new-r 20 10) (world-asteroids *w*))))))
+    (cond ((> new-r 20)
+           (world-asteroids-set!
+            *w*
+            (cons (make-asteroid a1 new-r 20 10)
+                  (cons (make-asteroid a1 new-r 20 10) (world-asteroids *w*)))))
+          (upgrade? (world-upgrades-set!
+                     *w*
+                     (cons (make-upgrade pos) (world-upgrades *w*)))
+                    (world-upgrade-timer-set! *w* 10))))
   (world-score-set! *w* (add1 (world-score *w*))))
 
 ;; Handle things that go off screen
 (define (check-collisions world)
   (let ((bullets (world-bullets world))
         (ship (world-ship world))
-        (asteroids (world-asteroids world)))
+        (asteroids (world-asteroids world))
+        (upgrades (world-upgrades world)))
     (cond ((find (cut collides? ship <>) asteroids) =>
            (lambda (a)
              (if (zero? (world-shield world))
                  (ship-wrecked)
                  (begin (world-asteroids-set! world (delete a asteroids))
-                        (asteroid-destroyed a))))))
+                        (asteroid-destroyed a)))))
+          ((find (cut collides? ship <>) upgrades) =>
+           (lambda (u)
+             (world-shield-set! world (+ 15 (world-shield world)))
+             (world-upgrades-set! world (delete u upgrades))
+             (when (null? (world-upgrades world))
+                   (world-upgrade-timer-set! world 0)))))
     (for-each
      (lambda (b)
        (cond ((find (cut collides? b <>) asteroids) =>
                 (asteroid-destroyed a)))))
      bullets)))
 
-
 ;; The actual doodle game loop starts here
 
 ;; First bring up a windown
 (set! *debugging* #f)
 (set! *paused* #f)
 (set! *muted* #f)
+(set! *start* (current-milliseconds))
+(set! *time* (current-milliseconds))
+(set! *frames* 0)
+(set! *fps* "??")
 
 ;; make us a global new world
 (world-inits
 
 (world-changes
  (lambda (events dt quit)
+   (inc! *frames*)
+   (set! *time* (current-milliseconds))
+   (set! *fps*
+         (truncate (/ *frames* (/ (- (add1 *time*) *start*) 1000) )))
+   (when (> (-  *time* *start*) 1000)
+         (set! *frames* 0)
+         (set! *start* *time*))
    (for-each
     (lambda (e)
       (match e
      (when (> shield 0)
              (circle (first pos) (second pos) (* 2.5 (thing-bounding-radius s)) `(0 1 0 ,(* 0.1 shield)))
              (when (not *paused*)
+                   (when (< 0 (world-upgrade-timer *w*))
+                           (world-upgrade-timer-set!
+                            *w*
+                            (max 0 (- (world-upgrade-timer *w*) dt))))
                (world-shield-set! *w* (max 0 (- shield dt))))))
 
+   (when (zero? (world-upgrade-timer *w*))
+         (world-upgrades-set! *w* '()))
+
    (when (and (not *paused*) *rotating*)
      (world-ship-set!
       *w*
 
    (draw-things (world-asteroids *w*)
                 (world-bullets *w*)
+                (world-upgrades *w*)
                 (list (world-ship *w*)))
    (draw-stats *w* dt)
    (show!)