Browse Source

Tweaks, fix pause

pull/633/head
Nikolay Igotti 4 years ago
parent
commit
b9d7d28997
  1. 8
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Game.kt
  2. 2
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Piece.kt
  3. 2
      examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/fallingBalls.kt
  4. 15
      examples/falling_balls_with_web/src/jsMain/kotlin/androidx/compose/web/with-web/App.kt
  5. 6
      examples/falling_balls_with_web/src/jvmMain/kotlin/App.kt

8
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Game.kt

@ -42,17 +42,17 @@ abstract class Game {
var numBlocks by mutableStateOf(5) var numBlocks by mutableStateOf(5)
fun isInBoundaries(pieceData: PieceData): Boolean = pieceData.position < size.height fun isInBoundaries(pieceData: PieceData) = pieceData.position < size.height
abstract fun saveTime() abstract fun now(): Long
fun togglePause() { fun togglePause() {
paused = !paused paused = !paused
saveTime() previousTime = Long.MAX_VALUE
} }
fun start() { fun start() {
saveTime() previousTime = now()
startTime = previousTime startTime = previousTime
clicked = 0 clicked = 0
started = true started = true

2
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/piece.kt → examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/Piece.kt

@ -16,7 +16,7 @@ import jetbrains.compose.common.shapes.CircleShape
import org.jetbrains.compose.common.demo.position import org.jetbrains.compose.common.demo.position
@Composable @Composable
fun piece(index: Int, piece: PieceData) { fun Piece(index: Int, piece: PieceData) {
val boxSize = 40.dp val boxSize = 40.dp
Box( Box(
Modifier Modifier

2
examples/falling_balls_with_web/src/commonMain/kotlin/fallingBalls/fallingBalls.kt

@ -86,7 +86,7 @@ fun fallingBalls(game: Game) {
} }
) { ) {
game.pieces.forEachIndexed { index, piece -> game.pieces.forEachIndexed { index, piece ->
piece(index, piece) Piece(index, piece)
} }
} }
} }

15
examples/falling_balls_with_web/src/jsMain/kotlin/androidx/compose/web/with-web/App.kt

@ -11,25 +11,20 @@ import androidx.compose.web.css.Style
import org.jetbrains.compose.web.ui.Styles import org.jetbrains.compose.web.ui.Styles
class JsGame : Game() { class JsGame : Game() {
override fun saveTime() { override fun now() = window.performance.now().toLong()
previousTime = window.performance.now().toLong()
}
} }
fun main() { fun main() {
val root = document.getElementById("root") as HTMLElement val root = document.getElementById("root") as HTMLElement
renderComposable( renderComposable(root = root) {
root = root
) {
Style(Styles) Style(Styles)
fallingBalls( val game = remember {
remember { JsGame().apply {
JsGame()?.apply {
width = root.offsetWidth width = root.offsetWidth
height = root.offsetHeight height = root.offsetHeight
} }
} }
) fallingBalls(game)
} }
} }

6
examples/falling_balls_with_web/src/jvmMain/kotlin/App.kt

@ -7,16 +7,14 @@ import org.jetbrains.compose.demo.falling.Game
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
class JvmGame : Game() { class JvmGame : Game() {
override fun saveTime() { override fun now() = System.nanoTime()
previousTime = System.nanoTime()
}
} }
fun main() { fun main() {
Window(title = "Demo", size = IntSize(600, 400)) { Window(title = "Demo", size = IntSize(600, 400)) {
fallingBalls( fallingBalls(
remember { remember {
JvmGame()?.apply { JvmGame().apply {
width = 600 width = 600
height = 400 height = 400
} }

Loading…
Cancel
Save