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