Advertisement
noradninja

FLashlightController

Mar 6th, 2025 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.24 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. public class FlashlightController : MonoBehaviour {
  6.     //bools
  7.     public static bool HasFlashlight = false;
  8.     public static bool FlashlightOff = true;
  9.     public static bool FlashlightDisabled = true;
  10.     public static bool delayButton = false;
  11.     public static  bool lightFocusing;
  12.     public static bool lightMovement;
  13.     public static bool isCharging;
  14.     public static bool chargeCheck;
  15.  
  16.     //GO
  17.     public Camera camObject;
  18.     public GameObject lightRoot;
  19.     public GameObject lightRig;
  20.     public GameObject lightObject;
  21.     public GameObject lightChargeObject;
  22.     public GameObject lightShaft;
  23.     public CanvasGroup UICanvasGroup;
  24.     public GameObject playerObject;
  25.     public GameObject currentTarget;
  26.    
  27.     //light
  28.     public Light flashlight;
  29.     //public Light vertlight;
  30.     //render
  31.     public Renderer lightBeam;
  32.     public Renderer lightHaze;
  33.     //color
  34.     public Color colorStart;
  35.     public Color colorEnd;
  36.     public Color colorTransparent;
  37.     //float
  38.     public float lightDuration = 600.0f;
  39.     public float currentCharge = 1.0f;
  40.     public Quaternion storedLightRotation;
  41.    
  42.     //UI
  43.     public TextMeshProUGUI batteryText;
  44.     //Audio
  45.     public AudioSource sfxSource;
  46.     public AudioClip[] flashlightClips;
  47.     public IEnumerator lightRoutine;
  48.     public IEnumerator chargeRoutine;
  49.    
  50.     //raycast
  51.     public RaycastHit hit;
  52.     public GameObject hitTarget;
  53.     public int currentTag;
  54.    
  55.     // Use this for initialization
  56.     void Start () {
  57.     HasFlashlight = false;
  58.     FlashlightOff = false;
  59.     FlashlightDisabled = true;
  60.         //event subs for
  61.         //L Trigger for firing
  62.         VitaInputManager.Instance.OnLTrig += LTrigEvent;
  63.         VitaInputManager.Instance.OnLTrigDown += LTrigDownEvent;
  64.         VitaInputManager.Instance.OnLTrigUp += LTrigUpEvent;
  65.         //square button for adding batteries
  66.         VitaInputManager.Instance.OnSquare += SquareEvent;
  67.         VitaInputManager.Instance.OnSquareDown += SquareDownEvent;
  68.         VitaInputManager.Instance.OnSquareUp += SquareUpEvent;
  69.         //triangle button for toggling on/off
  70.         VitaInputManager.Instance.OnTriangle += TriangleEvent;
  71.         VitaInputManager.Instance.OnTriangleDown += TriangleDownEvent;
  72.         VitaInputManager.Instance.OnTriangleUp += TriangleUpEvent;
  73.     }
  74.    
  75.     // Update is called once per frame
  76.     void Update ()
  77.     {
  78.         // if (HasFlashlight && !FlashlightOff &&
  79.         //     Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward),
  80.         //      out hit, flashlight.GetComponent<Light>().range))
  81.         // {
  82.         //  hitTarget = hit.transform.gameObject;
  83.         //  currentTag = hitTarget.layer;
  84.         //  if (currentTag != 12 && currentTag != 19)
  85.         //  {
  86.         //      hitTarget.layer = 22;  
  87.         //  }  
  88.         // }
  89.         // else if(hitTarget != null) hitTarget.layer = currentTag;
  90.         currentTarget = PlayerController.currentTarget;
  91.         if (currentCharge <= 0.05f){
  92.             FlashlightDisabled = true;
  93.             currentCharge = 0;
  94.             print("Flashlight Disabled");
  95.             sfxSource.Stop();
  96.         }
  97.         //lightFocusing = PlayerController.lightFocusing;
  98.         if (lightFocusing && currentTarget != null && !FlashlightDisabled)
  99.         {
  100.             if (currentTarget.GetComponentInParent<EnemyController>().isPlayerNear == true)
  101.             {
  102.                 PlayerController.lightMovement = false;
  103.                 //rotate flashlight
  104.                 Vector3 position = currentTarget.transform.position;
  105.                 Vector3 lightdir = position - lightRoot.transform.position;
  106.                 Quaternion lightlookRotation = Quaternion.LookRotation(lightdir);
  107.                 Vector3 lightrotation = Quaternion.Lerp(lightRoot.transform.rotation,
  108.                     lightlookRotation,
  109.                     Time.deltaTime * 21f).eulerAngles;
  110.                 lightRoot.transform.rotation = Quaternion.Euler(lightrotation);
  111.             }
  112.         }
  113.        
  114.    
  115.         if (!HasFlashlight) //no flashlight
  116.         {  
  117.             lightObject.SetActive(false);
  118.             lightRig.SetActive(false);
  119.         }
  120.         if (HasFlashlight && FlashlightOff && !FlashlightDisabled) //have flashlight but it's off
  121.         {
  122.             lightObject.SetActive(true);
  123.             lightRig.SetActive(false);
  124.         }
  125.         if (HasFlashlight && !FlashlightOff && !FlashlightDisabled) //have flashlight and it's on
  126.         {
  127.             lightObject.SetActive(true);
  128.             lightRig.SetActive(true);
  129.         }
  130.  
  131.         if (FlashlightDisabled && HasFlashlight)
  132.         {
  133.             lightObject.SetActive(true);
  134.             lightRig.SetActive(false);
  135.         }
  136.     }
  137.  
  138.     private void LTrigEvent()
  139.     {
  140.         if (HasFlashlight && currentCharge > 0.05f //light is on and has some charge left
  141.                           && !FlashlightDisabled && !FlashlightOff)
  142.         {
  143.             PlayerController.lightFocusing = true;
  144.            
  145.             Focus();
  146.        
  147.             if (camObject.fieldOfView < 28) camObject.fieldOfView = 28; //limit fov change
  148.             if (PlayerController.currentTarget != null) PlayerController.lightMovement = false;
  149.             isCharging = false;
  150.             storedLightRotation = lightRoot.transform.localRotation;
  151.    
  152.             if (!PlayerController.isStimulant) LerpFocalLength(0.087f, 0.095f,
  153.                                                     0.5f, 0.5f, 0.5f);
  154.            
  155.             if (UICanvasGroup.alpha < 1.0f){
  156.                 StartCoroutine(FadeAlpha(UICanvasGroup.alpha, 1.0f, 0.5f, 0.0f));
  157.             }
  158.         }
  159.     }
  160.  
  161.     private void LTrigUpEvent()
  162.     {
  163.         if (HasFlashlight)
  164.         {
  165.             if (lightRoutine != null) StopCoroutine(lightRoutine);
  166.             PlayerController.currentTarget = null;
  167.             isCharging = true;
  168.             if (!FlashlightDisabled && currentCharge >= 0.05f){
  169.                 float currentIntensity = flashlight.intensity;
  170.                 float currentAngle = flashlight.spotAngle;
  171.                 float currentSize = lightShaft.transform.localScale.x;
  172.                 Color currentColor = lightBeam.material.color;
  173.                 lightRoutine = FadeLightStaticInput(currentColor, colorStart, 0.25f, currentIntensity, 5.0f,
  174.                     currentAngle, 40, currentSize, 0.08f);
  175.                 StartCoroutine(lightRoutine);
  176.                 print("Fade Light up");
  177.             }
  178.             chargeRoutine = RechargeFlashlight (currentCharge,  20f);
  179.                 StartCoroutine(chargeRoutine);
  180.                 print("Charging");
  181.                 PlayerController.lightFocusing = false;
  182.             PlayerController.lightMovement = true;
  183.             //endLightRotation =  lightRoot.transform.localRotation;
  184.             lightRoot.transform.localRotation = storedLightRotation;
  185.             sfxSource.Stop();
  186.         }  
  187.     }
  188.  
  189.     private void LTrigDownEvent()
  190.     {
  191.         isCharging = false;
  192.         if (lightRoutine != null) StopCoroutine(lightRoutine);
  193.         if (chargeRoutine != null) StopCoroutine(chargeRoutine);
  194.         if (HasFlashlight && currentCharge < 0.05f) //light is off b/c dead battery
  195.         {
  196.    
  197.             StartCoroutine(FadeLightStaticInput(colorTransparent, colorStart, 0.25f, 0,
  198.                 10, 40, 40, 0.08f, 0.08f)); //fade in quick
  199.             StartCoroutine(FadeLightStaticInput(colorStart, colorTransparent,  0.25f,
  200.                 10, 0, 40, 40, 0.08f, 0.08f)); //fade out quick
  201.         }
  202.         else if (HasFlashlight && !FlashlightDisabled && currentCharge >= 0.05f)
  203.         {
  204.             sfxSource.pitch = Random.Range (0.7f, 1.0f);
  205.             sfxSource.PlayOneShot(flashlightClips[2]);
  206.             float currentIntensity = flashlight.intensity;
  207.             float currentAngle = flashlight.spotAngle;
  208.             float currentSize = lightShaft.transform.localScale.x;
  209.             Color currentColor = lightBeam.material.color;
  210.             float duration = lightDuration;
  211.             lightRoutine = FadeLightDynamicInput(currentColor, colorEnd, duration,
  212.                 currentIntensity, 100, 40, 0, 0.08f, 0.0f); // 'fire' light
  213.             StartCoroutine(lightRoutine);
  214.             print("Fade Light Down");
  215.        
  216.         }
  217.        
  218.     }
  219.    
  220.     private void SquareEvent()
  221.     {
  222.        
  223.     }
  224.     private void SquareDownEvent()
  225.     {
  226.         if (InventoryManager.batteryCount > 0 && currentCharge < 1.0f)
  227.         {
  228.             if (lightRoutine != null) StopCoroutine(lightRoutine);
  229.             if (chargeRoutine != null) StopCoroutine(chargeRoutine);
  230.             InventoryManager.batteryCount -= 1;
  231.             currentCharge += 0.5f;
  232.             lightChargeObject.GetComponent<Image>().fillAmount = currentCharge;
  233.             //change text formatting based on number of characters
  234.             batteryText.text = InventoryManager.batteryCount.ToString("D2");
  235.             if (FlashlightDisabled) FlashlightDisabled = false;
  236.             //add to charge and set progress bar based on charge amount
  237.             currentCharge += 0.5f;
  238.             if (currentCharge > 1.0f) currentCharge = 1.0f;
  239.             if (flashlight.intensity < 10) flashlight.intensity = flashlight.intensity + 5.0f;
  240.         }
  241.         //are we out of power and adding battery?  
  242.         if (FlashlightDisabled)
  243.         {
  244.             lightRoutine=
  245.             FadeLightStaticInput(lightBeam.material.color, colorStart,
  246.                     0.25f, flashlight.intensity, 5, 40, 40,
  247.                     0.08f, 0.08f);
  248.             FlashlightDisabled = false;
  249.             StartCoroutine(lightRoutine);
  250.             print("Fade Up on Battery from Dead");
  251.         }
  252.         //we are adding a battery when not being fired
  253.         if (!delayButton && HasFlashlight && !Input.GetButton("LTRIG") && !FlashlightDisabled)
  254.         {
  255.             lightRoutine =
  256.                 FadeLightStaticInput(lightBeam.material.color, colorStart, 0.25f, flashlight.intensity, 5, 40, 40, 0.08f, 0.08f);
  257.             StartCoroutine(lightRoutine);
  258.             print("Fade Up on Battery");
  259.             delayButton = true;
  260.             StartCoroutine(ButtonDelayTimer(0.25f));
  261.         }
  262.         //we are adding a battery while the flashlight is being fired
  263.         if (!delayButton && HasFlashlight && Input.GetButton("LTRIG") && !FlashlightDisabled)
  264.         {
  265.             lightRoutine=
  266.             FadeLightDynamicInput(lightBeam.material.color, colorEnd,
  267.                 lightDuration, flashlight.intensity, 15, 40, 25,
  268.                 0.08f, 0.040f); // 'fire' light
  269.             StartCoroutine(lightRoutine);
  270.             print("Fade up on Battery while Alive");
  271.         delayButton = true;
  272.             StartCoroutine(ButtonDelayTimer(0.25f));
  273.         }
  274.     }
  275.     private void SquareUpEvent()
  276.     {
  277.        
  278.     }
  279.     private void TriangleEvent()
  280.     {
  281.        
  282.     }
  283.     private void TriangleDownEvent()
  284.     {
  285.         if (!delayButton && HasFlashlight && currentCharge > 0.05f && !Input.GetButton("LTRIG"))
  286.         {
  287.             sfxSource.PlayOneShot(FlashlightOff ? flashlightClips[0] : flashlightClips[1]);
  288.             FlashlightOff = !FlashlightOff;
  289.             delayButton = true;
  290.             StartCoroutine(ButtonDelayTimer(0.25f));
  291.         }
  292.     }
  293.     private void TriangleUpEvent()
  294.     {
  295.        
  296.     }
  297.    
  298.     #region Methods
  299.     private void Focus()
  300.     {
  301.         ////Debug.Log("You're holding down the focus button!");
  302.         camObject.fieldOfView = camObject.fieldOfView - Time.deltaTime * 32; //zoom in
  303.  
  304.         // animator.SetBool("isRunning", false);
  305.         // animator.SetBool("isWalking", true);
  306.  
  307.     }
  308.     #endregion
  309.    
  310.     #region Coroutines
  311.     public static IEnumerator ButtonDelayTimer(float delay){
  312.         yield return new WaitForSeconds(delay);
  313.         delayButton = false;
  314.     }
  315.     private IEnumerator LerpFocalLength (float StartValue, float endValue, float endBloom, float endThreshold, float duration){
  316.         var time = 0.0f;
  317.         while (time < duration){
  318.            
  319.             var currentValue = Mathf.Lerp (StartValue, endValue, time/(duration/4));
  320.             camObject.GetComponent<Kino.Bokeh>().focalLength = currentValue;
  321.             time += Time.deltaTime;
  322.             yield return null;
  323.         }
  324.         camObject.GetComponent<Kino.Bokeh>().focalLength = endValue;
  325.     }
  326.    
  327.     private IEnumerator FadeAlpha (float StartValue, float endValue, float duration, float waitTime){
  328.         yield return new WaitForSeconds(waitTime);
  329.         float time = 0;
  330.         while (time <= duration){
  331.             var swap = Mathf.Lerp(StartValue, endValue, time/duration);
  332.             UICanvasGroup.alpha = swap;
  333.             time += Time.deltaTime;
  334.             yield return null;  
  335.         }
  336.         UICanvasGroup.alpha = endValue;
  337.     }
  338.    
  339.     private IEnumerator FadeLightStaticInput (Color StartColor, Color endColor, float duration,
  340.         float StartIntensity, float endIntensity, float StartAngle, float endAngle,
  341.         float StartSize, float endSize){
  342.         float time = 0;
  343.         var coneScale = lightShaft.transform.localScale;
  344.         while (time <= duration){
  345.             lightBeam.material.color = Color.Lerp(StartColor, endColor, time/(duration/2)); //lerp the colors from dark to light
  346.             lightHaze.material.color = Color.Lerp(StartColor, endColor, time/(duration/2)); //lerp the colors from dark to light
  347.             flashlight.intensity = Mathf.Lerp(StartIntensity,endIntensity,time/(duration/2));
  348.             flashlight.spotAngle = Mathf.Lerp(StartAngle,endAngle,time/(duration/2));
  349.             //vertlight.intensity = Mathf.Lerp(StartIntensity,endIntensity,time/(duration/2));
  350.             //vertlight.spotAngle = Mathf.Lerp(StartAngle,endAngle,time/(duration/2));
  351.             var scalar = Mathf.Lerp(StartSize,endSize,time/(duration/2));
  352.             lightShaft.transform.localScale = new Vector3(scalar, lightShaft.transform.localScale.y ,scalar);
  353.             time += Time.deltaTime;
  354.             yield return null;
  355.         }
  356.     }
  357.    
  358.     private IEnumerator FadeLightDynamicInput (Color StartColor, Color endColor, float duration,
  359.         float StartIntensity, float endIntensity, float StartAngle, float endAngle,
  360.         float StartSize, float endSize){
  361.         float time = 0;
  362.         //var currentCharge = this.currentCharge;
  363.         while (time <= duration){
  364.             lightBeam.material.color = Color.Lerp(StartColor, new Color
  365.                                                     (endColor.r, endColor.g, endColor.b, endColor.a * currentCharge), time/(duration/50)); //lerp the colors from dark to light
  366.             lightHaze.material.color = Color.Lerp(StartColor, new Color
  367.                                                     (endColor.r, endColor.g, endColor.b, endColor.a * currentCharge), time/(duration/50)); //lerp the colors from dark to light
  368.             flashlight.intensity = Mathf.Lerp(StartIntensity ,endIntensity * currentCharge,time/(duration/50));
  369.             flashlight.spotAngle = Mathf.Lerp(StartAngle,endAngle,time/(duration/50));
  370.             //vertlight.intensity = Mathf.Lerp(StartIntensity,endIntensity * currentCharge,time/(duration/50));
  371.             //vertlight.spotAngle = Mathf.Lerp(StartAngle,endAngle,time/(duration/50));
  372.             var scalar = Mathf.Lerp(StartSize,endSize,time/(duration/50));
  373.             currentCharge = Mathf.Lerp(currentCharge, 0, time / (duration*2));
  374.             lightShaft.transform.localScale = new Vector3(scalar, lightShaft.transform.localScale.y ,scalar);
  375.             lightChargeObject.GetComponent<Image>().fillAmount = Mathf.Lerp(currentCharge, 0, time/(duration));
  376.             time += Time.deltaTime;
  377.             yield return null;
  378.         }
  379.     }
  380.    
  381.     private IEnumerator RechargeFlashlight(float charge, float duration){
  382.         float time = 0.0f;
  383.         isCharging = true;
  384.         //yield return new WaitForSeconds(2f); //delay before charge happens
  385.         while (time < duration)
  386.         {
  387.             currentCharge = Mathf.Lerp(charge, 1, time / duration);
  388.             lightChargeObject.GetComponent<Image>().fillAmount = currentCharge;
  389.             time += Time.deltaTime;
  390.             yield return null;
  391.         }
  392.    
  393.         lightChargeObject.GetComponent<Image>().fillAmount = 1;
  394.         currentCharge = 1;
  395.         isCharging = false;
  396.         FlashlightDisabled = false;
  397.        
  398.        
  399.        
  400.         var currentIntensity = flashlight.intensity;
  401.         var currentAngle = flashlight.spotAngle;
  402.         var currentSize = lightShaft.transform.localScale.x;
  403.         var currentColor = lightBeam.material.color;
  404.         StartCoroutine(FadeLightStaticInput(currentColor, colorStart, 0.25f,
  405.             currentIntensity, 5.0f, currentAngle, 40,
  406.             currentSize, 0.08f));
  407.        
  408.     }
  409.  
  410.     #endregion
  411. }
  412.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement