Browse Source

Compose Bird - Improve Structure (#838)

* improve structure by reducing number of lines
(removed unnecessary divs, css, composable etc)

* fix resource not found issue
pull/851/head
theapache64 3 years ago committed by GitHub
parent
commit
f61920c4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      examples/web-compose-bird/settings.gradle.kts
  2. 107
      examples/web-compose-bird/src/jsMain/kotlin/main.kt

2
examples/web-compose-bird/settings.gradle.kts

@ -4,5 +4,5 @@ pluginManagement {
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
} }
} }
rootProject.name = "compose-bird" rootProject.name = "web-compose-bird"

107
examples/web-compose-bird/src/jsMain/kotlin/main.kt

@ -7,8 +7,11 @@ import data.GameFrame
import kotlinx.browser.document import kotlinx.browser.document
import kotlinx.browser.window import kotlinx.browser.window
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import org.jetbrains.compose.web.attributes.* import org.jetbrains.compose.web.attributes.InputType
import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.attributes.checked
import org.jetbrains.compose.web.attributes.disabled
import org.jetbrains.compose.web.css.marginTop
import org.jetbrains.compose.web.css.px
import org.jetbrains.compose.web.dom.* import org.jetbrains.compose.web.dom.*
import org.jetbrains.compose.web.renderComposable import org.jetbrains.compose.web.renderComposable
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
@ -31,20 +34,12 @@ fun main() {
} }
}) })
body.onclick = {
game.moveBirdUp()
}
renderComposable(rootElementId = "root") { renderComposable(rootElementId = "root") {
Div( Div(
attrs = { attrs = {
style { style {
display(DisplayStyle.Flex) property("text-align", "center")
justifyContent(JustifyContent.Center)
}
onClick {
game.moveBirdUp()
} }
} }
) { ) {
@ -60,29 +55,19 @@ fun main() {
} }
} }
Div { Header(gameFrame)
// Title Div(
GameTitle() attrs = {
Score(gameFrame) style {
Br() marginTop(30.px)
if (gameFrame.isGameOver || gameFrame.isGameWon) {
Div(
attrs = {
style {
display(DisplayStyle.Flex)
flexDirection(FlexDirection.Column)
justifyContent(JustifyContent.Center)
}
}
) {
GameStatus(gameFrame)
TryAgain()
} }
}
) {
if (gameFrame.isGameOver || gameFrame.isGameWon) {
GameResult(gameFrame)
} else { } else {
// Play area
repeat(ComposeBirdGame.ROWS) { rowIndex -> repeat(ComposeBirdGame.ROWS) { rowIndex ->
Div { Div {
repeat(ComposeBirdGame.COLUMNS) { columnIndex -> repeat(ComposeBirdGame.COLUMNS) { columnIndex ->
@ -90,12 +75,6 @@ fun main() {
InputType.Radio, InputType.Radio,
attrs = { attrs = {
style {
width(25.px)
height(25.px)
}
val tube = gameFrame.tubes.find { it.position == columnIndex } val tube = gameFrame.tubes.find { it.position == columnIndex }
val isTube = tube?.coordinates?.get(rowIndex) ?: false val isTube = tube?.coordinates?.get(rowIndex) ?: false
val isBird = val isBird =
@ -126,27 +105,20 @@ fun main() {
} }
@Composable @Composable
private fun TryAgain() { private fun Header(gameFrame: GameFrame) {
Button( // Game title
attrs = { H1 {
onClick { Text(value = "🐦 Compose Bird!")
window.location.reload()
}
}
) {
Text("Try Again!")
} }
// Game score
Text(value = "Your Score: ${gameFrame.score} || Top Score: ${ComposeBirdGame.TOTAL_TUBES}")
} }
@Composable @Composable
private fun GameStatus(gameFrame: GameFrame) { private fun GameResult(gameFrame: GameFrame) {
H2( // Game Status
attrs = { H2 {
style {
alignSelf(AlignSelf.Center)
}
}
) {
if (gameFrame.isGameWon) { if (gameFrame.isGameWon) {
Text("🚀 Won the game! 🚀") Text("🚀 Won the game! 🚀")
} else { } else {
@ -154,32 +126,15 @@ private fun GameStatus(gameFrame: GameFrame) {
Text("💀 Game Over 💀") Text("💀 Game Over 💀")
} }
} }
}
@Composable
private fun Score(gameFrame: GameFrame) {
Div(
attrs = {
style {
display(DisplayStyle.Flex)
justifyContent(JustifyContent.Center)
}
}
) {
Text("Your Score: ${gameFrame.score} || Top Score: ${ComposeBirdGame.TOTAL_TUBES}")
}
}
@Composable // Try Again
private fun GameTitle() { Button(
H1(
attrs = { attrs = {
style { onClick {
display(DisplayStyle.Flex) window.location.reload()
justifyContent(JustifyContent.Center)
} }
} }
) { ) {
Text("🐦 Compose Bird!") Text("Try Again!")
} }
} }
Loading…
Cancel
Save