Advertisement
world_killer

my power manager

Jan 14th, 2023
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using UnityEngine.UI;
  6. #nullable enable
  7.  
  8. #pragma warning disable CS8618
  9. public class PowerManager : MonoBehaviour
  10. {
  11.     public bool powerEnabled;
  12.     public bool debug;
  13.     [SerializeField] float regeneration = 10;
  14.     [SerializeField] float maxEnergy = 100;
  15.     public float energy = 100;
  16.     public List<Transform> interactibles = new List<Transform>();
  17.     public List<Transform> doorHandles = new List<Transform>();
  18.  
  19.     //transforms
  20.     [Header("Player parts")]
  21.     [Space(20)]
  22.     public Transform leftHand;
  23.     public Transform rightHand;
  24.     public Transform head;
  25.  
  26.     //script references
  27.     [Header("Scripts")]
  28.     [Space(20)]
  29.     [SerializeField] Movement movementScript;
  30.     [SerializeField] Telekinesis telekinesis;
  31.     [SerializeField] Push push;
  32.     [SerializeField] Glide glide;
  33.     [SerializeField] FireThrow fireThrow;
  34.  
  35.     [Header("Energy")]
  36.     [Space(20)]
  37.     [SerializeField] Image energyImage;
  38.     [SerializeField] Text energyText;
  39.  
  40.     //controller changews
  41.     #region controller changes
  42.     public void LeftGripButton(InputAction.CallbackContext context)
  43.     {
  44.         telekinesis.Change(context);
  45.         push.ActivateController(context);
  46.         glide.Change(context);
  47.         //fireThrow.Change(context);
  48.     }
  49.  
  50.     public void RightGripButton(InputAction.CallbackContext context)
  51.     {
  52.         //CaptureController(context.control.device);
  53.         push.ActivateController(context);
  54.     }
  55.  
  56.     #endregion
  57.  
  58.  
  59.     private void Start()
  60.     {
  61.         foreach (Rigidbody obj in FindObjectsOfType<Rigidbody>())
  62.         {
  63.             if(obj.transform.parent == null)
  64.             {
  65.                 interactibles.Add(obj.transform);
  66.                 continue;
  67.             }
  68.  
  69.             if (obj.transform.parent.name == "Camera Offset") continue;
  70.             interactibles.Add(obj.transform);
  71.         }
  72.  
  73.         GameObject[] handles = GameObject.FindGameObjectsWithTag("Handle");
  74.         foreach(GameObject handle in handles)
  75.         {
  76.             doorHandles.Add(handle.transform);
  77.         }
  78.  
  79.         foreach(Transform objects in interactibles)
  80.         {
  81.             //add rigidbody to objects that dont have them (in case i forget to add to an object)
  82.             Rigidbody? comp = null;
  83.             if(!objects.TryGetComponent(out comp) && objects.tag != "Handle")
  84.                 objects.gameObject.AddComponent<Rigidbody>();
  85.         }
  86.     }
  87.  
  88.     void ShowEnergyLevel()
  89.     {
  90.         float energyPercentage = energy / maxEnergy;
  91.         energyImage.rectTransform.sizeDelta = new Vector2(energy, 100);
  92.         energyText.text = "Energy: " + (int)(energyPercentage * 100) + "%";
  93.  
  94.     }
  95.  
  96.     void GenerateEnergy()
  97.     {
  98.         if (powerEnabled || energy >= maxEnergy) return;
  99.  
  100.         energy += regeneration * Time.deltaTime;
  101.         energy = Mathf.Clamp(energy, 0, maxEnergy);
  102.     }
  103.  
  104.     private void Update()
  105.     {
  106.         ShowEnergyLevel();
  107.         GenerateEnergy();
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement