custom xp reward following with boss and witch item is loot 🔥

SQLite
Lucas Delanier 2 years ago
parent 63b91ab270
commit 519439785e

@ -1,6 +1,4 @@
package com.example.shakecraft package com.example.shakecraft
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.hardware.Sensor import android.hardware.Sensor
@ -17,10 +15,7 @@ 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 com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Boss
import com.example.shakecraft.model.Generator import com.example.shakecraft.model.Generator
import com.example.shakecraft.model.Player
import kotlin.math.pow import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
@ -45,9 +40,8 @@ class BossFragment() : Fragment() {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var player = (activity as MainActivity).currentPlayer val player = (activity as MainActivity).currentPlayer
// Récupérez une référence à la ProgressBar dans la vue
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) val buttonCollect = view.findViewById<TextView>(R.id.backbutton)
buttonCollect.setOnClickListener{ buttonCollect.setOnClickListener{
@ -59,9 +53,9 @@ class BossFragment() : Fragment() {
image = view.findViewById(R.id.imageBoss) 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) image.setImageResource(boss.image)
@ -76,11 +70,12 @@ class BossFragment() : Fragment() {
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); val item = Generator.generateLootBoss(boss.possibleLoot)
println(item); println(item)
player.addItem(item); player.addItem(item)
boss = Generator.generateBoss(); player.gainXp(boss.xpReward)
println(boss); boss = Generator.generateBoss()
println(boss)
image.setImageResource(boss.image) image.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)
@ -91,14 +86,13 @@ class BossFragment() : Fragment() {
} }
if (acceleration > 40) { if (acceleration > 40) {
// Le téléphone a été secoué, mettre à jour la barre de chargement ici boss.takeDamage((acceleration/80).toInt())
boss.takeDamage((acceleration/80).toInt()); progressBar.progress = boss.life
progressBar.progress = boss.life;
} }
} }
} }
// Enregistrez l'écouteur de capteur d'accéléromètre
sensorManager.registerListener( sensorManager.registerListener(
accelerometerEventListener, accelerometerEventListener,
accelerometer, accelerometer,
@ -106,7 +100,7 @@ class BossFragment() : Fragment() {
) )
// Retournez la vue de fragment
return view return view
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {

@ -16,11 +16,11 @@ import android.view.ViewGroup
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.constraintlayout.widget.ConstraintLayout
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Generator import com.example.shakecraft.model.Generator
import com.example.shakecraft.model.Player
import kotlin.math.pow import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
@ -43,7 +43,7 @@ class CollectFragment() : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var player = (activity as MainActivity).currentPlayer val player = (activity as MainActivity).currentPlayer
// Récupérez une référence à la ProgressBar dans la vue // Récupérez une référence à la ProgressBar dans la vue
val view = inflater.inflate(R.layout.fragment_collect, container, false) val view = inflater.inflate(R.layout.fragment_collect, container, false)
@ -69,11 +69,12 @@ class CollectFragment() : Fragment() {
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(progressBar.progress == 100){ if(progressBar.progress == 100){
val item = Generator.generateLootCollection(); val item = Generator.generateLootCollection()
println(item); println(item)
player.addItem(item); player.addItem(item)
progressBar.progress = 0; player.gainXp(item.xpReward)
progressBar.progress = 0
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100) vibrator.vibrate(100)
@ -87,6 +88,7 @@ class CollectFragment() : Fragment() {
maVue.visibility = View.VISIBLE maVue.visibility = View.VISIBLE
image.setImageResource(item.image) image.setImageResource(item.image)
name.text = item.name name.text = item.name
xp.text = item.xpReward.toString()
maVue.postDelayed({ maVue.postDelayed({
maVue.visibility = View.GONE maVue.visibility = View.GONE

@ -5,20 +5,15 @@ 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.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [HomeFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
@ -31,6 +26,8 @@ class HomeFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
val player = (activity as MainActivity).currentPlayer
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{
@ -40,6 +37,21 @@ class HomeFragment : Fragment() {
buttonBoss.setOnClickListener{ buttonBoss.setOnClickListener{
findNavController().navigate(R.id.action_homeFragment_to_bossFragment) findNavController().navigate(R.id.action_homeFragment_to_bossFragment)
} }
val pseudo = view.findViewById<TextView>(R.id.pseudoTextView)
val progressbar = view.findViewById<ProgressBar>(R.id.levelProgressBar)
val level = view.findViewById<TextView>(R.id.levelTextView)
val rank = view.findViewById<TextView>(R.id.rankTextView)
val maxXp = view.findViewById<TextView>(R.id.maxXpTextView)
val xp = view.findViewById<TextView>(R.id.xpTextView)
pseudo.text = player.pseudo
level.text = player.level.toString()
rank.text = player.rank
xp.text = player.xp.toString()
maxXp.text = (player.level*100).toString()
progressbar.progress = player.xp
progressbar.max = player.level*100
return view return view
} }

@ -9,14 +9,14 @@ class Stub {
fun load() : List<Item>{ fun load() : List<Item>{
val items : MutableList<Item> = mutableListOf<Item>() val items : MutableList<Item> = mutableListOf<Item>()
items.add(Item(name = "Beech Log", rarity = 0, stack = 1, R.drawable.ic_anvil)); items.add(Item(name = "Beech Log", rarity = 1, stack = 1, R.drawable.ic_anvil, xpReward = 10 ))
items.add(Item(name = "Bronze Ore", rarity = 0, stack = 1, R.drawable.ic_anvil)); items.add(Item(name = "Bronze Ore", rarity = 2, stack = 1, R.drawable.ic_anvil, xpReward = 20))
items.add(Item(name = "Iron Ore", rarity = 0, stack = 1, R.drawable.ic_anvil)); items.add(Item(name = "Iron Ore", rarity = 3, stack = 1, R.drawable.ic_anvil, xpReward = 30))
items.add(Item(name = "Diamond", rarity = 0, stack = 1, R.drawable.ic_anvil)); items.add(Item(name = "Diamond", rarity = 3, stack = 1, R.drawable.ic_anvil, xpReward = 30))
return items; return items
} }
var currentPlayer : Player = Player("Winker",0); var currentPlayer : Player = Player("Winker",0)
} }

@ -7,11 +7,12 @@ class Boss (
var life: Int, var life: Int,
var maxlife: Int, var maxlife: Int,
var image: Int, var image: Int,
var xpReward: Int,
){ ){
val possibleLoot: List<Pair<Item, Double>> = listOf( val possibleLoot: List<Pair<Item, Double>> = listOf(
Pair(Item(name = "Monster Bones", rarity = 0, stack = 1, R.drawable.monster_bones), 0.7), Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.7),
Pair(Item(name = "Monster Eye", rarity = 0, stack = 1, R.drawable.monster_eyes), 0.25), Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.25),
Pair(Item(name = "Treasure Key", rarity = 0, stack = 1, R.drawable.treasure_key), 0.05), 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

@ -8,10 +8,10 @@ class Generator {
companion object { companion object {
fun generateLootCollection(): Item { fun generateLootCollection(): Item {
val possibleLoot: List<Pair<Item, Double>> = listOf( val possibleLoot: List<Pair<Item, Double>> = listOf(
Pair(Item(name = "Beech Log", rarity = 0, stack = 1, R.drawable.log2), 0.6), Pair(Item(name = "Beech Log", rarity = 1, stack = 1, R.drawable.log2, 10), 0.6),
Pair(Item(name = "Bronze Ore", rarity = 0, stack = 1, R.drawable.bronze_ore), 0.25), Pair(Item(name = "Bronze Ore", rarity = 2, stack = 1, R.drawable.bronze_ore, 20), 0.25),
Pair(Item(name = "Iron Ore", rarity = 0, stack = 1, R.drawable.iron_ore), 0.10), Pair(Item(name = "Iron Ore", rarity = 2, stack = 1, R.drawable.iron_ore, 25), 0.10),
Pair(Item(name = "Diamond", rarity = 0, stack = 1, R.drawable.diamond), 0.05), Pair(Item(name = "Diamond", rarity = 3, stack = 1, R.drawable.diamond, 30), 0.05),
) )
val rand = Random.nextDouble() val rand = Random.nextDouble()
@ -43,11 +43,11 @@ 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), 0.5), 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), 0.2), 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), 0.15), 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), 0.10), 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), 0.05), Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich, xpReward = 500), 0.05),
) )
val rand = Random.nextDouble() val rand = Random.nextDouble()

@ -1,12 +1,12 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import android.media.Image
data class Item( data class Item(
var name: String, var name: String,
var rarity: Int, var rarity: Int,
var stack: Int, var stack: Int,
var image: Int, var image: Int,
var xpReward: Int,
) )

@ -2,31 +2,67 @@ package com.example.shakecraft.model
class Player(val pseudo: String, var xp: Int = 0) { class Player(val pseudo: String, var xp: Int = 0) {
var level: Int = 1 var level: Int = 1
private set
val items: MutableList<Item> = mutableListOf() val items: MutableList<Item> = mutableListOf()
var rank: String = "Beginner"
fun changeRank(){
if(this.level<2){
this.rank = "Beginner"
}
else if(this.level<5){
this.rank = "Intermediate"
}
else if(this.level<8){
this.rank = "Proficient"
}
else if(this.level<11){
this.rank = "Expert"
}
else if(this.level<14){
this.rank = "Master"
}
else if(this.level<17){
this.rank = "Professional"
}
else if(this.level<20){
this.rank = "Champion"
}
else if(this.level<23){
this.rank = "Beginner"
}
else if(this.level<26){
this.rank = "Legend"
}
else if(this.level<26){
this.rank = "Invincible"
}
else if(this.level<29){
this.rank = "Divine"
}
}
fun addItem(item: Item) { fun addItem(item: Item) {
var findItem = items.find { it.name == item.name } val findItem = items.find { it.name == item.name }
if(findItem!= null){ if(findItem!= null){
findItem.stack += 1; findItem.stack += 1
} }
else{items.add(item)} else{items.add(item)}
} }
fun gainXp(xp: Int) {
this.xp += xp
if (this.xp >= this.level *100){
this.level +=1
this.xp = 0
changeRank()
}
}
fun removeItem(item: Item) { fun removeItem(item: Item) {
items.remove(item) items.remove(item)
} }
fun increaseXp(amount: Int) {
xp += amount
if (xp >= 100 * level) {
levelUp()
}
}
private fun levelUp() {
level++
println("$pseudo a atteint le niveau $level !")
}
} }

@ -15,7 +15,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="50dp" android:layout_marginTop="50dp"
android:paddingBottom="20dp" android:paddingBottom="20dp"
android:text="Home" android:text="@string/home_title"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="16sp" android:textSize="16sp"
android:textStyle="bold" android:textStyle="bold"
@ -44,6 +44,7 @@
app:layout_constraintStart_toStartOf="parent"> app:layout_constraintStart_toStartOf="parent">
<ImageView <ImageView
android:contentDescription="Landscape"
android:id="@+id/imageView2" android:id="@+id/imageView2"
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -56,7 +57,7 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/textView4" android:id="@+id/pseudoTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Pseudo" android:text="Pseudo"
@ -74,11 +75,17 @@
android:id="@+id/textView6" android:id="@+id/textView6"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Lvl. 2" android:text="Lvl."
android:textColor="@color/grey_300" />
<TextView
android:id="@+id/levelTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="@color/grey_300" /> android:textColor="@color/grey_300" />
<ProgressBar <ProgressBar
android:id="@+id/progressBar" android:id="@+id/levelProgressBar"
style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="5dp" android:layout_height="5dp"
@ -88,11 +95,22 @@
android:progress="20" android:progress="20"
android:progressDrawable="@drawable/custom_level_progressbar" /> android:progressDrawable="@drawable/custom_level_progressbar" />
<TextView
android:id="@+id/xpTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="349"
android:textColor="@color/white" />
<TextView <TextView
android:id="@+id/textView7" android:id="@+id/textView7"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="349/360" android:text="/"
android:textColor="@color/white" />
<TextView
android:id="@+id/maxXpTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white" /> android:textColor="@color/white" />
</LinearLayout> </LinearLayout>
@ -111,7 +129,7 @@
<TextView <TextView
android:id="@+id/textView7" android:id="@+id/rankTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Beginner" android:text="Beginner"

Loading…
Cancel
Save