From 0f79ef9af73eecbbdb25ac4b7709fac8e2838f05 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 26 May 2023 21:51:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imshit/aircraftwar/logic/game/Games.kt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt b/app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt index 21f0873..18352ba 100644 --- a/app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt +++ b/app/src/main/java/net/imshit/aircraftwar/logic/game/Games.kt @@ -66,18 +66,18 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) : const val SCORE_SIZE = 128f const val SCORE_X = 10f const val SCORE_Y = SCORE_SIZE + 10f + } - class AccelerateSensorListener(val callback: (Float, Float, Float) -> Unit) : - SensorEventListener { - override fun onSensorChanged(event: SensorEvent?) { - val ax = event?.values?.get(0) ?: 0.0f - val ay = event?.values?.get(1) ?: 0.0f - val az = event?.values?.get(2) ?: 0.0f - callback(ax, ay, az) - } - - override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} + class AccelerateSensorListener(val callback: (Float, Float, Float) -> Unit) : + SensorEventListener { + override fun onSensorChanged(event: SensorEvent?) { + val ax = event?.values?.get(0) ?: 0.0f + val ay = event?.values?.get(1) ?: 0.0f + val az = event?.values?.get(2) ?: 0.0f + callback(ax, ay, az) } + + override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} } /** used by design tool */ @@ -91,8 +91,6 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) : private lateinit var gravitySensorListener: AccelerateSensorListener var mainHandle: Handler? = null lateinit var images: ImageManager - private val musicStrategy: MusicStrategies = - if (soundMode) BasicMusicStrategy(context) else MuteMusicStrategy private var logicJob: Job? = null // configs @@ -100,6 +98,8 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) : // strategies abstract val generateStrategy: EnemyGenerateStrategies + private val musicStrategy: MusicStrategies = + if (soundMode) BasicMusicStrategy(context) else MuteMusicStrategy // status private var score = 0 @@ -161,12 +161,14 @@ sealed class Games(context: Context, attrs: AttributeSet?, soundMode: Boolean) : } private fun initHeroController() { + // 触摸屏 setOnTouchListener { view, motionEvent -> view.performClick() this.heroAircraft.x = motionEvent.x this.heroAircraft.y = motionEvent.y true } + // 重力感应 sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager with(sensorManager) { gravitySensor =