From b87b8aa17199c5a905011c1ab30ebf335b0f6d55 Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Wed, 7 Oct 2015 17:29:53 -0700 Subject: [PATCH] sounds are now updated every frame. --- desktop/gametodos.txt | 1 - desktop/src-common/advent/actions.clj | 2 + .../advent/screens/rooms/outside_jail.clj | 19 ++---- desktop/src-common/advent/screens/scene.clj | 61 +++++++++++++------ desktop/src-common/advent/utils.clj | 37 +++++++++++ desktop/src/advent/core/desktop_launcher.clj | 6 +- 6 files changed, 88 insertions(+), 38 deletions(-) diff --git a/desktop/gametodos.txt b/desktop/gametodos.txt index 7e9ea9d7..973d1ef4 100644 --- a/desktop/gametodos.txt +++ b/desktop/gametodos.txt @@ -8,7 +8,6 @@ PROGRAMMING + behind house left direction not great + spying broken + still can get into dialogue deadlock somehow -+ Go backwards on load. IOS + all mp3s diff --git a/desktop/src-common/advent/actions.clj b/desktop/src-common/advent/actions.clj index b8ab6f83..f785a1f8 100644 --- a/desktop/src-common/advent/actions.clj +++ b/desktop/src-common/advent/actions.clj @@ -649,11 +649,13 @@ :none)) (run-action entities (begin [this screen entities] + (utils/stop-all-sounds! entities) (let [ego (get-in entities [:room :entities :ego]) old-music (get-music (get-in entities [:room :music]) (get-in entities [:state :time])) entities (as-> entities e (assoc-in e [:room] (get-in entities [:rooms new-background])) (assoc-in e [:room :entities :ego] ego) + (assoc-in e [:current-sounds :value] []) (if between (between screen e) e) (assoc-in e [:state :last-room] new-background) (assoc-in e [:tweens :fade-in] (tween/tween :fade-in screen [:fade :opacity] 1.0 0.0 time)) diff --git a/desktop/src-common/advent/screens/rooms/outside_jail.clj b/desktop/src-common/advent/screens/rooms/outside_jail.clj index 122f495c..a6a5a03d 100644 --- a/desktop/src-common/advent/screens/rooms/outside_jail.clj +++ b/desktop/src-common/advent/screens/rooms/outside_jail.clj @@ -93,9 +93,6 @@ (actions/walk-straight-to entities :ego [142 96]) (actions/talk entities :ego "This must be Frankie Rockfist's secret stash!"))) -(defn fountain-vol [entities] - (utils/proximity-volume entities [172 120] :scale 0.5)) - (defn make [screen] (let [guard-sheet (texture! (utils/get-texture "inside-cafeteria/ladder-guard.png") :split 37 87) guard-stand (animation 0.1 [(aget guard-sheet 0 0)]) @@ -281,21 +278,13 @@ :scale-fn (utils/scaler-fn-with-baseline 40 0.001 1.3) :start-pos [145 15] - :update-fn (fn [_ entities] - (when-let [fountain-sound-id (get-in entities [:room :fountain-sound :id])] - (sound! (get-in entities [:room :fountain-sound :sound]) :set-volume fountain-sound-id (fountain-vol entities))) - entities) - - :stop-fn (fn [_ entities] - (when-let [fountain-sound-id (get-in entities [:room :fountain-sound :id])] - (sound! (get-in entities [:room :fountain-sound :sound]) :stop fountain-sound-id)) - entities) - - :apply-state (fn [_ entities] + :apply-state (fn [screen entities] (utils/fast-forward-particle (get-in entities [:room :entities :outside-particles])) (as-> entities entities - (assoc-in entities [:room :fountain-sound :id] (sound! (get-in entities [:room :fountain-sound :sound]) :loop (fountain-vol entities))) + (utils/play-sound! screen entities (get-in entities [:room :fountain-sound :sound]) + :fountain 0.5 [172 120] :loop) + (if (= :night (get-in entities [:state :time])) (make-night entities) (update-in entities [:room :entities] dissoc :candle-aura :candle-flame)) diff --git a/desktop/src-common/advent/screens/scene.clj b/desktop/src-common/advent/screens/scene.clj index de79da9c..99acf583 100644 --- a/desktop/src-common/advent/screens/scene.clj +++ b/desktop/src-common/advent/screens/scene.clj @@ -799,19 +799,42 @@ void main() :object nil})) entities)) -(defn play-key-sounds [entities] - (doseq [[target {:keys [previous-frame-index current-frame-index anim-sound-frames anim x y] :as e}] (get-in entities [:room :entities])] - (when (and (not= previous-frame-index current-frame-index) - ((set (keys anim-sound-frames)) anim)) - (when-let [[snd vol-scale] (get-in anim-sound-frames [anim current-frame-index])] - (let [vol (if (= target :ego) - (-> (* (/ (get-in entities [:room :entities :ego :scale-x]) 1.5) 0.75) - (* (or vol-scale 1.0)) - (utils/clamp-volume)) - (utils/proximity-volume entities [x y] :scale vol-scale)) - pan (/ (- (:x e) 160 ) 160)] - (sound! (or (snd e) - (snd (:sounds entities))) :play (utils/current-sound-volume vol) 1.0 pan)))))) +(defn play-sound-if-necessary [screen entities target {:keys [previous-frame-index current-frame-index anim-sound-frames anim x y] :as e}] + (if (and (not= previous-frame-index current-frame-index) + ((set (keys anim-sound-frames)) anim)) + (if-let [[snd vol-scale] (get-in anim-sound-frames [anim current-frame-index])] + (utils/play-sound! screen entities + (or (snd e) (snd (:sounds entities))) + target + vol-scale + [x y]) + entities) + entities)) + +(defn play-key-sounds [screen entities] + (if (= (get-in entities [:fade :opacity]) 0.0) + (loop [entities entities + [[target e] & rest] (seq (get-in entities [:room :entities]))] + (if e + (recur (play-sound-if-necessary screen entities target e) rest) + entities)) + entities)) + +(defn update-current-sound-vols! [entities] + (loop [entities entities + [{:keys [id sound duration loc vol-scale target loc]} & rest] (get-in entities [:current-sounds :value])] + (if id + (do + (sound! sound :set-volume id (utils/get-sound-volume entities target vol-scale loc)) + (recur entities rest)) + entities))) + +(defn remove-ended-sounds [screen entities] + (update-in entities [:current-sounds :value] + (fn [sounds] + (filter #(or (= :loop (:type %)) + (> (:ends-at %) (:total-time screen))) + sounds)))) (defn update-from-room [screen entities] (if-let [update-fn (get-in entities [:room :update-fn])] @@ -924,6 +947,8 @@ void main() :cam {:zoom 0.95 :paused? false :object nil} + :current-sounds {:object nil + :value []} :musics {:object nil :inside-antique (utils/make-music "music/inside-antique.ogg") :town-1 (utils/make-music "music/town-music-1.ogg") @@ -1061,12 +1086,10 @@ void main() (set! (. camera zoom) (:zoom (:cam entities))) (set! (.. camera position x) (:x (:cam entities) 160.0)) (set! (.. camera position y) (:y (:cam entities) 120.0))) - (let [entities (utils/update-override screen entities)] - - - (when (= (get-in entities [:fade :opacity]) - 0.0) - (play-key-sounds entities)) + (let [entities (utils/update-override screen entities) + entities (play-key-sounds screen entities) + entities (update-current-sound-vols! entities) + entities (remove-ended-sounds screen entities)] (doseq [m (vals (get-in entities [:musics]))] (when m (music! m :set-volume (utils/current-music-volume (get-in entities [:volume :value]))))) diff --git a/desktop/src-common/advent/utils.clj b/desktop/src-common/advent/utils.clj index 36b1af6e..48913ba4 100644 --- a/desktop/src-common/advent/utils.clj +++ b/desktop/src-common/advent/utils.clj @@ -399,3 +399,40 @@ (.getDeclaredField (name "config")) (doto (.setAccessible true)) (.get Gdx/graphics))) 60)))) + + +(defn get-sound-volume [entities target vol-scale [x y]] + (if (= target :ego) + (-> (* (/ (get-in entities [:room :entities :ego :scale-x]) 1.5) 0.75) + (* (or vol-scale 1.0)) + (clamp-volume)) + (proximity-volume entities [x y] :scale vol-scale))) + +(defn get-sound-pan [x] + (/ (- x 160 ) 160)) + + +(defn play-sound! + ([screen entities snd target vol-scale [x y]] + (play-sound! screen entities snd target vol-scale [x y] :once)) + + ([screen entities snd target vol-scale [x y] type] + (let [vol (get-sound-volume entities target vol-scale [x y]) + pan (get-sound-pan x) + sound-id (if (= :once type) + (sound! snd :play (current-sound-volume vol) 1.0 pan) + (sound! snd :loop (current-sound-volume vol) 1.0 pan)) ] + (update-in entities [:current-sounds :value] + conj {:id sound-id + :sound snd + :loc [x y] + :vol-scale vol-scale + :type type + :ends-at (if (= type :once) + (+ (:total-time screen) (sound! snd :duration)) + nil) + :target target})))) + +(defn stop-all-sounds! [entities] + (doseq [snd (get-in entities [:current-sounds :value])] + (sound! (:sound snd) :stop (:id snd)))) diff --git a/desktop/src/advent/core/desktop_launcher.clj b/desktop/src/advent/core/desktop_launcher.clj index 962efab1..3723e783 100644 --- a/desktop/src/advent/core/desktop_launcher.clj +++ b/desktop/src/advent/core/desktop_launcher.clj @@ -1,12 +1,12 @@ (ns advent.core.desktop-launcher (:require [advent.core :refer :all] - #_[clojure.tools.nrepl.server]) + [clojure.tools.nrepl.server]) (:import [com.badlogic.gdx.backends.lwjgl LwjglApplication LwjglApplicationConfiguration] [org.lwjgl.input Keyboard] [com.badlogic.gdx Gdx]) (:gen-class)) -#_(defmacro start-nrepl-expr [port] +(defmacro start-nrepl-expr [port] `(let [{port# :port} (clojure.tools.nrepl.server/start-server :port ~port)] (doseq [port-file# ["target/repl-port" ".nrepl-port"]] (-> port-file# @@ -19,7 +19,7 @@ (defn -main [& [port]] - #_(try + (try (when port (start-nrepl-expr (Integer/parseInt port))) (catch Exception e)) (let [cfg (LwjglApplicationConfiguration.)]