From 8aa241e9b40d4a5eca26b73b9cdbb02f3e34c022 Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Sun, 12 Mar 2023 01:08:28 +0100 Subject: [PATCH] custom item loot for each boss with little animaton on the boss :sparkles: --- .../com/example/shakecraft/BossFragment.kt | 60 +++++- .../com/example/shakecraft/HomeFragment.kt | 5 +- .../java/com/example/shakecraft/model/Boss.kt | 8 +- .../com/example/shakecraft/model/Generator.kt | 29 ++- .../view/adapter/AdapterBossLoot.kt | 54 ++++++ app/src/main/res/layout/fragment_boss.xml | 177 +++++++----------- app/src/main/res/layout/list_loot.xml | 64 +++++++ 7 files changed, 269 insertions(+), 128 deletions(-) create mode 100644 app/src/main/java/com/example/shakecraft/view/adapter/AdapterBossLoot.kt create mode 100644 app/src/main/res/layout/list_loot.xml diff --git a/app/src/main/java/com/example/shakecraft/BossFragment.kt b/app/src/main/java/com/example/shakecraft/BossFragment.kt index 3245ef0..84123e2 100644 --- a/app/src/main/java/com/example/shakecraft/BossFragment.kt +++ b/app/src/main/java/com/example/shakecraft/BossFragment.kt @@ -11,11 +11,16 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.animation.Animation +import android.view.animation.ScaleAnimation import android.widget.ImageView import android.widget.ProgressBar import android.widget.TextView import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.example.shakecraft.model.Generator +import com.example.shakecraft.view.adapter.AdapterBossLoot import kotlin.math.pow import kotlin.math.sqrt @@ -47,16 +52,44 @@ class BossFragment() : Fragment() { buttonCollect.setOnClickListener{ findNavController().navigate(R.id.action_bossFragment_to_homeFragment) } + val imageView = view.findViewById(R.id.imageBoss) + imageView.scaleX = 1.2f + imageView.scaleY = 1.2f + +// Créez une animation qui modifie la propriété scaleX et scaleY de l'image + val scaleAnimation = ScaleAnimation( + 1.2f, // de 2.0 à 1.0 + 1.0f, + 1.2f, + 1.0f, + Animation.RELATIVE_TO_SELF, + 0.5f, + Animation.RELATIVE_TO_SELF, + 0.5f + ) + + scaleAnimation.duration = 1000 // dure 1 seconde + scaleAnimation.repeatCount = Animation.INFINITE // répéter indéfiniment + scaleAnimation.repeatMode = Animation.REVERSE // inverser la direction de l'animation + +// Appliquez l'animation à l'image + imageView.startAnimation(scaleAnimation) progressBar = view.findViewById(R.id.progressBar) - image = view.findViewById(R.id.imageBoss) 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 - image.setImageResource(boss.image) + imageView.setImageResource(boss.image) + + + val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewBossLoot) + with(recyclerView) { + layoutManager = LinearLayoutManager(view.context) + adapter = AdapterBossLoot(boss.possibleLoot) + } // Créez un écouteur de capteur d'accéléromètre pour écouter les secousses @@ -76,9 +109,29 @@ class BossFragment() : Fragment() { player.gainXp(boss.xpReward) boss = Generator.generateBoss() println(boss) - image.setImageResource(boss.image) + imageView.setImageResource(boss.image) val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator vibrator.vibrate(100) + progressBar.max = boss.maxlife + with(recyclerView) { + layoutManager = LinearLayoutManager(view.context) + adapter = AdapterBossLoot(boss.possibleLoot) + } + val maVue = view.findViewById(R.id.toast) + val image = maVue.findViewById(R.id.imageViewLoot) + val name = maVue.findViewById(R.id.nameLoot) + val xp = maVue.findViewById(R.id.xpRewarded) + maVue.visibility = View.VISIBLE + image.setImageResource(item.image) + name.text = item.name + xp.text = item.xpReward.toString() + maVue.postDelayed({ + maVue.visibility = View.GONE + + }, 3000) + + // Définissez la propriété scaleX et scaleY de l'image sur 0.5f + @@ -88,6 +141,7 @@ class BossFragment() : Fragment() { if (acceleration > 40) { boss.takeDamage((acceleration/80).toInt()) progressBar.progress = boss.life + } } } diff --git a/app/src/main/java/com/example/shakecraft/HomeFragment.kt b/app/src/main/java/com/example/shakecraft/HomeFragment.kt index 33b9978..321024c 100644 --- a/app/src/main/java/com/example/shakecraft/HomeFragment.kt +++ b/app/src/main/java/com/example/shakecraft/HomeFragment.kt @@ -9,6 +9,7 @@ import android.view.ViewGroup import android.widget.ProgressBar import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout +import androidx.navigation.NavOptions import androidx.navigation.fragment.findNavController @@ -31,11 +32,11 @@ class HomeFragment : Fragment() { val view = inflater.inflate(R.layout.fragment_home,container,false) val buttonCollect = view.findViewById(R.id.buttonCollect) buttonCollect.setOnClickListener{ - findNavController().navigate(R.id.action_homeFragment_to_collectFragment) + findNavController().navigate(R.id.action_homeFragment_to_collectFragment, null, NavOptions.Builder().setPopUpTo(R.id.homeFragment, false).build()) } val buttonBoss = view.findViewById(R.id.buttonBoss) buttonBoss.setOnClickListener{ - findNavController().navigate(R.id.action_homeFragment_to_bossFragment) + findNavController().navigate(R.id.action_homeFragment_to_bossFragment, null, NavOptions.Builder().setPopUpTo(R.id.homeFragment, false).build()) } val pseudo = view.findViewById(R.id.pseudoTextView) diff --git a/app/src/main/java/com/example/shakecraft/model/Boss.kt b/app/src/main/java/com/example/shakecraft/model/Boss.kt index 147342b..62b9b1b 100644 --- a/app/src/main/java/com/example/shakecraft/model/Boss.kt +++ b/app/src/main/java/com/example/shakecraft/model/Boss.kt @@ -1,6 +1,5 @@ package com.example.shakecraft.model -import com.example.shakecraft.R class Boss ( var name: String, @@ -8,12 +7,9 @@ class Boss ( var maxlife: Int, var image: Int, var xpReward: Int, + val possibleLoot: List>, ){ - val possibleLoot: List> = listOf( - Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.7), - Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.25), - Pair(Item(name = "Treasure Key", rarity = 5, stack = 1, R.drawable.treasure_key, 50), 0.05), - ) + fun takeDamage(strength: Int) { this.life -= strength } diff --git a/app/src/main/java/com/example/shakecraft/model/Generator.kt b/app/src/main/java/com/example/shakecraft/model/Generator.kt index 20442a3..12728b0 100644 --- a/app/src/main/java/com/example/shakecraft/model/Generator.kt +++ b/app/src/main/java/com/example/shakecraft/model/Generator.kt @@ -43,11 +43,30 @@ class Generator { fun generateBoss(): Boss { val possibleBoss: List> = listOf( - Pair(Boss(name = "Margit the Fell Omen", life = 150, maxlife = 150, image = R.drawable.boss, xpReward = 100), 0.5), - Pair(Boss(name = "Godrick the Grafted", life = 200, maxlife = 200, image = R.drawable.skeleton, xpReward = 130), 0.2), - Pair(Boss(name = "Red Wolf of Radagon", life = 250, maxlife = 250, image = R.drawable.halberdier, xpReward = 210), 0.15), - Pair(Boss(name = "Old Banshee", life = 300, maxlife = 300, image = R.drawable.banshee, xpReward = 300), 0.10), - Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich, xpReward = 500), 0.05), + Pair(Boss(name = "Margit the Fell Omen", life = 150, maxlife = 150, image = R.drawable.boss, xpReward = 100, possibleLoot = listOf( + Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.7), + Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), + )), 0.5), + Pair(Boss(name = "Godrick the Grafted", life = 200, maxlife = 200, image = R.drawable.skeleton, xpReward = 130, possibleLoot = listOf( + Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.6), + Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), + Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 0.1), + )), 0.2), + Pair(Boss(name = "Red Wolf of Radagon", life = 250, maxlife = 250, image = R.drawable.halberdier, xpReward = 210, possibleLoot = listOf( + Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.6), + Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), + Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 0.1), + )), 0.15), + Pair(Boss(name = "Old Banshee", life = 300, maxlife = 300, image = R.drawable.banshee, xpReward = 300, possibleLoot = listOf( + Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.4), + Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.4), + Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 0.2), + )), 0.10), + Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich, xpReward = 500, possibleLoot = listOf( + Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.4), + Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), + Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 0.3), + )), 0.05), ) val rand = Random.nextDouble() diff --git a/app/src/main/java/com/example/shakecraft/view/adapter/AdapterBossLoot.kt b/app/src/main/java/com/example/shakecraft/view/adapter/AdapterBossLoot.kt new file mode 100644 index 0000000..bfb0da3 --- /dev/null +++ b/app/src/main/java/com/example/shakecraft/view/adapter/AdapterBossLoot.kt @@ -0,0 +1,54 @@ +package com.example.shakecraft.view.adapter + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView + +import com.example.shakecraft.R +import com.example.shakecraft.model.Item + + +class AdapterBossLoot(private val possibleLoot: List>) : RecyclerView.Adapter() { + + class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + val textView: TextView + val textViewDropRate: TextView + var imageView: ImageView + + + init { + // Define click listener for the ViewHolder's View + + textView = view.findViewById(R.id.item_name) + textViewDropRate = view.findViewById(R.id.item_stock) + imageView = view.findViewById(R.id.item_image) + } + fun bind(item: Pair) { + textView.text = item.first.name + textViewDropRate.text = (item.second*100).toString() + "%" + imageView.setImageResource(item.first.image) + } + } + + override fun getItemCount() = possibleLoot.size + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.list_loot, parent, false) + + return ViewHolder(view) + } + + + override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { + val item : Pair = possibleLoot[position] + viewHolder.bind(item) + } + + + + +} + diff --git a/app/src/main/res/layout/fragment_boss.xml b/app/src/main/res/layout/fragment_boss.xml index 4a2f073..47403a4 100644 --- a/app/src/main/res/layout/fragment_boss.xml +++ b/app/src/main/res/layout/fragment_boss.xml @@ -76,137 +76,90 @@ android:id="@+id/imageBoss" android:layout_width="150dp" android:layout_height="150dp" - app:layout_constraintBottom_toTopOf="@+id/linearLayout" + app:layout_constraintBottom_toTopOf="@+id/recyclerviewBossLoot" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/progressBar" tools:src="@drawable/skeleton"> - - - - - - - - - - - - - - - - - - - - - - - + app:layout_constraintTop_toBottomOf="@+id/imageBoss" + tools:listitem="@layout/list_item" /> + + + + - - + android:orientation="vertical" + android:layout_marginLeft="8dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toEndOf="@+id/imageViewLoot" + app:layout_constraintTop_toTopOf="parent"> + android:textStyle="bold" + android:textSize="15sp" + android:textColor="@color/white"> + + + + + + - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/layout/list_loot.xml b/app/src/main/res/layout/list_loot.xml new file mode 100644 index 0000000..60fcd41 --- /dev/null +++ b/app/src/main/res/layout/list_loot.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file