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)
// Set up boss
if(viewModel.currentBoss == null)
if(!viewModel.isBossInitialized)
viewModel.currentBoss = Generator.generateBoss()
setUpBoss(viewModel.currentBoss!!)
setUpBoss(viewModel.currentBoss)
// Set up RecyclerView for boss loot
setUpRecyclerView(view)
@ -104,7 +104,7 @@ class BossFragment() : Fragment() {
recyclerView = view.findViewById(R.id.recyclerviewBossLoot)
with(recyclerView) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterBossLoot(viewModel.currentBoss?.possibleLoot!!)
adapter = AdapterBossLoot(viewModel.currentBoss.possibleLoot)
}
}
private fun setUpActivityOrientation(){
@ -119,7 +119,7 @@ class BossFragment() : Fragment() {
toastView.visibility = View.VISIBLE
lootImage.setImageResource(item.type.image)
lootName.text = item.type.name
xpReward.text = viewModel.currentBoss?.xpReward.toString()
xpReward.text = viewModel.currentBoss.xpReward.toString()
toastView.postDelayed({
toastView.visibility = View.GONE
}, 3000)
@ -133,35 +133,35 @@ class BossFragment() : Fragment() {
}
override fun onSensorChanged(event: SensorEvent?) {
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
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100)
// 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.gainXp(viewModel.currentBoss!!.xpReward)
currentPlayer.gainXp(viewModel.currentBoss.xpReward)
// Show loot toast view for 3 seconds
displayToast(view,item)
// Spawn new boss and reset progress bar
viewModel.currentBoss = Generator.generateBoss()
setUpBoss(viewModel.currentBoss!!)
setUpBoss(viewModel.currentBoss)
//Update displayed information
setUpRecyclerView(view)
} else {
if(acceleration > 20){
viewModel.currentBoss!!.takeDamage(((acceleration / 80)+ currentPlayer.attack()/100).toDouble())
viewModel.currentBoss.takeDamage(((acceleration / 80)+ currentPlayer.attack()/100).toDouble())
}
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 buttonForgeMax: Button
private lateinit var numberCraftable: TextView
private lateinit var craftValue : TextView
val viewModel : MainViewModel by activityViewModels<MainViewModel>()
@ -69,6 +70,7 @@ class CraftFragment : Fragment() {
buttonForge = view.findViewById(R.id.buttonForge)
buttonForgeMax = view.findViewById(R.id.buttonForgeMax)
numberCraftable = view.findViewById(R.id.craftableNumber)
craftValue = view.findViewById(R.id.craftValue)
buttonBack.setOnClickListener{
findNavController().navigate(R.id.action_craftFragment_to_forgeFragment)
@ -79,8 +81,8 @@ class CraftFragment : Fragment() {
name.text = recipe.item.type.name
buttonForge.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()
craftValue.text = recipe.item.stack.toString()
buttonForge.setOnClickListener{
currentPlayer.craft(recipe)

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

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

@ -1,22 +1,16 @@
package com.example.shakecraft.data
import com.example.shakecraft.model.ItemManager.Companion.ITEMS
import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Tool
import com.example.shakecraft.model.*
class Stub {
fun load() : Player{
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
}

@ -1,6 +1,5 @@
package com.example.shakecraft.model
import com.example.shakecraft.R
import com.example.shakecraft.model.ItemManager.Companion.ITEMS
import kotlin.random.Random
class Generator {

@ -1,34 +1,31 @@
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)),
enum class ITEMS(val itemtype: ItemType){
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)),
// 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)),
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)),
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)),
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)),
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)),
// 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) {
var level: Int = 1
private set
val image: Int = R.drawable.player_image
var items: MutableList<Item> = mutableListOf()
private set
var rank: String = "Beginner"
private set
var equipedItem : Tool? = null
private set
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 }
if(findItem!= null){
println("findItem n: "+findItem.stack+" item nb:"+item.stack)
findItem.stack += item.stack
}
else{
@ -70,7 +74,7 @@ class Player(var pseudo: String, var xp: Int = 0) {
println("test")
for (i in 1..count) {
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) {
searchedItem.stack -= ingredient.stack
if (searchedItem.stack == 0) {
@ -94,4 +98,5 @@ class Player(var pseudo: String, var xp: Int = 0) {
return true
}
}

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

@ -8,5 +8,6 @@ import com.example.shakecraft.model.Player
class MainViewModel : ViewModel() {
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_gravity="start|center_vertical"
android:focusable="true"
android:drawableStart="@drawable/back"
android:drawableTint="@color/blue"
android:text="Ancient Forge"
android:text="@string/ancient_forge"
android:textColor="@color/blue"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="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
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Recipe"
android:text="@string/recipe"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
@ -63,6 +63,7 @@
app:layout_constraintTop_toBottomOf="@+id/frameLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -71,14 +72,32 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/item_recipe_background"
android:contentDescription="image of item"
app:srcCompat="@drawable/wooden_stick" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
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
@ -137,8 +156,8 @@
android:layout_marginHorizontal="10dp"
android:layout_marginBottom="5dp"
android:backgroundTint="@color/button_background_color"
android:text="Forge MAX"
android:textColor="@color/grey"
android:text="FORGE MAX"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/buttonForge"
app:layout_constraintEnd_toEndOf="parent"

Loading…
Cancel
Save