Allow the same sound effect to be played many times simultaneously.
This commit is contained in:
parent
e09efb46a0
commit
bcb235bd82
2 changed files with 50 additions and 11 deletions
21
game.scm
21
game.scm
|
@ -26,6 +26,7 @@
|
||||||
(dom media)
|
(dom media)
|
||||||
(dom window)
|
(dom window)
|
||||||
(game actors)
|
(game actors)
|
||||||
|
(game audio)
|
||||||
(game level)
|
(game level)
|
||||||
(game levels level-1)
|
(game levels level-1)
|
||||||
(game levels level-2)
|
(game levels level-2)
|
||||||
|
@ -58,9 +59,7 @@
|
||||||
(inexact->exact tile-width)
|
(inexact->exact tile-width)
|
||||||
(inexact->exact tile-height)))
|
(inexact->exact tile-height)))
|
||||||
(define* (load-sound-effect name #:key (volume 0.25))
|
(define* (load-sound-effect name #:key (volume 0.25))
|
||||||
(let ((audio (make-audio (string-append "assets/sounds/" name ".wav"))))
|
(make-sound-effect (string-append "assets/sounds/" name ".wav")))
|
||||||
(set-media-volume! audio volume)
|
|
||||||
audio))
|
|
||||||
(define audio:bump (load-sound-effect "bump"))
|
(define audio:bump (load-sound-effect "bump"))
|
||||||
(define audio:push (load-sound-effect "push"))
|
(define audio:push (load-sound-effect "push"))
|
||||||
(define audio:undo (load-sound-effect "undo"))
|
(define audio:undo (load-sound-effect "undo"))
|
||||||
|
@ -97,11 +96,11 @@
|
||||||
(set! *snapshots* (cons (copy-whactormap *actormap*) *snapshots*)))
|
(set! *snapshots* (cons (copy-whactormap *actormap*) *snapshots*)))
|
||||||
(define (rollback-snapshot!)
|
(define (rollback-snapshot!)
|
||||||
(match *snapshots*
|
(match *snapshots*
|
||||||
(() (media-play audio:no))
|
(() (play-sound-effect audio:no))
|
||||||
((snapshot . older-snapshots)
|
((snapshot . older-snapshots)
|
||||||
(set! *actormap* snapshot)
|
(set! *actormap* snapshot)
|
||||||
(set! *snapshots* older-snapshots)
|
(set! *snapshots* older-snapshots)
|
||||||
(media-play audio:undo))))
|
(play-sound-effect audio:undo))))
|
||||||
|
|
||||||
(define (sort lst compare)
|
(define (sort lst compare)
|
||||||
(match lst
|
(match lst
|
||||||
|
@ -210,19 +209,19 @@
|
||||||
(let ((result
|
(let ((result
|
||||||
(match ($ player 'event)
|
(match ($ player 'event)
|
||||||
(('bump)
|
(('bump)
|
||||||
(media-play audio:bump)
|
(play-sound-effect audio:bump)
|
||||||
#f)
|
#f)
|
||||||
(('push)
|
(('push)
|
||||||
(media-play audio:push)
|
(play-sound-effect audio:push)
|
||||||
#f)
|
#f)
|
||||||
(('exit)
|
(('exit)
|
||||||
(media-play audio:exit)
|
(play-sound-effect audio:exit)
|
||||||
'next-level)
|
'next-level)
|
||||||
(('die)
|
(('die)
|
||||||
(media-play audio:die)
|
(play-sound-effect audio:die)
|
||||||
#f)
|
#f)
|
||||||
(('gem)
|
(('gem)
|
||||||
(media-play audio:pickup)
|
(play-sound-effect audio:pickup)
|
||||||
;; TODO: Maybe show a little achievement popup when all gems
|
;; TODO: Maybe show a little achievement popup when all gems
|
||||||
;; are collected?
|
;; are collected?
|
||||||
(set! *gems* (cons *level-idx* *gems*))
|
(set! *gems* (cons *level-idx* *gems*))
|
||||||
|
@ -232,7 +231,7 @@
|
||||||
(save-snapshot!)
|
(save-snapshot!)
|
||||||
result))
|
result))
|
||||||
(begin
|
(begin
|
||||||
(media-play audio:no)
|
(play-sound-effect audio:no)
|
||||||
#f)))))
|
#f)))))
|
||||||
(when (eq? (do-move) 'next-level)
|
(when (eq? (do-move) 'next-level)
|
||||||
(next-level!)))
|
(next-level!)))
|
||||||
|
|
40
modules/game/audio.scm
Normal file
40
modules/game/audio.scm
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
(define-module (game audio)
|
||||||
|
#:use-module (dom element)
|
||||||
|
#:use-module (dom media)
|
||||||
|
#:use-module (ice-9 match)
|
||||||
|
#:use-module (srfi srfi-9)
|
||||||
|
#:export (make-sound-effect
|
||||||
|
sound-effect?
|
||||||
|
sound-effect-src
|
||||||
|
play-sound-effect))
|
||||||
|
|
||||||
|
(define-record-type <sound-effect>
|
||||||
|
(%make-sound-effect src elems)
|
||||||
|
sound-effect?
|
||||||
|
(src sound-effect-src)
|
||||||
|
(elems sound-effect-elems))
|
||||||
|
|
||||||
|
(define max-simultaneous 8)
|
||||||
|
|
||||||
|
(define (make-sound-effect src)
|
||||||
|
(let ((elems (make-vector max-simultaneous #f)))
|
||||||
|
(vector-set! elems 0 (make-audio src))
|
||||||
|
(%make-sound-effect src elems)))
|
||||||
|
|
||||||
|
(define* (play-sound-effect sound #:optional (volume 0.5))
|
||||||
|
(define (play elem)
|
||||||
|
(set-media-volume! elem volume)
|
||||||
|
(media-play elem))
|
||||||
|
(match sound
|
||||||
|
(($ <sound-effect> src elems)
|
||||||
|
(let lp ((i 0))
|
||||||
|
(when (< i max-simultaneous)
|
||||||
|
(match (vector-ref elems i)
|
||||||
|
(#f
|
||||||
|
(let ((elem (clone-element (vector-ref elems 0))))
|
||||||
|
(vector-set! elems i elem)
|
||||||
|
(play elem)))
|
||||||
|
(elem
|
||||||
|
(if (media-ended? elem)
|
||||||
|
(play elem)
|
||||||
|
(lp (1+ i))))))))))
|
Loading…
Add table
Reference in a new issue