diff --git a/docs/assets/js/constants.js b/docs/assets/js/constants.js index f2c99ce..1e8b9c7 100644 --- a/docs/assets/js/constants.js +++ b/docs/assets/js/constants.js @@ -37,7 +37,8 @@ var constants = { }, lifeFuncs:{ breath:{ - fullBreath: 200 + fullBreath: 200, + cough_interval_secs: 4.0 }, cardio:{ optimalPressure: 50 diff --git a/docs/assets/js/player/lifeFunctions.js b/docs/assets/js/player/lifeFunctions.js index bbc1f01..dedb94e 100644 --- a/docs/assets/js/player/lifeFunctions.js +++ b/docs/assets/js/player/lifeFunctions.js @@ -19,8 +19,14 @@ let justBlinked = false; function updateLife() { if(keyDown[k.x]) { - if(breath === 0) currentBreathMode = breathMode.inhale; - else if(breath === constants.lifeFuncs.breath.fullBreath) currentBreathMode = breathMode.exhale; + if (breath === 0) { + soundAssets.inhale.play(); + currentBreathMode = breathMode.inhale; + } + else if (breath === constants.lifeFuncs.breath.fullBreath) { + soundAssets.exhale.play(); + currentBreathMode = breathMode.exhale; + } } breathe(); @@ -50,6 +56,7 @@ function breathe() { fullBreathTimer++; if(fullBreathTimer >= 600) { //cough and lose breath or something + handleCough(); } } else { fullBreathTimer = 0; @@ -62,6 +69,7 @@ function breathe() { noBreathTimer++; if(noBreathTimer >= 300) { //cough and lose breath or something + handleCough(); } } else { noBreathTimer = 0; @@ -70,6 +78,27 @@ function breathe() { } }; +// Tracker for if we are currently coughing +let _nextCoughAllowedTime = 0; + +/** + * Handle player coughing without spamming the sound buffer + */ +function handleCough() { + + // Only cough if we are past the cough time + if (getCurrentTimeSeconds() >= _nextCoughAllowedTime) { + + console.log("[LifeFunctions] Coughing") + + // Set the next allowed cough time + _nextCoughAllowedTime = getCurrentTimeSeconds() + constants.lifeFuncs.breath.cough_interval_secs; + + // Play the cough audio + soundAssets.cough.play(); + } +} + function heartbeat() { pressure+=10; if(pressure>=100){ diff --git a/docs/assets/js/sounds/sounds.js b/docs/assets/js/sounds/sounds.js index af498c8..c7d3f3f 100644 --- a/docs/assets/js/sounds/sounds.js +++ b/docs/assets/js/sounds/sounds.js @@ -13,13 +13,16 @@ // This exists to give nicer names to files let soundAssetMap = { "debug-ding": "./assets/sounds/debug-ding.mp3", - "footstep1":"./assets/sounds/footsteps/footstep1.mp3", - "footstep2":"./assets/sounds/footsteps/footstep2.mp3", - "footstep3":"./assets/sounds/footsteps/footstep3.mp3", - "footstep4":"./assets/sounds/footsteps/footstep4.mp3", - "footstep5":"./assets/sounds/footsteps/footstep5.mp3", - "footstep6":"./assets/sounds/footsteps/footstep6.mp3", - "heartbeat":"./assets/sounds/heartbeat.mp3" + "footstep1": "./assets/sounds/footsteps/footstep1.mp3", + "footstep2": "./assets/sounds/footsteps/footstep2.mp3", + "footstep3": "./assets/sounds/footsteps/footstep3.mp3", + "footstep4": "./assets/sounds/footsteps/footstep4.mp3", + "footstep5": "./assets/sounds/footsteps/footstep5.mp3", + "footstep6": "./assets/sounds/footsteps/footstep6.mp3", + "heartbeat": "./assets/sounds/heartbeat.mp3", + "inhale": "./assets/sounds/breathing/inhale.mp3", + "exhale": "./assets/sounds/breathing/exhale.mp3", + "cough":"./assets/sounds/cough.mp3" } // All available sounds @@ -31,7 +34,10 @@ let soundAssets = { footstep4: new SoundSnippet("footstep4", audioAssetType.sfx), footstep5: new SoundSnippet("footstep5", audioAssetType.sfx), footstep6: new SoundSnippet("footstep6", audioAssetType.sfx), - heartbeat: new SoundSnippet("heartbeat", audioAssetType.sfx) + heartbeat: new SoundSnippet("heartbeat", audioAssetType.sfx), + inhale: new SoundSnippet("inhale", audioAssetType.sfx), + exhale: new SoundSnippet("exhale", audioAssetType.sfx), + cough: new SoundSnippet("cough", audioAssetType.sfx) } /** diff --git a/docs/assets/js/utils.js b/docs/assets/js/utils.js index c134261..9aaa776 100644 --- a/docs/assets/js/utils.js +++ b/docs/assets/js/utils.js @@ -3,6 +3,10 @@ function clamp(value, min, max) { return Math.min(max, Math.max(min, value)); } +function getCurrentTimeSeconds() { + return new Date().getTime() / 1000; +} + // linear interpolation towards somewhere function lerp(start, end, amt) { return (1 - amt) * start + amt * end; } diff --git a/docs/assets/sounds/breathing/exhale.mp3 b/docs/assets/sounds/breathing/exhale.mp3 new file mode 100644 index 0000000..559d358 Binary files /dev/null and b/docs/assets/sounds/breathing/exhale.mp3 differ diff --git a/docs/assets/sounds/breathing/inhale.mp3 b/docs/assets/sounds/breathing/inhale.mp3 new file mode 100644 index 0000000..8e9ceec Binary files /dev/null and b/docs/assets/sounds/breathing/inhale.mp3 differ diff --git a/docs/assets/sounds/cough.mp3 b/docs/assets/sounds/cough.mp3 new file mode 100644 index 0000000..f5cc6a0 Binary files /dev/null and b/docs/assets/sounds/cough.mp3 differ