You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
4.1 KiB
123 lines
4.1 KiB
package iut.android.pierrepierre
|
|
|
|
import android.content.Context
|
|
import android.hardware.Sensor
|
|
import android.hardware.SensorEvent
|
|
import android.hardware.SensorEventListener
|
|
import android.hardware.SensorManager
|
|
import android.os.Bundle
|
|
import android.os.CountDownTimer
|
|
import android.view.View
|
|
import android.widget.ProgressBar
|
|
import android.widget.TextView
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import iut.android.pierrepierre.model.User
|
|
|
|
class MoleGridActivity : AppCompatActivity() {
|
|
///https://developer.android.com/topic/libraries/view-binding#kotlin
|
|
|
|
private lateinit var gridViewMole : RecyclerView
|
|
|
|
private lateinit var progressBar: ProgressBar
|
|
|
|
//User
|
|
private var user : User = User()
|
|
private lateinit var scoreDisplay : TextView
|
|
|
|
//sensor
|
|
private val sensorManager by lazy { getSystemService(Context.SENSOR_SERVICE) as SensorManager }
|
|
private lateinit var sensorEventListener: SensorEventListener
|
|
|
|
//private var value = 0
|
|
|
|
|
|
//Countdown
|
|
private lateinit var countDownTimer: CountDownTimer
|
|
private lateinit var DisplaycountDownTimer: TextView
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.mole_grid)
|
|
gridViewMole = findViewById(R.id.MoleGrid)
|
|
progressBar = findViewById(R.id.progressBar)
|
|
scoreDisplay = findViewById(R.id.score_display)
|
|
DisplaycountDownTimer = findViewById(R.id.CountDown)
|
|
}
|
|
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
|
|
|
|
val MoleName = arrayOf(
|
|
"Mole1", "Mole2", "Mole3", "Mole4",
|
|
"Mole5", "Mole6", "Mole7", "Mole8", "Mole9", "Mole10", "Mole11", "Mole12"
|
|
)
|
|
val MoleImages = intArrayOf( // au cas ou on change les images de chaque taupes
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule,
|
|
R.drawable.limule
|
|
)
|
|
|
|
|
|
|
|
val shakeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
|
|
|
sensorManager.registerListener(object : SensorEventListener {
|
|
override fun onSensorChanged(event: SensorEvent) {
|
|
val x = event.values[0]
|
|
val y = event.values[1]
|
|
val z = event.values[2]
|
|
|
|
// Calcul de l'accélération totale
|
|
val acceleration = Math.sqrt((x * x + y * y + z * z).toDouble())
|
|
|
|
// Si l'accélération est supérieure à 12, et que la progresse bar est au maximum, la progressBar est réinitialisée à 0
|
|
if (acceleration > 12 && progressBar.progress == 100) {
|
|
progressBar.progress = 0
|
|
}
|
|
}
|
|
|
|
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
|
|
}, shakeSensor, SensorManager.SENSOR_DELAY_GAME)
|
|
|
|
|
|
// utilisation d'un objet anno
|
|
countDownTimer = object : CountDownTimer(90000, 1000) {
|
|
|
|
|
|
override fun onTick(millisUntilFinished: Long) {
|
|
val min = millisUntilFinished/60000
|
|
val timerMsg = getString(R.string.timer, min, (((millisUntilFinished/1000) - 60 * min).toString()).padStart(2, '0') )
|
|
DisplaycountDownTimer.text = timerMsg
|
|
}
|
|
|
|
override fun onFinish() {
|
|
//mettre la fin de parti ici => appel de l'interface de fin
|
|
DisplaycountDownTimer.text = "STOP!"
|
|
}
|
|
}.start()
|
|
|
|
|
|
gridViewMole.layoutManager = GridLayoutManager(this, 4)
|
|
gridViewMole.adapter = MyGridAdapter(user, MoleName, MoleImages, progressBar, scoreDisplay)
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
sensorManager.unregisterListener(sensorEventListener)
|
|
countDownTimer.cancel()
|
|
}
|
|
}
|