diff --git a/app/src/main/java/com/example/ellegadodepintia/TabernaActivity.kt b/app/src/main/java/com/example/ellegadodepintia/TabernaActivity.kt index 47a4210baa45537e5ff7b103b632f6c6f643dec8..995444e8d8f5671dab1caa9755fcf542ddfc265f 100644 --- a/app/src/main/java/com/example/ellegadodepintia/TabernaActivity.kt +++ b/app/src/main/java/com/example/ellegadodepintia/TabernaActivity.kt @@ -14,15 +14,25 @@ class TabernaActivity : AppCompatActivity() { GameState.jugador.addObserver { actualizarNivelTaberna() } val botonMejorar = findViewById<TextView>(R.id.botonMejorar) + val textoInformacion = findViewById<TextView>(R.id.textoInformacion) actualizarNivelTaberna() + botonMejorar.setOnClickListener { val confirmacionFragment = ModalConfirmacion() + + confirmacionFragment.onDismissListener = { mejorar -> + if (mejorar) { + "Se ha realizado la mejora con éxito".also { textoInformacion.text = it } + } else { + "No se ha podido realizar la mejora".also { textoInformacion.text = it } + } + } confirmacionFragment.show(supportFragmentManager, "confirmacion") } } - private fun actualizarNivelTaberna(){ + private fun actualizarNivelTaberna() { val textoNivel = findViewById<TextView>(R.id.nivelActual) "Nivel actual: ${GameState.jugador.nivelCiudad}".also { textoNivel.text = it } diff --git a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/ModalConfirmacion.kt b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/ModalConfirmacion.kt index cacbf64fc49a3a31d11ffb644e31145de8ca1c51..e1ae727cebdad71c2402621eb2e5e231756a506d 100644 --- a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/ModalConfirmacion.kt +++ b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/ModalConfirmacion.kt @@ -1,6 +1,5 @@ package com.example.ellegadodepintia.exploradoresDePintia -import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -11,11 +10,12 @@ import androidx.fragment.app.DialogFragment import com.example.ellegadodepintia.R import com.example.ellegadodepintia.exploradoresDePintia.model.GameState import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.Objeto +import com.example.ellegadodepintia.repositorios.RepositorioJugador class ModalConfirmacion : DialogFragment() { var objeto: Objeto? = null var coste: Int? = null - private var onDismissListener: (() -> Unit)? = null + var onDismissListener: ((isMejora: Boolean) -> Unit)? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -29,29 +29,33 @@ class ModalConfirmacion : DialogFragment() { val nivelActual = GameState.jugador.nivelCiudad val costeNuevoNivel = 50 * nivelActual + val monedasJugador = GameState.jugador.monedas val textoInformacion = view.findViewById<TextView>(R.id.textoInformacion) ("Vas a mejorar la taberna por el coste de $costeNuevoNivel monedas. ¿Estás seguro?").also { textoInformacion.text = it } val botonMejorar = view.findViewById<Button>(R.id.botonMejorar) botonMejorar.setOnClickListener{ - val nObjetos = (nivelActual + 1) * 2 - GameState.jugador.actualizarObjetosTienda(nObjetos) - GameState.jugador.actualizarObjetosArmeria(nObjetos) - GameState.jugador.actualizarNivelCiudad(nivelActual + 1) - "Se ha subido de nivel la taberna correctamente".also { textoInformacion.text = it } - dismiss() + if (monedasJugador >= costeNuevoNivel){ + val nObjetos = (nivelActual + 1) * 2 + GameState.jugador.actualizarObjetosTienda(nObjetos) + GameState.jugador.actualizarObjetosArmeria(nObjetos) + GameState.jugador.actualizarNivelCiudad(nivelActual + 1) + GameState.jugador.actualizarMonedas(monedasJugador - costeNuevoNivel) + RepositorioJugador.setMonedas(monedasJugador - costeNuevoNivel) + onDismissListener?.invoke(true) + dismiss() + } else { + onDismissListener?.invoke(false) + dismiss() + } + } val botonCancelar = view.findViewById<Button>(R.id.botonCancelar) botonCancelar.setOnClickListener{ + onDismissListener?.invoke(false) dismiss() } } - - override fun onDismiss(dialog: DialogInterface) { - super.onDismiss(dialog) - onDismissListener?.invoke() - } - }