|
|
@ -19,7 +19,10 @@ import android.widget.TextView
|
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
|
|
|
import com.example.shakecraft.model.Boss
|
|
|
|
import com.example.shakecraft.model.Generator
|
|
|
|
import com.example.shakecraft.model.Generator
|
|
|
|
|
|
|
|
import com.example.shakecraft.model.Item
|
|
|
|
|
|
|
|
import com.example.shakecraft.model.Player
|
|
|
|
import com.example.shakecraft.view.adapter.AdapterBossLoot
|
|
|
|
import com.example.shakecraft.view.adapter.AdapterBossLoot
|
|
|
|
import kotlin.math.pow
|
|
|
|
import kotlin.math.pow
|
|
|
|
import kotlin.math.sqrt
|
|
|
|
import kotlin.math.sqrt
|
|
|
@ -27,38 +30,61 @@ import kotlin.math.sqrt
|
|
|
|
|
|
|
|
|
|
|
|
class BossFragment() : Fragment() {
|
|
|
|
class BossFragment() : Fragment() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private lateinit var sensorManager: SensorManager
|
|
|
|
private lateinit var sensorManager: SensorManager
|
|
|
|
private lateinit var accelerometer: Sensor
|
|
|
|
private lateinit var accelerometer: Sensor
|
|
|
|
private lateinit var accelerometerEventListener: SensorEventListener
|
|
|
|
private lateinit var accelerometerEventListener: SensorEventListener
|
|
|
|
private lateinit var progressBar: ProgressBar
|
|
|
|
private lateinit var progressBar: ProgressBar
|
|
|
|
private lateinit var image: ImageView
|
|
|
|
private lateinit var image: ImageView
|
|
|
|
|
|
|
|
private lateinit var buttonCollect: TextView
|
|
|
|
|
|
|
|
private lateinit var recyclerView: RecyclerView
|
|
|
|
|
|
|
|
private lateinit var boss: Boss
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
): View? {
|
|
|
|
|
|
|
|
|
|
|
|
val player = (activity as MainActivity).currentPlayer
|
|
|
|
val currentPlayer = (activity as MainActivity).currentPlayer
|
|
|
|
|
|
|
|
|
|
|
|
val view = inflater.inflate(R.layout.fragment_boss, container, false)
|
|
|
|
val view = inflater.inflate(R.layout.fragment_boss, container, false)
|
|
|
|
val buttonCollect = view.findViewById<TextView>(R.id.backbutton)
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize views
|
|
|
|
|
|
|
|
initializeViews(view)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up boss
|
|
|
|
|
|
|
|
boss = Generator.generateBoss()
|
|
|
|
|
|
|
|
setUpBoss(boss)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up RecyclerView for boss loot
|
|
|
|
|
|
|
|
setUpRecyclerView(view)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up accelerometer listener
|
|
|
|
|
|
|
|
setUpAccelerometerListener(view, currentPlayer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set up activity orientation
|
|
|
|
|
|
|
|
setUpActivityOrientation()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return fragment view
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initializeViews(view: View) {
|
|
|
|
|
|
|
|
progressBar = view.findViewById(R.id.progressBar)
|
|
|
|
|
|
|
|
image = view.findViewById(R.id.imageBoss)
|
|
|
|
|
|
|
|
buttonCollect = view.findViewById<TextView>(R.id.backbutton)
|
|
|
|
buttonCollect.setOnClickListener{
|
|
|
|
buttonCollect.setOnClickListener{
|
|
|
|
findNavController().navigate(R.id.action_bossFragment_to_homeFragment)
|
|
|
|
findNavController().navigate(R.id.action_bossFragment_to_homeFragment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val imageView = view.findViewById<ImageView>(R.id.imageBoss)
|
|
|
|
}
|
|
|
|
imageView.scaleX = 1.2f
|
|
|
|
private fun setUpBoss(boss: Boss) {
|
|
|
|
imageView.scaleY = 1.2f
|
|
|
|
progressBar.max = boss.maxlife
|
|
|
|
|
|
|
|
progressBar.progress = boss.life
|
|
|
|
|
|
|
|
image.setImageResource(boss.image)
|
|
|
|
|
|
|
|
|
|
|
|
// Créez une animation qui modifie la propriété scaleX et scaleY de l'image
|
|
|
|
// Create scale animation for boss image
|
|
|
|
val scaleAnimation = ScaleAnimation(
|
|
|
|
val scaleAnimation = ScaleAnimation(
|
|
|
|
1.2f, // de 2.0 à 1.0
|
|
|
|
1.2f, // from 1.2 to 1.0
|
|
|
|
1.0f,
|
|
|
|
1.0f,
|
|
|
|
1.2f,
|
|
|
|
1.2f,
|
|
|
|
1.0f,
|
|
|
|
1.0f,
|
|
|
@ -67,110 +93,84 @@ class BossFragment() : Fragment() {
|
|
|
|
Animation.RELATIVE_TO_SELF,
|
|
|
|
Animation.RELATIVE_TO_SELF,
|
|
|
|
0.5f
|
|
|
|
0.5f
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
scaleAnimation.duration = 1000 // lasts 1 second
|
|
|
|
scaleAnimation.duration = 1000 // dure 1 seconde
|
|
|
|
scaleAnimation.repeatCount = Animation.INFINITE // repeat indefinitely
|
|
|
|
scaleAnimation.repeatCount = Animation.INFINITE // répéter indéfiniment
|
|
|
|
scaleAnimation.repeatMode = Animation.REVERSE // reverse animation direction
|
|
|
|
scaleAnimation.repeatMode = Animation.REVERSE // inverser la direction de l'animation
|
|
|
|
image.startAnimation(scaleAnimation)
|
|
|
|
|
|
|
|
}
|
|
|
|
// Appliquez l'animation à l'image
|
|
|
|
private fun setUpRecyclerView(view: View) {
|
|
|
|
imageView.startAnimation(scaleAnimation)
|
|
|
|
recyclerView = view.findViewById(R.id.recyclerviewBossLoot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
progressBar = view.findViewById(R.id.progressBar)
|
|
|
|
|
|
|
|
sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
|
|
|
|
|
|
|
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
|
|
|
|
|
|
|
var boss = Generator.generateBoss()
|
|
|
|
|
|
|
|
progressBar.max = boss.maxlife
|
|
|
|
|
|
|
|
progressBar.progress = boss.life
|
|
|
|
|
|
|
|
imageView.setImageResource(boss.image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewBossLoot)
|
|
|
|
|
|
|
|
with(recyclerView) {
|
|
|
|
with(recyclerView) {
|
|
|
|
layoutManager = LinearLayoutManager(view.context)
|
|
|
|
layoutManager = LinearLayoutManager(view.context)
|
|
|
|
adapter = AdapterBossLoot(boss.possibleLoot)
|
|
|
|
adapter = AdapterBossLoot(boss.possibleLoot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpActivityOrientation(){
|
|
|
|
// Créez un écouteur de capteur d'accéléromètre pour écouter les secousses
|
|
|
|
val activity = requireActivity()
|
|
|
|
|
|
|
|
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun displayToast(view: View, item: Item){
|
|
|
|
|
|
|
|
val toastView = view.findViewById<View>(R.id.toast)
|
|
|
|
|
|
|
|
val lootImage = toastView.findViewById<ImageView>(R.id.imageViewLoot)
|
|
|
|
|
|
|
|
val lootName = toastView.findViewById<TextView>(R.id.nameLoot)
|
|
|
|
|
|
|
|
val xpReward = toastView.findViewById<TextView>(R.id.xpRewarded)
|
|
|
|
|
|
|
|
toastView.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
lootImage.setImageResource(item.image)
|
|
|
|
|
|
|
|
lootName.text = item.name
|
|
|
|
|
|
|
|
xpReward.text = boss.xpReward.toString()
|
|
|
|
|
|
|
|
toastView.postDelayed({
|
|
|
|
|
|
|
|
toastView.visibility = View.GONE
|
|
|
|
|
|
|
|
}, 3000)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpAccelerometerListener(view: View, currentPlayer: Player) {
|
|
|
|
|
|
|
|
sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
|
|
|
|
|
|
|
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
|
|
|
accelerometerEventListener = object : SensorEventListener {
|
|
|
|
accelerometerEventListener = object : SensorEventListener {
|
|
|
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
|
|
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
|
|
|
// Ne faites rien ici
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onSensorChanged(event: SensorEvent?) {
|
|
|
|
override fun onSensorChanged(event: SensorEvent?) {
|
|
|
|
val acceleration = sqrt(
|
|
|
|
val acceleration = sqrt(event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2))
|
|
|
|
event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2)
|
|
|
|
if (boss.life <= 0) {
|
|
|
|
)
|
|
|
|
|
|
|
|
if(boss.life <= 0){
|
|
|
|
|
|
|
|
val item = Generator.generateLootBoss(boss.possibleLoot)
|
|
|
|
|
|
|
|
println(item)
|
|
|
|
|
|
|
|
player.addItem(item)
|
|
|
|
|
|
|
|
player.gainXp(boss.xpReward)
|
|
|
|
|
|
|
|
val maVue = view.findViewById<View>(R.id.toast)
|
|
|
|
|
|
|
|
val image = maVue.findViewById<ImageView>(R.id.imageViewLoot)
|
|
|
|
|
|
|
|
val name = maVue.findViewById<TextView>(R.id.nameLoot)
|
|
|
|
|
|
|
|
val xp = maVue.findViewById<TextView>(R.id.xpRewarded)
|
|
|
|
|
|
|
|
maVue.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
image.setImageResource(item.image)
|
|
|
|
|
|
|
|
name.text = item.name
|
|
|
|
|
|
|
|
xp.text = boss.xpReward.toString()
|
|
|
|
|
|
|
|
maVue.postDelayed({
|
|
|
|
|
|
|
|
maVue.visibility = View.GONE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, 3000)
|
|
|
|
//Vibration to signal the death of the boss
|
|
|
|
boss = Generator.generateBoss()
|
|
|
|
|
|
|
|
println(boss)
|
|
|
|
|
|
|
|
imageView.setImageResource(boss.image)
|
|
|
|
|
|
|
|
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
|
|
|
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
|
|
|
|
vibrator.vibrate(100)
|
|
|
|
vibrator.vibrate(100)
|
|
|
|
progressBar.max = boss.maxlife
|
|
|
|
|
|
|
|
progressBar.progress = boss.life
|
|
|
|
|
|
|
|
with(recyclerView) {
|
|
|
|
|
|
|
|
layoutManager = LinearLayoutManager(view.context)
|
|
|
|
|
|
|
|
adapter = AdapterBossLoot(boss.possibleLoot)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Définissez la propriété scaleX et scaleY de l'image sur 0.5f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate a loot item and XP reward
|
|
|
|
|
|
|
|
val item = Generator.generateLootBoss(boss.possibleLoot)
|
|
|
|
|
|
|
|
currentPlayer.addItem(item)
|
|
|
|
|
|
|
|
currentPlayer.gainXp(boss.xpReward)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Show loot toast view for 3 seconds
|
|
|
|
|
|
|
|
displayToast(view,item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Spawn new boss and reset progress bar
|
|
|
|
|
|
|
|
boss = Generator.generateBoss()
|
|
|
|
|
|
|
|
setUpBoss(boss)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Update displayed information
|
|
|
|
|
|
|
|
setUpRecyclerView(view)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (acceleration > 40) {
|
|
|
|
// Reduce boss life based on acceleration value
|
|
|
|
boss.takeDamage((acceleration/80).toInt())
|
|
|
|
boss.takeDamage((acceleration / 80).toInt())
|
|
|
|
progressBar.progress = boss.life
|
|
|
|
progressBar.progress = boss.life
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register accelerometer sensor earphone with manager
|
|
|
|
sensorManager.registerListener(
|
|
|
|
sensorManager.registerListener(
|
|
|
|
accelerometerEventListener,
|
|
|
|
accelerometerEventListener,
|
|
|
|
accelerometer,
|
|
|
|
accelerometer,
|
|
|
|
SensorManager.SENSOR_DELAY_GAME
|
|
|
|
SensorManager.SENSOR_DELAY_GAME
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val activity = requireActivity()
|
|
|
|
|
|
|
|
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
super.onDestroy()
|
|
|
|
|
|
|
|
|
|
|
|
// Désenregistrez l'écouteur de capteur d'accéléromètre lorsque le fragment est détruit
|
|
|
|
// Unregister the accelerometer sensor listener when the fragment is destroyed
|
|
|
|
sensorManager.unregisterListener(accelerometerEventListener)
|
|
|
|
sensorManager.unregisterListener(accelerometerEventListener)
|
|
|
|
val activity = requireActivity()
|
|
|
|
val activity = requireActivity()
|
|
|
|
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
|
|
|
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
|
|
|