Advertisement
evelynshilosky

ChoppableTree - Part 31

Feb 1st, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(BoxCollider))]
  6. public class ChoppableTree : MonoBehaviour
  7. {
  8.    
  9.     public bool playerInRange;
  10.     public bool canBeChopped;
  11.  
  12.     public float treeMaxHealth;
  13.     public float treeHealth;
  14.  
  15.     public Animator animator;
  16.  
  17.     public float caloriesSpentChoppingWood = 20;
  18.  
  19.  
  20.     private void Start()
  21.     {
  22.         treeHealth = treeMaxHealth;
  23.         animator = transform.parent.transform.parent.GetComponent<Animator>();
  24.     }
  25.  
  26.  
  27.     private void OnTriggerEnter(Collider other)
  28.     {
  29.         if (other.CompareTag("Player"))
  30.         {
  31.             playerInRange = true;
  32.         }
  33.     }
  34.  
  35.  
  36.  
  37.  
  38.     private void OnTriggerExit(Collider other)
  39.     {
  40.         if (other.CompareTag("Player"))
  41.         {
  42.             playerInRange = false;
  43.         }
  44.     }
  45.  
  46.  
  47.     public void GetHit()
  48.     {
  49.  
  50.         animator.SetTrigger("shake");
  51.  
  52.         treeHealth -= 1;
  53.         PlayerState.Instance.currentCalories -= caloriesSpentChoppingWood;
  54.  
  55.  
  56.         if (treeHealth <= 0)
  57.         {
  58.             SoundManager.Instance.PlaySound(SoundManager.Instance.chopSound);
  59.             TreeIsDead();
  60.         }
  61.  
  62.     }
  63.  
  64.    
  65.     void TreeIsDead()
  66.     {
  67.         Vector3 treePosition = transform.position;
  68.  
  69.         Destroy(transform.parent.transform.parent.gameObject);
  70.         canBeChopped = false;
  71.         SelectionManager.Instance.selectedTree = null;
  72.         SelectionManager.Instance.chopHolder.gameObject.SetActive(false);
  73.  
  74.         GameObject brokenTree = Instantiate(Resources.Load<GameObject>("ChoppedTree"),
  75.             new Vector3(treePosition.x,treePosition.y+1, treePosition.z), Quaternion.Euler(0, 0, 0));
  76.  
  77.     }
  78.  
  79.     private void Update()
  80.     {
  81.         if (canBeChopped)
  82.         {
  83.             GlobalState.Instance.resourceHealth = treeHealth;
  84.             GlobalState.Instance.resourceMaxHealth = treeMaxHealth;
  85.         }
  86.     }
  87.  
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement