Advertisement
ransomink

Flashlight

Sep 6th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. public var flashlight          : Light;           // The flashlight GameObject (must have Light component)
  4. public var switchOn            : AudioClip;       // Switch on  sound
  5. public var switchOff           : AudioClip;       // Switch off sound
  6. public var isOn                : boolean = false; // Is the light on?
  7. public var alpha               : float;           // Flashlight color
  8. public var charge              : float;           // Battery charge speed
  9. public var curPower            : float;           // Current battery power
  10. public var maxPower            : float;           // Maximum battery power
  11. public var flickerTime         : float;           // Flashlight flicker threshold (percent until flashlight flickers)
  12. public var flickerDuration     : float;           // Time between each flicker
  13.  
  14. private var _isBatteryFull     : boolean;         // Is the battery fully charged?
  15. private var _defaultIntensity  : float;           // Reference to the flashlight intensity
  16.  
  17. /// <summary> Turn off/on the flashlight (When a key is pressed). </summary>
  18. public function UseFlashlight()
  19. {
  20.     // Toggle flashlight? (Pressed "F" key)
  21.     if ( Input.GetKeyDown( KeyCode.F ) )
  22.     {
  23.         ToggleFlashlight();
  24.        
  25.         Debug.Log( "Flashlight On: " + isOn );
  26.     }
  27.    
  28.     // Is flashlight on?
  29.     if ( isOn == true )
  30.     {
  31.         // Use a function to drain /decrease the battery power over time
  32.         DrainBattery ();
  33.     }
  34.     // Battery full?
  35.     else if ( _isBatteryFull == false )
  36.     {
  37.         // Use a function to charge/increase the battery power over time
  38.         ChargeBattery();
  39.     }
  40. }
  41.  
  42. /// <summary> Drain the flashlight battery (While in use). </summary>
  43. public function DrainBattery()
  44. {
  45.     _isBatteryFull = false;
  46.    
  47.     // Battery active?
  48.     if ( curPower > 0.0 )
  49.     {
  50.         Debug.Log( "Battery in use..." );
  51.        
  52.         curPower -= Time.deltaTime * charge;
  53.        
  54.         Debug.Log( "Battery power: " + curPower );
  55.        
  56.         // Flicker?
  57.         if ( curPower < maxPower / flickerTime )
  58.         {
  59.             FlickerFlashlight();
  60.         }
  61.     }
  62.     else
  63.     {
  64.         // Once the battery is dead, automatically toggle the light off
  65.         ToggleFlashlight();
  66.     }
  67. }
  68.  
  69. /// <summary> Charge the flashlight battery. </summary>
  70. public function ChargeBattery()
  71. {
  72.     // Charged?
  73.     if ( curPower >= maxPower )
  74.     {
  75.         curPower       = maxPower;
  76.         _isBatteryFull = true;
  77.         return;
  78.     }
  79.    
  80.     Debug.Log( "Charging battery..." );
  81.    
  82.     // Flicker?
  83.     if ( curPower < maxPower / flickerTime )
  84.     {
  85.         // Fade the color back to white if within flickerTime threshold
  86.         FadeFlashlight();
  87.     }
  88.     else
  89.     {
  90.         // Once the flashlight passes the flickerTime threshold, its color will reset to full
  91.         SetFlashlight( 1.0 );
  92.     }
  93.    
  94.     curPower += Time.deltaTime * ( charge / 2 );
  95.    
  96.     Debug.Log( "Battery power: " + curPower   );
  97. }
  98.  
  99. /// <summary> Turn on the flashlight. </summary>
  100. public function FlashlightOn()
  101. {
  102.     isOn               = true;
  103.     flashlight.enabled = isOn;
  104. }
  105.  
  106. /// <summary> Turn off the flashlight. </summary>
  107. public function FlashlightOff()
  108. {
  109.     isOn               = false;
  110.     flashlight.enabled =  isOn;
  111. }
  112.  
  113. /// <summary> Toggle the flashlight (Turn on-and-off). </summary>
  114. public function ToggleFlashlight()
  115. {
  116.     // If isOn is true, it will be flipped to false and vice-versa.
  117.     isOn               = !isOn;
  118.    
  119.     // Set the flashlight to your flag.
  120.     flashlight.enabled =  isOn;
  121. }
  122.  
  123. /// <summary> Fade the flashlight color. </summary>
  124. public function FadeFlashlight()
  125. {
  126.     alpha            = curPower / maxPower;
  127.     flashlight.color = Color( alpha, alpha, alpha, alpha );
  128. }
  129.  
  130. /// <summary> Set the flashlight color. </summary>
  131. public function SetFlashlight( value : float )
  132. {
  133.     alpha            = value;
  134.     flashlight.color = Color( alpha, alpha, alpha, alpha );
  135. }
  136.  
  137. /// <summary> Flicker the flashlight. </summary>
  138. public function FlickerFlashlight()
  139. {
  140.     FadeFlashlight();
  141.    
  142.     var phi       : float = Time.time / flickerDuration;
  143.     var amplitude : float = Mathf.Cos( phi ) * 0.5 + _defaultIntensity;
  144.     flashlight.intensity  = amplitude + Random.Range( 0.1, 1.0 );
  145. }
  146.  
  147. function Start()
  148. {
  149.     // In my opinion, it is better to use a public variable and drag the flashlight
  150.     // inside the slot in the inspector so as to avoid searching for the gameobject.
  151.    
  152.     // In case you forget to do that, here we check to see
  153.     // if there is a light and search for it if necessary.
  154.     if ( flashlight == null )
  155.     {
  156.         Debug.LogWarning( "There is no flashlight GameObject attached to this component! Searching scene now..." );
  157.        
  158.         // OPTION 1 - Using Find/FindWithTag
  159.         // GameObject.FindWithTag is less expensive to use than
  160.         // GameObject.Find but its use is not recommended every frame.
  161.         flashlight = GameObject.FindWithTag( "Flashlight" ).GetComponent( Light );
  162.        
  163.         // OPTION 2 - Better choice
  164.         // Instead of searching for the GameObject, this script should
  165.         // be attached to the actual flashlight scene object. Then you
  166.         // can reference this GameObject if the flashlight is null
  167.         flashlight = GetComponent( Light );
  168.     }
  169.    
  170.     flashlight.enabled = isOn;
  171.     _defaultIntensity  = flashlight.intensity;
  172.     curPower           = maxPower;
  173. }
  174.  
  175. function Update()
  176. {
  177.     UseFlashlight();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement