Skip to content
Snippets Groups Projects
Commit 2b2e2dc1 authored by izajime's avatar izajime
Browse files

Feature: mejoras en la tienda, no funciona el notifyOberservers

parent f53c5961
Branches
No related tags found
2 merge requests!65Feature:,!51Tienda to guapa
......@@ -3,13 +3,14 @@ package com.example.ellegadodepintia
import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
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.LayoutUtils
import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.ObjetoConsumible
import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.Objeto
import com.example.ellegadodepintia.repositorios.RepositorioJugador
import com.example.ellegadodepintia.repositorios.RepositorioObjetos
......@@ -18,16 +19,30 @@ class TiendaActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tienda)
GameState.jugador.addObserver { LayoutUtils.actualizarMonedasTienda(this) }
GameState.jugador.addObserver { LayoutUtils.actualizarMonedasTienda(this)
cargarObjetos()}
val nivelCiudad = GameState.jugador.nivelCiudad
val nObjetos = nivelCiudad * 2
val objetos = RepositorioObjetos.obtenerObjetosAleatoriosTienda(nObjetos)
val preciosGenerados = ArrayList<Int>()
val botonRefrescar = findViewById<Button>(R.id.botonRefrescar)
botonRefrescar.setOnClickListener {
GameState.jugador.actualizarObjetosTienda(nObjetos)
}
val textoMonedas = findViewById<TextView>(R.id.textoMonedas)
GameState.jugador.monedas.toString().also { textoMonedas.text = it }
cargarObjetos()
}
private fun cargarObjetos(){
val nivelCiudad = GameState.jugador.nivelCiudad
val nObjetos = nivelCiudad * 2
val objetos = RepositorioObjetos.convertirNombresAObjetos(GameState.jugador.objetosTienda)
val preciosGenerados = ArrayList<Int>()
val slots = listOf<ImageView>(
findViewById(R.id.slot01),
findViewById(R.id.slot02),
......@@ -71,7 +86,7 @@ class TiendaActivity : AppCompatActivity() {
slots.forEachIndexed { index, slot ->
if (index < nObjetos) {
slot.setImageResource(objetos[index].imagen)
slot.setImageResource(objetos[index]!!.imagen)
slot.setOnClickListener {
val modalDetallesFragment = ModalDetallesCompra().apply {
objeto = objetos[index]
......@@ -100,14 +115,16 @@ class TiendaActivity : AppCompatActivity() {
}
}
private fun ejecutarCompra(objeto: ObjetoConsumible, coste: Int){
private fun ejecutarCompra(objeto: Objeto?, 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)
if (objeto != null) {
GameState.jugador.conseguirObjeto(objeto)
}
"Objeto comprado y añadido al inventario".also { texto.text = it }
}
}
......
......@@ -15,6 +15,8 @@ import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.Objeto
import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.ObjetoEquipable
import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.ObjetoUtilidad
import com.example.ellegadodepintia.exploradoresDePintia.model.objeto.efectoObjeto.EfectoEquipableAumento
import com.example.ellegadodepintia.repositorios.RepositorioJugador
import com.example.ellegadodepintia.repositorios.RepositorioObjetos
class Jugador {
var nivelCiudad = 1
......@@ -27,6 +29,7 @@ class Jugador {
var habilidades = ArrayList<Habilidad>(1)
var buffos = ArrayList<EfectoBuffo>()
var monedas = 0
var objetosTienda = ArrayList<String>(6)
init {
atributos[Atributo.Vida] = 10
......@@ -169,6 +172,13 @@ class Jugador {
notifyObservers()
}
fun actualizarObjetosTienda(nObjetos : Int){
val nuevosObjetos = RepositorioObjetos.obtenerObjetosAleatoriosTienda(nObjetos)
this.objetosTienda = nuevosObjetos
RepositorioJugador.setObjetosTienda(nuevosObjetos)
notifyObservers()
}
fun reset() {
observers.clear()
......@@ -185,6 +195,7 @@ class Jugador {
vidaMax = 10
energiaMax = 10
actualizarObjetosTienda(nivelCiudad*2)
}
}
......@@ -14,6 +14,14 @@ object RepositorioJugador {
db.collection("users").document(email).get().addOnSuccessListener {
GameState.jugador.monedas = (it.get("monedas") as Long).toInt()
GameState.jugador.nivelCiudad = (it.get("nivelCiudad") as Long).toInt()
val objetosTienda = (it.get("objetosTienda") as? List<*>)
?.filterIsInstance<String>()
?.toCollection(ArrayList())
?: ArrayList()
println(objetosTienda)
GameState.jugador.objetosTienda = objetosTienda
}
}
......@@ -21,7 +29,8 @@ object RepositorioJugador {
db.collection("users").document(email).set(
hashMapOf("monedas" to 0,
"username" to username,
"nivelCiudad" to 1)
"nivelCiudad" to 1,
"objetosTienda" to RepositorioObjetos.obtenerObjetosAleatoriosTienda(2))
)
}
......@@ -30,6 +39,11 @@ object RepositorioJugador {
userRef.set(hashMapOf("monedas" to monedas), SetOptions.merge())
}
fun setObjetosTienda(objetosTienda : ArrayList<String>) {
val userRef = db.collection("users").document(email)
userRef.set(hashMapOf("objetosTienda" to objetosTienda), SetOptions.merge())
}
fun cargarJugadores(callback: (List<PlayerLeaderBoard>) -> Unit) {
db.collection("users")
.get()
......
......@@ -86,8 +86,18 @@ object RepositorioObjetos {
return objetos[nombre]
}
fun obtenerObjetosAleatoriosTienda(cantidad: Int): List<ObjetoConsumible> {
fun obtenerObjetosAleatoriosTienda(cantidad: Int): ArrayList<String> {
val consumibles = objetos.values.filterIsInstance<ObjetoConsumible>()
return List(cantidad) { consumibles.random() }
return ArrayList<String>().apply {
repeat(cantidad) {
add(consumibles.random().nombre)
}
}
}
fun convertirNombresAObjetos(nombres: List<String>): List<Objeto?> {
return nombres.map { nombre ->
obtenerObjetoPorNombre(nombre)
}
}
}
\ No newline at end of file
......@@ -433,6 +433,21 @@
app:layout_constraintEnd_toEndOf="parent"
android:gravity="center"/>
<Button
android:id="@+id/botonRefrescar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="@color/pintiaButton"
android:fontFamily="@font/pixeled"
android:text="Refrescar tienda por 1 moneda"
android:textColor="@color/pintiaButtonText"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/textoInformacion"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<LinearLayout
android:id="@+id/bottom_navigation"
style="?android:attr/buttonBarStyle"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment