Created
October 13, 2021 16:44
-
-
Save GRizzi91/6ec48ab4a3eb9cc5ae80d4989c84fad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltViewModel | |
class GameViewModel @Inject constructor() : ViewModel() { | |
private val field = Field() | |
private val stateFlow = MutableStateFlow(field.getActualField()) | |
val state : StateFlow<Array<IntArray>> = stateFlow | |
init { | |
viewModelScope.launch { | |
while (!field.isGameEnded) { | |
delay(500) | |
// The tick method contains all the | |
// operating logic of the game. | |
field.tick() | |
stateFlow.emit(field.getActualField()) | |
} | |
} | |
} | |
fun moveRight() { | |
viewModelScope.launch { | |
field.moveRight() | |
stateFlow.emit(field.getActualField()) | |
} | |
} | |
fun moveLeft() { | |
viewModelScope.launch { | |
field.moveLeft() | |
stateFlow.emit(field.getActualField()) | |
} | |
} | |
fun turn() { | |
viewModelScope.launch { | |
field.turnPiece() | |
stateFlow.emit(field.getActualField()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment