Recipe working but with a little issue when add to inventory x2 items :(

SQLite
Lucas Delanier 2 years ago
parent 5cd26ecbac
commit aa6e3afcb0

@ -17,6 +17,7 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false

@ -0,0 +1,88 @@
package com.example.shakecraft
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Recipe
import com.example.shakecraft.model.RecipeManager
import com.example.shakecraft.view.adapter.AdapterMaterials
class CraftFragment : Fragment() {
private lateinit var buttonBack: TextView
private lateinit var recyclerViewMaterials: RecyclerView
private lateinit var recipe: Recipe
private lateinit var image: ImageView
private lateinit var name: TextView
private lateinit var buttonForge: Button
private lateinit var numberCraftable: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val currentPlayer = (activity as MainActivity).currentPlayer
val view = inflater.inflate(R.layout.fragment_craft, container, false)
recipe = arguments?.getParcelable("recipe")!!
initializeViews(view, currentPlayer)
setUpRecyclerView(view,currentPlayer)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
private fun setUpRecyclerView(view: View, currentPlayer: Player) {
recyclerViewMaterials = view.findViewById(R.id.RecyclerviewMaterials)
with(recyclerViewMaterials) {
layoutManager = LinearLayoutManager(view.context)
if(recipe.type == "Objects")
adapter = AdapterMaterials(recipe.ingredients, currentPlayer)
}
}
private fun initializeViews(view: View, currentPlayer: Player) {
buttonBack = view.findViewById(R.id.backbutton)
buttonForge = view.findViewById(R.id.buttonForge)
numberCraftable = view.findViewById(R.id.craftableNumber)
buttonBack.setOnClickListener{
findNavController().navigate(R.id.action_craftFragment_to_forgeFragment)
}
image = view.findViewById(R.id.item_image)
name = view.findViewById(R.id.item_name)
image.setImageResource(recipe.item.image)
name.text = recipe.item.name
buttonForge.isEnabled = RecipeManager.isCraftable(recipe,currentPlayer)
numberCraftable.text = RecipeManager.HowManyCraftable(recipe,currentPlayer).toString()
buttonForge.setOnClickListener{
currentPlayer.craft(recipe)
}
}
}

@ -1,28 +1,46 @@
package com.example.shakecraft
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.model.Recipe
import com.example.shakecraft.model.RecipeManager
import com.example.shakecraft.view.adapter.AdapterRecipe
class ForgeFragment : Fragment() {
class ForgeFragment : Fragment(), AdapterRecipe.OnItemClickListener {
private lateinit var buttonBack: TextView
private lateinit var recyclerViewObjects: RecyclerView
private lateinit var recyclerViewTools: RecyclerView
private lateinit var recyclerViewBlacksmithing: RecyclerView
private lateinit var recipe: Recipe
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onItemClick(position: Int, type: String) {
recipe = when(type){
"Objects" -> RecipeManager.recipeListObjects[position]
"Tools" -> RecipeManager.recipeListTools[position]
"Blacksmithing" -> RecipeManager.recipeListBlacksmithing[position]
else -> {RecipeManager.recipeListObjects[position]}
}
val bundle = bundleOf("recipe" to recipe)
findNavController().navigate(R.id.action_forgeFragment_to_craftFragment, bundle)
}
private fun initializeViews(view: View) {
buttonBack = view.findViewById<TextView>(R.id.backbutton)
buttonBack.setOnClickListener{
@ -39,26 +57,26 @@ class ForgeFragment : Fragment() {
// Initialize views
initializeViews(view)
setUpRecyclerView(view)
setUpRecyclerView(view, this)
return view
}
private fun setUpRecyclerView(view: View) {
private fun setUpRecyclerView(view: View, listener: AdapterRecipe.OnItemClickListener) {
recyclerViewObjects = view.findViewById(R.id.RecyclerviewObjects)
with(recyclerViewObjects) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterRecipe(RecipeManager.recipeListObjects)
adapter = AdapterRecipe(RecipeManager.recipeListObjects, listener)
}
recyclerViewTools = view.findViewById(R.id.RecyclerviewTools)
with(recyclerViewTools) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterRecipe(RecipeManager.recipeListTools)
adapter = AdapterRecipe(RecipeManager.recipeListTools, listener)
}
recyclerViewBlacksmithing = view.findViewById(R.id.RecyclerviewBlacksmithing)
with(recyclerViewBlacksmithing) {
layoutManager = LinearLayoutManager(view.context)
adapter = AdapterRecipe(RecipeManager.recipeListBlacksmithing)
adapter = AdapterRecipe(RecipeManager.recipeListBlacksmithing, listener)
}
}

@ -11,13 +11,13 @@ import androidx.core.view.WindowInsetsControllerCompat
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.shakecraft.model.Player
import com.example.shakecraft.model.Recipe
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
var currentPlayer = Player("Winker",0)
@RequiresApi(Build.VERSION_CODES.R)
private fun hideSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, false)
@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() {
hideSystemUI()
setContentView(R.layout.activity_main)
bottomNav = findViewById(R.id.bottomNavigationView)
val navController = findNavController(R.id.fragment)
bottomNav.setupWithNavController(navController)
@ -55,4 +56,5 @@ class MainActivity : AppCompatActivity() {
}

@ -1,12 +1,46 @@
package com.example.shakecraft.model
import android.os.Parcel
import android.os.Parcelable
data class Item(
class Item(
var name: String,
var rarity: Int = 1,
var stack: Int = 1,
var image: Int,
var xpReward: Int = 0,
)
) : Parcelable{
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(name)
parcel.writeInt(rarity)
parcel.writeInt(stack)
parcel.writeInt(image)
parcel.writeInt(xpReward)
}
companion object CREATOR : Parcelable.Creator<Item> {
override fun createFromParcel(parcel: Parcel): Item {
return Item(
parcel.readString()!!,
parcel.readInt(),
parcel.readInt(),
parcel.readInt(),
parcel.readInt()
)
}
override fun newArray(size: Int): Array<Item?> {
return arrayOfNulls(size)
}
}
}

@ -44,8 +44,10 @@ class Player(val pseudo: String, var xp: Int = 0) {
}
fun addItem(item: Item) {
val findItem = items.find { it.name == item.name }
if(findItem!= null){
findItem.stack += 1
println("findItem n: "+findItem.stack+" item nb:"+item.stack)
findItem.stack += item.stack
}
else{items.add(item)}
}
@ -73,19 +75,19 @@ class Player(val pseudo: String, var xp: Int = 0) {
}
fun craft(recipe: Recipe) : Boolean{
println("test")
for (ingredient in recipe.ingredients) {
val searchedItem = items.find { it.name == recipe.item.name }
val searchedItem = items.find { it.name == ingredient.name }
if(searchedItem != null) {
searchedItem.stack -= recipe.item.stack
searchedItem.stack -= ingredient.stack
if (searchedItem.stack == 0){
items.remove(searchedItem)
}
return true
}
else
return false
}
return false
println("item:"+recipe.item.stack)
addItem(recipe.item)
return true
}
}

@ -1,4 +1,29 @@
package com.example.shakecraft.model
class Recipe(val item: Item, val ingredients : List<Item>, val type: String) {
import android.os.Parcel
import android.os.Parcelable
class Recipe(val item: Item, val ingredients : List<Item>, val type: String) : Parcelable {
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(item, flags)
parcel.writeTypedList(ingredients)
parcel.writeString(type)
}
companion object CREATOR : Parcelable.Creator<Recipe> {
override fun createFromParcel(parcel: Parcel): Recipe {
return Recipe(
parcel.readParcelable(Item::class.java.classLoader)!!,
parcel.createTypedArrayList(Item.CREATOR)!!,
parcel.readString()!!
)
}
override fun newArray(size: Int): Array<Recipe?> {
return arrayOfNulls(size)
}
}
}

@ -74,6 +74,22 @@ class RecipeManager {
return true
}
fun HowManyCraftable(recipe: Recipe, player: Player): Int{
var divisedList = mutableListOf<Int>()
if(isCraftable(recipe,player)==false)
return 0
else{
for(element in recipe.ingredients){
println("cc")
val itemSearch = player.items.find { it.name == element.name }
if(itemSearch!= null)
divisedList.add(itemSearch.stack / element.stack)
}
return divisedList.min()
}
}
}
}

@ -0,0 +1,61 @@
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
import com.example.shakecraft.R
import com.example.shakecraft.model.Item
import com.example.shakecraft.model.Player
import com.example.shakecraft.view.viewholder.ViewHolderInventory
class AdapterMaterials(private val materials: List<Item>, val currentplayer: Player) : RecyclerView.Adapter<AdapterMaterials.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
val textViewNumberNeeded: TextView
val textViewNumberPlayer: TextView
var imageView: ImageView
init {
// Define click listener for the ViewHolder's View
textView = view.findViewById(R.id.item_name)
textViewNumberPlayer = view.findViewById(R.id.numberCurrentPlayer)
textViewNumberNeeded = view.findViewById(R.id.numberNeeded)
imageView = view.findViewById(R.id.item_image)
}
fun bind(item: Item, currentplayer: Player) {
textView.text = item.name
val itemSearch = currentplayer.items.find { it.name == item.name }
textViewNumberNeeded.text = item.stack.toString()
textViewNumberPlayer.text = if (itemSearch != null) itemSearch.stack.toString() else "0"
imageView.setImageResource(item.image)
}
}
override fun getItemCount() = materials.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.list_craft, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val item : Item = materials[position]
viewHolder.bind(item, currentplayer)
}
}

@ -3,33 +3,51 @@ package com.example.shakecraft.view.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView.OnItemClickListener
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.MainActivity
import com.example.shakecraft.R
import com.example.shakecraft.model.Recipe
class AdapterRecipe(private val recipelist: List<Recipe>) : RecyclerView.Adapter<AdapterRecipe.ViewHolder>() {
class AdapterRecipe(
private val recipelist: List<Recipe>,
private val listener: OnItemClickListener
) :
RecyclerView.Adapter<AdapterRecipe.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
val textView: TextView
var imageView: ImageView
init {
// Define click listener for the ViewHolder's View
itemView.setOnClickListener(this)
textView = view.findViewById(R.id.item_name)
imageView = view.findViewById(R.id.item_image)
}
override fun onClick(v: View?) {
val position = adapterPosition
val type = recipelist.first().type
if(position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, type)
}
}
fun bind(recipe: Recipe) {
textView.text = recipe.item.name
imageView.setImageResource(recipe.item.image)
}
}
interface OnItemClickListener{
fun onItemClick(position: Int, type: String)
}
override fun getItemCount() = recipelist.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--When the button is not enabled-->
<item android:state_enabled="false" android:color="@color/grey" />
<!--When button is enabled-->
<item android:state_enabled="true" android:color="@color/blue" />
<!--Default Text Color-->
<item android:color="#db402c" />
</selector>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/grey_100" />
</shape>
</item>
<item android:top="1dp" android:bottom="1dp" >
<shape android:shape="rectangle">
<solid android:color="@color/black_800" />
</shape>
</item>
</layer-list>

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/blue" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>

@ -0,0 +1,175 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
android:background="@color/black_800"
android:scaleType="center"
tools:context=".CollectFragment">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="50dp"
android:paddingBottom="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="20dp">
<TextView
android:id="@+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:focusable="true"
android:drawableStart="@drawable/back"
android:drawableTint="@color/blue"
android:text="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" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Recipe"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/recipelistcard"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frameLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="start"
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" />
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/white"
android:textStyle="bold"
tools:text="Wooden Stick" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/recipelistcard"
android:orientation="vertical"
android:paddingBottom="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:text="Materials"
android:textColor="@color/grey_400"
android:textSize="17dp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RecyclerviewMaterials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:overScrollMode="never"
tools:itemCount="3"
tools:listitem="@layout/list_craft" />
</LinearLayout>
<Button
android:id="@+id/buttonForge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginBottom="10dp"
android:text="Forge"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:backgroundTint="@color/button_background_color"
android:textColor="@color/white"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/buttonForge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="@drawable/craftable_background"
android:paddingVertical="5dp">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Craftable "
android:textColor="@color/grey_400"/>
<TextView
android:id="@+id/craftableNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="804"
android:textColor="@color/white"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,79 @@
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:contentDescription="image of item"
android:background="@drawable/item_background"
android:id="@+id/item_image"
android:layout_width="40dp"
android:layout_height="40dp"
app:srcCompat="@drawable/log2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/item_name"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textStyle="bold"
tools:text="Beech Log"/>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/numberCurrentPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textStyle="bold"
tools:text="804"/>
<TextView
android:id="@+id/item_stock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey_300"
android:text="/"/>
<TextView
android:id="@+id/numberNeeded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey_300"
tools:text="1"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

@ -67,5 +67,20 @@
app:enterAnim="@android:anim/fade_in"
app:popUpTo="@id/homeFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_forgeFragment_to_craftFragment"
app:destination="@id/craftFragment" />
</fragment>
<fragment
android:id="@+id/craftFragment"
android:name="com.example.shakecraft.CraftFragment"
android:label="CraftFragment" >
<action
android:id="@+id/action_craftFragment_to_forgeFragment"
app:destination="@id/forgeFragment"
app:enterAnim="@android:anim/fade_in" />
<argument
android:name="recipe"
app:argType="com.example.shakecraft.model.Recipe" />
</fragment>
</navigation>

@ -6,9 +6,9 @@
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<item name="colorSecondary">@color/blue</item>
<item name="colorSecondaryVariant">@color/purple_700</item>
<item name="colorOnSecondary">@color/white</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="21">?attr/colorPrimary</item>
<!-- Customize your theme here. -->

Loading…
Cancel
Save