new class Tool for equipable item 🔨
continuous-integration/drone/push Build is passing Details

SQLite
Lucas Delanier 2 years ago
parent 2342ca782b
commit f21502c844

@ -79,7 +79,7 @@ class BossFragment() : Fragment() {
} }
private fun setUpBoss(boss: Boss) { private fun setUpBoss(boss: Boss) {
progressBar.max = boss.maxlife progressBar.max = boss.maxlife
progressBar.progress = boss.life progressBar.progress = boss.life.toInt()
image.setImageResource(boss.image) image.setImageResource(boss.image)
// Create scale animation for boss image // Create scale animation for boss image
@ -153,9 +153,14 @@ class BossFragment() : Fragment() {
setUpRecyclerView(view) setUpRecyclerView(view)
} else { } else {
// Reduce boss life based on acceleration value if(acceleration > 20){
boss.takeDamage((acceleration / 80).toInt()) boss.takeDamage(((acceleration / 80)+ currentPlayer.attack()/100).toDouble())
progressBar.progress = boss.life }
else{
boss.takeDamage(currentPlayer.attack().toDouble()/100)
}
progressBar.progress = boss.life.toInt()
} }
} }
} }

@ -5,38 +5,60 @@ 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.Toast
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.model.Player import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Tool
import com.example.shakecraft.view.adapter.AdapterInventory import com.example.shakecraft.view.adapter.AdapterInventory
class InventoryFragment() : Fragment( ) { class InventoryFragment() : Fragment( ), AdapterInventory.OnItemLongClickListener {
private lateinit var currentPlayer: Player
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
override fun onItemLongClick(position: Int) {
if(currentPlayer.items[position] is Tool) {
if (currentPlayer.equipeItem(currentPlayer.items[position]) == true)
Toast.makeText(
context,
currentPlayer.items[position].type.name + " was well equipped",
Toast.LENGTH_SHORT
).show()
else
Toast.makeText(
context,
currentPlayer.items[position].type.name + " has been well unequipped",
Toast.LENGTH_SHORT
).show()
println("equiped item")
setUpRecyclerView(view?.parent as ViewGroup, this)
}
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
val currentPlayer = (activity as MainActivity).currentPlayer currentPlayer = (activity as MainActivity).currentPlayer
val view = inflater.inflate(R.layout.fragment_inventory, container, false) val view = inflater.inflate(R.layout.fragment_inventory, container, false)
// Initialize views // Initialize views
setUpRecyclerView(view, currentPlayer) setUpRecyclerView(view, this)
return view return view
} }
private fun setUpRecyclerView(view: View, currentPlayer: Player) { private fun setUpRecyclerView(view: View,listener: AdapterInventory.OnItemLongClickListener ) {
recyclerView = view.findViewById(R.id.recyclerviewInventory) recyclerView = view.findViewById(R.id.recyclerviewInventory)
with(recyclerView) { with(recyclerView) {
layoutManager = LinearLayoutManager(view.context) layoutManager = LinearLayoutManager(view.context)
adapter = AdapterInventory(currentPlayer.items) adapter = AdapterInventory(currentPlayer.items, listener , currentPlayer)
} }
} }
} }

@ -3,6 +3,7 @@ package com.example.shakecraft.data
import com.example.shakecraft.model.ItemManager.Companion.ITEMS import com.example.shakecraft.model.ItemManager.Companion.ITEMS
import com.example.shakecraft.model.Item import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Tool
class Stub { class Stub {
@ -12,6 +13,8 @@ class Stub {
val items : MutableList<Item> = mutableListOf() val items : MutableList<Item> = mutableListOf()
items.add(Item(type = ITEMS.BEECH_LOG.itemtype, stack = 30)) items.add(Item(type = ITEMS.BEECH_LOG.itemtype, stack = 30))
items.add(Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 30)) items.add(Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 30))
items.add(Tool(type = ITEMS.BRONZE_SWORD.itemtype, stack = 1, damage = 4))
items.add(Tool(type = ITEMS.DIAMOND_AXE.itemtype, stack = 1, damage = 8))
currentPlayer.items = items currentPlayer.items = items
return currentPlayer return currentPlayer

@ -3,14 +3,14 @@ package com.example.shakecraft.model
class Boss ( class Boss (
var name: String, var name: String,
var life: Int, var life: Double,
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>>,
){ ){
fun takeDamage(strength: Int) { fun takeDamage(strength: Double) {
this.life -= strength this.life -= strength
} }
} }

@ -43,26 +43,26 @@ 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, possibleLoot = listOf( Pair(Boss(name = "Margit the Fell Omen", life = 150.0, maxlife = 150, image = R.drawable.boss, xpReward = 100, possibleLoot = listOf(
Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.7), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.7),
Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
)), 0.5), )), 0.5),
Pair(Boss(name = "Godrick the Grafted", life = 200, maxlife = 200, image = R.drawable.skeleton, xpReward = 130, possibleLoot = listOf( Pair(Boss(name = "Godrick the Grafted", life = 200.0, maxlife = 200, image = R.drawable.skeleton, xpReward = 130, possibleLoot = listOf(
Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.6), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.6),
Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.1), Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.1),
)), 0.2), )), 0.2),
Pair(Boss(name = "Red Wolf of Radagon", life = 250, maxlife = 250, image = R.drawable.halberdier, xpReward = 210, possibleLoot = listOf( Pair(Boss(name = "Red Wolf of Radagon", life = 250.0, maxlife = 250, image = R.drawable.halberdier, xpReward = 210, possibleLoot = listOf(
Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.6), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.6),
Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.1), Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.1),
)), 0.15), )), 0.15),
Pair(Boss(name = "Old Banshee", life = 300, maxlife = 300, image = R.drawable.banshee, xpReward = 300, possibleLoot = listOf( Pair(Boss(name = "Old Banshee", life = 300.0, maxlife = 300, image = R.drawable.banshee, xpReward = 300, possibleLoot = listOf(
Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.4), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.4),
Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.4), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.4),
Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.2), Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.2),
)), 0.10), )), 0.10),
Pair(Boss(name = "Margit the Fell Omen", life = 500, maxlife = 500, image = R.drawable.lich, xpReward = 500, possibleLoot = listOf( Pair(Boss(name = "Margit the Fell Omen", life = 500.0, maxlife = 500, image = R.drawable.lich, xpReward = 500, possibleLoot = listOf(
Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.4), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.4),
Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.3), Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.3),

@ -4,7 +4,7 @@ import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
class Item( open class Item(
val type: ItemType, val type: ItemType,
var stack: Int = 1, var stack: Int = 1,
) : Parcelable { ) : Parcelable {

@ -7,6 +7,7 @@ class Player(val pseudo: String, var xp: Int = 0) {
val image: Int = R.drawable.player_image val image: Int = R.drawable.player_image
var items: MutableList<Item> = mutableListOf() var items: MutableList<Item> = mutableListOf()
var rank: String = "Beginner" var rank: String = "Beginner"
var equipedItem : Tool? = null
fun changeRank(){ fun changeRank(){
@ -30,7 +31,14 @@ class Player(val pseudo: String, var xp: Int = 0) {
println("findItem n: "+findItem.stack+" item nb:"+item.stack) println("findItem n: "+findItem.stack+" item nb:"+item.stack)
findItem.stack += item.stack findItem.stack += item.stack
} }
else{items.add(Item(type = item.type, stack = item.stack))} else{
if(item is Tool){
items.add(Tool(type = item.type, stack = item.stack, 4))
}
else{
items.add(Item(type = item.type, stack = item.stack))
}
}
} }
fun gainXp(xp: Int) { fun gainXp(xp: Int) {
@ -42,6 +50,12 @@ class Player(val pseudo: String, var xp: Int = 0) {
} }
} }
fun attack() : Int{
if(equipedItem == null)
return 0
return equipedItem!!.damage
}
fun hasItem(item: Item) : Boolean{ fun hasItem(item: Item) : Boolean{
for (playeritem in items){ for (playeritem in items){
@ -68,4 +82,14 @@ class Player(val pseudo: String, var xp: Int = 0) {
return true return true
} }
fun equipeItem(item : Item) : Boolean{
if(equipedItem == item) {
println("ca jarte")
equipedItem = null
return false
}
equipedItem = item as Tool
return true
}
} }

@ -26,19 +26,19 @@ class RecipeManager {
val recipeListTools : List<Recipe> = listOf( val recipeListTools : List<Recipe> = listOf(
Recipe( Recipe(
Item(type = ITEMS.BRONZE_SWORD.itemtype, stack = 1),listOf( Tool(type = ITEMS.BRONZE_SWORD.itemtype, stack = 1, damage = 4),listOf(
Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5),
Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 10) Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 10)
), "Tools"), ), "Tools"),
Recipe( Recipe(
Item(type = ITEMS.WIZARD_STAFF.itemtype, stack = 1),listOf( Tool(type = ITEMS.WIZARD_STAFF.itemtype, stack = 1, damage = 6),listOf(
Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 10), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 10),
Item(type = ITEMS.MONSTER_EYE.itemtype, stack = 20), Item(type = ITEMS.MONSTER_EYE.itemtype, stack = 20),
), "Tools"), ), "Tools"),
Recipe( Recipe(
Item(type = ITEMS.DIAMOND_AXE.itemtype, stack = 1),listOf( Tool(type = ITEMS.DIAMOND_AXE.itemtype, stack = 1, damage = 8),listOf(
Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5),
Item(type = ITEMS.DIAMOND.itemtype, stack = 10), Item(type = ITEMS.DIAMOND.itemtype, stack = 10),

@ -0,0 +1,4 @@
package com.example.shakecraft.model
class Tool(type : ItemType, stack: Int,val damage : Int) : Item(type, stack) {
}

@ -8,29 +8,56 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.R import com.example.shakecraft.R
import com.example.shakecraft.model.Item import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Tool
class AdapterInventory(
private val inventory: List<Item>,
private val listener: OnItemLongClickListener,
private val currentPlayer : Player
) :
RecyclerView.Adapter<AdapterInventory.ViewHolder>() {
class AdapterInventory(private val inventory: List<Item>) : RecyclerView.Adapter<AdapterInventory.ViewHolder>() { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnLongClickListener {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView val textView: TextView
val textViewNumber: TextView val textViewNumber: TextView
var imageView: ImageView var imageView: ImageView
var armorIndicator : ImageView
init { init {
// Define click listener for the ViewHolder's View // Define click listener for the ViewHolder's View
itemView.setOnLongClickListener(this)
textView = view.findViewById(R.id.item_name) textView = view.findViewById(R.id.item_name)
textViewNumber = view.findViewById(R.id.item_stock) textViewNumber = view.findViewById(R.id.item_stock)
imageView = view.findViewById(R.id.item_image) imageView = view.findViewById(R.id.item_image)
armorIndicator = view.findViewById(R.id.armor_indicator)
}
override fun onLongClick(v: View?): Boolean {
val position = adapterPosition
if(position != RecyclerView.NO_POSITION) {
listener.onItemLongClick(position)
}
return true
} }
fun bind(item: Item) { fun bind(item: Item) {
textView.text = item.type.name textView.text = item.type.name
textViewNumber.text = item.stack.toString() textViewNumber.text = item.stack.toString()
imageView.setImageResource(item.type.image) imageView.setImageResource(item.type.image)
if (item is Tool) {
println("tool")
armorIndicator.visibility = View.VISIBLE
if(currentPlayer.equipedItem == item) armorIndicator.setImageResource(R.drawable.armor_equiped_icon) else armorIndicator.setImageResource(R.drawable.armor_icon)
}
} }
}
interface OnItemLongClickListener{
fun onItemLongClick(position: Int)
} }
override fun getItemCount() = inventory.size override fun getItemCount() = inventory.size

Binary file not shown.

After

Width:  |  Height:  |  Size: 639 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 929 B

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
@ -9,7 +8,7 @@
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp"> android:layout_marginVertical="5dp">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -20,6 +19,7 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<ImageView <ImageView
android:contentDescription="@string/lib_name"
android:id="@+id/item_image" android:id="@+id/item_image"
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="60dp" android:layout_height="60dp"
@ -28,6 +28,7 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@ -39,6 +40,13 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textColor="@color/white" android:textColor="@color/white"
android:textStyle="bold" /> android:textStyle="bold" />
<ImageView
android:id="@+id/armor_indicator"
android:visibility="gone"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="5dp" />
<View <View
android:layout_width="wrap_content" android:layout_width="wrap_content"

Loading…
Cancel
Save