Skip to content

Commit

Permalink
refactor: handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-shop committed May 27, 2023
1 parent 276ea04 commit 161766f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 50 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/net/imshit/aircraftwar/gui/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ class GameActivity : AppCompatActivity() {
gameMode = getSerializableExtra("gameMode", Difficulty::class.java) ?: Difficulty.EASY
soundMode = getBooleanExtra("soundMode", true)
}
val game: Games = Games.getGames(this, gameMode, soundMode).apply {
mainHandle = object : Handler(Looper.getMainLooper()) {
val game: Games =
Games.getGames(this, gameMode, soundMode, object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
onGameOver(gameMode, msg.what)
}
}
}
})
with(ActivityGameBinding.inflate(layoutInflater)) {
root.addView(game, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
setContentView(root)
Expand Down
16 changes: 5 additions & 11 deletions app/src/main/java/net/imshit/aircraftwar/logic/game/EasyGame.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package net.imshit.aircraftwar.logic.game

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import net.imshit.aircraftwar.element.generate.enemy.EasyEnemyGenerateStrategy

class EasyGame(context: Context, attrs: AttributeSet?, soundMode: Boolean) : Games(
context = context, attrs = attrs, soundMode = soundMode
) {
constructor(context: Context, soundMode: Boolean) : this(
context = context, attrs = null, soundMode = soundMode
)

/** used by design tool */
constructor(context: Context, attrs: AttributeSet?) : this(
context = context, attrs = attrs, soundMode = false
)
class EasyGame(context: Context, attrs: AttributeSet?, soundMode: Boolean, handler: Handler) :
Games(
context = context, attrs = attrs, soundMode = soundMode, handler = handler
) {

override val generateStrategy = EasyEnemyGenerateStrategy(this)

Expand Down
23 changes: 10 additions & 13 deletions app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ import net.imshit.aircraftwar.logic.music.MuteMusicStrategy
import kotlin.math.max
import kotlin.math.min

sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
SurfaceView(context, attrs), SurfaceHolder.Callback, CoroutineScope by CoroutineScope(
sealed class Games(
context: Context, attrs: AttributeSet?, soundMode: Boolean, private val handler: Handler?
) : SurfaceView(context, attrs), SurfaceHolder.Callback, CoroutineScope by CoroutineScope(
Dispatchers.Default
) {
companion object {
fun getGames(context: Context, gameMode: Difficulty, soundMode: Boolean): Games {
fun getGames(
context: Context, gameMode: Difficulty, soundMode: Boolean, handler: Handler
): Games {
return when (gameMode) {
Difficulty.EASY -> EasyGame(context, soundMode)
Difficulty.MEDIUM -> MediumGame(context, soundMode)
Difficulty.HARD -> HardGame(context, soundMode)
Difficulty.EASY -> EasyGame(context, null, soundMode, handler)
Difficulty.MEDIUM -> MediumGame(context, null, soundMode, handler)
Difficulty.HARD -> HardGame(context, null, soundMode, handler)
}
}

Expand Down Expand Up @@ -80,16 +83,10 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
}

/** used by design tool */
constructor(context: Context, attrs: AttributeSet?) : this(
context = context, attrs = attrs, soundMode = false
)

// utils
private lateinit var sensorManager: SensorManager
private var gravitySensor: Sensor? = null
private lateinit var gravitySensorListener: AccelerateSensorListener
var mainHandle: Handler? = null
lateinit var images: ImageManager
private var logicJob: Job? = null

Expand Down Expand Up @@ -320,7 +317,7 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) :
stop()
this.isGameOver = true
this.musicStrategy.playGameOver()
this.mainHandle?.sendMessage(Message.obtain().apply {
this.handler?.sendMessage(Message.obtain().apply {
what = score
})
}
Expand Down
16 changes: 5 additions & 11 deletions app/src/main/java/net/imshit/aircraftwar/logic/game/HardGame.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package net.imshit.aircraftwar.logic.game

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import net.imshit.aircraftwar.element.generate.enemy.HardEnemyGenerateStrategy

class HardGame(context: Context, attrs: AttributeSet?, soundMode: Boolean) : Games(
context = context, attrs = attrs, soundMode = soundMode
) {
constructor(context: Context, soundMode: Boolean) : this(
context = context, attrs = null, soundMode = soundMode
)

/** used by design tool */
constructor(context: Context, attrs: AttributeSet?) : this(
context = context, attrs = attrs, soundMode = false
)
class HardGame(context: Context, attrs: AttributeSet?, soundMode: Boolean, handler: Handler) :
Games(
context = context, attrs = attrs, soundMode = soundMode, handler = handler
) {

override val generateStrategy = HardEnemyGenerateStrategy(this)

Expand Down
16 changes: 5 additions & 11 deletions app/src/main/java/net/imshit/aircraftwar/logic/game/MediumGame.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package net.imshit.aircraftwar.logic.game

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import net.imshit.aircraftwar.element.generate.enemy.MediumEnemyGenerateStrategy

class MediumGame(context: Context, attrs: AttributeSet?, soundMode: Boolean) : Games(
context = context, attrs = attrs, soundMode = soundMode
) {
constructor(context: Context, soundMode: Boolean) : this(
context = context, attrs = null, soundMode = soundMode
)

/** used by design tool */
constructor(context: Context, attrs: AttributeSet?) : this(
context = context, attrs = attrs, soundMode = false
)
class MediumGame(context: Context, attrs: AttributeSet?, soundMode: Boolean, handler: Handler) :
Games(
context = context, attrs = attrs, soundMode = soundMode, handler = handler
) {

override val generateStrategy = MediumEnemyGenerateStrategy(this)

Expand Down

0 comments on commit 161766f

Please sign in to comment.