8 using System.Collections;
10 namespace Valve.VR.InteractionSystem
15 public enum BalloonColor { Red, OrangeRed, Orange, YellowOrange, Yellow, GreenYellow, Green, BlueGreen, Blue, VioletBlue, Violet, RedViolet, LightGray, DarkGray, Random };
19 public GameObject popPrefab;
21 public float maxVelocity = 5f;
23 public float lifetime = 15f;
24 public bool burstOnLifetimeEnd =
false;
26 public GameObject lifetimeEndParticlePrefab;
29 private float destructTime = 0f;
30 private float releaseTime = 99999f;
33 private float lastSoundTime = 0f;
34 private float soundDelay = 0.2f;
36 private Rigidbody balloonRigidbody;
38 private bool bParticlesSpawned =
false;
40 private static float s_flLastDeathSound = 0f;
46 destructTime = Time.time + lifetime + Random.value;
47 hand = GetComponentInParent<Hand>();
48 balloonRigidbody = GetComponent<Rigidbody>();
55 if ( ( destructTime != 0 ) && ( Time.time > destructTime ) )
57 if ( burstOnLifetimeEnd )
59 SpawnParticles( lifetimeEndParticlePrefab, lifetimeEndSound );
62 Destroy( gameObject );
68 private void SpawnParticles( GameObject particlePrefab,
SoundPlayOneshot sound )
71 if ( bParticlesSpawned )
76 bParticlesSpawned =
true;
78 if ( particlePrefab != null )
80 GameObject particleObject = Instantiate( particlePrefab, transform.position, transform.rotation ) as GameObject;
81 particleObject.GetComponent<ParticleSystem>().Play();
82 Destroy( particleObject, 2f );
87 float lastSoundDiff = Time.time - s_flLastDeathSound;
88 if ( lastSoundDiff < 0.1f )
90 sound.volMax *= 0.25f;
91 sound.volMin *= 0.25f;
94 s_flLastDeathSound = Time.time;
103 if ( balloonRigidbody.velocity.sqrMagnitude > maxVelocity )
105 balloonRigidbody.velocity *= 0.97f;
111 private void ApplyDamage()
113 SpawnParticles( popPrefab, null );
114 Destroy( gameObject );
119 void OnCollisionEnter( Collision collision )
121 if ( bParticlesSpawned )
126 Hand collisionParentHand = null;
130 if ( balloonColliderScript != null && balloonColliderScript.physParent != null )
132 collisionParentHand = balloonColliderScript.physParent.GetComponentInParent<
Hand>();
135 if ( Time.time > ( lastSoundTime + soundDelay ) )
137 if ( collisionParentHand != null )
139 if ( Time.time > ( releaseTime + soundDelay ) )
141 collisionSound.Play();
142 lastSoundTime = Time.time;
147 collisionSound.Play();
148 lastSoundTime = Time.time;
153 if ( destructTime > 0 )
158 if ( balloonRigidbody.velocity.magnitude > ( maxVelocity * 10 ) )
160 balloonRigidbody.velocity = balloonRigidbody.velocity.normalized * maxVelocity;
165 ushort collisionStrength = (ushort)Mathf.Clamp( Util.RemapNumber( collision.relativeVelocity.magnitude, 0f, 3f, 500f, 800f ), 500f, 800f );
167 hand.controller.TriggerHapticPulse( collisionStrength );
173 public void SetColor( BalloonColor color )
175 GetComponentInChildren<MeshRenderer>().material.color = BalloonColorToRGB( color );
180 private Color BalloonColorToRGB( BalloonColor balloonColorVar )
182 Color defaultColor =
new Color( 255, 0, 0 );
184 switch ( balloonColorVar )
186 case BalloonColor.Red:
187 return new Color( 237, 29, 37, 255 ) / 255;
188 case BalloonColor.OrangeRed:
189 return new Color( 241, 91, 35, 255 ) / 255;
190 case BalloonColor.Orange:
191 return new Color( 245, 140, 31, 255 ) / 255;
192 case BalloonColor.YellowOrange:
193 return new Color( 253, 185, 19, 255 ) / 255;
194 case BalloonColor.Yellow:
195 return new Color( 254, 243, 0, 255 ) / 255;
196 case BalloonColor.GreenYellow:
197 return new Color( 172, 209, 54, 255 ) / 255;
198 case BalloonColor.Green:
199 return new Color( 0, 167, 79, 255 ) / 255;
200 case BalloonColor.BlueGreen:
201 return new Color( 108, 202, 189, 255 ) / 255;
202 case BalloonColor.Blue:
203 return new Color( 0, 119, 178, 255 ) / 255;
204 case BalloonColor.VioletBlue:
205 return new Color( 82, 80, 162, 255 ) / 255;
206 case BalloonColor.Violet:
207 return new Color( 102, 46, 143, 255 ) / 255;
208 case BalloonColor.RedViolet:
209 return new Color( 182, 36, 102, 255 ) / 255;
210 case BalloonColor.LightGray:
211 return new Color( 192, 192, 192, 255 ) / 255;
212 case BalloonColor.DarkGray:
213 return new Color( 128, 128, 128, 255 ) / 255;
214 case BalloonColor.Random:
215 int randomColor = Random.Range( 0, 12 );
216 return BalloonColorToRGB( (BalloonColor)randomColor );