Balloon.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: BALLOONS!!
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class Balloon : MonoBehaviour
  12. {
  13. public enum BalloonColor { Red, OrangeRed, Orange, YellowOrange, Yellow, GreenYellow, Green, BlueGreen, Blue, VioletBlue, Violet, RedViolet, LightGray, DarkGray, Random };
  14. private Hand hand;
  15. public GameObject popPrefab;
  16. public float maxVelocity = 5f;
  17. public float lifetime = 15f;
  18. public bool burstOnLifetimeEnd = false;
  19. public GameObject lifetimeEndParticlePrefab;
  20. public SoundPlayOneshot lifetimeEndSound;
  21. private float destructTime = 0f;
  22. private float releaseTime = 99999f;
  23. public SoundPlayOneshot collisionSound;
  24. private float lastSoundTime = 0f;
  25. private float soundDelay = 0.2f;
  26. private Rigidbody balloonRigidbody;
  27. private bool bParticlesSpawned = false;
  28. private static float s_flLastDeathSound = 0f;
  29. //-------------------------------------------------
  30. void Start()
  31. {
  32. destructTime = Time.time + lifetime + Random.value;
  33. hand = GetComponentInParent<Hand>();
  34. balloonRigidbody = GetComponent<Rigidbody>();
  35. }
  36. //-------------------------------------------------
  37. void Update()
  38. {
  39. if ( ( destructTime != 0 ) && ( Time.time > destructTime ) )
  40. {
  41. if ( burstOnLifetimeEnd )
  42. {
  43. SpawnParticles( lifetimeEndParticlePrefab, lifetimeEndSound );
  44. }
  45. Destroy( gameObject );
  46. }
  47. }
  48. //-------------------------------------------------
  49. private void SpawnParticles( GameObject particlePrefab, SoundPlayOneshot sound )
  50. {
  51. // Don't do this twice
  52. if ( bParticlesSpawned )
  53. {
  54. return;
  55. }
  56. bParticlesSpawned = true;
  57. if ( particlePrefab != null )
  58. {
  59. GameObject particleObject = Instantiate( particlePrefab, transform.position, transform.rotation ) as GameObject;
  60. particleObject.GetComponent<ParticleSystem>().Play();
  61. Destroy( particleObject, 2f );
  62. }
  63. if ( sound != null )
  64. {
  65. float lastSoundDiff = Time.time - s_flLastDeathSound;
  66. if ( lastSoundDiff < 0.1f )
  67. {
  68. sound.volMax *= 0.25f;
  69. sound.volMin *= 0.25f;
  70. }
  71. sound.Play();
  72. s_flLastDeathSound = Time.time;
  73. }
  74. }
  75. //-------------------------------------------------
  76. void FixedUpdate()
  77. {
  78. // Slow-clamp velocity
  79. if ( balloonRigidbody.velocity.sqrMagnitude > maxVelocity )
  80. {
  81. balloonRigidbody.velocity *= 0.97f;
  82. }
  83. }
  84. //-------------------------------------------------
  85. private void ApplyDamage()
  86. {
  87. SpawnParticles( popPrefab, null );
  88. Destroy( gameObject );
  89. }
  90. //-------------------------------------------------
  91. void OnCollisionEnter( Collision collision )
  92. {
  93. if ( bParticlesSpawned )
  94. {
  95. return;
  96. }
  97. Hand collisionParentHand = null;
  98. BalloonHapticBump balloonColliderScript = collision.gameObject.GetComponent<BalloonHapticBump>();
  99. if ( balloonColliderScript != null && balloonColliderScript.physParent != null )
  100. {
  101. collisionParentHand = balloonColliderScript.physParent.GetComponentInParent<Hand>();
  102. }
  103. if ( Time.time > ( lastSoundTime + soundDelay ) )
  104. {
  105. if ( collisionParentHand != null ) // If the collision was with a controller
  106. {
  107. if ( Time.time > ( releaseTime + soundDelay ) ) // Only play sound if it's not immediately after release
  108. {
  109. collisionSound.Play();
  110. lastSoundTime = Time.time;
  111. }
  112. }
  113. else // Collision was not with a controller, play sound
  114. {
  115. collisionSound.Play();
  116. lastSoundTime = Time.time;
  117. }
  118. }
  119. if ( destructTime > 0 ) // Balloon is released away from the controller, don't do the haptic stuff that follows
  120. {
  121. return;
  122. }
  123. if ( balloonRigidbody.velocity.magnitude > ( maxVelocity * 10 ) )
  124. {
  125. balloonRigidbody.velocity = balloonRigidbody.velocity.normalized * maxVelocity;
  126. }
  127. if ( hand != null )
  128. {
  129. ushort collisionStrength = (ushort)Mathf.Clamp( Util.RemapNumber( collision.relativeVelocity.magnitude, 0f, 3f, 500f, 800f ), 500f, 800f );
  130. hand.TriggerHapticPulse( collisionStrength );
  131. }
  132. }
  133. //-------------------------------------------------
  134. public void SetColor( BalloonColor color )
  135. {
  136. GetComponentInChildren<MeshRenderer>().material.color = BalloonColorToRGB( color );
  137. }
  138. //-------------------------------------------------
  139. private Color BalloonColorToRGB( BalloonColor balloonColorVar )
  140. {
  141. Color defaultColor = new Color( 255, 0, 0 );
  142. switch ( balloonColorVar )
  143. {
  144. case BalloonColor.Red:
  145. return new Color( 237, 29, 37, 255 ) / 255;
  146. case BalloonColor.OrangeRed:
  147. return new Color( 241, 91, 35, 255 ) / 255;
  148. case BalloonColor.Orange:
  149. return new Color( 245, 140, 31, 255 ) / 255;
  150. case BalloonColor.YellowOrange:
  151. return new Color( 253, 185, 19, 255 ) / 255;
  152. case BalloonColor.Yellow:
  153. return new Color( 254, 243, 0, 255 ) / 255;
  154. case BalloonColor.GreenYellow:
  155. return new Color( 172, 209, 54, 255 ) / 255;
  156. case BalloonColor.Green:
  157. return new Color( 0, 167, 79, 255 ) / 255;
  158. case BalloonColor.BlueGreen:
  159. return new Color( 108, 202, 189, 255 ) / 255;
  160. case BalloonColor.Blue:
  161. return new Color( 0, 119, 178, 255 ) / 255;
  162. case BalloonColor.VioletBlue:
  163. return new Color( 82, 80, 162, 255 ) / 255;
  164. case BalloonColor.Violet:
  165. return new Color( 102, 46, 143, 255 ) / 255;
  166. case BalloonColor.RedViolet:
  167. return new Color( 182, 36, 102, 255 ) / 255;
  168. case BalloonColor.LightGray:
  169. return new Color( 192, 192, 192, 255 ) / 255;
  170. case BalloonColor.DarkGray:
  171. return new Color( 128, 128, 128, 255 ) / 255;
  172. case BalloonColor.Random:
  173. int randomColor = Random.Range( 0, 12 );
  174. return BalloonColorToRGB( (BalloonColor)randomColor );
  175. }
  176. return defaultColor;
  177. }
  178. }
  179. }