Create simple drop game

This commit is contained in:
2026-02-06 11:22:38 +00:00
parent 84958b2304
commit cbace1e07e
36 changed files with 1161 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
eclipse.project.name = appName + '-core'
dependencies {
api "com.badlogicgames.ashley:ashley:$ashleyVersion"
api "com.badlogicgames.gdx:gdx-ai:$aiVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.kotcrab.vis:vis-ui:$visUiVersion"
api "io.github.libktx:ktx-actors:$ktxVersion"
api "io.github.libktx:ktx-ai:$ktxVersion"
api "io.github.libktx:ktx-app:$ktxVersion"
api "io.github.libktx:ktx-artemis:$ktxVersion"
api "io.github.libktx:ktx-ashley:$ktxVersion"
api "io.github.libktx:ktx-assets-async:$ktxVersion"
api "io.github.libktx:ktx-assets:$ktxVersion"
api "io.github.libktx:ktx-async:$ktxVersion"
api "io.github.libktx:ktx-box2d:$ktxVersion"
api "io.github.libktx:ktx-collections:$ktxVersion"
api "io.github.libktx:ktx-freetype-async:$ktxVersion"
api "io.github.libktx:ktx-freetype:$ktxVersion"
api "io.github.libktx:ktx-graphics:$ktxVersion"
api "io.github.libktx:ktx-i18n:$ktxVersion"
api "io.github.libktx:ktx-inject:$ktxVersion"
api "io.github.libktx:ktx-json:$ktxVersion"
api "io.github.libktx:ktx-log:$ktxVersion"
api "io.github.libktx:ktx-math:$ktxVersion"
api "io.github.libktx:ktx-preferences:$ktxVersion"
api "io.github.libktx:ktx-reflect:$ktxVersion"
api "io.github.libktx:ktx-scene2d:$ktxVersion"
api "io.github.libktx:ktx-style:$ktxVersion"
api "io.github.libktx:ktx-tiled:$ktxVersion"
api "io.github.libktx:ktx-vis-style:$ktxVersion"
api "io.github.libktx:ktx-vis:$ktxVersion"
api "net.onedaybeard.artemis:artemis-odb:$artemisOdbVersion"
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion"
if(enableGraalNative == 'true') {
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
}
}

View File

@@ -0,0 +1,138 @@
package com.badlogic.drop
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Gdx.audio
import com.badlogic.gdx.audio.Music
import com.badlogic.gdx.audio.Sound
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.Texture.TextureFilter.Linear
import com.badlogic.gdx.graphics.g2d.Sprite
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.ScreenUtils
import com.badlogic.gdx.utils.viewport.FitViewport
import ktx.app.KtxGame
import ktx.app.KtxScreen
import ktx.assets.disposeSafely
import ktx.assets.toInternalFile
import ktx.async.KtxAsync
import ktx.graphics.use
class Main : KtxGame<KtxScreen>() {
override fun create() {
KtxAsync.initiate()
addScreen(FirstScreen())
setScreen<FirstScreen>()
}
class FirstScreen : KtxScreen {
private val backgroundTexture: Texture = Texture("background.png".toInternalFile(), true).apply { setFilter(Linear, Linear) }
private val bucketTexture: Texture = Texture("bucket.png".toInternalFile(), true).apply { setFilter(Linear, Linear) }
private val dropTexture: Texture = Texture("drop.png".toInternalFile(), true).apply { setFilter(Linear, Linear) }
private val dropSound: Sound = audio.newSound("drop.mp3".toInternalFile())
private val music: Music = audio.newMusic("music.mp3".toInternalFile()).apply {
isLooping = true
volume = 0.5f
play()
}
private val bucketSprite: Sprite = Sprite(bucketTexture).apply { setSize(1f, 1f) }
private val image = Texture("logo.png".toInternalFile(), true).apply { setFilter(Linear, Linear) }
private val batch = SpriteBatch()
private val viewport = FitViewport(8f, 5f)
private var touchPos: Vector2 = Vector2()
private var dropletSpriteList: MutableList<Sprite> = mutableListOf()
private var dropTimer: Float = 0f
private val bucketRectangle: Rectangle = Rectangle()
private val dropletRectangle: Rectangle = Rectangle()
override fun resize(width: Int, height: Int) {
viewport.update(width, height, true)
}
override fun render(delta: Float) {
input()
logic()
draw()
}
override fun dispose() {
image.disposeSafely()
batch.disposeSafely()
}
private fun input() {
if (Gdx.input.isTouched) {
touchPos.set(Gdx.input.x.toFloat(), Gdx.input.y.toFloat())
viewport.unproject(touchPos)
bucketSprite.setCenterX(touchPos.x)
}
}
private fun logic() {
val worldWidth: Float = viewport.worldWidth
bucketSprite.x = MathUtils.clamp(bucketSprite.x, 0f, worldWidth - bucketSprite.width)
val delta: Float = Gdx.graphics.deltaTime
bucketRectangle.set(bucketSprite.x, bucketSprite.y, bucketSprite.width, bucketSprite.height)
for (sprite in dropletSpriteList.reversed()) {
sprite.translateY(-2f * delta)
dropletRectangle.set(sprite.x, sprite.y, sprite.width, sprite.height)
if (sprite.y < -sprite.height) dropletSpriteList.remove(sprite)
else if (bucketRectangle.overlaps(dropletRectangle)) {
dropletSpriteList.remove(sprite)
dropSound.play()
}
}
dropTimer += delta
if (dropTimer >= 1f)
{
dropTimer = 0f
dropletSpriteList.add(createDroplet())
}
//Gdx.app.log("Droplets", dropletSpriteList.size.toString() + " in List")
}
private fun draw() {
ScreenUtils.clear(Color.BLACK)
viewport.apply()
batch.setProjectionMatrix(viewport.camera.combined)
batch.use {
it.draw(backgroundTexture, 0f, 0f, viewport.worldWidth, viewport.worldHeight)
for (s in dropletSpriteList) {
s.draw(it)
}
bucketSprite.draw(it)
}
}
private fun createDroplet(): Sprite {
val dropWidth: Float = 1f
val dropHeight: Float = 1f
val worldWidth: Float = viewport.worldWidth
val worldHeight: Float = viewport.worldHeight
val dropSprite: Sprite = Sprite(dropTexture).apply {
setSize(dropWidth, dropHeight)
x = MathUtils.random(0f, worldWidth - dropWidth)
y = worldHeight //- dropHeight // Spawn droplet above world for cleaner transition in?
} // We don't add the object to the set here because apply is really meant for doing stuff with members
return dropSprite
}
}
}