#lang planet dyoo/whalesong
(require (planet dyoo/whalesong/world)
(planet dyoo/whalesong/image)
(planet dyoo/whalesong/js))
(void (call-method body "css" "margin" 0))
(void (call-method body "css" "padding" 0))
(void (call-method body "css" "overflow" "hidden"))
(define GRAVITY-FACTOR 1)
(define-struct posn (x y))
(define-struct drop (posn velocity color size))
(define (random-drop)
(make-drop (make-posn (random (viewport-width)) 0)
(+ 5 (random 10)) (random-choice (list "gray" "darkgray"
"white" "blue"
"lightblue"
"darkblue"))
(random 10) ))
(define (random-choice elts)
(list-ref elts (random (length elts))))
(define-struct world (sky ))
(define (my-filter f l)
(cond
[(null? l)
'()]
[(f (car l))
(cons (car l)
(my-filter f (cdr l)))]
[else
(my-filter f (cdr l))]))
(define (tick w)
(make-world
(my-filter not-on-floor?
(map drop-descend (cons (random-drop)
(cons (random-drop)
(world-sky w)))))))
(define (drop-descend a-drop)
(cond
[(> (posn-y (drop-posn a-drop)) (viewport-height))
a-drop]
[else
(make-drop (posn-descend (drop-posn a-drop) (drop-velocity a-drop))
(+ GRAVITY-FACTOR (drop-velocity a-drop))
(drop-color a-drop)
(drop-size a-drop))]))
(define (posn-descend a-posn n)
(make-posn (posn-x a-posn)
(+ n (posn-y a-posn))))
(define (on-floor? a-drop)
(> (posn-y (drop-posn a-drop))
(viewport-height)))
(define (not-on-floor? a-drop) (not (on-floor? a-drop)))
(define (make-drop-image color size)
(circle size "solid" color))
(define (place-drop a-drop a-scene)
(place-image (make-drop-image (drop-color a-drop)
(drop-size a-drop))
(posn-x (drop-posn a-drop))
(posn-y (drop-posn a-drop))
a-scene))
(define (my-foldl f acc lst)
(cond
[(null? lst)
acc]
[else
(my-foldl f
(f (car lst) acc)
(cdr lst))]))
(define (draw w)
(my-foldl place-drop (empty-scene (viewport-width) (viewport-height)) (world-sky w)))
(big-bang (make-world '())
(to-draw draw)
(on-tick tick))