IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
BalloonSpawner.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Spawns balloons
4 //
5 //=============================================================================
6 
7 using UnityEngine;
8 using System.Collections;
9 
10 namespace Valve.VR.InteractionSystem
11 {
12  //-------------------------------------------------------------------------
13  public class BalloonSpawner : MonoBehaviour
14  {
15  public float minSpawnTime = 5f;
16  public float maxSpawnTime = 15f;
17  private float nextSpawnTime;
18  public GameObject balloonPrefab;
19 
20  public bool autoSpawn = true;
21  public bool spawnAtStartup = true;
22 
23  public bool playSounds = true;
24  public SoundPlayOneshot inflateSound;
25  public SoundPlayOneshot stretchSound;
26 
27  public bool sendSpawnMessageToParent = false;
28 
29  public float scale = 1f;
30 
31  public Transform spawnDirectionTransform;
32  public float spawnForce;
33 
34  public bool attachBalloon = false;
35 
36  public Balloon.BalloonColor color = Balloon.BalloonColor.Random;
37 
38 
39  //-------------------------------------------------
40  void Start()
41  {
42  if ( balloonPrefab == null )
43  {
44  return;
45  }
46 
47  if ( autoSpawn && spawnAtStartup )
48  {
49  SpawnBalloon( color );
50  nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
51  }
52  }
53 
54 
55  //-------------------------------------------------
56  void Update()
57  {
58  if ( balloonPrefab == null )
59  {
60  return;
61  }
62 
63  if ( ( Time.time > nextSpawnTime ) && autoSpawn )
64  {
65  SpawnBalloon( color );
66  nextSpawnTime = Random.Range( minSpawnTime, maxSpawnTime ) + Time.time;
67  }
68  }
69 
70 
71  //-------------------------------------------------
72  public GameObject SpawnBalloon( Balloon.BalloonColor color = Balloon.BalloonColor.Red )
73  {
74  if ( balloonPrefab == null )
75  {
76  return null;
77  }
78  GameObject balloon = Instantiate( balloonPrefab, transform.position, transform.rotation ) as GameObject;
79  balloon.transform.localScale = new Vector3( scale, scale, scale );
80  if ( attachBalloon )
81  {
82  balloon.transform.parent = transform;
83  }
84 
85  if ( sendSpawnMessageToParent )
86  {
87  if ( transform.parent != null )
88  {
89  transform.parent.SendMessage( "OnBalloonSpawned", balloon, SendMessageOptions.DontRequireReceiver );
90  }
91  }
92 
93  if ( playSounds )
94  {
95  if ( inflateSound != null )
96  {
97  inflateSound.Play();
98  }
99  if ( stretchSound != null )
100  {
101  stretchSound.Play();
102  }
103  }
104  balloon.GetComponentInChildren<Balloon>().SetColor( color );
105  if ( spawnDirectionTransform != null )
106  {
107  balloon.GetComponentInChildren<Rigidbody>().AddForce( spawnDirectionTransform.forward * spawnForce );
108  }
109 
110  return balloon;
111  }
112 
113 
114  //-------------------------------------------------
115  public void SpawnBalloonFromEvent( int color )
116  {
117  // Copy of SpawnBalloon using int because we can't pass in enums through the event system
118  SpawnBalloon( (Balloon.BalloonColor)color );
119  }
120  }
121 }