From 28dae815ecc9572c0531248af46664bb6203c2e5 Mon Sep 17 00:00:00 2001 From: Andrew Kemp Date: Wed, 11 Mar 2020 12:58:40 -0400 Subject: [PATCH] Added buzzing & fixed first word bug --- app/src/main/AndroidManifest.xml | 1 + .../guesstheword/screens/game/GameFragment.kt | 25 +++++++++++++ .../screens/game/GameViewModel.kt | 36 ++++++++++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 149563e..35c4172 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,6 +17,7 @@ + = 26) { + it.vibrate(VibrationEffect.createWaveform(pattern, VibrationEffect.DEFAULT_AMPLITUDE)) + } else { + @Suppress("DEPRECATION") + it.vibrate(100) + } + } + } + } diff --git a/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt b/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt index cfe46d3..d4e4c3c 100644 --- a/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt +++ b/app/src/main/java/com/example/android/guesstheword/screens/game/GameViewModel.kt @@ -7,12 +7,25 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Transformations import androidx.lifecycle.ViewModel +private val CORRECT_BUZZ_PATTERN = longArrayOf(100, 100, 100, 100, 100, 100) +private val PANIC_BUZZ_PATTERN = longArrayOf(0, 200) +private val GAME_OVER_BUZZ_PATTERN = longArrayOf(0, 2000) +private val NO_BUZZ_PATTERN = longArrayOf(0) + +enum class BuzzType(val pattern: LongArray) { + CORRECT(CORRECT_BUZZ_PATTERN), + GAME_OVER(GAME_OVER_BUZZ_PATTERN), + COUNTDOWN_PANIC(PANIC_BUZZ_PATTERN), + NO_BUZZ(NO_BUZZ_PATTERN) +} + class GameViewModel(): ViewModel() { companion object { - // These represent different important times // This is when the game is over const val DONE = 0L + // This is the time when the phone will start buzzing each second + private const val COUNTDOWN_PANIC_SECONDS = 10L // This is the number of milliseconds in a second const val ONE_SECOND = 1000L // This is the total time of the game @@ -37,6 +50,10 @@ class GameViewModel(): ViewModel() { DateUtils.formatElapsedTime(it / ONE_SECOND) }; + private val _eventBuzz = MutableLiveData() + val eventBuzz: LiveData + get() = _eventBuzz + private val _eventGameFinished = MutableLiveData() val eventGameFinished: LiveData get() = _eventGameFinished @@ -45,16 +62,20 @@ class GameViewModel(): ViewModel() { private lateinit var wordList: MutableList init { - resetList() - nextWord() _eventGameFinished.value = false _word.value = "" _score.value = 0 + resetList() + nextWord() + timer = object : CountDownTimer(COUNTDOWN_TIME, ONE_SECOND) { override fun onTick(millisUntilFinished: Long) { _gameTimer.value = millisUntilFinished + if (millisUntilFinished / ONE_SECOND <= COUNTDOWN_PANIC_SECONDS) { + _eventBuzz.value = BuzzType.COUNTDOWN_PANIC + } } override fun onFinish() { @@ -104,7 +125,6 @@ class GameViewModel(): ViewModel() { * Moves to the next word in the list */ private fun nextWord() { - //Select and remove a word from the list if (wordList.isEmpty()) { endGame() } else { @@ -121,17 +141,23 @@ class GameViewModel(): ViewModel() { fun onCorrect() { _score.value = (score.value)?.inc() + _eventBuzz.value = BuzzType.CORRECT nextWord() } fun endGame() { if (_gameTimer.value?.toInt() == 0) { - timer.cancel() + _gameTimer.value = DONE } + _eventBuzz.value = BuzzType.GAME_OVER _eventGameFinished.value = true } fun onGameFinishComplete() { _eventGameFinished.value = false } + + fun onBuzzComplete() { + _eventBuzz.value = BuzzType.NO_BUZZ + } } \ No newline at end of file