mirror of
https://github.com/cmclark00/mintris.git
synced 2025-05-18 17:35:21 +01:00
Refactor: Introduce ViewModel for score/level, fix game over flow
This commit is contained in:
parent
5952cac760
commit
0a5bf6bb7e
5 changed files with 151 additions and 168 deletions
40
app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt
Normal file
40
app/src/main/java/com/pixelmintdrop/MainActivityViewModel.kt
Normal file
|
@ -0,0 +1,40 @@
|
|||
package com.pixelmintdrop
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class MainActivityViewModel : ViewModel() {
|
||||
|
||||
// Private MutableLiveData for internal updates
|
||||
private val _currentScore = MutableLiveData<Long>(0L)
|
||||
private val _currentLevel = MutableLiveData<Int>(1)
|
||||
|
||||
// Public LiveData for observation by the Activity
|
||||
val currentScore: LiveData<Long> = _currentScore
|
||||
val currentLevel: LiveData<Int> = _currentLevel
|
||||
|
||||
// Example function to update the score (logic would be moved here)
|
||||
fun incrementScore(points: Long) {
|
||||
_currentScore.value = (_currentScore.value ?: 0L) + points
|
||||
// Potentially add logic here to check for level up based on score
|
||||
}
|
||||
|
||||
// Function to set the score directly
|
||||
fun setScore(score: Long) {
|
||||
_currentScore.value = score
|
||||
}
|
||||
|
||||
// Example function to update the level
|
||||
fun setLevel(level: Int) {
|
||||
_currentLevel.value = level
|
||||
}
|
||||
|
||||
fun resetGame() {
|
||||
_currentScore.value = 0L
|
||||
_currentLevel.value = 1
|
||||
// Reset other game state within the ViewModel as needed
|
||||
}
|
||||
|
||||
// Add other state variables and logic related to game state here
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue