Advertisement
evelynshilosky

ChoppableTree - Part 15

Jun 11th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 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.             TreeIsDead();
  59.         }
  60.  
  61.     }
  62.  
  63.    
  64.     void TreeIsDead()
  65.     {
  66.         Vector3 treePosition = transform.position;
  67.  
  68.         Destroy(transform.parent.transform.parent.gameObject);
  69.         canBeChopped = false;
  70.         SelectionManager.Instance.selectedTree = null;
  71.         SelectionManager.Instance.chopHolder.gameObject.SetActive(false);
  72.  
  73.         GameObject brokenTree = Instantiate(Resources.Load<GameObject>("ChoppedTree"),
  74.             new Vector3(treePosition.x,treePosition.y+1, treePosition.z), Quaternion.Euler(0, 0, 0));
  75.  
  76.     }
  77.  
  78.     private void Update()
  79.     {
  80.         if (canBeChopped)
  81.         {
  82.             GlobalState.Instance.resourceHealth = treeHealth;
  83.             GlobalState.Instance.resourceMaxHealth = treeMaxHealth;
  84.         }
  85.     }
  86.  
  87. }
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement