コード例 #1
0
    public void MineTree(Collider collider)
    {
        // Add item to inventory if possible
        bool stickMined = itemCollector.AddItemFromSource(collider.gameObject.GetComponent <IItemSource>());

        if (!stickMined)
        {
            // not possible; exit
            return;
        }

        // Gets the tree that was hit
        GlobalTree hitTree = collider.GetComponent <GlobalTreeInstance>().GetGlobalTree();

        hitTree.RemoveItem();

        // Change texture when there are no sticks available to be mined.
        if (hitTree.GetNumberItemsRemaining() == 0)
        {
            int newProtoIdx = -1;
            foreach (Tuple <int, int> tup in treePrototypeMapping)
            {
                if (tup.Item1 == hitTree.GetTreeInstance().prototypeIndex)
                {
                    newProtoIdx = tup.Item2;
                }
            }

            if (newProtoIdx != -1)
            {
                // Sets current terrainData to improve readability.
                TerrainData terrain = Terrain.activeTerrains[hitTree.GetTerrainIndex()].terrainData;

                // Copies current trees into a new array.
                TreeInstance[] currentTreeList = new TreeInstance[terrain.treeInstances.Length];
                System.Array.Copy(terrain.treeInstances,
                                  currentTreeList, terrain.treeInstances.Length);

                // Modifies the hit tree.
                currentTreeList[hitTree.GetTreeIndex()].prototypeIndex = newProtoIdx;

                // Overwrites terrain data. NOTE: this is permenant. To redo, you will have to
                // delete the tree and replace it, or create a method that changes all trees of
                // the new type back to the old type.
                terrain.treeInstances = currentTreeList;
            }
            else
            {
                Debug.Log("Did not find a prototype indx");
            }

            Destroy(hitTree.GetCollider());
        }
    }
コード例 #2
0
 public void SetGlobalTree(GlobalTree tree)
 {
     this.tree = tree;
 }