implement flyweight design pattern :pinched_fingers: 👦
continuous-integration/drone/push Build is passing Details

SQLite
Lucas Delanier 2 years ago
parent e3451ca8f4
commit b7c8cad9d2

@ -115,8 +115,8 @@ class BossFragment() : Fragment() {
val lootName = toastView.findViewById<TextView>(R.id.nameLoot) val lootName = toastView.findViewById<TextView>(R.id.nameLoot)
val xpReward = toastView.findViewById<TextView>(R.id.xpRewarded) val xpReward = toastView.findViewById<TextView>(R.id.xpRewarded)
toastView.visibility = View.VISIBLE toastView.visibility = View.VISIBLE
lootImage.setImageResource(item.image) lootImage.setImageResource(item.type.image)
lootName.text = item.name lootName.text = item.type.name
xpReward.text = boss.xpReward.toString() xpReward.text = boss.xpReward.toString()
toastView.postDelayed({ toastView.postDelayed({
toastView.visibility = View.GONE toastView.visibility = View.GONE

@ -76,9 +76,9 @@ class CollectFragment() : Fragment() {
val name = maVue.findViewById<TextView>(R.id.nameLoot) val name = maVue.findViewById<TextView>(R.id.nameLoot)
val xp = maVue.findViewById<TextView>(R.id.xpRewarded) val xp = maVue.findViewById<TextView>(R.id.xpRewarded)
maVue.visibility = View.VISIBLE maVue.visibility = View.VISIBLE
image.setImageResource(item.image) image.setImageResource(item.type.image)
name.text = item.name name.text = item.type.name
xp.text = item.xpReward.toString() xp.text = item.type.xpReward.toString()
maVue.postDelayed({ maVue.postDelayed({
maVue.visibility = View.GONE maVue.visibility = View.GONE
@ -102,7 +102,7 @@ class CollectFragment() : Fragment() {
// Generate a resource item and XP reward // Generate a resource item and XP reward
val item = Generator.generateLootCollection() val item = Generator.generateLootCollection()
currentPlayer.addItem(item) currentPlayer.addItem(item)
currentPlayer.gainXp(item.xpReward) currentPlayer.gainXp(item.type.xpReward)
//reset to 0 the progress bar //reset to 0 the progress bar
progressBar.progress = 0 progressBar.progress = 0

@ -72,8 +72,8 @@ class CraftFragment : Fragment() {
} }
image = view.findViewById(R.id.item_image) image = view.findViewById(R.id.item_image)
name = view.findViewById(R.id.item_name) name = view.findViewById(R.id.item_name)
image.setImageResource(recipe.item.image) image.setImageResource(recipe.item.type.image)
name.text = recipe.item.name name.text = recipe.item.type.name
buttonForge.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer) buttonForge.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer)
numberCraftable.text = RecipeManager.HowManyCraftable(recipe,currentPlayer).toString() numberCraftable.text = RecipeManager.HowManyCraftable(recipe,currentPlayer).toString()

@ -1,6 +1,6 @@
package com.example.shakecraft.data package com.example.shakecraft.data
import com.example.shakecraft.R 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
@ -8,9 +8,9 @@ import com.example.shakecraft.model.Player
class Stub { class Stub {
fun load() : Player{ fun load() : Player{
var currentPlayer : Player = Player("Winker",0) val currentPlayer = Player("Winker",0)
val items : MutableList<Item> = mutableListOf<Item>() val items : MutableList<Item> = mutableListOf()
items.add(Item(name = "Beech Log", rarity = 1, stack = 30, R.drawable.log2, xpReward = 10 )) items.add(Item(type = ITEMS.BEECH_LOG.itemtype, stack = 30))
currentPlayer.items = items currentPlayer.items = items
return currentPlayer return currentPlayer

@ -1,6 +1,6 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import com.example.shakecraft.R import com.example.shakecraft.R
import com.example.shakecraft.model.itemManager.Companion.ITEMS
import kotlin.random.Random import kotlin.random.Random
class Generator { class Generator {
@ -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 = 1, stack = 1, R.drawable.log2, 10), 0.6), Pair(Item(type = ITEMS.BEECH_LOG.itemtype), 0.6),
Pair(Item(name = "Bronze Ore", rarity = 2, stack = 1, R.drawable.bronze_ore, 20), 0.25), Pair(Item(type = ITEMS.BRONZE_ORE.itemtype), 0.25),
Pair(Item(name = "Iron Ore", rarity = 2, stack = 1, R.drawable.iron_ore, 25), 0.10), Pair(Item(type = ITEMS.IRON_ORE.itemtype), 0.10),
Pair(Item(name = "Diamond", rarity = 3, stack = 1, R.drawable.diamond, 30), 0.05), Pair(Item(type = ITEMS.DIAMOND.itemtype), 0.05),
) )
val rand = Random.nextDouble() val rand = Random.nextDouble()
@ -44,28 +44,28 @@ 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, maxlife = 150, image = R.drawable.boss, xpReward = 100, possibleLoot = listOf(
Pair(Item(name = "Monster Bones", rarity = 1, stack = 1, R.drawable.monster_bones, 10), 0.7), Pair(Item(type = ITEMS.MONSTER_BONES.itemtype), 0.7),
Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 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, 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(type = ITEMS.MONSTER_BONES.itemtype), 0.6),
Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 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, 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(type = ITEMS.MONSTER_BONES.itemtype), 0.6),
Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 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, 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(type = ITEMS.MONSTER_BONES.itemtype), 0.4),
Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.4), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.4),
Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 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, 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(type = ITEMS.MONSTER_BONES.itemtype), 0.4),
Pair(Item(name = "Monster Eye", rarity = 2, stack = 1, R.drawable.monster_eyes, 20), 0.3), Pair(Item(type = ITEMS.MONSTER_EYE.itemtype), 0.3),
Pair(Item(name = "Treasure Key", rarity = 2, stack = 1, R.drawable.treasure_key, 20), 0.3), Pair(Item(type = ITEMS.TREASURE_KEY.itemtype), 0.3),
)), 0.05), )), 0.05),
) )
val rand = Random.nextDouble() val rand = Random.nextDouble()

@ -5,11 +5,8 @@ import android.os.Parcelable
class Item( class Item(
var name: String, val type: ItemType,
var rarity: Int = 1,
var stack: Int = 1, var stack: Int = 1,
var image: Int,
var xpReward: Int = 0,
) : Parcelable { ) : Parcelable {
@ -18,20 +15,20 @@ class Item(
} }
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(name) parcel.writeString(type.name)
parcel.writeInt(rarity) parcel.writeInt(type.rarity)
parcel.writeInt(type.image)
parcel.writeInt(type.xpReward)
parcel.writeInt(stack) parcel.writeInt(stack)
parcel.writeInt(image)
parcel.writeInt(xpReward)
} }
companion object CREATOR : Parcelable.Creator<Item> { companion object CREATOR : Parcelable.Creator<Item> {
override fun createFromParcel(parcel: Parcel): Item { override fun createFromParcel(parcel: Parcel): Item {
return Item( return Item(
parcel.readString()!!, ItemType(parcel.readString()!!,parcel.readInt(),
parcel.readInt(), parcel.readInt(),
parcel.readInt(), parcel.readInt(),),
parcel.readInt(),
parcel.readInt() parcel.readInt()
) )
} }

@ -0,0 +1,6 @@
package com.example.shakecraft.model
class ItemType (val name : String,val image : Int,val rarity : Int, val xpReward : Int){
}

@ -43,13 +43,13 @@ class Player(val pseudo: String, var xp: Int = 0) {
} }
} }
fun addItem(item: Item) { fun addItem(item: Item) {
val findItem = items.find { it.name == item.name } val findItem = items.find { it.type.name == item.type.name }
if(findItem!= null){ if(findItem!= null){
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(item.name, item.rarity, item.stack, item.image, item.xpReward))} else{items.add(Item(type = item.type, stack = item.stack))}
} }
fun gainXp(xp: Int) { fun gainXp(xp: Int) {
@ -61,13 +61,10 @@ class Player(val pseudo: String, var xp: Int = 0) {
} }
} }
fun removeItem(item: Item) {
items.remove(item)
}
fun hasItem(item: Item) : Boolean{ fun hasItem(item: Item) : Boolean{
for (playeritem in items){ for (playeritem in items){
if(playeritem.name == item.name && playeritem.stack >= item.stack){ if(playeritem.type.name == item.type.name && playeritem.stack >= item.stack){
return true return true
} }
} }
@ -77,7 +74,7 @@ class Player(val pseudo: String, var xp: Int = 0) {
fun craft(recipe: Recipe) : Boolean{ fun craft(recipe: Recipe) : Boolean{
println("test") println("test")
for (ingredient in recipe.ingredients) { for (ingredient in recipe.ingredients) {
val searchedItem = items.find { it.name == ingredient.name } val searchedItem = items.find { it.type.name == ingredient.type.name }
if(searchedItem != null) { if(searchedItem != null) {
searchedItem.stack -= ingredient.stack searchedItem.stack -= ingredient.stack
if (searchedItem.stack == 0){ if (searchedItem.stack == 0){

@ -1,6 +1,5 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import com.example.shakecraft.model.itemManager.Companion.ITEMS
import com.example.shakecraft.R
class RecipeManager { class RecipeManager {
@ -10,51 +9,51 @@ class RecipeManager {
val recipeListObjects : List<Recipe> = listOf( val recipeListObjects : List<Recipe> = listOf(
Recipe( Recipe(
Item("Wooden Stick", image = R.drawable.wooden_stick, stack = 1),listOf( Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 1),listOf(
Item("Wooden Plank", image = R.drawable.wooden_plank, stack = 2) Item(type = ITEMS.WOODEN_PLANK.itemtype, stack = 2)
), "Objects"), ), "Objects"),
Recipe( Recipe(
Item("Wooden Plank", image = R.drawable.wooden_plank, stack = 3),listOf( Item(type = ITEMS.WOODEN_PLANK.itemtype, stack = 3),listOf(
Item("Beech Log", image = R.drawable.log2, stack = 1) Item(type = ITEMS.BEECH_LOG.itemtype, stack = 1)
), "Objects"), ), "Objects"),
Recipe( Recipe(
Item("Wooden Ball", image = R.drawable.wooden_ball, stack = 1),listOf( Item(type = ITEMS.WOODEN_BALL.itemtype, stack = 1),listOf(
Item("Wooden Stick", image = R.drawable.wooden_stick, stack = 2), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 2),
Item("Wooden Plank", image = R.drawable.wooden_plank, stack = 2) Item(type = ITEMS.WOODEN_PLANK.itemtype, stack = 2)
), "Objects"), ), "Objects"),
) )
val recipeListTools : List<Recipe> = listOf( val recipeListTools : List<Recipe> = listOf(
Recipe( Recipe(
Item("Bronze Sword", image = R.drawable.bronze_sword, stack = 1),listOf( Item(type = ITEMS.BRONZE_SWORD.itemtype, stack = 1),listOf(
Item("Wooden Stick", image = R.drawable.wooden_stick, stack = 5), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5),
Item("Bronze Ingot", image = R.drawable.bronze_ore, stack = 10) Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 10)
), "Tools"), ), "Tools"),
Recipe( Recipe(
Item("Wizard Staff", image = R.drawable.wizard_staff, stack = 1),listOf( Item(type = ITEMS.WIZARD_STAFF.itemtype, stack = 1),listOf(
Item("Wooden Stick", image = R.drawable.wooden_stick, stack = 10), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 10),
Item("Monster Eye", image = R.drawable.monster_eyes, stack = 20), Item(type = ITEMS.MONSTER_EYE.itemtype, stack = 20),
), "Tools"), ), "Tools"),
Recipe( Recipe(
Item("Diamond Axe", image = R.drawable.diamond_axe, stack = 1),listOf( Item(type = ITEMS.DIAMOND_AXE.itemtype, stack = 1),listOf(
Item("Wooden Stick", image = R.drawable.wooden_stick, stack = 5), Item(type = ITEMS.WOODEN_STICK.itemtype, stack = 5),
Item("Diamond", image = R.drawable.diamond, stack = 10), Item(type = ITEMS.DIAMOND.itemtype, stack = 10),
), "Tools"), ), "Tools"),
) )
val recipeListBlacksmithing : List<Recipe> = listOf( val recipeListBlacksmithing : List<Recipe> = listOf(
Recipe( Recipe(
Item("Bronze Ingot", image = R.drawable.bronze_ingot, stack = 1),listOf( Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 1),listOf(
Item("Bronze Ore", image = R.drawable.bronze_ore, stack = 5) Item(type = ITEMS.BRONZE_ORE.itemtype, stack = 5)
), "Blacksmithing"), ), "Blacksmithing"),
Recipe( Recipe(
Item("Iron Ingot", image = R.drawable.iron_ingot, stack = 1),listOf( Item(type = ITEMS.IRON_INGOT.itemtype, stack = 1),listOf(
Item("Iron Ore", image = R.drawable.iron_ore, stack = 5) Item(type = ITEMS.IRON_ORE.itemtype, stack = 5)
), "Blacksmithing"), ), "Blacksmithing"),
@ -75,13 +74,13 @@ class RecipeManager {
} }
fun HowManyCraftable(recipe: Recipe, player: Player): Int{ fun HowManyCraftable(recipe: Recipe, player: Player): Int{
var divisedList = mutableListOf<Int>() val divisedList = mutableListOf<Int>()
if(isCraftable(recipe,player)==false) if(isCraftable(recipe,player)==false)
return 0 return 0
else{ else{
for(element in recipe.ingredients){ for(element in recipe.ingredients){
println("cc") println("cc")
val itemSearch = player.items.find { it.name == element.name } val itemSearch = player.items.find { it.type.name == element.type.name }
if(itemSearch!= null) if(itemSearch!= null)
divisedList.add(itemSearch.stack / element.stack) divisedList.add(itemSearch.stack / element.stack)
} }

@ -0,0 +1,34 @@
package com.example.shakecraft.model
import com.example.shakecraft.R
class itemManager {
companion object {
enum class ITEMS(val itemtype: ItemType){
// Craftable items and resources
BEECH_LOG(ItemType(name = "Beech Log", image = R.drawable.log2, rarity = 1, xpReward = 10)),
WOODEN_STICK(ItemType(name = "Wooden Stick", image = R.drawable.wooden_stick, rarity = 1, xpReward = 0)),
WOODEN_PLANK(ItemType(name = "Wooden Plank", image = R.drawable.wooden_plank, rarity = 1, xpReward = 0)),
WOODEN_BALL(ItemType(name = "Wooden Ball", image = R.drawable.wooden_ball, rarity = 1, xpReward = 0)),
WIZARD_STAFF(ItemType(name = "Wizard Staff", image = R.drawable.wizard_staff, rarity = 3, xpReward = 0)),
DIAMOND(ItemType(name = "Diamond", image = R.drawable.diamond, rarity = 3, xpReward = 30)),
DIAMOND_AXE(ItemType(name = "Diamond Axe", image = R.drawable.diamond_axe, rarity = 3, xpReward = 0)),
BRONZE_ORE(ItemType(name = "Bronze Ore", image = R.drawable.bronze_ore, rarity = 2, xpReward = 20)),
BRONZE_INGOT(ItemType(name = "Bronze Ingot", image = R.drawable.bronze_ingot, rarity = 1, xpReward = 0)),
BRONZE_SWORD(ItemType(name = "Bronze Sword", image = R.drawable.bronze_sword, rarity = 2, xpReward = 0)),
IRON_ORE(ItemType(name = "Iron Ore", image = R.drawable.iron_ore, rarity = 2, xpReward = 25)),
IRON_INGOT(ItemType(name = "Iron Ingot", image = R.drawable.iron_ingot, rarity = 1, xpReward = 0)),
// Lootable items
MONSTER_BONES(ItemType(name = "Monster Bones", image = R.drawable.monster_bones, rarity = 1, xpReward = 10)),
MONSTER_EYE(ItemType(name = "Monster Eye", image = R.drawable.monster_eyes, rarity = 2, xpReward = 20)),
TREASURE_KEY(ItemType(name = "Treasure Key", image = R.drawable.treasure_key, rarity = 2, xpReward = 20)),
}
}
}

@ -27,9 +27,9 @@ class AdapterBossLoot(private val possibleLoot: List<Pair<Item, Double>>) : Recy
imageView = view.findViewById(R.id.item_image) imageView = view.findViewById(R.id.item_image)
} }
fun bind(item: Pair<Item, Double>) { fun bind(item: Pair<Item, Double>) {
textView.text = item.first.name textView.text = item.first.type.name
textViewDropRate.text = (item.second*100).toString() + "%" textViewDropRate.text = (item.second*100).toString()
imageView.setImageResource(item.first.image) imageView.setImageResource(item.first.type.image)
} }
} }

@ -27,9 +27,9 @@ class AdapterInventory(private val inventory: List<Item>) : RecyclerView.Adapter
imageView = view.findViewById(R.id.item_image) imageView = view.findViewById(R.id.item_image)
} }
fun bind(item: Item) { fun bind(item: Item) {
textView.text = item.name textView.text = item.type.name
textViewNumber.text = item.stack.toString() textViewNumber.text = item.stack.toString()
imageView.setImageResource(item.image) imageView.setImageResource(item.type.image)
} }
} }

@ -31,12 +31,12 @@ class AdapterMaterials(private val materials: List<Item>, val currentplayer: Pla
imageView = view.findViewById(R.id.item_image) imageView = view.findViewById(R.id.item_image)
} }
fun bind(item: Item, currentplayer: Player) { fun bind(item: Item, currentplayer: Player) {
textView.text = item.name textView.text = item.type.name
val itemSearch = currentplayer.items.find { it.name == item.name } val itemSearch = currentplayer.items.find { it.type.name == item.type.name }
textViewNumberNeeded.text = item.stack.toString() textViewNumberNeeded.text = item.stack.toString()
textViewNumberPlayer.text = if (itemSearch != null) itemSearch.stack.toString() else "0" textViewNumberPlayer.text = if (itemSearch != null) itemSearch.stack.toString() else "0"
imageView.setImageResource(item.image) imageView.setImageResource(item.type.image)
} }
} }

@ -37,8 +37,8 @@ class AdapterRecipe(
} }
} }
fun bind(recipe: Recipe) { fun bind(recipe: Recipe) {
textView.text = recipe.item.name textView.text = recipe.item.type.name
imageView.setImageResource(recipe.item.image) imageView.setImageResource(recipe.item.type.image)
} }
} }

Loading…
Cancel
Save