//RETURNS THE INDEX OF THE ITEM IN THE LIST IF IT EXISTS ELSE -1 public int ItemExistCraft(CraftItemController item, List <CraftItemController> list) { for (int i = 0; i < list.Count; i++) { if (item.name == list[i].name) { return(i); } } return(-1); }
////CRAFT FUNCTIONS//////////////////////CRAFT FUNCTIONS////////////////////////////////CRAFT FUNCTIONS /// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////CRAFT FUNCTIONS//////////////////CRAFT FUNCTIONS///////////////////CRAFT FUNCTIONS////////////////////////////////// /// /////////////////////////////////CRAFT FUNCTIONS//////////////////////CRAFT FUNCTIONS/////////////////CRAFT FUNCTIONS //THIS IS EXECUTED WHEN THE EVENT IS A DOUBLE CLICK //SWITCH ITEM BETWEEN PANELS public void switchPanel(CraftItemController item, GameObject otherPanel) { if (otherPanel.name == "Craft Panel") //SWITCH FROM ITEM PANEL TO CRAFT PANEL { int i = ItemExistCraft(item, CraftitemList); Debug.Log("switch index" + i); if (i == -1) { Debug.Log("NEW ITEM"); item.count = 1; item.countTEXT = CraftController._instance.selectedItem.Find("count").GetComponent <Text> (); CraftController._instance.selectedItem.Find("count").GetComponent <Text> ().text = item.count.ToString(); item.countTEXT.text = item.count.ToString(); CraftitemList.Add(item); } else { Debug.Log("REPLACE"); item.countTEXT = CraftController._instance.selectedItem.Find("count").GetComponent <Text> (); CraftController._instance.selectedItem.Find("count").GetComponent <Text> ().text = item.count.ToString(); item.countTEXT.text = item.count.ToString(); replaceitemCraft(i, item, CraftitemList); } Debug.Log("before remove itemCount is " + itemPanelList.Count); removeItemCraft(item, itemPanelList); } else //SWITCH FROM CRAFT PANEL TO ITEM PANEL { int i = ItemExistCraft(item, itemPanelList); Debug.Log("switch index" + i); if (i == -1) { Debug.Log("NEW ITEM"); item.count = 1; item.countTEXT = CraftController._instance.selectedItem.Find("count").GetComponent <Text> (); CraftController._instance.selectedItem.Find("count").GetComponent <Text> ().text = item.count.ToString(); item.countTEXT.text = item.count.ToString(); itemPanelList.Add(item); } else { Debug.Log("REPLACE"); item.countTEXT = CraftController._instance.selectedItem.Find("count").GetComponent <Text> (); CraftController._instance.selectedItem.Find("count").GetComponent <Text> ().text = item.count.ToString(); item.countTEXT.text = item.count.ToString(); replaceitemCraft(i, item, itemPanelList); } removeItemCraft(item, CraftitemList); } //PANEL MAJ CraftController._instance.selectedItem.transform.parent.parent.gameObject.GetComponent <CraftController> ().createInventory(); CraftController._instance.selectedItem.transform.parent.parent.gameObject. GetComponent <CraftController> ().otherPanel.GetComponent <CraftController> ().createInventory(); }
//CONVERT ITEM TO CRAFTITEM public item convertToItem(CraftItemController myitem) { item newitem1 = new item(); newitem1.name = myitem.name; newitem1.sprite = myitem.sprite; newitem1.countTEXT = myitem.countTEXT; newitem1.count = myitem.count; newitem1.mytype = myitem.mytype; newitem1.coords = myitem.coords; return(newitem1); }
//CONVERT A CRAFTITEM TO ITEM public CraftItemController convertToCraft(item myitem) { CraftItemController newitem1 = new CraftItemController(); newitem1.name = myitem.name; newitem1.sprite = myitem.sprite; newitem1.countTEXT = myitem.countTEXT; newitem1.count = myitem.count; newitem1.coords = myitem.coords; newitem1.mytype = myitem.mytype; return(newitem1); }
//INCREASE THE COUNT OF AN ITEM BY 1 //CREATE A NEW ITEM AND REPLACE IT WITH THE OLD ONE public void replaceitemCraft(int i, CraftItemController item, List <CraftItemController> list) { CraftItemController newitem = new CraftItemController(); newitem.name = list [i].name; newitem.sprite = list [i].sprite; newitem.count = list [i].count + 1; newitem.countTEXT = CraftController._instance.selectedItem.transform.GetComponent <CraftItemController> ().countTEXT; newitem.countTEXT.text = newitem.count.ToString(); newitem.coords = list [i].coords; newitem.mytype = list [i].mytype; list.RemoveAt(i); list.Add(newitem); }
//REMOVE THE ITEM FROM THE LIST IF THERE IS ONLY ONE OF IT .. ELSE THE COUNT VARIABLE DECREASES BY ONE public void removeItemCraft(CraftItemController item, List <CraftItemController> list) { int j = ItemExistCraft(item, list); if (list[j].count == 1) { list.RemoveAt(j); CraftController._instance.createInventory(); } else { list[j].count--; CraftController._instance.createInventory(); } }
//THIS FUNCTION DISPLAYS THE COMPONENTS OF THE CLICKED COMPOUND public void displayComponents(CraftItemController compound) { for (int i = 0; i < CompoundList.Count; i++) { if (compound.name == CompoundList [i].name) { CraftitemList.Clear(); for (int j = 0; j < CompoundList [i].mycomponents.Count; j++) { CraftitemList.Add(CompoundList [i].mycomponents [j]); } break; } } CraftController._instance.createInventory(); CraftController._instance.otherPanel.GetComponent <CraftController> ().createInventory(); }
void OnGUI() { Event e = Event.current; //THE EVENT IS A MOUSE EVENT AND WE HAVE A DOUBLE CLICK if (e.isMouse && e.type == EventType.MouseDown && e.clickCount == 2 && CraftController._instance.selectedItem.gameObject == gameObject) { CraftItemController item1 = new CraftItemController(); item1.name = CraftController._instance.selectedItem.GetComponent <CraftItemController>().name; item1.sprite = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().sprite; item1.coords = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().coords; item1.count = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().count; item1.countTEXT = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().countTEXT; item1.mytype = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().mytype; if (item1.mytype.Equals("composant")) //WHEN I CLICK ON A COMPONENT //OTHERPANEL IS THE PANEL THAT ITEM WILL GO TO { otherPanel = CraftController._instance.selectedItem.transform.parent.parent.gameObject.GetComponent <CraftController> ().otherPanel; //SWITCH THE ITEM GameBD._insctance.switchPanel(item1, otherPanel); //CHECK IS THERE A MATCH BETWEEN MY COMPOUNDS AND MY COMPONENTS int i = GameBD._insctance.IsAMatch(); //IF THERE IS ONE "i" IS THE INDEX OF THE COMPOUND THAT WE MATCHED ELSE IT'S -1 //IF THERE IS A MATCH !! if (i != -1) { string myname = GameBD.CompoundList [i].name; //GET THE COMPOUND FROM THE DATABASE CraftItemController item = GameBD._insctance.convertToCraft(GameBD._insctance.FindItem(myname)); //ASSIGN THE COMPOUND ( sprite , type , name ) TO THE COMPOUND SLOT CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <SpriteRenderer> ().sprite = item.sprite; CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <CraftItemController> ().mytype = item.mytype; CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <CraftItemController> ().name = item.name; } else { // THERE IS NO MATCH "i" == -1 //DELETE THE COMPOUND IF IT'S THERE Debug.Log("delete craft item"); CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <SpriteRenderer> ().sprite = null; CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <CraftItemController> ().mytype = null; } } else //WHEN I CLICK ON A COMPOUND { if (CraftController._instance.selectedItem.transform.parent.parent.gameObject.GetComponent <CraftController> (). selectedPanel.name == "item_panel") { CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <SpriteRenderer> ().sprite = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().sprite; CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <CraftItemController> ().mytype = CraftController._instance.selectedItem.GetComponent <CraftItemController> ().mytype; // NAME HERE WILL BE NULL TO TEST EITHER WE WANT TO HAVE THE COMPONENTS OR THE COMPOUND WHEN WE VALIDATE THE CRAFT ACTION CraftController._instance.compoundSlot.transform.Find("compound"). GetComponent <CraftItemController> ().name = null; GameBD._insctance.displayComponents(item1); GameBD._insctance.removeItemCraft(item1, GameBD.itemPanelList); } } } }
// public Transform getSelectedItem(){ // return selectedItem; // } public void createInventory() // CREATE ITEMPANEL AND CRAFTPANEL { foreach (Transform t in this.transform) //DESTROY THE OLD PANELS { Destroy(t.gameObject); } for (int i = 1; i <= inventorySize.x; i++) //CREATE SLOTS FOR THE ITEMPANEL AND CRAFTPANEL { for (int j = 1; j <= inventorySize.y; j++) { GameObject slot = Instantiate(craft_slot) as GameObject; slot.transform.SetParent(this.transform, false); slot.name = "slot_" + j + "_" + i; slot.GetComponent <RectTransform> ().anchoredPosition = new Vector3((windowSize.x) / (inventorySize.x) * i, (windowSize.y) / (inventorySize.y) * -j, 1); slot.GetComponent <CraftSlotContoller> ().coords = new Vector2(i, j); if (otherPanel.name == "Craft Panel") //CREATE ITEMS FOR THE ITEMPANEL { if (i + (j - 1) * 3 <= GameBD.itemPanelList.Count) { GameObject item = Instantiate(craft_item) as GameObject; item.transform.SetParent(slot.transform); CraftItemController newitem = item.GetComponent <CraftItemController> (); newitem.name = GameBD.itemPanelList [(i + (j - 1) * 3) - 1].name; newitem.count = GameBD.itemPanelList [(i + (j - 1) * 3) - 1].count; newitem.mytype = GameBD.itemPanelList [(i + (j - 1) * 3) - 1].mytype; newitem.countTEXT = item.transform.Find("count").GetComponent <Text> (); newitem.countTEXT.text = newitem.count.ToString(); newitem.sprite = GameBD.itemPanelList [(i + (j - 1) * 3) - 1].sprite; item.name = newitem.name; item.GetComponent <RectTransform> ().anchoredPosition = Vector3.zero; item.GetComponent <RectTransform> ().localScale = new Vector3(23, 23, 1); item.GetComponent <SpriteRenderer> ().sprite = newitem.sprite; if (item.GetComponent <CraftItemController> ().coords == Vector2.zero) { item.GetComponent <CraftItemController> ().coords = slot.GetComponent <CraftSlotContoller> ().coords; } } } else //CREATE ITEMS FOR THE CRAFTPANEL { if (i + (j - 1) * 3 <= GameBD.CraftitemList.Count) { GameObject item = Instantiate(craft_item) as GameObject; item.transform.SetParent(slot.transform); CraftItemController newitem = item.GetComponent <CraftItemController> (); newitem.name = GameBD.CraftitemList [(i + (j - 1) * 3) - 1].name; newitem.count = GameBD.CraftitemList [(i + (j - 1) * 3) - 1].count; newitem.mytype = GameBD.CraftitemList [(i + (j - 1) * 3) - 1].mytype; newitem.countTEXT = item.transform.Find("count").GetComponent <Text> (); newitem.countTEXT.text = newitem.count.ToString(); newitem.sprite = GameBD.CraftitemList [(i + (j - 1) * 3) - 1].sprite; item.name = newitem.name; item.GetComponent <RectTransform> ().anchoredPosition = Vector3.zero; item.GetComponent <RectTransform> ().localScale = new Vector3(23, 23, 1); item.GetComponent <SpriteRenderer> ().sprite = newitem.sprite; if (item.GetComponent <CraftItemController> ().coords == Vector2.zero) { item.GetComponent <CraftItemController> ().coords = slot.GetComponent <CraftSlotContoller> ().coords; } } } } } }