Skip to content

Instantly share code, notes, and snippets.

@GRizzi91
Created October 13, 2021 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GRizzi91/6ec48ab4a3eb9cc5ae80d4989c84fad9 to your computer and use it in GitHub Desktop.
Save GRizzi91/6ec48ab4a3eb9cc5ae80d4989c84fad9 to your computer and use it in GitHub Desktop.
@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