Browse Source

Update web-compose-bird example (#934)

* bump compose version to 0.5.0-build262 and multiplatform version to 1.5.21

* add method comments

* update demo gif
pull/942/head
theapache64 3 years ago committed by GitHub
parent
commit
c63ade0ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      examples/web-compose-bird/build.gradle.kts
  2. BIN
      examples/web-compose-bird/demo.gif
  3. 12
      examples/web-compose-bird/src/jsMain/kotlin/core/ComposeBirdGame.kt
  4. 3
      examples/web-compose-bird/src/jsMain/kotlin/core/Game.kt

4
examples/web-compose-bird/build.gradle.kts

@ -1,7 +1,7 @@
// Add compose gradle plugin // Add compose gradle plugin
plugins { plugins {
kotlin("multiplatform") version "1.5.10" kotlin("multiplatform") version "1.5.21"
id("org.jetbrains.compose") version "0.5.0-build235" id("org.jetbrains.compose") version "0.5.0-build262"
} }
group = "com.theapache64.composebird" group = "com.theapache64.composebird"
version = "1.0.0-alpha01" version = "1.0.0-alpha01"

BIN
examples/web-compose-bird/demo.gif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 MiB

After

Width:  |  Height:  |  Size: 2.5 MiB

12
examples/web-compose-bird/src/jsMain/kotlin/core/ComposeBirdGame.kt

@ -7,6 +7,9 @@ import data.GameFrame
import data.Tube import data.Tube
import kotlin.js.Date import kotlin.js.Date
/**
* Game logic
*/
class ComposeBirdGame : Game { class ComposeBirdGame : Game {
companion object { companion object {
@ -39,6 +42,9 @@ class ComposeBirdGame : Game {
) )
} }
/**
* To build a random level
*/
private fun buildLevel(): List<Tube> { private fun buildLevel(): List<Tube> {
return mutableListOf<Tube>().apply { return mutableListOf<Tube>().apply {
var tubesAdded = 0 var tubesAdded = 0
@ -59,6 +65,9 @@ class ComposeBirdGame : Game {
} }
/**
* To build a random vertical tube/pipe
*/
private fun buildRandomTube(): List<Boolean> { private fun buildRandomTube(): List<Boolean> {
// creating a full tube // creating a full tube
val tube = mutableListOf<Boolean>().apply { val tube = mutableListOf<Boolean>().apply {
@ -130,6 +139,9 @@ class ComposeBirdGame : Game {
} }
} }
/**
* To check if the bird collided with the tube (collision-detection)
*/
private fun isCollidedWithTube(newBirdPos: Int, tubes: List<Tube>): Boolean { private fun isCollidedWithTube(newBirdPos: Int, tubes: List<Tube>): Boolean {
val birdTube = tubes.find { it.position == BIRD_COLUMN } val birdTube = tubes.find { it.position == BIRD_COLUMN }
return birdTube?.coordinates?.get(newBirdPos) ?: false return birdTube?.coordinates?.get(newBirdPos) ?: false

3
examples/web-compose-bird/src/jsMain/kotlin/core/Game.kt

@ -3,6 +3,9 @@ package core
import androidx.compose.runtime.State import androidx.compose.runtime.State
import data.GameFrame import data.GameFrame
/**
* A generic game interface
*/
interface Game { interface Game {
val gameFrame: State<GameFrame> val gameFrame: State<GameFrame>
fun step() fun step()

Loading…
Cancel
Save