boss combat working with radom loot 🔨

change_to_navgraph
Lucas Delanier 2 years ago
parent d791327b73
commit bf3db1a233

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="2208661a3b057ece" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-03-10T23:31:57.060360300Z" />
</component>
</project>

@ -41,6 +41,8 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'com.google.ar.sceneform:filament-android:1.17.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

@ -1,35 +1,117 @@
package com.example.shakecraft
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.ActivityInfo
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.os.Vibrator
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Boss
import com.example.shakecraft.model.Generator
import com.example.shakecraft.model.Player
import kotlin.math.pow
import kotlin.math.sqrt
// 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 [BossFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class BossFragment : Fragment() {
class BossFragment(var player: Player) : Fragment() {
var stubdata = Stub().load();
private lateinit var sensorManager: SensorManager
private lateinit var accelerometer: Sensor
private lateinit var accelerometerEventListener: SensorEventListener
private lateinit var progressBar: ProgressBar
private lateinit var image: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_boss, container, false)
// Récupérez une référence à la ProgressBar dans la vue
val view = inflater.inflate(R.layout.fragment_boss, container, false)
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)
// Créez un écouteur de capteur d'accéléromètre pour écouter les secousses
accelerometerEventListener = object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
// Ne faites rien ici
}
@SuppressLint("ServiceCast")
override fun onSensorChanged(event: SensorEvent?) {
val acceleration = sqrt(
event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2)
)
if(boss.life <= 0){
val item = Generator.generateLootBoss(boss.possibleLoot);
println(item);
player.addItem(item);
boss = Generator.generateBoss();
println(boss);
image.setImageResource(boss.image)
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100)
}
if (acceleration > 40) {
// Le téléphone a été secoué, mettre à jour la barre de chargement ici
boss.takeDamage((acceleration/80).toInt());
progressBar.progress = boss.life;
}
}
}
// Enregistrez l'écouteur de capteur d'accéléromètre
sensorManager.registerListener(
accelerometerEventListener,
accelerometer,
SensorManager.SENSOR_DELAY_GAME
)
// Retournez la vue de fragment
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val activity = requireActivity()
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
override fun onDestroy() {
super.onDestroy()
// Désenregistrez l'écouteur de capteur d'accéléromètre lorsque le fragment est détruit
sensorManager.unregisterListener(accelerometerEventListener)
val activity = requireActivity()
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
}

@ -2,7 +2,6 @@ package com.example.shakecraft
import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.SENSOR_SERVICE
import android.content.pm.ActivityInfo
import android.hardware.Sensor
import android.hardware.SensorEvent
@ -14,17 +13,12 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.GeneratorLoot
import com.example.shakecraft.model.Generator
import com.example.shakecraft.model.Player
import com.example.shakecraft.view.adapter.AdapterInventory
import kotlin.math.pow
import kotlin.math.sqrt
@ -40,6 +34,8 @@ class CollectFragment(var player: Player) : Fragment() {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
@ -65,32 +61,31 @@ class CollectFragment(var player: Player) : Fragment() {
event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2)
)
if(progressBar.progress == 100){
val item = Generator.generateLootCollection();
println(item);
player.addItem(item);
progressBar.progress = 0;
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100)
val item = GeneratorLoot.generateLootCollection();
println(item);
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
val fadeIn = AlphaAnimation(0f, 1f)
fadeIn.duration = 200
maVue.startAnimation(fadeIn)
image.setImageResource(item.image)
name.text = item.name
maVue.postDelayed({
maVue.visibility = View.GONE
image.setImageResource(item.image)
name.text = item.name
}, 3000)
player.addItem(item);
progressBar.progress = 0;
}
if (acceleration > 40) {
// Le téléphone a été secoué, mettre à jour la barre de chargement ici

@ -0,0 +1,19 @@
package com.example.shakecraft.model
import com.example.shakecraft.R
class Boss (
var name: String,
var life: Int,
var maxlife: Int,
var image: Int,
){
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 Eye", rarity = 0, stack = 1, R.drawable.monster_bones), 0.25),
Pair(Item(name = "Treasure Key", rarity = 0, stack = 1, R.drawable.treasure_key), 0.05),
)
fun takeDamage(strength: Int) {
this.life -= strength;
}
}

@ -0,0 +1,65 @@
package com.example.shakecraft.model
import com.example.shakecraft.R
import kotlin.random.Random
class Generator {
companion object {
fun generateLootCollection(): Item {
val possibleLoot: List<Pair<Item, Double>> = listOf(
Pair(Item(name = "Beech Log", rarity = 0, stack = 1, R.drawable.log2), 0.6),
Pair(Item(name = "Bronze Ore", rarity = 0, stack = 1, R.drawable.bronze_ore), 0.25),
Pair(Item(name = "Iron Ore", rarity = 0, stack = 1, R.drawable.iron_ore), 0.10),
Pair(Item(name = "Diamond", rarity = 0, stack = 1, R.drawable.diamond), 0.05),
)
val rand = Random.nextDouble()
var cumulativeProb = 0.0
for (element in possibleLoot) {
cumulativeProb += element.second
if (rand < cumulativeProb) {
return element.first
}
}
// Si aucun élément n'a été choisi, retourner le dernier élément de la liste
return possibleLoot.last().first
}
fun generateLootBoss( possibleLoot : List<Pair<Item, Double>>): Item {
val rand = Random.nextDouble()
var cumulativeProb = 0.0
for (element in possibleLoot) {
cumulativeProb += element.second
if (rand < cumulativeProb) {
return element.first
}
}
// Si aucun élément n'a été choisi, retourner le dernier élément de la liste
return possibleLoot.last().first
}
fun generateBoss(): Boss {
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 = "Godrick the Grafted", life = 200, maxlife = 200, image = R.drawable.skeleton), 0.2),
Pair(Boss(name = "Red Wolf of Radagon", life = 250, maxlife = 250, image = R.drawable.halberdier), 0.15),
Pair(Boss(name = "Old Banshee", life = 300, maxlife = 300, image = R.drawable.banshee), 0.10),
Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich), 0.05),
)
val rand = Random.nextDouble()
var cumulativeProb = 0.0
for (element in possibleBoss) {
cumulativeProb += element.second
if (rand < cumulativeProb) {
return element.first
}
}
// Si aucun élément n'a été choisi, retourner le dernier élément de la liste
return possibleBoss.last().first
}
}
}

@ -1,29 +0,0 @@
package com.example.shakecraft.model
import com.example.shakecraft.R
import kotlin.random.Random
class GeneratorLoot {
companion object {
fun generateLootCollection(): Item {
val possibleLoot: List<Pair<Item, Double>> = listOf(
Pair(Item(name = "Beech Log", rarity = 0, stack = 1, R.drawable.log2), 0.6),
Pair(Item(name = "Bronze Ore", rarity = 0, stack = 1, R.drawable.bronze_ore), 0.25),
Pair(Item(name = "Iron Ore", rarity = 0, stack = 1, R.drawable.iron_ore), 0.10),
Pair(Item(name = "Diamond", rarity = 0, stack = 1, R.drawable.diamond), 0.05),
)
val rand = Random.nextDouble()
var cumulativeProb = 0.0
for (element in possibleLoot) {
cumulativeProb += element.second
if (rand < cumulativeProb) {
return element.first
}
}
// Si aucun élément n'a été choisi, retourner le dernier élément de la liste
return possibleLoot.last().first
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

@ -59,28 +59,154 @@
app:layout_constraintTop_toBottomOf="@+id/frameLayout" />
<ProgressBar
android:id="@+id/progressBar2"
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginHorizontal="50dp"
android:max="100"
android:progress="20"
android:max="300"
android:progress="300"
android:progressDrawable="@drawable/custom_boss_progressbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ListView
<ImageView
android:layout_marginTop="20dp"
android:id="@+id/imageBoss"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar"
tools:src="@drawable/skeleton"></ImageView>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="100dp"
android:dividerHeight="0dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar2"
tools:listitem="@layout/list_item" />
app:layout_constraintTop_toBottomOf="@+id/imageBoss">
<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_bones"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Monster Bones"
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="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
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/treasure_key"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Iron Ore"
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="5%"
android:textColor="@color/grey_300"
android:textStyle="bold"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -228,7 +228,6 @@
android:layout_margin="5dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/log2"
android:background="@drawable/item_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
@ -248,7 +247,6 @@
android:id="@+id/nameLoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log tree"
android:textStyle="bold"
android:textSize="15sp"
android:textColor="@color/white"></TextView>
@ -263,7 +261,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10"
android:text="10"
android:textColor="@color/grey_300"
android:textSize="10sp"></TextView>
<TextView

@ -182,7 +182,7 @@
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout3"
android:id="@+id/collectbutton"
android:layout_width="match_parent"
android:layout_marginHorizontal="15dp"
android:layout_height="60dp"
@ -249,7 +249,7 @@
android:background="@drawable/rounded_border_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout3">
app:layout_constraintTop_toBottomOf="@+id/collectbutton">
<LinearLayout

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav"
app:startDestination="@id/mainActivity">
<activity
android:id="@+id/mainActivity"
android:name="com.example.shakecraft.MainActivity"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/bossFragment"
android:name="com.example.shakecraft.BossFragment"
android:label="fragment_boss"
tools:layout="@layout/fragment_boss" />
<fragment
android:id="@+id/collectFragment"
android:name="com.example.shakecraft.CollectFragment"
android:label="fragment_collect"
tools:layout="@layout/fragment_collect" >
<action
android:id="@+id/action_collectFragment_to_homeFragment"
app:destination="@id/homeFragment" />
</fragment>
<fragment
android:id="@+id/inventoryFragment"
android:name="com.example.shakecraft.InventoryFragment"
android:label="fragment_inventory"
tools:layout="@layout/fragment_inventory" />
<fragment
android:id="@+id/plusFragment"
android:name="com.example.shakecraft.PlusFragment"
android:label="fragment_plus"
tools:layout="@layout/fragment_plus" />
<fragment
android:id="@+id/homeFragment"
android:name="com.example.shakecraft.HomeFragment"
android:label="HomeFragment" >
<action
android:id="@+id/action_homeFragment_to_collectFragment"
app:destination="@id/collectFragment" />
<action
android:id="@+id/action_homeFragment_to_bossFragment"
app:destination="@id/bossFragment" />
</fragment>
</navigation>
Loading…
Cancel
Save