private void ActivateSwingEffect() { RaycastHit hit = raycast.GetHitObject(); hasActivatedSwingEffect = true; if (hit.collider != null) { // If player hits a tree with an axe if (hit.collider.CompareTag("TreeCollider")) { treeController.MineTree(hit.collider); } else if (hit.collider.CompareTag("RockCollider")) { itemCollector.AddItemFromSource(hit.collider.gameObject.GetComponent <RockController>()); } else if (hit.collider.CompareTag("Bush")) { itemCollector.AddItemFromSource(hit.collider.gameObject.GetComponent <ItemSource>()); } else if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Enemy")) // If player swings at an enemy { attack.Attack(hit.collider.gameObject); } } }
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()); } }