Added buzzing & fixed first word bug

This commit is contained in:
2020-03-11 12:58:40 -04:00
parent 9989e9baef
commit 28dae815ec
3 changed files with 57 additions and 5 deletions

View File

@@ -17,6 +17,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.guesstheword"> package="com.example.android.guesstheword">
<uses-permission android:name="android.permission.VIBRATE" />
<application <application
android:name=".GuessItApplication" android:name=".GuessItApplication"

View File

@@ -16,18 +16,24 @@
package com.example.android.guesstheword.screens.game package com.example.android.guesstheword.screens.game
import android.content.Context
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.NavHostFragment.findNavController import androidx.navigation.fragment.NavHostFragment.findNavController
import com.example.android.guesstheword.R import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.GameFragmentBinding import com.example.android.guesstheword.databinding.GameFragmentBinding
/** /**
* Fragment where the game is played * Fragment where the game is played
*/ */
@@ -60,6 +66,13 @@ class GameFragment : Fragment() {
} }
}) })
viewModel.eventBuzz.observe(this, Observer {
if (it != BuzzType.NO_BUZZ) {
buzz(it.pattern)
viewModel.onBuzzComplete()
}
})
return binding.root return binding.root
} }
@@ -72,4 +85,16 @@ class GameFragment : Fragment() {
findNavController(this).navigate(action) findNavController(this).navigate(action)
} }
private fun buzz(pattern: LongArray) {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator?.let {
if (Build.VERSION.SDK_INT >= 26) {
it.vibrate(VibrationEffect.createWaveform(pattern, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
@Suppress("DEPRECATION")
it.vibrate(100)
}
}
}
} }

View File

@@ -7,12 +7,25 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel 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() { class GameViewModel(): ViewModel() {
companion object { companion object {
// These represent different important times
// This is when the game is over // This is when the game is over
const val DONE = 0L 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 // This is the number of milliseconds in a second
const val ONE_SECOND = 1000L const val ONE_SECOND = 1000L
// This is the total time of the game // This is the total time of the game
@@ -37,6 +50,10 @@ class GameViewModel(): ViewModel() {
DateUtils.formatElapsedTime(it / ONE_SECOND) DateUtils.formatElapsedTime(it / ONE_SECOND)
}; };
private val _eventBuzz = MutableLiveData<BuzzType>()
val eventBuzz: LiveData<BuzzType>
get() = _eventBuzz
private val _eventGameFinished = MutableLiveData<Boolean>() private val _eventGameFinished = MutableLiveData<Boolean>()
val eventGameFinished: LiveData<Boolean> val eventGameFinished: LiveData<Boolean>
get() = _eventGameFinished get() = _eventGameFinished
@@ -45,16 +62,20 @@ class GameViewModel(): ViewModel() {
private lateinit var wordList: MutableList<String> private lateinit var wordList: MutableList<String>
init { init {
resetList()
nextWord()
_eventGameFinished.value = false _eventGameFinished.value = false
_word.value = "" _word.value = ""
_score.value = 0 _score.value = 0
resetList()
nextWord()
timer = object : CountDownTimer(COUNTDOWN_TIME, ONE_SECOND) { timer = object : CountDownTimer(COUNTDOWN_TIME, ONE_SECOND) {
override fun onTick(millisUntilFinished: Long) { override fun onTick(millisUntilFinished: Long) {
_gameTimer.value = millisUntilFinished _gameTimer.value = millisUntilFinished
if (millisUntilFinished / ONE_SECOND <= COUNTDOWN_PANIC_SECONDS) {
_eventBuzz.value = BuzzType.COUNTDOWN_PANIC
}
} }
override fun onFinish() { override fun onFinish() {
@@ -104,7 +125,6 @@ class GameViewModel(): ViewModel() {
* Moves to the next word in the list * Moves to the next word in the list
*/ */
private fun nextWord() { private fun nextWord() {
//Select and remove a word from the list
if (wordList.isEmpty()) { if (wordList.isEmpty()) {
endGame() endGame()
} else { } else {
@@ -121,17 +141,23 @@ class GameViewModel(): ViewModel() {
fun onCorrect() { fun onCorrect() {
_score.value = (score.value)?.inc() _score.value = (score.value)?.inc()
_eventBuzz.value = BuzzType.CORRECT
nextWord() nextWord()
} }
fun endGame() { fun endGame() {
if (_gameTimer.value?.toInt() == 0) { if (_gameTimer.value?.toInt() == 0) {
timer.cancel() _gameTimer.value = DONE
} }
_eventBuzz.value = BuzzType.GAME_OVER
_eventGameFinished.value = true _eventGameFinished.value = true
} }
fun onGameFinishComplete() { fun onGameFinishComplete() {
_eventGameFinished.value = false _eventGameFinished.value = false
} }
fun onBuzzComplete() {
_eventBuzz.value = BuzzType.NO_BUZZ
}
} }