Skip to content

Commit

Permalink
add game.properties support
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Mar 19, 2024
1 parent 977aea1 commit cea6a99
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 13 deletions.
4 changes: 4 additions & 0 deletions assets/game.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
debugPhysic=false
enableProfiling=false
# NONE = 0 / DEBUG = 3 / INFO = 2 / ERROR = 1
logLevel=3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.quillraven.github.quillyjumper

enum class GamePropertyKey(val key: String) {
DEBUG_PHYSIC("debugPhysic"),
ENABLE_PROFILING("enableProfiling"),
LOG_LEVEL("logLevel"),
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.quillraven.github.quillyjumper

import com.badlogic.gdx.Application
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.physics.box2d.FixtureDef
import com.badlogic.gdx.physics.box2d.World
import com.badlogic.gdx.utils.ObjectMap
import com.badlogic.gdx.utils.PropertiesUtils
import com.quillraven.github.quillyjumper.screen.LoadingScreen
import com.quillraven.github.quillyjumper.util.getOrDefault
import ktx.app.KtxGame
import ktx.app.KtxScreen
import ktx.app.clearScreen
import ktx.assets.disposeSafely
import ktx.log.logger
import ktx.math.vec2

typealias PhysicWorld = World
Expand All @@ -25,6 +30,12 @@ class Quillyjumper : KtxGame<KtxScreen>() {
private val assets: Assets by lazy { Assets() }

override fun create() {
Gdx.files.internal("game.properties").reader().use {
PropertiesUtils.load(GAME_PROPERTIES, it)
}
Gdx.app.logLevel = GAME_PROPERTIES.getOrDefault(GamePropertyKey.LOG_LEVEL, Application.LOG_INFO)
log.debug { "Log level is ${Gdx.app.logLevel}" }

Gdx.input.inputProcessor = InputMultiplexer()

addScreen(LoadingScreen(this, batch, assets))
Expand All @@ -42,8 +53,10 @@ class Quillyjumper : KtxGame<KtxScreen>() {
}

companion object {
private val log = logger<Quillyjumper>()
const val UNIT_SCALE = 1 / 16f // 16 pixels == 1 meter in Box2D
val GRAVITY = vec2(0f, -30f)
val OBJECT_FIXTURES = mutableMapOf<GameObject, List<FixtureDef>>()
val GAME_PROPERTIES = ObjectMap<String, String>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import com.badlogic.gdx.utils.viewport.FitViewport
import com.badlogic.gdx.utils.viewport.Viewport
import com.github.quillraven.fleks.configureWorld
import com.quillraven.github.quillyjumper.Assets
import com.quillraven.github.quillyjumper.GamePropertyKey
import com.quillraven.github.quillyjumper.MapAsset
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.GAME_PROPERTIES
import com.quillraven.github.quillyjumper.Quillyjumper.Companion.GRAVITY
import com.quillraven.github.quillyjumper.event.GameEventDispatcher
import com.quillraven.github.quillyjumper.event.GameEventListener
import com.quillraven.github.quillyjumper.event.MapChangeEvent
import com.quillraven.github.quillyjumper.input.KeyboardInputProcessor
import com.quillraven.github.quillyjumper.inputMultiplexer
import com.quillraven.github.quillyjumper.system.*
import com.quillraven.github.quillyjumper.util.getOrDefault
import ktx.app.KtxScreen
import ktx.assets.disposeSafely
import ktx.box2d.createWorld
Expand Down Expand Up @@ -44,8 +47,12 @@ class GameScreen(batch: Batch, private val assets: Assets) : KtxScreen {
add(AnimationSystem())
add(CameraSystem())
add(RenderSystem())
// add(PhysicRenderDebugSystem())
add(GlProfilerSystem())
if (GAME_PROPERTIES.getOrDefault(GamePropertyKey.DEBUG_PHYSIC, false)) {
add(PhysicRenderDebugSystem())
}
if (GAME_PROPERTIES.getOrDefault(GamePropertyKey.ENABLE_PROFILING, false)) {
add(GlProfilerSystem())
}
}
}
private val keyboardProcessor = KeyboardInputProcessor(world)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.quillraven.github.quillyjumper.util

import com.badlogic.gdx.utils.ObjectMap
import com.quillraven.github.quillyjumper.GamePropertyKey

inline fun <reified T> ObjectMap<String, String>.getOrDefault(gamePropKey: GamePropertyKey, defaultValue: T): T =
getOrDefault(gamePropKey.key, defaultValue)

inline fun <reified T> ObjectMap<String, String>.getOrDefault(key: String, defaultValue: T): T {
val strValue = this.get(key) ?: return defaultValue

return when (T::class) {
Int::class -> (strValue.toInt() as T)
Float::class -> (strValue.toFloat() as T)
Boolean::class -> (strValue.toBoolean() as T)
else -> (strValue as T)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.quillraven.github.quillyjumper.util

import com.badlogic.gdx.utils.ObjectMap
import ktx.collections.set
import org.junit.jupiter.api.assertAll
import kotlin.test.Test
import kotlin.test.assertEquals

class CollectionUtilsTest {

@Test
fun testGetProperty() {
val testMap = ObjectMap<String, String>().apply {
set("intKey1", "1")
set("intKey2", "-1")
set("floatKey1", "1.1")
set("floatKey2", "-1.1")
set("booleanKey1", "true")
set("booleanKey2", "false")
set("strKey", "string")
}

assertAll(
// verify access of keys which are present
{ assertEquals(1, testMap.getOrDefault("intKey1", 0)) },
{ assertEquals(-1, testMap.getOrDefault("intKey2", 0)) },
{ assertEquals(1.1f, testMap.getOrDefault("floatKey1", 0f)) },
{ assertEquals(-1.1f, testMap.getOrDefault("floatKey2", 0f)) },
{ assertEquals(true, testMap.getOrDefault("booleanKey1", false)) },
{ assertEquals(false, testMap.getOrDefault("booleanKey2", true)) },
{ assertEquals("string", testMap.getOrDefault("strKey", "")) },
// verify access of key which is not present
{ assertEquals("defaultValue", testMap.getOrDefault("invalidKey", "defaultValue")) }
)
}

}
23 changes: 12 additions & 11 deletions tiled-project.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
},
"activeFile": "assets/maps/test.tmx",
"expandedProjectPaths": [
".",
"assets/maps",
"assets",
"lwjgl3/build"
"assets/maps",
"lwjgl3/build",
"."
],
"fileStates": {
"": {
Expand All @@ -19,8 +19,8 @@
"scale": 3.598125,
"selectedLayer": -1,
"viewCenter": {
"x": 173.2846968907417,
"y": 159.66649296508598
"x": 170.9223553934341,
"y": 158.83272537780093
}
},
"assets/maps/objects.tmx#objects": {
Expand All @@ -46,11 +46,11 @@
"expandedObjectLayers": [
3
],
"scale": 8,
"selectedLayer": 3,
"scale": 4,
"selectedLayer": 1,
"viewCenter": {
"x": 69.125,
"y": 39.3125
"x": 146.25,
"y": 82.125
}
}
},
Expand All @@ -64,14 +64,15 @@
"map.width": 10,
"openFiles": [
"assets/maps/objects.tmx",
"assets/maps/test.tmx"
"assets/maps/test.tmx",
"assets/maps/objects.tsx"
],
"project": "tiled-project.tiled-project",
"property.type": "float",
"recentFiles": [
"assets/maps/objects.tmx",
"assets/maps/test.tmx",
"assets/maps/objects.tsx",
"assets/maps/test.tmx",
"assets/maps/terrain.tsx",
"assets/maps/terrain_extruded.tsx"
],
Expand Down

0 comments on commit cea6a99

Please sign in to comment.