Skip to content

Commit ddc87f5

Browse files
committed
add detekt and cleanup code smells
1 parent 85e3931 commit ddc87f5

30 files changed

+322
-227
lines changed

buildSrc/src/main/kotlin/versions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ object Versions {
77
const val gdx = "1.9.11-SNAPSHOT"
88
const val ktx = "1.9.10-SNAPSHOT"
99
const val ashley = "1.7.3"
10+
const val detekt = "1.7.4"
1011
val java = JavaVersion.VERSION_1_8
12+
const val jvm = "1.8"
1113
}

config/detekt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
style:
2+
MagicNumber:
3+
ignoreNumbers: '-1,0,0.001,0.5,1,2,100,1000'

core/build.gradle.kts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
kotlin("jvm")
3+
id("io.gitlab.arturbosch.detekt") version Versions.detekt
34
}
45

56
dependencies {
@@ -20,3 +21,26 @@ java {
2021
sourceCompatibility = Versions.java
2122
targetCompatibility = Versions.java
2223
}
24+
25+
detekt {
26+
failFast = true
27+
buildUponDefaultConfig = true
28+
config = files("${rootProject.projectDir}/config/detekt.yml")
29+
30+
reports {
31+
html.enabled = true
32+
xml.enabled = false
33+
txt.enabled = false
34+
}
35+
}
36+
37+
tasks {
38+
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
39+
kotlinOptions {
40+
jvmTarget = Versions.jvm
41+
}
42+
}
43+
withType<io.gitlab.arturbosch.detekt.Detekt> {
44+
jvmTarget = Versions.jvm
45+
}
46+
}

core/src/main/kotlin/com/github/quillraven/darkmatter/game.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/Game.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package com.github.quillraven.darkmatter
22

33
import com.badlogic.gdx.Application
44
import com.badlogic.gdx.Gdx
5-
import com.badlogic.gdx.graphics.g2d.Batch
6-
import com.badlogic.gdx.graphics.g2d.SpriteBatch
75
import com.badlogic.gdx.graphics.profiling.GLProfiler
86
import com.badlogic.gdx.scenes.scene2d.Stage
97
import com.badlogic.gdx.utils.viewport.FitViewport
@@ -25,9 +23,8 @@ const val V_HEIGHT = 16
2523
const val UNIT_SCALE = 1 / 8f
2624

2725
class Game : KtxGame<KtxScreen>() {
28-
val batch: Batch by lazy { SpriteBatch() }
2926
val stage: Stage by lazy {
30-
Stage(FitViewport(V_WIDTH_PIXELS.toFloat(), V_HEIGHT_PIXELS.toFloat()), batch)
27+
Stage(FitViewport(V_WIDTH_PIXELS.toFloat(), V_HEIGHT_PIXELS.toFloat()))
3128
}
3229
val assets: AssetStorage by lazy {
3330
KtxAsync.initiate()
@@ -59,7 +56,6 @@ class Game : KtxGame<KtxScreen>() {
5956
}
6057

6158
super.dispose()
62-
batch.dispose()
6359
assets.dispose()
6460
stage.dispose()
6561
}

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/component/animation.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/component/AnimationComponent.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.badlogic.gdx.utils.Array
77
import com.badlogic.gdx.utils.Pool
88
import ktx.ashley.mapperFor
99

10+
private const val DEFAULT_FRAME_DURATION = 1 / 20f
11+
1012
enum class AnimationType(
1113
val atlasKey: String,
1214
val playMode: Animation.PlayMode = Animation.PlayMode.LOOP,
@@ -19,15 +21,15 @@ enum class AnimationType(
1921
LIFE("life"),
2022
SHIELD("shield", speed = 0.75f),
2123
DARK_MATTER("dark_matter", speed = 3f),
22-
EXPLOSION("explosion", Animation.PlayMode.NORMAL, 0.5f)
24+
EXPLOSION("explosion", Animation.PlayMode.NORMAL, speed = 0.5f)
2325
}
2426

2527
class Animation2D(
2628
val type: AnimationType,
2729
keyFrames: Array<out TextureRegion>,
2830
playMode: PlayMode = PlayMode.LOOP,
2931
speed: Float = 1f
30-
) : Animation<TextureRegion>((1 / 20f) / speed, keyFrames, playMode)
32+
) : Animation<TextureRegion>((DEFAULT_FRAME_DURATION) / speed, keyFrames, playMode)
3133

3234
class AnimationComponent : Component, Pool.Poolable {
3335
var type = AnimationType.NONE

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/component/player.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/component/PlayerComponent.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import com.badlogic.ashley.core.Component
44
import com.badlogic.gdx.utils.Pool
55
import ktx.ashley.mapperFor
66

7+
private const val MAX_LIFE = 100f
8+
private const val MAX_SHIELD = 100f
9+
710
class PlayerComponent : Component, Pool.Poolable {
8-
var life = 100f
9-
var maxLife = 100f
11+
var life = MAX_LIFE
12+
var maxLife = MAX_LIFE
1013
var shield = 0f
11-
var maxShield = 100f
14+
var maxShield = MAX_SHIELD
1215
var distance = 0f
1316

1417
override fun reset() {
15-
life = 100f
16-
maxLife = 100f
18+
life = MAX_LIFE
19+
maxLife = MAX_LIFE
1720
shield = 0f
18-
maxShield = 100f
21+
maxShield = MAX_SHIELD
1922
distance = 0f
2023
}
2124

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/animation.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/AnimationSystem.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,27 @@ class AnimationSystem(
3434

3535

3636
override fun processEntity(entity: Entity, deltaTime: Float) {
37-
entity[AnimationComponent.mapper]?.let { aniCmp ->
38-
if (aniCmp.type == AnimationType.NONE) {
39-
LOG.error { "No aniCmp type specified" }
40-
return
41-
}
37+
val aniCmp = entity[AnimationComponent.mapper]
38+
require(aniCmp != null) { "Entity |entity| must have an AnimationComponent. entity=$entity" }
39+
val graphic = entity[GraphicComponent.mapper]
40+
require(graphic != null) { "Entity |entity| must have a GraphicComponent. entity=$entity" }
4241

43-
if (aniCmp.animation.type == aniCmp.type) {
44-
// animation is correct -> update it
45-
aniCmp.stateTime += deltaTime
46-
} else {
47-
// change animation
48-
aniCmp.stateTime = 0f
49-
aniCmp.animation = getAnimation(aniCmp.type)
50-
}
42+
if (aniCmp.type == AnimationType.NONE) {
43+
LOG.error { "No aniCmp type specified" }
44+
return
45+
}
5146

52-
val frame = aniCmp.animation.getKeyFrame(aniCmp.stateTime)
53-
entity[GraphicComponent.mapper]?.setSpriteRegion(frame)
47+
if (aniCmp.animation.type == aniCmp.type) {
48+
// animation is correct -> update it
49+
aniCmp.stateTime += deltaTime
50+
} else {
51+
// change animation
52+
aniCmp.stateTime = 0f
53+
aniCmp.animation = getAnimation(aniCmp.type)
5454
}
55+
56+
val frame = aniCmp.animation.getKeyFrame(aniCmp.stateTime)
57+
graphic.setSpriteRegion(frame)
5558
}
5659

5760
private fun getAnimation(type: AnimationType): Animation2D {

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/attach.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/AttachSystem.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ class AttachSystem :
2626
}
2727

2828
override fun processEntity(entity: Entity, deltaTime: Float) {
29-
entity[AttachComponent.mapper]?.let { attach ->
30-
// update position for rendering
31-
entity[TransformComponent.mapper]?.let { transform ->
32-
attach.entity[TransformComponent.mapper]?.let { attachTransform ->
33-
transform.interpolatedPosition.set(
34-
attachTransform.interpolatedPosition.x + attach.offset.x,
35-
attachTransform.interpolatedPosition.y + attach.offset.y,
36-
transform.position.z
37-
)
38-
}
39-
}
29+
val attach = entity[AttachComponent.mapper]
30+
require(attach != null) { "Entity |entity| must have an AttachComponent. entity=$entity" }
31+
val graphic = entity[GraphicComponent.mapper]
32+
require(graphic != null) { "Entity |entity| must have a GraphicComponent. entity=$entity" }
33+
val transform = entity[TransformComponent.mapper]
34+
require(transform != null) { "Entity |entity| must have a TransformComponent. entity=$entity" }
35+
36+
// update position
37+
attach.entity[TransformComponent.mapper]?.let { attachTransform ->
38+
transform.interpolatedPosition.set(
39+
attachTransform.interpolatedPosition.x + attach.offset.x,
40+
attachTransform.interpolatedPosition.y + attach.offset.y,
41+
transform.position.z
42+
)
43+
}
4044

41-
// update graphic alpha
42-
entity[GraphicComponent.mapper]?.let { graphic ->
43-
attach.entity[GraphicComponent.mapper]?.let { attachGraphic ->
44-
graphic.sprite.setAlpha(attachGraphic.sprite.color.a)
45-
}
46-
}
45+
// update graphic alpha
46+
attach.entity[GraphicComponent.mapper]?.let { attachGraphic ->
47+
graphic.sprite.setAlpha(attachGraphic.sprite.color.a)
4748
}
4849
}
4950

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/cameraShake.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/CameraShakeSystem.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import com.github.quillraven.darkmatter.event.GameEventType
1414
import ktx.math.vec3
1515

1616
private const val MAX_SHAKE_INSTANCES = 4
17+
private const val SHAKE_DURATION = 0.25f
18+
private const val SHAKE_DISTORTION = 0.25f
1719

1820
private class CameraShake : Pool.Poolable {
1921
var maxDistortion = 0f // in world units
@@ -99,8 +101,8 @@ class CameraShakeSystem(
99101
override fun onEvent(type: GameEventType, data: GameEvent?) {
100102
if (activeShakes.size < MAX_SHAKE_INSTANCES) {
101103
activeShakes.add(shakePool.obtain().apply {
102-
duration = 0.25f
103-
maxDistortion = 0.25f
104+
duration = SHAKE_DURATION
105+
maxDistortion = SHAKE_DISTORTION
104106
})
105107
}
106108
}

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/damage.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/DamageSystem.kt

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import ktx.ashley.exclude
1919
import ktx.ashley.get
2020
import kotlin.math.max
2121

22+
const val DAMAGE_AREA_HEIGHT = 2f
2223
private const val DAMAGE_PER_SECOND = 25f
2324
private const val DAM_SOUND_LENGTH = 0.6f
2425
private const val BLOCK_SOUND_LENGTH = 0.6f
26+
private const val DEATH_EXPLOSION_SIZE = 1.5f
27+
private const val DEATH_EXPLOSION_DURATION = 0.9f
2528

2629
class DamageSystem(
2730
private val gameEventManager: GameEventManager,
@@ -56,48 +59,49 @@ class DamageSystem(
5659
}
5760

5861
override fun processEntity(entity: Entity, deltaTime: Float) {
59-
entity[TransformComponent.mapper]?.let { transform ->
60-
entity[PlayerComponent.mapper]?.let { player ->
61-
if (transform.position.y <= 2f) {
62-
var damage = DAMAGE_PER_SECOND * deltaTime
63-
if (player.shield > 0f) {
64-
val blockAmount = player.shield
65-
blocked = true
66-
player.shield = max(player.shield - damage, 0f)
67-
damage -= blockAmount
68-
if (damage <= 0f) {
69-
// entire damage was blocked
70-
return
71-
}
72-
}
62+
val transform = entity[TransformComponent.mapper]
63+
require(transform != null) { "Entity |entity| must have a TransformComponent. entity=$entity" }
64+
val player = entity[PlayerComponent.mapper]
65+
require(player != null) { "Entity |entity| must have a PlayerComponent. entity=$entity" }
66+
67+
if (transform.position.y <= DAMAGE_AREA_HEIGHT) {
68+
var damage = DAMAGE_PER_SECOND * deltaTime
69+
if (player.shield > 0f) {
70+
val blockAmount = player.shield
71+
blocked = true
72+
player.shield = max(player.shield - damage, 0f)
73+
damage -= blockAmount
74+
if (damage <= 0f) {
75+
// entire damage was blocked
76+
return
77+
}
78+
}
7379

74-
damageTaken = true
75-
player.life -= damage
80+
damageTaken = true
81+
player.life -= damage
7682

77-
gameEventManager.dispatchEvent(GameEventType.PLAYER_DAMAGED, GameEventPlayerDamaged.apply {
78-
this.player = entity
79-
})
80-
if (player.life <= 0f) {
81-
entity.add(engine.createComponent(RemoveComponent::class.java).apply {
82-
delay = 1f
83-
})
84-
entity[GraphicComponent.mapper]?.sprite?.setAlpha(0f)
85-
engine.entity {
86-
with<TransformComponent> {
87-
size.set(1.5f, 1.5f)
88-
setInitialPosition(transform.position.x, transform.position.y, 2f)
89-
}
90-
with<AnimationComponent> {
91-
type = AnimationType.EXPLOSION
92-
}
93-
with<GraphicComponent>()
94-
with<RemoveComponent> {
95-
delay = 0.9f
96-
}
97-
}
98-
audioService.play(SoundAsset.EXPLOSION)
83+
gameEventManager.dispatchEvent(GameEventType.PLAYER_DAMAGED, GameEventPlayerDamaged.apply {
84+
this.player = entity
85+
})
86+
if (player.life <= 0f) {
87+
entity.add(engine.createComponent(RemoveComponent::class.java).apply {
88+
delay = 1f
89+
})
90+
entity[GraphicComponent.mapper]?.sprite?.setAlpha(0f)
91+
engine.entity {
92+
with<TransformComponent> {
93+
size.set(DEATH_EXPLOSION_SIZE, DEATH_EXPLOSION_SIZE)
94+
setInitialPosition(transform.position.x, transform.position.y, 2f)
95+
}
96+
with<AnimationComponent> {
97+
type = AnimationType.EXPLOSION
98+
}
99+
with<GraphicComponent>()
100+
with<RemoveComponent> {
101+
delay = DEATH_EXPLOSION_DURATION
99102
}
100103
}
104+
audioService.play(SoundAsset.EXPLOSION)
101105
}
102106
}
103107
}

core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/debug.kt renamed to core/src/main/kotlin/com/github/quillraven/darkmatter/ecs/system/DebugSystem.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import ktx.ashley.get
1818
import kotlin.math.max
1919
import kotlin.math.min
2020

21+
private const val SHIELD_GAIN = 25f
22+
private const val PLAYER_DAMAGE = 5f
23+
private const val NUM_SOUNDS_TO_TEST = 3
24+
private const val WINDOW_INFO_UPDATE_INTERVAL = 0.25f
25+
2126
class DebugSystem(
2227
private val gameEventManager: GameEventManager,
2328
private val audioService: AudioService
24-
) : IntervalIteratingSystem(allOf(PlayerComponent::class).get(), 0.25f) {
29+
) : IntervalIteratingSystem(allOf(PlayerComponent::class).get(), WINDOW_INFO_UPDATE_INTERVAL) {
2530
override fun processEntity(entity: Entity) {
2631
entity[PlayerComponent.mapper]?.let { player ->
2732
entity[TransformComponent.mapper]?.let { transform ->
@@ -34,11 +39,11 @@ class DebugSystem(
3439
}
3540
Gdx.input.isKeyPressed(Input.Keys.NUM_2) -> {
3641
// add shield
37-
player.shield = min(player.maxShield, player.shield + 25f)
42+
player.shield = min(player.maxShield, player.shield + SHIELD_GAIN)
3843
}
3944
Gdx.input.isKeyPressed(Input.Keys.NUM_3) -> {
4045
// remove shield
41-
player.shield = max(0f, player.shield - 25f)
46+
player.shield = max(0f, player.shield - SHIELD_GAIN)
4247
}
4348
Gdx.input.isKeyPressed(Input.Keys.NUM_4) -> {
4449
// disable movement
@@ -50,7 +55,7 @@ class DebugSystem(
5055
}
5156
Gdx.input.isKeyPressed(Input.Keys.NUM_6) -> {
5257
// trigger player damage event
53-
player.life = max(1f, player.life - 5f)
58+
player.life = max(1f, player.life - PLAYER_DAMAGE)
5459
gameEventManager.dispatchEvent(GameEventType.PLAYER_DAMAGED, GameEventPlayerDamaged.apply {
5560
this.player = entity
5661
})
@@ -62,13 +67,15 @@ class DebugSystem(
6267
}
6368
Gdx.input.isKeyPressed(Input.Keys.NUM_8) -> {
6469
// play three random sounds
65-
repeat(3) {
70+
repeat(NUM_SOUNDS_TO_TEST) {
6671
audioService.play(SoundAsset.values()[MathUtils.random(0, SoundAsset.values().size - 1)])
6772
}
6873
}
6974
}
7075

71-
Gdx.graphics.setTitle("Dark Matter Debug - pos:${transform.position}, life:${player.life}, shield:${player.shield}")
76+
Gdx.graphics.setTitle(
77+
"Dark Matter Debug - pos:${transform.position}, life:${player.life}, shield:${player.shield}"
78+
)
7279
}
7380
}
7481
}

0 commit comments

Comments
 (0)