shake working with inventory and little popup ⚗️

change_to_navgraph
Lucas Delanier 2 years ago
parent 24011493ad
commit d791327b73

@ -0,0 +1,17 @@
<?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>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -23,6 +24,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

@ -1,5 +1,6 @@
package com.example.shakecraft
import android.hardware.SensorManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
@ -17,16 +18,10 @@ private const val ARG_PARAM2 = "param2"
* create an instance of this fragment.
*/
class BossFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
@ -37,23 +32,4 @@ class BossFragment : Fragment() {
return inflater.inflate(R.layout.fragment_boss, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BossFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
BossFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

@ -1,59 +1,127 @@
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
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.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.Player
import com.example.shakecraft.view.adapter.AdapterInventory
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 [CollectFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class CollectFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
class CollectFragment(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
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_collect, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment CollectFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
CollectFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
// Récupérez une référence à la ProgressBar dans la vue
val view = inflater.inflate(R.layout.fragment_collect, container, false)
progressBar = view.findViewById(R.id.progressBar)
sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
// 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(progressBar.progress == 100){
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)
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
progressBar.progress += (acceleration/20).toInt() // Incrémente la progression de la barre de 10 unités
}
}
}
// 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
}
}

@ -8,22 +8,13 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Player
import com.example.shakecraft.view.adapter.AdapterInventory
// 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 [InventoryFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class InventoryFragment : Fragment() {
class InventoryFragment(var player: Player) : Fragment( ) {
var stubdata = Stub().load();
override fun onCreateView(
@ -35,7 +26,7 @@ class InventoryFragment : Fragment() {
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewInventory)
with(recyclerView) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterInventory(stubdata)
adapter = AdapterInventory(player.items)
}
return view
}

@ -18,6 +18,7 @@ class MainActivity : AppCompatActivity() {
private var progressValue = 20
private var lastShakePosition = 0f
var currentPlayer = Stub().currentPlayer;
// Function to hide NavigationBar
@RequiresApi(Build.VERSION_CODES.R)
@ -52,11 +53,11 @@ class MainActivity : AppCompatActivity() {
true
}
R.id.inventory -> {
loadFragment(InventoryFragment())
loadFragment(InventoryFragment(currentPlayer))
true
}
R.id.plus -> {
loadFragment(CollectFragment())
loadFragment(CollectFragment(currentPlayer))
true
}
else -> false

@ -1,17 +1,22 @@
package com.example.shakecraft.data
import com.example.shakecraft.R
import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player
class Stub {
fun load() : List<Item>{
val items : MutableList<Item> = mutableListOf<Item>()
items.add(Item(name = "Beech Log", rarity = 0, stack = 1));
items.add(Item(name = "Bronze Ore", rarity = 0, stack = 1));
items.add(Item(name = "Iron Ore", rarity = 0, stack = 1));
items.add(Item(name = "Diamond", rarity = 0, stack = 1));
items.add(Item(name = "Beech Log", rarity = 0, stack = 1, R.drawable.ic_anvil));
items.add(Item(name = "Bronze Ore", rarity = 0, stack = 1, R.drawable.ic_anvil));
items.add(Item(name = "Iron Ore", rarity = 0, stack = 1, R.drawable.ic_anvil));
items.add(Item(name = "Diamond", rarity = 0, stack = 1, R.drawable.ic_anvil));
return items;
}
var currentPlayer : Player = Player("Winker",0);
}

@ -0,0 +1,29 @@
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
}
}
}

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

@ -0,0 +1,32 @@
package com.example.shakecraft.model
class Player(val pseudo: String, var xp: Int = 0) {
var level: Int = 1
private set
val items: MutableList<Item> = mutableListOf()
fun addItem(item: Item) {
var findItem = items.find { it.name == item.name }
if(findItem!= null){
findItem.stack += 1;
}
else{items.add(item)}
}
fun removeItem(item: 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 !")
}
}

@ -3,6 +3,7 @@ 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 androidx.recyclerview.widget.RecyclerView.Adapter
@ -15,13 +16,21 @@ class AdapterInventory(private val inventory: List<Item>) : RecyclerView.Adapter
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
val textViewNumber: TextView
var imageView: ImageView
init {
// Define click listener for the ViewHolder's View
textView = view.findViewById(R.id.item_name)
textViewNumber = view.findViewById(R.id.item_stock)
imageView = view.findViewById(R.id.item_image)
}
fun bind(item: Item) {
textView.text = item.name
textViewNumber.text = item.stack.toString()
imageView.setImageResource(item.image)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black_800"/>
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
<stroke android:width="1dp"
android:color="@color/white" />
</shape>

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black_800"/>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"/>
</shape>

@ -59,28 +59,224 @@
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:progress="0"
android:progressDrawable="@drawable/custom_collect_progressbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ListView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="100dp"
android:dividerHeight="0dp"
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/progressBar">
<LinearLayout
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/log2"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log tree"
android:textStyle="bold"
android:textColor="@color/white"
android:layout_marginLeft="10dp"
android:textSize="16dp"></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:textStyle="bold"
android:textColor="@color/grey_300"
android:text="60%"></TextView>
</LinearLayout>
<LinearLayout
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/bronze_ore"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bronze Ore"
android:textStyle="bold"
android:textColor="@color/white"
android:layout_marginLeft="10dp"
android:textSize="16dp"></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:textStyle="bold"
android:textColor="@color/grey_300"
android:text="25%"></TextView>
</LinearLayout>
<LinearLayout
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/iron_ore"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Iron Ore"
android:textStyle="bold"
android:textColor="@color/white"
android:layout_marginLeft="10dp"
android:textSize="16dp"></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:textStyle="bold"
android:textColor="@color/grey_300"
android:text="10%"></TextView>
</LinearLayout>
<LinearLayout
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_background"
android:src="@drawable/diamond"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diamond"
android:textStyle="bold"
android:textColor="@color/white"
android:layout_marginLeft="10dp"
android:textSize="16dp"></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:textStyle="bold"
android:textColor="@color/grey_300"
android:text="5%"></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:visibility="invisible"
tools:visibility="visible"
android:id="@+id/toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:paddingVertical="2dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="@drawable/toast_notification">
<ImageView
android:id="@+id/imageViewLoot"
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"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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">
<TextView
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>
<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: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>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1,6 +1,7 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
@ -36,7 +37,6 @@
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Beech Log"
android:textColor="@color/white"
android:textStyle="bold" />
@ -50,11 +50,18 @@
android:id="@+id/item_stock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1,6K"
android:textColor="@color/grey_300"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/bottomNav"
android:background="@color/grey_delimiter"></View>
</LinearLayout>

@ -10,6 +10,7 @@
<color name="blue">#6D8DFC</color>
<color name="black_800">#1D1C22</color>
<color name="grey_100">#343339</color>
<color name="grey_delimiter">#302F35</color>
<color name="grey">#2D2C32</color>
<color name="grey_300">#ADACB2</color>
<color name="red">#FF3D3D</color>

@ -3,6 +3,5 @@
<string name="inventory_title">Inventory</string>
<string name="home_title">Home</string>
<string name="plus_title">Plus</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
Loading…
Cancel
Save