//Creates a flower from the given full index public void createFlowerFromIndex(uint flowerIndex) { petalIndex = flowerProperties.getPetalIndexFromIndex(flowerIndex); pistilIndex = flowerProperties.getPistilIndexFromIndex(flowerIndex); stemIndex = flowerProperties.getStemIndexFromIndex(flowerIndex); leafIndex = flowerProperties.getLeafIndexFromIndex(flowerIndex); petalColorIndex = flowerProperties.getPetalColorIndexFromIndex(flowerIndex); pistilColorIndex = flowerProperties.getPistilColorIndexFromIndex(flowerIndex); stemColorIndex = flowerProperties.getStemColorIndexFromIndex(flowerIndex); leafColorIndex = flowerProperties.getLeafColorIndexFromIndex(flowerIndex); rebuildFlower(); recolorFlower(); }
//Builds a flower with the given index private void buildFlower(uint inIndex) { FlowerProperties flowerProperties = GameObject.Find("World").GetComponent <FlowerProperties>(); //Set the indexes int stemIndex = flowerProperties.getStemIndexFromIndex(inIndex); int petalIndex = flowerProperties.getPetalIndexFromIndex(inIndex); int pistilIndex = flowerProperties.getPistilIndexFromIndex(inIndex); int leafIndex = flowerProperties.getLeafIndexFromIndex(inIndex); //Set the colors int stemColor = flowerProperties.getStemColorIndexFromIndex(inIndex); int leafColor = flowerProperties.getLeafColorIndexFromIndex(inIndex); int petalColor = flowerProperties.getPetalColorIndexFromIndex(inIndex); int pistilColor = flowerProperties.getPistilColorIndexFromIndex(inIndex); //Destroy the flower before building a new one //Destroy the stem that destroys everything if (rootObject != null) { GameObject.Destroy(rootObject); rootObject = null; } //Instantiate the given stem object rootObject = Instantiate(flowerProperties.flowerStem[stemIndex], transform.GetChild(0)); rootObject.layer = LayerMask.NameToLayer("UI"); Material stemMat = flowerProperties.stemMaterial; rootObject.GetComponent <MeshRenderer>().material = stemMat; rootObject.GetComponent <MeshRenderer>().material.color = flowerProperties.flowerColors[stemColor]; //Add leaves and petal to the stem bool petalHasBeenFound = false; GameObject petalObject = null; foreach (Transform child in rootObject.transform) { if (!petalHasBeenFound && child.gameObject.name.ToString().Equals("spetal")) { //It's the petal so it has been found petalHasBeenFound = true; //Now spawn it petalObject = Instantiate(flowerProperties.flowerPetal[petalIndex], child); petalObject.layer = LayerMask.NameToLayer("UI"); //Color the material and apply it Material petalMat = flowerProperties.petalMaterial; petalObject.GetComponent <MeshRenderer>().material = petalMat; petalObject.GetComponent <MeshRenderer>().material.color = flowerProperties.flowerColors[petalColor];; } else { //It's a leaf so spawn it GameObject leaf = Instantiate(flowerProperties.flowerLeaf[leafIndex], child); leaf.layer = LayerMask.NameToLayer("UI"); //Color the material and apply it Material leafMat = flowerProperties.leafMaterial; leaf.GetComponent <MeshRenderer>().material = leafMat; leaf.GetComponent <MeshRenderer>().material.color = flowerProperties.flowerColors[leafColor]; } } if (petalObject != null) { //Add pistil to the petal GameObject pistilObject = Instantiate(flowerProperties.flowerPistil[pistilIndex], petalObject.transform.GetChild(0)); pistilObject.layer = LayerMask.NameToLayer("UI"); //Color the material and apply it Material pistilMat = flowerProperties.pistilMaterial; pistilObject.GetComponent <MeshRenderer>().material = pistilMat; pistilObject.GetComponent <MeshRenderer>().material.color = flowerProperties.flowerColors[pistilColor]; } //Now add the root object to the ui rootObject.transform.parent = gameObject.transform.GetChild(0); rootObject.transform.localPosition = Vector3.zero; rootObject.transform.localScale = Vector3.one * 80; //Give it a random rotation rootObject.transform.Rotate(0, Random.Range(0, 6.28f), 0); }
/// <summary> /// Syncs the player inventory with the flowers in the player inventory home base panel /// </summary> private void PlayerInventorySync() { FlowerProperties fp = GameObject.Find("World").GetComponent <FlowerProperties>(); UIControllerScript uic = otherCanvas.GetComponent <UIControllerScript>(); //Now fill the other canvas and the UI inventory with the right items //Loop through the entire inventory for (int ii = 0; ii < fpc.inventorySize; ii++) { MenuSlot hInventoryItem = playerInventoryContainer.GetChild(ii).GetComponent <MenuSlot>(); Placeable slotPlaceable = uic.getInventoryDataAtSlot(ii); uint currentSlotID = 0; bool currentSlotIsFlower = false; if (slotPlaceable != null) { currentSlotID = slotPlaceable.id; currentSlotIsFlower = slotPlaceable.isFlower; } //If there's an item in the home base player inventory if (hInventoryItem.getItem() != null) { uic.inventoryClearSlot(ii); GameObject newObj; //Create a placeable based on the slot's id if (hInventoryItem.getItem().isFlower) { //Make a flower newObj = Instantiate(Resources.Load("Prefabs/FlowerPrefab", typeof(GameObject)) as GameObject); FlowerObj p = newObj.GetComponent <FlowerObj>(); p.id = hInventoryItem.getItem().id; int[] petal = new int[2] { fp.getPetalIndexFromIndex(p.id), fp.getPetalColorIndexFromIndex(p.id) }; int[] pistil = new int[2] { fp.getPistilIndexFromIndex(p.id), fp.getPistilColorIndexFromIndex(p.id) }; int[] leaf = new int[2] { fp.getLeafIndexFromIndex(p.id), fp.getLeafColorIndexFromIndex(p.id) }; int[] stem = new int[2] { fp.getStemIndexFromIndex(p.id), fp.getStemColorIndexFromIndex(p.id) }; int[] position = new int[2] { -1, -1 }; p.init(petal, stem, pistil, leaf, position, transform.root); p.alive = true; uic.setSlot(ii, p); uic.setSlotCount(ii, hInventoryItem.getItem().count); Destroy(newObj); } else { //Make a decoration newObj = Instantiate(fp.getDecorationObject(hInventoryItem.getItem().id)); Placeable p = newObj.GetComponent <Placeable>(); p.id = hInventoryItem.getItem().id; p.isFlower = hInventoryItem.getItem().isFlower; p.flowerGridPos = new int[2] { -1, -1 }; uic.setSlot(ii, p); uic.setSlotCount(ii, hInventoryItem.getItem().count); Destroy(newObj); } } else //If there's not an item in the home base player inventory slot { //Check to see if the player has an item in the inventory slot if (uic.getInventoryDataAtSlot(ii) != null) { //The player had an item in the slot but moved it to the home base //So clear the slot uic.inventoryClearSlot(ii); } } } }