#lang s-exp "../moby-lang.ss"
(define-struct world (playlist time volume))
(define init-time 40)
(define fade-time 30)
(define init-volume 75)
(define UNINITIALIZED 'uninitialized)
(define initial-world
(make-world UNINITIALIZED init-time init-volume))
(define (tick a-world)
(cond
[(eq? (world-playlist a-world) UNINITIALIZED)
a-world]
[else
(local [(define t
(sub1 (world-time a-world)))]
(make-world
(world-playlist a-world)
t
(cond
[(< t 0) 0]
[(< t fade-time)
(quotient (* t init-volume)
fade-time)]
[else init-volume])))]))
(define (shake a-world)
(make-world (world-playlist a-world)
init-time
init-volume))
(define (get-effects a-world)
(list (change-volume a-world)
(play-music a-world)
(sleep-check a-world)))
(define (change-volume a-world)
(make-effect:set-sound-volume
(world-volume a-world)))
(define (play-music a-world)
(cond
[(eq? (world-playlist a-world) UNINITIALIZED)
empty]
[else
(cond
[(> (world-volume a-world) 0)
(make-effect:play-sound (world-playlist a-world))]
[(<= (world-volume a-world) 0)
(make-effect:pause-sound (world-playlist a-world))])]))
(define (sleep-check a-world)
(cond [(should-stay-awake? a-world)
(make-effect:set-wake-lock 6)]
[else
(make-effect:release-wake-lock)]))
(define (should-stay-awake? a-world)
(>= (sub1 (world-time a-world)) 0))
(define (update-playlist a-world a-playlist)
(make-world a-playlist
(world-time a-world)
(world-volume a-world)))
(define (draw a-world)
(list (js-div '(("id" "top")))
(list (js-button!
identity
(lambda (w)
(make-effect:pick-playlist update-playlist)))
(list (js-text "Pick playlist")))
(list (js-p '(("id" "aPara")))
(list (js-text
(string-append
"volume = "
(number->string
(world-volume a-world))))))))
(define (draw-css a-world)
'(("top" ("border-style" "solid"))
("aPara" ("font-size" "30px"))))
(js-big-bang initial-world
(on-tick! 1 tick get-effects)
(on-shake! shake get-effects)
(on-draw draw draw-css))