diff --git a/app/src/main/java/com/example/ellegadodepintia/TiendaActivity.kt b/app/src/main/java/com/example/ellegadodepintia/TiendaActivity.kt index 6db47be7ebcc82c8de5735fdc5dbc04d78e13b9c..a7251d3bf05a33fde599ec0662fbe67e1970821a 100644 --- a/app/src/main/java/com/example/ellegadodepintia/TiendaActivity.kt +++ b/app/src/main/java/com/example/ellegadodepintia/TiendaActivity.kt @@ -1,8 +1,15 @@ package com.example.ellegadodepintia +import android.graphics.Color import android.os.Bundle +import android.view.ViewGroup import android.widget.ImageView +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import com.example.ellegadodepintia.exploradoresDePintia.ModalDetallesCompra +import com.example.ellegadodepintia.exploradoresDePintia.model.GameState +import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.ObjetoConsumible +import com.example.ellegadodepintia.repositorios.RepositorioJugador import com.example.ellegadodepintia.repositorios.RepositorioObjetos class TiendaActivity : AppCompatActivity() { @@ -10,11 +17,16 @@ class TiendaActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_tienda) - val objetos = RepositorioObjetos.obtenerObjetosAleatoriosTienda(6) + val nivelCiudad = GameState.jugador.nivelCiudad + val nObjetos = nivelCiudad * 2 + val objetos = RepositorioObjetos.obtenerObjetosAleatoriosTienda(nObjetos) + val preciosGenerados = ArrayList<Int>() - //TODO hay que hacer que dependiendo del nivel de la ciudad haga más cosas - val slots = listOf( - findViewById<ImageView>(R.id.slot01), + val textoMonedas = findViewById<TextView>(R.id.textoMonedas) + GameState.jugador.monedas.toString().also { textoMonedas.text = it } + + val slots = listOf<ImageView>( + findViewById(R.id.slot01), findViewById(R.id.slot02), findViewById(R.id.slot03), findViewById(R.id.slot04), @@ -22,8 +34,78 @@ class TiendaActivity : AppCompatActivity() { findViewById(R.id.slot06) ) + val precios = listOf<TextView>( + findViewById(R.id.precio01), + findViewById(R.id.precio02), + findViewById(R.id.precio03), + findViewById(R.id.precio04), + findViewById(R.id.precio05), + findViewById(R.id.precio06) + ) + + val monedas = listOf<ImageView>( + findViewById(R.id.moneda01), + findViewById(R.id.moneda02), + findViewById(R.id.moneda03), + findViewById(R.id.moneda04), + findViewById(R.id.moneda05), + findViewById(R.id.moneda06) + ) + + precios.forEachIndexed { index, precio -> + if (index < nObjetos) { + val resultado = (30..75).random() / nivelCiudad + preciosGenerados.add(resultado) + resultado.toString().also { precio.text = it } + } else { + val nivelTaberna = index / 2 + 1 + "Taberna lvl $nivelTaberna".also { precio.text = it } + precio.textSize = 11f + precio.setTextColor(Color.RED) + + } + } + slots.forEachIndexed { index, slot -> - slot.setImageResource(objetos[index].imagen) + if (index < nObjetos) { + slot.setImageResource(objetos[index].imagen) + slot.setOnClickListener { + val modalDetallesFragment = ModalDetallesCompra().apply { + objeto = objetos[index] + coste = preciosGenerados[index] + } + + modalDetallesFragment.onDismissListener = { accionRealizada -> + if (accionRealizada) { + ejecutarCompra(objetos[index], preciosGenerados[index]) + } + } + + modalDetallesFragment.show(supportFragmentManager, "modalDetalles") + } + } else { + slot.setImageResource(R.drawable.asi_candado_tienda) + slot.scaleType = ImageView.ScaleType.CENTER + } + } + + monedas.forEachIndexed { index, moneda -> + if (index >= nObjetos) { + val parent = moneda.parent as? ViewGroup + parent?.removeView(moneda) + } + } + } + + private fun ejecutarCompra(objeto: ObjetoConsumible, coste: Int){ + val texto = findViewById<TextView>(R.id.textoInformacion) + if (GameState.jugador.monedas < coste){ + "No hay dinero".also { texto.text = it } + }else{ + GameState.jugador.actualizarMonedas(GameState.jugador.monedas - coste) + RepositorioJugador.setMonedas(GameState.jugador.monedas) + GameState.jugador.conseguirObjeto(objeto) + "Objeto comprado y añadido al inventario".also { texto.text = it } } } -} \ No newline at end of file +} diff --git a/app/src/main/res/drawable/asi_candado_tienda.png b/app/src/main/res/drawable/asi_candado_tienda.png new file mode 100644 index 0000000000000000000000000000000000000000..54e9192f897fe5e2285dba96950d0a9b878cca18 Binary files /dev/null and b/app/src/main/res/drawable/asi_candado_tienda.png differ diff --git a/app/src/main/res/layout/activity_tienda.xml b/app/src/main/res/layout/activity_tienda.xml index 72bf1c15f040f8dc3321a42ff214aa6fe2563ef4..0de1d78bdaa84757f0d691cb01d334fefc9a8177 100644 --- a/app/src/main/res/layout/activity_tienda.xml +++ b/app/src/main/res/layout/activity_tienda.xml @@ -26,6 +26,37 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + <LinearLayout + android:id="@+id/layoutMonedas" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="horizontal" + android:padding="5dp" + android:background="@drawable/style_texto_border" + android:gravity="center" + android:layout_marginBottom="8dp" + app:layout_constraintTop_toBottomOf="@id/tituloJuego" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent"> + + <TextView + android:id="@+id/textoMonedas" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:fontFamily="@font/pixeled" + android:text="100" + android:textColor="#401201" + android:textSize="20sp" /> + + <ImageView + android:layout_width="20dp" + android:layout_height="20dp" + android:layout_marginStart="4dp" + android:contentDescription="Icono de moneda" + android:src="@drawable/logo_pintia" + android:scaleType="centerCrop" /> + </LinearLayout> + <GridLayout android:id="@+id/inventarioGrid" android:layout_width="wrap_content" @@ -39,7 +70,7 @@ android:rowCount="3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/tituloJuego"> + app:layout_constraintTop_toBottomOf="@id/layoutMonedas"> <LinearLayout android:orientation="vertical" @@ -86,6 +117,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda01" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -142,6 +174,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda02" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -200,6 +233,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda03" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -256,6 +290,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda04" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -312,6 +347,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda05" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -368,6 +404,7 @@ android:textSize="20sp" /> <ImageView + android:id="@+id/moneda06" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginStart="4dp" @@ -380,6 +417,22 @@ </GridLayout> + <TextView + android:id="@+id/textoInformacion" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Aquí aparecera la información relevante de las compras" + android:textSize="15sp" + android:fontFamily="@font/pixeled" + android:padding="6sp" + android:textColor="#401201" + android:background="@drawable/style_texto_border" + android:layout_marginTop="10dp" + app:layout_constraintTop_toBottomOf="@id/inventarioGrid" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + android:gravity="center"/> + <LinearLayout android:id="@+id/bottom_navigation" style="?android:attr/buttonBarStyle"