mirror of
https://github.com/cmclark00/mintris.git
synced 2025-05-17 23:55:21 +01:00
Refactor: Move selectedLevel and randomModeEnabled to ViewModel
This commit is contained in:
parent
301bf1e64f
commit
2e2cd92192
2 changed files with 75 additions and 24 deletions
|
@ -86,12 +86,10 @@ class MainActivity : AppCompatActivity(),
|
||||||
// Game state
|
// Game state
|
||||||
private var piecesPlaced = 0
|
private var piecesPlaced = 0
|
||||||
private var gameStartTime = 0L
|
private var gameStartTime = 0L
|
||||||
private var selectedLevel = 1
|
|
||||||
private val maxLevel = 20
|
private val maxLevel = 20
|
||||||
private var lastLines = 0 // Track the previous lines count
|
private var lastLines = 0 // Track the previous lines count
|
||||||
private var lastLinesGroup = 0 // Track which 10-line group we're in (0-9, 10-19, etc.)
|
private var lastLinesGroup = 0 // Track which 10-line group we're in (0-9, 10-19, etc.)
|
||||||
private var lastRandomLevel = 0 // Track the level at which we last did a random change
|
private var lastRandomLevel = 0 // Track the level at which we last did a random change
|
||||||
private var isRandomModeEnabled = false
|
|
||||||
private var currentTheme = PlayerProgressionManager.THEME_CLASSIC
|
private var currentTheme = PlayerProgressionManager.THEME_CLASSIC
|
||||||
private val handler = Handler(Looper.getMainLooper())
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
|
@ -241,9 +239,11 @@ class MainActivity : AppCompatActivity(),
|
||||||
gameMusic.setEnabled(enabled)
|
gameMusic.setEnabled(enabled)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Load random mode setting
|
// Observe Random Mode state
|
||||||
isRandomModeEnabled = getSharedPreferences("com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE)
|
viewModel.isRandomModeEnabled.observe(this, Observer { enabled ->
|
||||||
.getBoolean("random_mode_enabled", false)
|
// Update the switch UI
|
||||||
|
binding.randomModeSwitch?.isChecked = enabled
|
||||||
|
})
|
||||||
|
|
||||||
// Initialize gamepad controller
|
// Initialize gamepad controller
|
||||||
gamepadController = GamepadController(gameView)
|
gamepadController = GamepadController(gameView)
|
||||||
|
@ -397,7 +397,7 @@ class MainActivity : AppCompatActivity(),
|
||||||
val newHighScore = highScoreManager.isHighScore(finalScore)
|
val newHighScore = highScoreManager.isHighScore(finalScore)
|
||||||
statsManager.endSession() // End session after calculations
|
statsManager.endSession() // End session after calculations
|
||||||
|
|
||||||
Log.d(TAG, "Game Over. Score: $finalScore, Level: ${viewModel.currentLevel.value}, Lines: ${gameBoard.lines}, Start Level: $selectedLevel, New High Score: $newHighScore, XP Gained: $xpGained")
|
Log.d(TAG, "Game Over. Score: $finalScore, Level: ${viewModel.currentLevel.value}, Lines: ${gameBoard.lines}, Start Level: ${viewModel.selectedLevel.value}, New High Score: $newHighScore, XP Gained: $xpGained")
|
||||||
|
|
||||||
// Show appropriate screen: Progression or Game Over directly
|
// Show appropriate screen: Progression or Game Over directly
|
||||||
if (xpGained > 0 || newHighScore) {
|
if (xpGained > 0 || newHighScore) {
|
||||||
|
@ -480,16 +480,18 @@ class MainActivity : AppCompatActivity(),
|
||||||
|
|
||||||
binding.pauseLevelUpButton.setOnClickListener {
|
binding.pauseLevelUpButton.setOnClickListener {
|
||||||
gameHaptics.performHapticFeedback(it, HapticFeedbackConstants.VIRTUAL_KEY)
|
gameHaptics.performHapticFeedback(it, HapticFeedbackConstants.VIRTUAL_KEY)
|
||||||
if (selectedLevel < maxLevel) {
|
val currentSelected = viewModel.selectedLevel.value ?: 1
|
||||||
selectedLevel++
|
if (currentSelected < maxLevel) {
|
||||||
|
viewModel.setSelectedLevel(currentSelected + 1)
|
||||||
updateLevelSelector()
|
updateLevelSelector()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.pauseLevelDownButton.setOnClickListener {
|
binding.pauseLevelDownButton.setOnClickListener {
|
||||||
gameHaptics.performHapticFeedback(it, HapticFeedbackConstants.VIRTUAL_KEY)
|
gameHaptics.performHapticFeedback(it, HapticFeedbackConstants.VIRTUAL_KEY)
|
||||||
if (selectedLevel > 1) {
|
val currentSelected = viewModel.selectedLevel.value ?: 1
|
||||||
selectedLevel--
|
if (currentSelected > 1) {
|
||||||
|
viewModel.setSelectedLevel(currentSelected - 1)
|
||||||
updateLevelSelector()
|
updateLevelSelector()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -531,6 +533,16 @@ class MainActivity : AppCompatActivity(),
|
||||||
|
|
||||||
// Initialize pause menu items for gamepad navigation
|
// Initialize pause menu items for gamepad navigation
|
||||||
initPauseMenuNavigation()
|
initPauseMenuNavigation()
|
||||||
|
|
||||||
|
// Set up random mode switch
|
||||||
|
binding.randomModeSwitch?.apply {
|
||||||
|
isChecked = viewModel.isRandomModeEnabled.value ?: false
|
||||||
|
isEnabled = progressionManager.getPlayerLevel() >= 5
|
||||||
|
setOnCheckedChangeListener { _, isChecked ->
|
||||||
|
// Only need to call toggle, ViewModel handles saving
|
||||||
|
viewModel.toggleRandomMode()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -653,14 +665,11 @@ class MainActivity : AppCompatActivity(),
|
||||||
|
|
||||||
// Set up random mode switch
|
// Set up random mode switch
|
||||||
binding.randomModeSwitch?.apply {
|
binding.randomModeSwitch?.apply {
|
||||||
isChecked = isRandomModeEnabled
|
isChecked = viewModel.isRandomModeEnabled.value ?: false
|
||||||
isEnabled = progressionManager.getPlayerLevel() >= 5
|
isEnabled = progressionManager.getPlayerLevel() >= 5
|
||||||
setOnCheckedChangeListener { _, isChecked ->
|
setOnCheckedChangeListener { _, isChecked ->
|
||||||
isRandomModeEnabled = isChecked
|
// Only need to call toggle, ViewModel handles saving
|
||||||
getSharedPreferences("com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE)
|
viewModel.toggleRandomMode()
|
||||||
.edit()
|
|
||||||
.putBoolean("random_mode_enabled", isChecked)
|
|
||||||
.apply()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -835,8 +844,8 @@ class MainActivity : AppCompatActivity(),
|
||||||
* Update the level selector display
|
* Update the level selector display
|
||||||
*/
|
*/
|
||||||
private fun updateLevelSelector() {
|
private fun updateLevelSelector() {
|
||||||
binding.pauseLevelText.text = selectedLevel.toString()
|
binding.pauseLevelText.text = viewModel.selectedLevel.value.toString()
|
||||||
gameBoard.updateLevel(selectedLevel)
|
gameBoard.updateLevel(viewModel.selectedLevel.value ?: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -879,10 +888,10 @@ class MainActivity : AppCompatActivity(),
|
||||||
* Start a new game
|
* Start a new game
|
||||||
*/
|
*/
|
||||||
private fun startGame() {
|
private fun startGame() {
|
||||||
Log.d(TAG, "Starting game at level $selectedLevel")
|
Log.d(TAG, "Starting game at level ${viewModel.selectedLevel.value}")
|
||||||
// Reset game state
|
// Reset game state
|
||||||
viewModel.resetGame() // Resets score and level in ViewModel
|
viewModel.resetGame() // Resets score and level in ViewModel
|
||||||
viewModel.setLevel(selectedLevel) // Set initial level from selection
|
viewModel.setLevel(viewModel.selectedLevel.value ?: 1) // Set initial level from selection
|
||||||
piecesPlaced = 0
|
piecesPlaced = 0
|
||||||
gameStartTime = System.currentTimeMillis()
|
gameStartTime = System.currentTimeMillis()
|
||||||
lastLines = 0
|
lastLines = 0
|
||||||
|
@ -953,7 +962,7 @@ class MainActivity : AppCompatActivity(),
|
||||||
val newHighScore = highScoreManager.isHighScore(finalScore)
|
val newHighScore = highScoreManager.isHighScore(finalScore)
|
||||||
statsManager.endSession() // End session after calculations
|
statsManager.endSession() // End session after calculations
|
||||||
|
|
||||||
Log.d(TAG, "Game Over. Score: $finalScore, Level: ${viewModel.currentLevel.value}, Lines: ${gameBoard.lines}, Start Level: $selectedLevel, New High Score: $newHighScore, XP Gained: $xpGained")
|
Log.d(TAG, "Game Over. Score: $finalScore, Level: ${viewModel.currentLevel.value}, Lines: ${gameBoard.lines}, Start Level: ${viewModel.selectedLevel.value}, New High Score: $newHighScore, XP Gained: $xpGained")
|
||||||
|
|
||||||
// Show appropriate screen: Progression or Game Over directly
|
// Show appropriate screen: Progression or Game Over directly
|
||||||
if (xpGained > 0 || newHighScore) {
|
if (xpGained > 0 || newHighScore) {
|
||||||
|
@ -997,7 +1006,7 @@ class MainActivity : AppCompatActivity(),
|
||||||
// Reset session stats
|
// Reset session stats
|
||||||
statsManager.startNewSession()
|
statsManager.startNewSession()
|
||||||
progressionManager.startNewSession()
|
progressionManager.startNewSession()
|
||||||
gameBoard.updateLevel(selectedLevel)
|
gameBoard.updateLevel(viewModel.selectedLevel.value ?: 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun restartGame() {
|
private fun restartGame() {
|
||||||
|
@ -2007,7 +2016,7 @@ class MainActivity : AppCompatActivity(),
|
||||||
binding.comboText.text = gameBoard.getCombo().toString()
|
binding.comboText.text = gameBoard.getCombo().toString()
|
||||||
|
|
||||||
// If random mode is enabled, check if we should change theme or block skin
|
// If random mode is enabled, check if we should change theme or block skin
|
||||||
if (isRandomModeEnabled) {
|
if (viewModel.isRandomModeEnabled.value == true) {
|
||||||
// Get the current 10-line group (0 for 0-9, 1 for 10-19, etc.)
|
// Get the current 10-line group (0 for 0-9, 1 for 10-19, etc.)
|
||||||
val currentLinesGroup = lines / 10
|
val currentLinesGroup = lines / 10
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,11 @@ package com.pixelmintdrop
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import android.app.Application
|
||||||
|
import androidx.lifecycle.AndroidViewModel
|
||||||
|
import android.content.Context
|
||||||
|
|
||||||
class MainActivityViewModel : ViewModel() {
|
class MainActivityViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
|
|
||||||
// Private MutableLiveData for internal updates
|
// Private MutableLiveData for internal updates
|
||||||
private val _currentScore = MutableLiveData<Long>(0L)
|
private val _currentScore = MutableLiveData<Long>(0L)
|
||||||
|
@ -22,6 +25,18 @@ class MainActivityViewModel : ViewModel() {
|
||||||
val isMusicEnabled: LiveData<Boolean> = _isMusicEnabled
|
val isMusicEnabled: LiveData<Boolean> = _isMusicEnabled
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
|
|
||||||
|
// --- Game Settings State ---
|
||||||
|
private val _selectedLevel = MutableLiveData<Int>(1) // Default start level
|
||||||
|
val selectedLevel: LiveData<Int> = _selectedLevel
|
||||||
|
|
||||||
|
private val _isRandomModeEnabled = MutableLiveData<Boolean>(false)
|
||||||
|
val isRandomModeEnabled: LiveData<Boolean> = _isRandomModeEnabled
|
||||||
|
// ---------------------------
|
||||||
|
|
||||||
|
init { // Load initial random mode setting
|
||||||
|
loadSettings()
|
||||||
|
}
|
||||||
|
|
||||||
// Example function to update the score (logic would be moved here)
|
// Example function to update the score (logic would be moved here)
|
||||||
fun incrementScore(points: Long) {
|
fun incrementScore(points: Long) {
|
||||||
_currentScore.value = (_currentScore.value ?: 0L) + points
|
_currentScore.value = (_currentScore.value ?: 0L) + points
|
||||||
|
@ -58,5 +73,32 @@ class MainActivityViewModel : ViewModel() {
|
||||||
}
|
}
|
||||||
// --------------------------
|
// --------------------------
|
||||||
|
|
||||||
|
// --- Game Settings Logic ---
|
||||||
|
fun setSelectedLevel(level: Int) {
|
||||||
|
_selectedLevel.value = level
|
||||||
|
}
|
||||||
|
|
||||||
|
fun toggleRandomMode() {
|
||||||
|
val newValue = !(_isRandomModeEnabled.value ?: false)
|
||||||
|
_isRandomModeEnabled.value = newValue
|
||||||
|
saveRandomModeSetting(newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadSettings() {
|
||||||
|
val prefs = getApplication<Application>().getSharedPreferences(
|
||||||
|
"com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE
|
||||||
|
)
|
||||||
|
_isRandomModeEnabled.value = prefs.getBoolean("random_mode_enabled", false)
|
||||||
|
// Load other settings if needed
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveRandomModeSetting(enabled: Boolean) {
|
||||||
|
val prefs = getApplication<Application>().getSharedPreferences(
|
||||||
|
"com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE
|
||||||
|
)
|
||||||
|
prefs.edit().putBoolean("random_mode_enabled", enabled).apply()
|
||||||
|
}
|
||||||
|
// ---------------------------
|
||||||
|
|
||||||
// Add other state variables and logic related to game state here
|
// Add other state variables and logic related to game state here
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue