Add FirstOpen model
This commit is contained in:
parent
5545e346e8
commit
5815b6957a
7 changed files with 58 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
|||
package dev.lucasnlm.antimine.common.level
|
||||
|
||||
import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
|
||||
import dev.lucasnlm.antimine.common.level.database.models.Save
|
||||
import dev.lucasnlm.antimine.common.level.database.models.SaveStatus
|
||||
import dev.lucasnlm.antimine.common.level.database.models.Stats
|
||||
|
@ -16,7 +17,7 @@ class LevelFacade {
|
|||
private val randomGenerator: Random
|
||||
private val startTime = System.currentTimeMillis()
|
||||
private var saveId = 0
|
||||
private var firstOpen = 0
|
||||
private var firstOpen: FirstOpen = FirstOpen.Unknown
|
||||
|
||||
var hasMines = false
|
||||
private set
|
||||
|
@ -113,7 +114,7 @@ class LevelFacade {
|
|||
}
|
||||
}
|
||||
|
||||
firstOpen = safeIndex
|
||||
firstOpen = FirstOpen.Position(safeIndex)
|
||||
field.filterNot { it.safeZone }
|
||||
.toSet()
|
||||
.shuffled(randomGenerator)
|
||||
|
|
|
@ -5,6 +5,7 @@ import androidx.room.RoomDatabase
|
|||
import androidx.room.TypeConverters
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.DifficultyConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.FieldConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.FirstOpenConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.MinefieldConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.SaveStatusConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.dao.SaveDao
|
||||
|
@ -22,7 +23,8 @@ import dev.lucasnlm.antimine.common.level.database.models.Stats
|
|||
FieldConverter::class,
|
||||
SaveStatusConverter::class,
|
||||
MinefieldConverter::class,
|
||||
DifficultyConverter::class
|
||||
DifficultyConverter::class,
|
||||
FirstOpenConverter::class
|
||||
)
|
||||
abstract class AppDataBase : RoomDatabase() {
|
||||
abstract fun saveDao(): SaveDao
|
||||
|
|
|
@ -4,7 +4,6 @@ import androidx.room.TypeConverter
|
|||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
|
||||
class DifficultyConverter {
|
||||
|
||||
@TypeConverter
|
||||
fun toDifficulty(difficulty: Int): Difficulty =
|
||||
when (difficulty) {
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package dev.lucasnlm.antimine.common.level.database.converters
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
|
||||
|
||||
class FirstOpenConverter {
|
||||
@TypeConverter
|
||||
fun toFirstOpen(value: Int): FirstOpen = when {
|
||||
(value < 0) -> FirstOpen.Unknown
|
||||
else -> FirstOpen.Position(value)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toInteger(firstOpen: FirstOpen): Int = firstOpen.toInt()
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package dev.lucasnlm.antimine.common.level.database.models
|
||||
|
||||
/**
|
||||
* Used to define the position of the first open square.
|
||||
*/
|
||||
sealed class FirstOpen {
|
||||
/**
|
||||
* Used before the first step or before this value be recorded.
|
||||
*/
|
||||
object Unknown : FirstOpen()
|
||||
|
||||
/**
|
||||
* Describes the [value] of the first step.
|
||||
*/
|
||||
class Position (
|
||||
val value: Int
|
||||
) : FirstOpen()
|
||||
|
||||
override fun toString(): String = when(this) {
|
||||
is Position -> value.toString()
|
||||
else -> "Unknown"
|
||||
}
|
||||
|
||||
fun toInt(): Int = when (this) {
|
||||
is Position -> value
|
||||
else -> -1
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ import dev.lucasnlm.antimine.common.level.models.Area
|
|||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
import dev.lucasnlm.antimine.common.level.models.Minefield
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.FieldConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.FirstOpenConverter
|
||||
import dev.lucasnlm.antimine.common.level.database.converters.SaveStatusConverter
|
||||
|
||||
@Entity
|
||||
|
@ -30,8 +31,9 @@ data class Save(
|
|||
@ColumnInfo(name = "difficulty")
|
||||
val difficulty: Difficulty,
|
||||
|
||||
@TypeConverters(FirstOpenConverter::class)
|
||||
@ColumnInfo(name = "firstOpen")
|
||||
val firstOpen: Int,
|
||||
val firstOpen: FirstOpen,
|
||||
|
||||
@TypeConverters(SaveStatusConverter::class)
|
||||
@ColumnInfo(name = "status")
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.os.Handler
|
|||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dev.lucasnlm.antimine.common.level.LevelFacade
|
||||
import dev.lucasnlm.antimine.common.level.database.models.FirstOpen
|
||||
import dev.lucasnlm.antimine.common.level.models.Area
|
||||
import dev.lucasnlm.antimine.common.level.models.Difficulty
|
||||
import dev.lucasnlm.antimine.common.level.models.Event
|
||||
|
@ -96,10 +97,6 @@ class GameViewModel(
|
|||
else -> eventObserver.postValue(Event.ResumeGame)
|
||||
}
|
||||
|
||||
if (save.firstOpen > 0) {
|
||||
levelFacade.singleClick(save.firstOpen)
|
||||
}
|
||||
|
||||
analyticsManager.sentEvent(Analytics.ResumePreviousGame())
|
||||
return setup
|
||||
}
|
||||
|
@ -111,8 +108,10 @@ class GameViewModel(
|
|||
|
||||
val setup = save.minefield
|
||||
levelFacade = LevelFacade(setup, save.seed).apply {
|
||||
plantMinesExcept(save.firstOpen, true)
|
||||
singleClick(save.firstOpen)
|
||||
if (save.firstOpen is FirstOpen.Position) {
|
||||
plantMinesExcept(save.firstOpen.value, true)
|
||||
singleClick(save.firstOpen.value)
|
||||
}
|
||||
}
|
||||
|
||||
mineCount.postValue(setup.mines)
|
||||
|
@ -127,7 +126,7 @@ class GameViewModel(
|
|||
setup, currentDifficulty,
|
||||
levelFacade.seed,
|
||||
useAccessibilityMode(),
|
||||
save.firstOpen
|
||||
save.firstOpen.toInt()
|
||||
)
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue