little arrangements to make the code more kotlin spirit ⚗️
continuous-integration/drone/push Build is passing Details

viewmodel
Lucas Delanier 2 years ago
parent 218e85d571
commit a851991274

@ -55,9 +55,9 @@ class BossFragment() : Fragment() {
initializeViews(view) initializeViews(view)
// Set up boss // Set up boss
if(viewModel.currentBoss == null) if(!viewModel.isBossInitialized)
viewModel.currentBoss = Generator.generateBoss() viewModel.currentBoss = Generator.generateBoss()
setUpBoss(viewModel.currentBoss!!) setUpBoss(viewModel.currentBoss)
// Set up RecyclerView for boss loot // Set up RecyclerView for boss loot
setUpRecyclerView(view) setUpRecyclerView(view)
@ -104,7 +104,7 @@ class BossFragment() : Fragment() {
recyclerView = view.findViewById(R.id.recyclerviewBossLoot) recyclerView = view.findViewById(R.id.recyclerviewBossLoot)
with(recyclerView) { with(recyclerView) {
layoutManager = LinearLayoutManager(view.context) layoutManager = LinearLayoutManager(view.context)
adapter = AdapterBossLoot(viewModel.currentBoss?.possibleLoot!!) adapter = AdapterBossLoot(viewModel.currentBoss.possibleLoot)
} }
} }
private fun setUpActivityOrientation(){ private fun setUpActivityOrientation(){
@ -119,7 +119,7 @@ class BossFragment() : Fragment() {
toastView.visibility = View.VISIBLE toastView.visibility = View.VISIBLE
lootImage.setImageResource(item.type.image) lootImage.setImageResource(item.type.image)
lootName.text = item.type.name lootName.text = item.type.name
xpReward.text = viewModel.currentBoss?.xpReward.toString() xpReward.text = viewModel.currentBoss.xpReward.toString()
toastView.postDelayed({ toastView.postDelayed({
toastView.visibility = View.GONE toastView.visibility = View.GONE
}, 3000) }, 3000)
@ -133,35 +133,35 @@ class BossFragment() : Fragment() {
} }
override fun onSensorChanged(event: SensorEvent?) { override fun onSensorChanged(event: SensorEvent?) {
val acceleration = sqrt(event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2)) val acceleration = sqrt(event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2))
if (viewModel.currentBoss?.life!! <= 0) { if (viewModel.currentBoss.life <= 0) {
//Vibration to signal the death of the boss //Vibration to signal the death of the boss
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100) vibrator.vibrate(100)
// Generate a loot item and XP reward // Generate a loot item and XP reward
val item = Generator.generateLootBoss(viewModel.currentBoss!!.possibleLoot) val item = Generator.generateLootBoss(viewModel.currentBoss.possibleLoot)
currentPlayer.addItem(item) currentPlayer.addItem(item)
currentPlayer.gainXp(viewModel.currentBoss!!.xpReward) currentPlayer.gainXp(viewModel.currentBoss.xpReward)
// Show loot toast view for 3 seconds // Show loot toast view for 3 seconds
displayToast(view,item) displayToast(view,item)
// Spawn new boss and reset progress bar // Spawn new boss and reset progress bar
viewModel.currentBoss = Generator.generateBoss() viewModel.currentBoss = Generator.generateBoss()
setUpBoss(viewModel.currentBoss!!) setUpBoss(viewModel.currentBoss)
//Update displayed information //Update displayed information
setUpRecyclerView(view) setUpRecyclerView(view)
} else { } else {
if(acceleration > 20){ if(acceleration > 20){
viewModel.currentBoss!!.takeDamage(((acceleration / 80)+ currentPlayer.attack()/100).toDouble()) viewModel.currentBoss.takeDamage(((acceleration / 80)+ currentPlayer.attack()/100).toDouble())
} }
else{ else{
viewModel.currentBoss!!.takeDamage(currentPlayer.attack().toDouble()/100) viewModel.currentBoss.takeDamage(currentPlayer.attack().toDouble()/100)
} }
progressBar.progress = viewModel.currentBoss!!.life.toInt() progressBar.progress = viewModel.currentBoss.life.toInt()
} }
} }

@ -28,6 +28,7 @@ class CraftFragment : Fragment() {
private lateinit var buttonForge: Button private lateinit var buttonForge: Button
private lateinit var buttonForgeMax: Button private lateinit var buttonForgeMax: Button
private lateinit var numberCraftable: TextView private lateinit var numberCraftable: TextView
private lateinit var craftValue : TextView
val viewModel : MainViewModel by activityViewModels<MainViewModel>() val viewModel : MainViewModel by activityViewModels<MainViewModel>()
@ -69,6 +70,7 @@ class CraftFragment : Fragment() {
buttonForge = view.findViewById(R.id.buttonForge) buttonForge = view.findViewById(R.id.buttonForge)
buttonForgeMax = view.findViewById(R.id.buttonForgeMax) buttonForgeMax = view.findViewById(R.id.buttonForgeMax)
numberCraftable = view.findViewById(R.id.craftableNumber) numberCraftable = view.findViewById(R.id.craftableNumber)
craftValue = view.findViewById(R.id.craftValue)
buttonBack.setOnClickListener{ buttonBack.setOnClickListener{
findNavController().navigate(R.id.action_craftFragment_to_forgeFragment) findNavController().navigate(R.id.action_craftFragment_to_forgeFragment)
@ -79,8 +81,8 @@ class CraftFragment : Fragment() {
name.text = recipe.item.type.name name.text = recipe.item.type.name
buttonForge.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer) buttonForge.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer)
buttonForgeMax.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer) buttonForgeMax.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer)
buttonForgeMax.text = "CRAFT MAX (${RecipeManager.HowManyCraftable(recipe, currentPlayer)})"
numberCraftable.text = RecipeManager.HowManyCraftable(recipe,currentPlayer).toString() numberCraftable.text = RecipeManager.HowManyCraftable(recipe,currentPlayer).toString()
craftValue.text = recipe.item.stack.toString()
buttonForge.setOnClickListener{ buttonForge.setOnClickListener{
currentPlayer.craft(recipe) currentPlayer.craft(recipe)

@ -84,9 +84,10 @@ class HomeFragment : Fragment() {
progressbar.progress = currentPlayer.xp progressbar.progress = currentPlayer.xp
progressbar.max = currentPlayer.level*100 progressbar.max = currentPlayer.level*100
playermage.setImageResource(currentPlayer.image) playermage.setImageResource(currentPlayer.image)
if(currentPlayer.equipedItem?.type?.image != null) equipeditem.setImageResource( if(currentPlayer.equipedItem?.type?.image != null) {
currentPlayer.equipedItem!!.type.image) equipeditem.setImageResource(currentPlayer.equipedItem!!.type.image)
if(isRaining == true){ }
if(isRaining){
eventFishing.visibility = View.VISIBLE eventFishing.visibility = View.VISIBLE
} }

@ -23,20 +23,13 @@ class InventoryFragment() : Fragment( ), AdapterInventory.OnItemLongClickListene
} }
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
if(viewModel.currentPlayer.items[position] is Tool) { if (viewModel.currentPlayer.items[position] is Tool) {
if (viewModel.currentPlayer.equipeItem(viewModel.currentPlayer.items[position]) == true) val text = if (viewModel.currentPlayer.equipeItem(viewModel.currentPlayer.items[position]) ) " was well equipped" else " has been well unequipped"
Toast.makeText(
Toast.makeText( context,
context, viewModel.currentPlayer.items[position].type.name + text,
viewModel.currentPlayer.items[position].type.name + " was well equipped", Toast.LENGTH_SHORT
Toast.LENGTH_SHORT ).show()
).show()
else
Toast.makeText(
context,
viewModel.currentPlayer.items[position].type.name + " has been well unequipped",
Toast.LENGTH_SHORT
).show()
setUpRecyclerView(view?.parent as ViewGroup, this) setUpRecyclerView(view?.parent as ViewGroup, this)
} }
} }

@ -1,22 +1,16 @@
package com.example.shakecraft.data package com.example.shakecraft.data
import com.example.shakecraft.model.ItemManager.Companion.ITEMS import com.example.shakecraft.model.*
import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Tool
class Stub { class Stub {
fun load() : Player{ fun load() : Player{
val currentPlayer = Player("Winker",0) val currentPlayer = Player("Winker",0)
val items : MutableList<Item> = mutableListOf()
items.add(Item(type = ITEMS.BEECH_LOG.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.addItem(Item(type = ITEMS.BEECH_LOG.itemtype, stack = 30))
currentPlayer.addItem(Item(type = ITEMS.BRONZE_INGOT.itemtype, stack = 30))
currentPlayer.addItem(Tool(type = ITEMS.DIAMOND_AXE.itemtype, stack = 1, damage = 8))
return currentPlayer return currentPlayer
} }

@ -1,6 +1,5 @@
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 {

@ -1,34 +1,31 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import com.example.shakecraft.R import com.example.shakecraft.R
class ItemManager {
companion object {
enum class ITEMS(val itemtype: ItemType){
// Craftable items and resources enum class ITEMS(val itemtype: ItemType){
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)), // Craftable items and resources
DIAMOND_AXE(ItemType(name = "Diamond Axe", image = R.drawable.diamond_axe, rarity = 3, xpReward = 0)), 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)),
BRONZE_ORE(ItemType(name = "Bronze Ore", image = R.drawable.bronze_ore, rarity = 2, xpReward = 20)), DIAMOND(ItemType(name = "Diamond", image = R.drawable.diamond, rarity = 3, xpReward = 30)),
BRONZE_INGOT(ItemType(name = "Bronze Ingot", image = R.drawable.bronze_ingot, rarity = 1, xpReward = 0)), DIAMOND_AXE(ItemType(name = "Diamond Axe", image = R.drawable.diamond_axe, rarity = 3, 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)), BRONZE_ORE(ItemType(name = "Bronze Ore", image = R.drawable.bronze_ore, rarity = 2, xpReward = 20)),
IRON_INGOT(ItemType(name = "Iron Ingot", image = R.drawable.iron_ingot, rarity = 1, xpReward = 0)), 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)),
// 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)),
}
} }
}

@ -4,10 +4,15 @@ import com.example.shakecraft.R
class Player(var pseudo: String, var xp: Int = 0) { class Player(var pseudo: String, var xp: Int = 0) {
var level: Int = 1 var level: Int = 1
private set
val image: Int = R.drawable.player_image val image: Int = R.drawable.player_image
var items: MutableList<Item> = mutableListOf() var items: MutableList<Item> = mutableListOf()
private set
var rank: String = "Beginner" var rank: String = "Beginner"
private set
var equipedItem : Tool? = null var equipedItem : Tool? = null
private set
fun changeRank(){ fun changeRank(){
@ -28,7 +33,6 @@ class Player(var pseudo: String, var xp: Int = 0) {
val findItem = items.find { it.type.name == item.type.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)
findItem.stack += item.stack findItem.stack += item.stack
} }
else{ else{
@ -70,7 +74,7 @@ class Player(var pseudo: String, var xp: Int = 0) {
println("test") println("test")
for (i in 1..count) { for (i in 1..count) {
for (ingredient in recipe.ingredients) { for (ingredient in recipe.ingredients) {
val searchedItem = items.find { it.type.name == ingredient.type.name } val searchedItem = items.find { it.type == ingredient.type }
if (searchedItem != null) { if (searchedItem != null) {
searchedItem.stack -= ingredient.stack searchedItem.stack -= ingredient.stack
if (searchedItem.stack == 0) { if (searchedItem.stack == 0) {
@ -94,4 +98,5 @@ class Player(var pseudo: String, var xp: Int = 0) {
return true return true
} }
} }

@ -1,5 +1,4 @@
package com.example.shakecraft.model package com.example.shakecraft.model
import com.example.shakecraft.model.ItemManager.Companion.ITEMS
class RecipeManager { class RecipeManager {
@ -75,15 +74,14 @@ class RecipeManager {
fun HowManyCraftable(recipe: Recipe, player: Player): Int{ fun HowManyCraftable(recipe: Recipe, player: Player): Int{
val divisedList = mutableListOf<Int>() val divisedList = mutableListOf<Int>()
if(isCraftable(recipe,player)==false) return if (!isCraftable(recipe,player)) 0
return 0
else{ else{
for(element in recipe.ingredients){ for(element in recipe.ingredients){
val itemSearch = player.items.find { it.type.name == element.type.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)
} }
return divisedList.min() divisedList.min()
} }
} }

@ -8,5 +8,6 @@ import com.example.shakecraft.model.Player
class MainViewModel : ViewModel() { class MainViewModel : ViewModel() {
var currentPlayer : Player = Stub().load() var currentPlayer : Player = Stub().load()
var currentBoss : Boss? = null lateinit var currentBoss : Boss
val isBossInitialized get() = this::currentBoss.isInitialized
} }

@ -26,22 +26,22 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical" android:layout_gravity="start|center_vertical"
android:focusable="true" android:focusable="true"
android:drawableStart="@drawable/back" android:text="@string/ancient_forge"
android:drawableTint="@color/blue"
android:text="Ancient Forge"
android:textColor="@color/blue" android:textColor="@color/blue"
android:textSize="16sp" android:textSize="16sp"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
app:drawableStartCompat="@drawable/back"
app:drawableTint="@color/blue" />
<TextView <TextView
android:id="@+id/textView2" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:text="Recipe" android:text="@string/recipe"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="16sp" android:textSize="16sp"
android:textStyle="bold" android:textStyle="bold"
@ -63,6 +63,7 @@
app:layout_constraintTop_toBottomOf="@+id/frameLayout"> app:layout_constraintTop_toBottomOf="@+id/frameLayout">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -71,14 +72,32 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
<ImageView android:layout_width="wrap_content"
android:id="@+id/item_image" android:layout_height="wrap_content">
android:layout_width="50dp"
android:layout_height="50dp" <ImageView
android:background="@drawable/item_recipe_background" android:id="@+id/item_image"
android:contentDescription="image of item" android:layout_width="50dp"
app:srcCompat="@drawable/wooden_stick" /> android:layout_height="50dp"
android:background="@drawable/item_recipe_background"
android:contentDescription="@string/image_of_item"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/wooden_stick" />
<TextView
android:id="@+id/craftValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="1"
android:paddingRight="2dp"
android:textColor="@color/white" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView <TextView
@ -137,8 +156,8 @@
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:layout_marginBottom="5dp" android:layout_marginBottom="5dp"
android:backgroundTint="@color/button_background_color" android:backgroundTint="@color/button_background_color"
android:text="Forge MAX" android:text="FORGE MAX"
android:textColor="@color/grey" android:textColor="@color/white"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/buttonForge" app:layout_constraintBottom_toTopOf="@+id/buttonForge"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"

Loading…
Cancel
Save