custom item loot for each boss with little animaton on the boss

SQLite
Lucas Delanier 2 years ago
parent 519439785e
commit 8aa241e9b4

@ -11,11 +11,16 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ImageView import android.widget.ImageView
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import androidx.navigation.fragment.findNavController 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.model.Generator
import com.example.shakecraft.view.adapter.AdapterBossLoot
import kotlin.math.pow import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
@ -47,16 +52,44 @@ class BossFragment() : Fragment() {
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
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) progressBar = view.findViewById(R.id.progressBar)
image = view.findViewById(R.id.imageBoss)
sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
var boss = Generator.generateBoss() var boss = Generator.generateBoss()
progressBar.max = boss.maxlife progressBar.max = boss.maxlife
progressBar.progress = boss.life 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 // 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) player.gainXp(boss.xpReward)
boss = Generator.generateBoss() boss = Generator.generateBoss()
println(boss) println(boss)
image.setImageResource(boss.image) 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
with(recyclerView) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterBossLoot(boss.possibleLoot)
}
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 = 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) { if (acceleration > 40) {
boss.takeDamage((acceleration/80).toInt()) boss.takeDamage((acceleration/80).toInt())
progressBar.progress = boss.life progressBar.progress = boss.life
} }
} }
} }

@ -9,6 +9,7 @@ import android.view.ViewGroup
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
@ -31,11 +32,11 @@ class HomeFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_home,container,false) val view = inflater.inflate(R.layout.fragment_home,container,false)
val buttonCollect = view.findViewById<ConstraintLayout>(R.id.buttonCollect) val buttonCollect = view.findViewById<ConstraintLayout>(R.id.buttonCollect)
buttonCollect.setOnClickListener{ 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<ConstraintLayout>(R.id.buttonBoss) val buttonBoss = view.findViewById<ConstraintLayout>(R.id.buttonBoss)
buttonBoss.setOnClickListener{ 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<TextView>(R.id.pseudoTextView) val pseudo = view.findViewById<TextView>(R.id.pseudoTextView)

@ -1,6 +1,5 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import com.example.shakecraft.R
class Boss ( class Boss (
var name: String, var name: String,
@ -8,12 +7,9 @@ class Boss (
var maxlife: Int, var maxlife: Int,
var image: Int, var image: Int,
var xpReward: Int, var xpReward: Int,
val possibleLoot: List<Pair<Item, Double>>,
){ ){
val possibleLoot: List<Pair<Item, Double>> = 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) { fun takeDamage(strength: Int) {
this.life -= strength this.life -= strength
} }

@ -43,11 +43,30 @@ class Generator {
fun generateBoss(): Boss { fun generateBoss(): Boss {
val possibleBoss: List<Pair<Boss, Double>> = listOf( val possibleBoss: List<Pair<Boss, Double>> = listOf(
Pair(Boss(name = "Margit the Fell Omen", life = 150, maxlife = 150, image = R.drawable.boss, xpReward = 100), 0.5), Pair(Boss(name = "Margit the Fell Omen", life = 150, maxlife = 150, image = R.drawable.boss, xpReward = 100, possibleLoot = listOf(
Pair(Boss(name = "Godrick the Grafted", life = 200, maxlife = 200, image = R.drawable.skeleton, xpReward = 130), 0.2), Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.7),
Pair(Boss(name = "Red Wolf of Radagon", life = 250, maxlife = 250, image = R.drawable.halberdier, xpReward = 210), 0.15), Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3),
Pair(Boss(name = "Old Banshee", life = 300, maxlife = 300, image = R.drawable.banshee, xpReward = 300), 0.10), )), 0.5),
Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich, xpReward = 500), 0.05), 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() val rand = Random.nextDouble()

@ -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<Pair<Item, Double>>) : RecyclerView.Adapter<AdapterBossLoot.ViewHolder>() {
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<Item, Double>) {
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<Item, Double> = possibleLoot[position]
viewHolder.bind(item)
}
}

@ -76,137 +76,90 @@
android:id="@+id/imageBoss" android:id="@+id/imageBoss"
android:layout_width="150dp" android:layout_width="150dp"
android:layout_height="150dp" android:layout_height="150dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout" app:layout_constraintBottom_toTopOf="@+id/recyclerviewBossLoot"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" app:layout_constraintTop_toBottomOf="@+id/progressBar"
tools:src="@drawable/skeleton"></ImageView> tools:src="@drawable/skeleton"></ImageView>
<LinearLayout <androidx.recyclerview.widget.RecyclerView
android:id="@+id/linearLayout" android:id="@+id/recyclerviewBossLoot"
android:overScrollMode="never"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="300dp"
android:layout_marginHorizontal="50dp" android:layout_marginHorizontal="40dp"
android:layout_marginTop="20dp"
android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageBoss"> app:layout_constraintTop_toBottomOf="@+id/imageBoss"
tools:listitem="@layout/list_item" />
<LinearLayout
android:layout_width="match_parent" <androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="wrap_content" android:visibility="invisible"
android:layout_marginBottom="5dp" tools:visibility="visible"
android:gravity="center_vertical" android:id="@+id/toast"
android:orientation="horizontal"> android:layout_width="wrap_content"
android:layout_height="wrap_content"
<ImageView android:paddingRight="20dp"
android:layout_width="50dp" android:paddingVertical="2dp"
android:layout_height="50dp" android:layout_marginBottom="10dp"
android:background="@drawable/item_background" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/monster_bones"></ImageView> app:layout_constraintStart_toStartOf="parent"
android:background="@drawable/toast_notification">
<TextView
android:layout_width="wrap_content" <ImageView
android:layout_height="wrap_content" android:id="@+id/imageViewLoot"
android:layout_marginLeft="10dp" android:layout_margin="5dp"
android:text="Monster Bones" android:layout_width="40dp"
android:textColor="@color/white" android:layout_height="40dp"
android:textSize="16dp" android:background="@drawable/item_background"
android:textStyle="bold"></TextView> app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
<View app:layout_constraintTop_toTopOf="parent" />
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="70%"
android:textColor="@color/grey_300"
android:textStyle="bold"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/monster_eyes"
></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Monster eye"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold"></TextView>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25%"
android:textColor="@color/grey_300"
android:textStyle="bold"></TextView>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="5dp" android:orientation="vertical"
android:gravity="center_vertical" android:layout_marginLeft="8dp"
android:orientation="horizontal"> app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
<ImageView app:layout_constraintStart_toEndOf="@+id/imageViewLoot"
android:layout_width="50dp" app:layout_constraintTop_toTopOf="parent">
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/treasure_key"></ImageView>
<TextView <TextView
android:id="@+id/nameLoot"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:textStyle="bold"
android:text="Iron Ore" android:textSize="15sp"
android:textColor="@color/white" android:textColor="@color/white"></TextView>
android:textSize="16dp"
android:textStyle="bold"></TextView> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/xpRewarded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10"
android:textColor="@color/grey_300"
android:textSize="10sp"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xp"
android:textColor="@color/grey_300"
android:textSize="10sp"></TextView>
</LinearLayout>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5%"
android:textColor="@color/grey_300"
android:textStyle="bold"></TextView>
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:contentDescription="image of item"
android:background="@drawable/item_background"
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="@drawable/ic_key" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/item_name"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textStyle="bold" />
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/item_stock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey_300"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Loading…
Cancel
Save