Skip to content

Commit

Permalink
Add game success and failure audio feedback (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasuryat committed Dec 17, 2021
1 parent 31aabde commit b4cee63
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.jayasuryat.uigame.feedback.MusicManager
import com.jayasuryat.uigame.feedback.VibrationManager
import com.jayasuryat.uigame.logic.GameState
import com.jayasuryat.util.LogCompositions
Expand All @@ -17,6 +18,7 @@ internal fun GameFeedback(

val context = LocalContext.current
val vibrationManager = remember { VibrationManager(context) }
val musicManager = remember { MusicManager(context) }

when (gameState.value) {

Expand All @@ -25,10 +27,12 @@ internal fun GameFeedback(
is GameState.GameStarted -> Unit

is GameState.GameEnded.GameCompleted -> {
musicManager.success()
vibrationManager.shortVibrationNow()
}

is GameState.GameEnded.GameOver -> {
musicManager.failure()
vibrationManager.mediumVibrationNow()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ internal class MusicManager(private val context: Context) {
private val sfxCancel: MediaPlayer by mediaPlayerOf(R.raw.cancel) {
setVolume(0.02f, 0.02f)
}
private val sfxSuccess: MediaPlayer by mediaPlayerOf(R.raw.success) {
setVolume(0.02f, 0.02f)
}

private val sfxFailure: MediaPlayer by mediaPlayerOf(R.raw.failure) {
setVolume(0.02f, 0.02f)
}
// endregion

// region : Public API
fun pop() = sfxPop.start()
fun affirmative() = sfxAffirmative.start()
fun cancel() = sfxCancel.start()
fun success() = sfxSuccess.start()
fun failure() = sfxFailure.start()

fun dispose() {
tlc.onEachInitialized { player ->
Expand Down
Binary file added ui-game/src/main/res/raw/failure.mp3
Binary file not shown.
Binary file added ui-game/src/main/res/raw/success.mp3
Binary file not shown.

0 comments on commit b4cee63

Please sign in to comment.